diff --git a/toolkit/tools/imagegen/installutils/installutils.go b/toolkit/tools/imagegen/installutils/installutils.go index eac6af18b67..5d8c14b4cc3 100644 --- a/toolkit/tools/imagegen/installutils/installutils.go +++ b/toolkit/tools/imagegen/installutils/installutils.go @@ -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 } diff --git a/toolkit/tools/internal/file/file.go b/toolkit/tools/internal/file/file.go index cb530e6f1f3..15d6dde3f9f 100644 --- a/toolkit/tools/internal/file/file.go +++ b/toolkit/tools/internal/file/file.go @@ -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)