diff --git a/cmd/control/install.go b/cmd/control/install.go index 68e1e6ec..6c085c48 100755 --- a/cmd/control/install.go +++ b/cmd/control/install.go @@ -336,48 +336,25 @@ func runInstall(image, installType, cloudConfig, device, kappend string, force, func mountBootIso() error { deviceName := "/dev/sr0" deviceType := "iso9660" - { // force the defer - mountsFile, err := os.Open("/proc/mounts") - if err != nil { - log.Errorf("failed to read /proc/mounts %s", err) - return err - } - defer mountsFile.Close() + if d, t := util.Blkid("RancherOS"); d != "" { + deviceName = d + deviceType = t + } - if partitionMounted(deviceName, mountsFile) { - return nil - } + mountsFile, err := os.Open("/proc/mounts") + if err != nil { + log.Errorf("failed to read /proc/mounts %s", err) + return err + } + defer mountsFile.Close() + + if partitionMounted(deviceName, mountsFile) { + return nil } os.MkdirAll("/bootiso", 0755) - - // find the installation device - cmd := exec.Command("blkid", "-L", "RancherOS") - log.Debugf("Run(%v)", cmd) - cmd.Stderr = os.Stderr - out, err := cmd.Output() - if err != nil { - log.Errorf("Failed to get RancherOS boot device: %s", err) - return err - } - deviceName = strings.TrimSpace(string(out)) - log.Debugf("blkid found -L RancherOS: %s", deviceName) - - cmd = exec.Command("blkid", deviceName) - log.Debugf("Run(%v)", cmd) - cmd.Stderr = os.Stderr - if out, err = cmd.Output(); err != nil { - log.Errorf("Failed to get RancherOS boot device type: %s", err) - return err - } - deviceType = strings.TrimSpace(string(out)) - s1 := strings.Split(deviceType, "TYPE=\"") - s2 := strings.Split(s1[1], "\"") - deviceType = s2[0] - log.Debugf("blkid type of %s: %s", deviceName, deviceType) - - cmd = exec.Command("mount", "-t", deviceType, deviceName, "/bootiso") - log.Debugf("Run(%v)", cmd) + cmd := exec.Command("mount", "-t", deviceType, deviceName, "/bootiso") + log.Infof("mount (%#v)", cmd) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr err = cmd.Run() @@ -750,27 +727,21 @@ func mountdevice(baseName, bootDir, partition string, raw bool) (string, string, // Don't use ResolveDevice - it can fail, whereas `blkid -L LABEL` works more often cfg := config.LoadConfig() - cmd := exec.Command("blkid", "-L", "RANCHER_BOOT") - log.Debugf("Run(%v)", cmd) - cmd.Stderr = os.Stderr - if out, err := cmd.Output(); err == nil { - partition = strings.TrimSpace(string(out)) + if d, _ := util.Blkid("RANCHER_BOOT"); d != "" { + partition = d baseName = filepath.Join(baseName, "boot") } else { if dev := util.ResolveDevice(cfg.Rancher.State.Dev); dev != "" { // try the rancher.state.dev setting partition = dev } else { - cmd := exec.Command("blkid", "-L", "RANCHER_STATE") - log.Debugf("Run(%v)", cmd) - cmd.Stderr = os.Stderr - if out, err := cmd.Output(); err == nil { - partition = strings.TrimSpace(string(out)) + if d, _ := util.Blkid("RANCHER_STATE"); d != "" { + partition = d } } } device := "" - cmd = exec.Command("lsblk", "-no", "pkname", partition) + cmd := exec.Command("lsblk", "-no", "pkname", partition) log.Debugf("Run(%v)", cmd) cmd.Stderr = os.Stderr if out, err := cmd.Output(); err == nil { diff --git a/tests/installer_test.go b/tests/installer_test.go index 66db79d0..22a7fde6 100755 --- a/tests/installer_test.go +++ b/tests/installer_test.go @@ -80,3 +80,40 @@ sync // TEST parted output? (gpt non-uefi == legacy_boot) s.Stop(c) } + +func (s *QemuSuite) TestInstallAlpine(c *C) { + // ./scripts/run --no-format --append "rancher.debug=true" --iso --fresh + runArgs := []string{ + "--iso", + "--fresh", + } + version := "" + { + s.RunQemuWith(c, runArgs...) + + s.MakeCall("sudo ros console switch -f alpine") + c.Assert(s.WaitForSSH(), IsNil) + + version = s.CheckOutput(c, version, Not(Equals), "sudo ros -v") + fmt.Printf("installing %s", version) + + s.CheckCall(c, ` +set -ex +echo "ssh_authorized_keys:" > config.yml +echo " - $(cat /home/rancher/.ssh/authorized_keys)" >> config.yml +sudo ros install --force --no-reboot --device /dev/vda -c config.yml +sync +`) + time.Sleep(500 * time.Millisecond) + s.Stop(c) + } + + // ./scripts/run --no-format --append "rancher.debug=true" + runArgs = []string{ + "--boothd", + } + s.RunQemuWith(c, runArgs...) + + s.CheckOutput(c, version, Equals, "sudo ros -v") + s.Stop(c) +} diff --git a/util/util_linux.go b/util/util_linux.go index cb9596fa..2eb5ba68 100755 --- a/util/util_linux.go +++ b/util/util_linux.go @@ -3,10 +3,15 @@ package util import ( + "bufio" + "bytes" "os" + "os/exec" + "strings" "syscall" "github.com/docker/docker/pkg/mount" + "github.com/rancher/os/log" ) func mountProc() error { @@ -43,3 +48,32 @@ func Mount(device, directory, fsType, options string) error { func Unmount(target string) error { return mount.Unmount(target) } + +func Blkid(label string) (deviceName, deviceType string) { + // Not all blkid's have `blkid -L label (see busybox/alpine) + cmd := exec.Command("blkid") + cmd.Stderr = os.Stderr + out, err := cmd.Output() + if err != nil { + log.Errorf("Failed to run blkid: %s", err) + return + } + r := bytes.NewReader(out) + s := bufio.NewScanner(r) + for s.Scan() { + line := s.Text() + log.Debugf("blkid: %s", cmd, line) + if !strings.Contains(line, `LABEL="`+label+`"`) { + continue + } + d := strings.Split(line, ":") + deviceName = d[0] + + s1 := strings.Split(line, `TYPE="`) + s2 := strings.Split(s1[1], `"`) + deviceType = s2[0] + log.Debugf("blkid type of %s: %s", deviceName, deviceType) + return + } + return +}