From a3978bc89d255a9f83f9a7354e720d46e78424fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20L=C3=B6nnegren?= Date: Thu, 14 Dec 2023 09:47:58 +0100 Subject: [PATCH] Read mount env-vars before kernel-cmdline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Lönnegren --- cmd/config/config.go | 51 ++++++++++++++----- cmd/config/config_test.go | 9 +++- pkg/constants/constants.go | 6 +-- .../system/oem/00_rootfs.yaml | 2 +- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/cmd/config/config.go b/cmd/config/config.go index c04d47b53a0..1142d677b89 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -317,6 +317,11 @@ func applyKernelCmdline(r *v1.RunConfig, mount *v1.MountSpec) error { r.Logger.Errorf("Error parsing cmdline %s", cmd) return errors.New("Unknown image path") } + case "elemental.overlay", "rd.cos.overlay": + err := applyMountOverlay(mount, val) + if err != nil { + return err + } case "elemental.oemlabel", "rd.cos.oemlabel": mount.Partitions.OEM.FilesystemLabel = val } @@ -332,19 +337,9 @@ func applyMountEnvVars(r *v1.RunConfig, mount *v1.MountSpec) error { if overlay != "" { r.Logger.Debugf("Setting ephemeral settings based on OVERLAY") - split := strings.Split(overlay, ":") - if len(split) != 2 { - return fmt.Errorf("Unknown format of OVERLAY env-var: %s", overlay) - } - - mount.Ephemeral.Type = split[0] - - if split[0] == constants.Tmpfs { - mount.Ephemeral.Size = split[1] - } - - if split[0] == constants.Block { - mount.Ephemeral.Device = split[1] + err := applyMountOverlay(mount, overlay) + if err != nil { + return err } } @@ -369,6 +364,36 @@ func applyMountEnvVars(r *v1.RunConfig, mount *v1.MountSpec) error { return nil } +func applyMountOverlay(mount *v1.MountSpec, overlay string) error { + split := strings.Split(overlay, ":") + + if len(split) == 2 && split[0] == constants.Tmpfs { + mount.Ephemeral.Device = "" + mount.Ephemeral.Type = split[0] + mount.Ephemeral.Size = split[1] + return nil + } + + mount.Ephemeral.Type = constants.Block + mount.Ephemeral.Size = "" + + blockSplit := strings.Split(overlay, "=") + if len(blockSplit) != 2 { + return fmt.Errorf("Unknown block overlay '%s'", overlay) + } + + switch blockSplit[0] { + case "LABEL": + mount.Ephemeral.Device = fmt.Sprintf("/dev/disk/by-label/%s", blockSplit[1]) + case "UUID": + mount.Ephemeral.Device = fmt.Sprintf("/dev/disk/by-uuid/%s", blockSplit[1]) + default: + return fmt.Errorf("Unknown block overlay '%s'", overlay) + } + + return nil +} + func ReadResetSpec(r *v1.RunConfig, flags *pflag.FlagSet) (*v1.ResetSpec, error) { reset, err := config.NewResetSpec(r.Config) if err != nil { diff --git a/cmd/config/config_test.go b/cmd/config/config_test.go index 48640e79491..e361fa2cef8 100644 --- a/cmd/config/config_test.go +++ b/cmd/config/config_test.go @@ -273,7 +273,7 @@ var _ = Describe("Config", Label("config"), func() { err = fs.Mkdir("/proc", constants.DirPerm) Expect(err).Should(BeNil()) - err = fs.WriteFile("/proc/cmdline", []byte("root=LABEL=COS_STATE elemental.image=active"), 0444) + err = fs.WriteFile("/proc/cmdline", []byte("root=LABEL=COS_STATE elemental.image=active elemental.overlay=tmpfs:30%"), 0444) Expect(err).Should(BeNil()) cfg, err = ReadConfigRun("fixtures/config/", nil, mounter) @@ -469,6 +469,13 @@ var _ = Describe("Config", Label("config"), func() { // Set by kernel cmdline Expect(spec.Mode).To(Equal("active")) }) + It("picks kernel cmdline first then env-vars", func() { + err := os.Setenv("OVERLAY", "UUID=1234") + spec, err := ReadMountSpec(cfg, nil) + Expect(err).ShouldNot(HaveOccurred()) + Expect(spec.Ephemeral.Type).To(Equal("tmpfs")) + Expect(spec.Ephemeral.Size).To(Equal("30%")) + }) }) }) }) diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 4a82628d209..6f9549fc276 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -263,10 +263,8 @@ func GetInitKeyEnvMap() map[string]string { // GetMountKeyEnvMap returns environment variable bindings to MountSpec data func GetMountKeyEnvMap() map[string]string { return map[string]string{ - "write-fstab": "WRITE_FSTAB", - "write-sentinel": "WRITE_SENTINEL", - "sysroot": "SYSROOT", - "read-kernel-cmdline": "READ_KERNEL_CMDLINE", + "write-fstab": "WRITE_FSTAB", + "sysroot": "SYSROOT", } } diff --git a/pkg/features/embedded/cloud-config-essentials/system/oem/00_rootfs.yaml b/pkg/features/embedded/cloud-config-essentials/system/oem/00_rootfs.yaml index 445d8a841ed..93daa5b3fa9 100644 --- a/pkg/features/embedded/cloud-config-essentials/system/oem/00_rootfs.yaml +++ b/pkg/features/embedded/cloud-config-essentials/system/oem/00_rootfs.yaml @@ -15,7 +15,7 @@ stages: rootfs: - if: '[ ! -f "/run/elemental/recovery_mode" ]' name: "Layout configuration" - environment_file: /run/elemental/mount-layout.env + environment_file: /run/cos/cos-layout.env environment: OVERLAY: "tmpfs:25%" initramfs: