Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions toolkit/tools/imagegen/installutils/installutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,24 +499,23 @@ func calculateTotalPackages(packages []string, installRoot string) (totalPackage
return
}

// addMachineID creates the /etc/machine-id file in the installChroot
func addMachineID(installChroot *safechroot.Chroot) (err error) {
// From https://www.freedesktop.org/software/systemd/man/machine-id.html:
// For operating system images which are created once and used on multiple
// machines, for example for containers or in the cloud, /etc/machine-id
// should be an empty file in the generic file system image. An ID will be
// generated during boot and saved to this file if possible.

const (
squashErrors = false
setupProgram = "/bin/systemd-machine-id-setup"
machineIDFile = "/etc/machine-id"
machineIDFilePerms = 0644
)

// Check if systemd-machine-id-setup is present before invoking it,
// some images will not use systemd (such as a container)
exists, _ := file.PathExists(filepath.Join(installChroot.RootDir(), setupProgram))
if !exists {
logger.Log.Debugf("'%s' not found inside chroot '%s', skipping adding machine ID", setupProgram, installChroot.RootDir())
return
}

ReportAction("Configuring machine id")

err = installChroot.UnsafeRun(func() error {
return shell.ExecuteLive(squashErrors, setupProgram)
return file.Create(machineIDFile, machineIDFilePerms)
})
return
}
Expand Down
12 changes: 12 additions & 0 deletions toolkit/tools/internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ func ReadLines(path string) (lines []string, err error) {
return lines, scanner.Err()
}

// Create creates a new file with the provided Unix permissions
func Create(dst string, perm os.FileMode) (err error) {
logger.Log.Debugf("Creating (%s) with mode (%v)", dst, perm)

dstFile, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL, perm)
if err != nil {
return
}
defer dstFile.Close()
return
}

// Write writes a string to the file dst.
func Write(data string, dst string) (err error) {
logger.Log.Debugf("Writing to (%s)", dst)
Expand Down