Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Add a eject-cd flag for install (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itxaka authored Mar 25, 2022
1 parent c6d4318 commit 1b81b18
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ func init() {
installCmd.Flags().BoolP("force-gpt", "", false, "Forces a GPT partition table")
installCmd.Flags().BoolP("tty", "", false, "Add named tty to grub")
installCmd.Flags().BoolP("force", "", false, "Force install")
installCmd.Flags().BoolP("eject-cd", "", false, "Try to eject the cd on reboot, only valid if booting from iso")
addSharedInstallUpgradeFlags(installCmd)
}
40 changes: 40 additions & 0 deletions pkg/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ var _ = Describe("Actions", func() {
Describe("Install Action", Label("install"), func() {
var device, cmdFail string
var err error
var cmdline func() ([]byte, error)

BeforeEach(func() {
device = "/some/device"
Expand Down Expand Up @@ -395,6 +396,11 @@ var _ = Describe("Actions", func() {
{"label": "COS_PERSISTENT", "type": "part", "path": "/some/device4"}
]
}`), nil
case "cat":
if args[0] == "/proc/cmdline" {
return cmdline()
}
return []byte{}, nil
default:
return []byte{}, nil
}
Expand All @@ -411,6 +417,11 @@ var _ = Describe("Actions", func() {
Expect(err).To(BeNil())
_, err = fs.Create(grubCfg)
Expect(err).To(BeNil())

// Set default cmdline function so we dont panic :o
cmdline = func() ([]byte, error) {
return []byte{}, nil
}
})

It("Successfully installs", func() {
Expand All @@ -420,6 +431,35 @@ var _ = Describe("Actions", func() {
Expect(runner.IncludesCmds([][]string{{"reboot", "-f"}}))
})

It("Sets the executable /run/cos/ejectcd so systemd can eject the cd on restart", func() {
_ = utils.MkdirAll(fs, "/usr/lib/systemd/system-shutdown", constants.DirPerm)
_, err := fs.Stat("/usr/lib/systemd/system-shutdown/eject")
Expect(err).To(HaveOccurred())
// Override cmdline to return like we are booting from cd
cmdline = func() ([]byte, error) {
return []byte("cdroot"), nil
}
config.Target = device
config.EjectCD = true
Expect(action.InstallRun(config)).To(BeNil())
_, err = fs.Stat("/usr/lib/systemd/system-shutdown/eject")
Expect(err).ToNot(HaveOccurred())
file, err := fs.ReadFile("/usr/lib/systemd/system-shutdown/eject")
Expect(err).ToNot(HaveOccurred())
Expect(file).To(ContainSubstring(constants.EjectScript))
})

It("ejectcd does nothing if we are not booting from cd", func() {
_ = utils.MkdirAll(fs, "/usr/lib/systemd/system-shutdown", constants.DirPerm)
_, err := fs.Stat("/usr/lib/systemd/system-shutdown/eject")
Expect(err).To(HaveOccurred())
config.Target = device
config.EjectCD = true
Expect(action.InstallRun(config)).To(BeNil())
_, err = fs.Stat("/usr/lib/systemd/system-shutdown/eject")
Expect(err).To(HaveOccurred())
})

It("Successfully installs despite hooks failure", Label("hooks"), func() {
cloudInit.Error = true
config.Target = device
Expand Down
9 changes: 9 additions & 0 deletions pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ func InstallRun(config *v1.RunConfig) (err error) { //nolint:gocyclo
return err
}

// If we want to eject the cd, create the required executable so the cd is ejected at shutdown
if config.EjectCD && utils.BootedFrom(config.Runner, "cdroot") {
config.Logger.Infof("Writing eject script")
err = config.Fs.WriteFile("/usr/lib/systemd/system-shutdown/eject", []byte(cnst.EjectScript), 0744)
if err != nil {
config.Logger.Warnf("Could not write eject script, cdrom wont be ejected automatically: %s", err)
}
}

// Reboot, poweroff or nothing
if config.Reboot {
config.Logger.Infof("Rebooting in 5 seconds")
Expand Down
3 changes: 3 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ const (
// Default directory and file fileModes
DirPerm = os.ModeDir | os.ModePerm
FilePerm = 0666

// Eject script
EjectScript = "#!/bin/sh\n/usr/bin/eject -rmF"
)

func GetCloudInitPaths() []string {
Expand Down
1 change: 1 addition & 0 deletions pkg/types/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type RunConfig struct {
ImgSize uint `yaml:"DEFAULT_IMAGE_SIZE,omitempty" mapstructure:"DEFAULT_IMAGE_SIZE"`
Directory string `yaml:"directory,omitempty" mapstructure:"directory"`
ResetPersistent bool `yaml:"reset-persistent,omitempty" mapstructure:"reset-persistent"`
EjectCD bool `yaml:"eject-cd,omitempty" mapstructure:"eject-cd"`
// Internally used to track stuff around
PartTable string
BootFlag string
Expand Down

0 comments on commit 1b81b18

Please sign in to comment.