From 0088a8e0a0342cf1559bf86a9664f4f15318805f Mon Sep 17 00:00:00 2001 From: Chris Co Date: Sat, 26 Sep 2020 02:26:01 +0000 Subject: [PATCH 1/6] installutils: Supply blank /etc/machine-id file 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. --- .../imagegen/installutils/installutils.go | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/toolkit/tools/imagegen/installutils/installutils.go b/toolkit/tools/imagegen/installutils/installutils.go index eac6af18b67..9a959503631 100644 --- a/toolkit/tools/imagegen/installutils/installutils.go +++ b/toolkit/tools/imagegen/installutils/installutils.go @@ -499,24 +499,24 @@ 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" + squashErrors = false + setupProgram = "touch" + machineIDFile = "/etc/machine-id" ) - // 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 shell.ExecuteLive(squashErrors, setupProgram, machineIDFile) }) return } From c06bcf414f752b03ca42f57d9a54209eaa3991be Mon Sep 17 00:00:00 2001 From: Chris Co Date: Mon, 28 Sep 2020 20:24:49 +0000 Subject: [PATCH 2/6] Use install with file permissions instead of touch --- toolkit/tools/imagegen/installutils/installutils.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/toolkit/tools/imagegen/installutils/installutils.go b/toolkit/tools/imagegen/installutils/installutils.go index 9a959503631..b64a72a5a73 100644 --- a/toolkit/tools/imagegen/installutils/installutils.go +++ b/toolkit/tools/imagegen/installutils/installutils.go @@ -508,15 +508,17 @@ func addMachineID(installChroot *safechroot.Chroot) (err error) { // generated during boot and saved to this file if possible. const ( - squashErrors = false - setupProgram = "touch" - machineIDFile = "/etc/machine-id" + squashErrors = false + setupProgram = "install" + machineIDFile = "/etc/machine-id" + machineIDFilePerms = "0644" ) ReportAction("Configuring machine id") err = installChroot.UnsafeRun(func() error { - return shell.ExecuteLive(squashErrors, setupProgram, machineIDFile) + setupArgs := []string{"-m", machineIDFilePerms, "/dev/null", machineIDFile} + return shell.ExecuteLive(squashErrors, setupProgram, setupArgs...) }) return } From d59eb5093e17a85ba83f3e5403025f3da56186eb Mon Sep 17 00:00:00 2001 From: Chris Co Date: Mon, 28 Sep 2020 22:45:33 +0000 Subject: [PATCH 3/6] Directly create file instead of calling to shell --- toolkit/tools/imagegen/installutils/installutils.go | 7 +++---- toolkit/tools/internal/file/file.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/toolkit/tools/imagegen/installutils/installutils.go b/toolkit/tools/imagegen/installutils/installutils.go index b64a72a5a73..22b5d2998c9 100644 --- a/toolkit/tools/imagegen/installutils/installutils.go +++ b/toolkit/tools/imagegen/installutils/installutils.go @@ -509,16 +509,15 @@ func addMachineID(installChroot *safechroot.Chroot) (err error) { const ( squashErrors = false - setupProgram = "install" machineIDFile = "/etc/machine-id" - machineIDFilePerms = "0644" + machineIDFilePerms = 0644 ) ReportAction("Configuring machine id") err = installChroot.UnsafeRun(func() error { - setupArgs := []string{"-m", machineIDFilePerms, "/dev/null", machineIDFile} - return shell.ExecuteLive(squashErrors, setupProgram, setupArgs...) + file.Create(machineIDFile, machineIDFilePerms) + return file.Create(machineIDFile, machineIDFilePerms) }) return } diff --git a/toolkit/tools/internal/file/file.go b/toolkit/tools/internal/file/file.go index cb530e6f1f3..966363ed8d1 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 file with the provided Unix permissions +func Create(dst string, perm os.FileMode) (err error) { + logger.Log.Debugf("Creating (%s) with perm (%v)", dst, perm) + + dstFile, err := os.OpenFile(dst, os.O_CREATE, 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) From e0165803c2db8e96c2e6be06103b5e0a4b25fdef Mon Sep 17 00:00:00 2001 From: Chris Co Date: Mon, 28 Sep 2020 23:59:36 +0000 Subject: [PATCH 4/6] Remove unnecessary const --- toolkit/tools/imagegen/installutils/installutils.go | 1 - 1 file changed, 1 deletion(-) diff --git a/toolkit/tools/imagegen/installutils/installutils.go b/toolkit/tools/imagegen/installutils/installutils.go index 22b5d2998c9..42e5a6c64ab 100644 --- a/toolkit/tools/imagegen/installutils/installutils.go +++ b/toolkit/tools/imagegen/installutils/installutils.go @@ -508,7 +508,6 @@ func addMachineID(installChroot *safechroot.Chroot) (err error) { // generated during boot and saved to this file if possible. const ( - squashErrors = false machineIDFile = "/etc/machine-id" machineIDFilePerms = 0644 ) From 8370f480b27e5128d08bed7142bfa3c8fc344622 Mon Sep 17 00:00:00 2001 From: Chris Co Date: Tue, 29 Sep 2020 00:00:40 +0000 Subject: [PATCH 5/6] Remove corner case test code --- toolkit/tools/imagegen/installutils/installutils.go | 1 - 1 file changed, 1 deletion(-) diff --git a/toolkit/tools/imagegen/installutils/installutils.go b/toolkit/tools/imagegen/installutils/installutils.go index 42e5a6c64ab..5d8c14b4cc3 100644 --- a/toolkit/tools/imagegen/installutils/installutils.go +++ b/toolkit/tools/imagegen/installutils/installutils.go @@ -515,7 +515,6 @@ func addMachineID(installChroot *safechroot.Chroot) (err error) { ReportAction("Configuring machine id") err = installChroot.UnsafeRun(func() error { - file.Create(machineIDFile, machineIDFilePerms) return file.Create(machineIDFile, machineIDFilePerms) }) return From f83df7856a6ae36b45b461a22e1e5e2008e68846 Mon Sep 17 00:00:00 2001 From: Chris Co Date: Tue, 29 Sep 2020 00:34:17 +0000 Subject: [PATCH 6/6] Enforce new file creation --- toolkit/tools/internal/file/file.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/toolkit/tools/internal/file/file.go b/toolkit/tools/internal/file/file.go index 966363ed8d1..15d6dde3f9f 100644 --- a/toolkit/tools/internal/file/file.go +++ b/toolkit/tools/internal/file/file.go @@ -101,11 +101,11 @@ func ReadLines(path string) (lines []string, err error) { return lines, scanner.Err() } -// Create creates a file with the provided Unix permissions +// 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 perm (%v)", dst, perm) + logger.Log.Debugf("Creating (%s) with mode (%v)", dst, perm) - dstFile, err := os.OpenFile(dst, os.O_CREATE, perm) + dstFile, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL, perm) if err != nil { return }