Skip to content

Commit

Permalink
Read mount env-vars before kernel-cmdline
Browse files Browse the repository at this point in the history
Signed-off-by: Fredrik Lönnegren <[email protected]>
  • Loading branch information
frelon committed Dec 14, 2023
1 parent 32e03d3 commit 504e490
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
51 changes: 38 additions & 13 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
}

Expand All @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion cmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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%"))
})
})
})
})
6 changes: 2 additions & 4 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 504e490

Please sign in to comment.