Skip to content

Commit d83bcb5

Browse files
authored
Merge pull request #147 from praveenkumar/fix_146
Fix #146 Make /boot configurable to allow different live iso to work with driver
2 parents 9d6adee + db9f7dd commit d83bcb5

File tree

1 file changed

+57
-33
lines changed

1 file changed

+57
-33
lines changed

xhyve/xhyve.go

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434
const (
3535
isoFilename = "boot2docker.iso"
3636
isoMountPath = "b2d-image"
37+
defaultBootKernel = ""
38+
defaultBootInitrd = ""
3739
defaultBoot2DockerURL = ""
3840
defaultBootCmd = "loglevel=3 user=docker console=ttyS0 console=tty0 noembed nomodeset norestore waitusb=10 base host=boot2docker"
3941
defaultCPU = 1
@@ -70,15 +72,18 @@ type Driver struct {
7072
Virtio9pFolder string
7173
NFSShare bool
7274

73-
BootCmd string
74-
Initrd string
75-
Vmlinuz string
75+
BootCmd string
76+
BootKernel string
77+
BootInitrd string
78+
Initrd string
79+
Vmlinuz string
7680
}
7781

7882
var (
7983
ErrMachineExist = errors.New("machine already exists")
8084
ErrMachineNotExist = errors.New("machine does not exist")
8185
diskRegexp = regexp.MustCompile("^/dev/disk([0-9]+)")
86+
kernelRegexp = regexp.MustCompile(`(vmlinu[xz]|bzImage)[\d]*`)
8287
)
8388

8489
// NewDriver creates a new VirtualBox driver with default settings.
@@ -90,6 +95,8 @@ func NewDriver(hostName, storePath string) *Driver {
9095
},
9196
Boot2DockerURL: defaultBoot2DockerURL,
9297
BootCmd: defaultBootCmd,
98+
BootKernel: defaultBootKernel,
99+
BootInitrd: defaultBootInitrd,
93100
CPU: defaultCPU,
94101
CaCertPath: defaultCaCertPath,
95102
DiskSize: defaultDiskSize,
@@ -114,6 +121,18 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
114121
Usage: "Command of booting kexec protocol",
115122
Value: defaultBootCmd,
116123
},
124+
mcnflag.StringFlag{
125+
EnvVar: "XHYVE_BOOT_KERNEL",
126+
Name: "xhyve-boot-kernel",
127+
Usage: "Absolute path to kernel file (like /boot/vmlinuz64)",
128+
Value: defaultBootKernel,
129+
},
130+
mcnflag.StringFlag{
131+
EnvVar: "XHYVE_BOOT_INITRD",
132+
Name: "xhyve-boot-initrd",
133+
Usage: "Absolute path to ramdisk file (like /boot/initrd.img)",
134+
Value: defaultBootInitrd,
135+
},
117136
mcnflag.StringFlag{
118137
EnvVar: "XHYVE_BOOT2DOCKER_URL",
119138
Name: "xhyve-boot2docker-url",
@@ -197,6 +216,8 @@ func (d *Driver) DriverName() string {
197216
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
198217
d.Boot2DockerURL = flags.String("xhyve-boot2docker-url")
199218
d.BootCmd = flags.String("xhyve-boot-cmd")
219+
d.BootKernel = flags.String("xhyve-boot-kernel")
220+
d.BootInitrd = flags.String("xhyve-boot-initrd")
200221
d.CPU = flags.Int("xhyve-cpu-count")
201222
if d.CPU < 1 {
202223
d.CPU = int(runtime.NumCPU())
@@ -214,10 +235,6 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
214235
d.Virtio9p = flags.Bool("xhyve-virtio-9p")
215236
d.Virtio9pFolder = "/Users"
216237

217-
// docker-machine used boot2docker.iso by default
218-
d.Vmlinuz = "vmlinuz64"
219-
d.Initrd = "initrd.img"
220-
221238
return nil
222239
}
223240

@@ -376,7 +393,6 @@ func (d *Driver) Create() error {
376393
return err
377394
}
378395

379-
log.Infof("Extracting %s and %s from %s...", d.Vmlinuz, d.Initrd, isoFilename)
380396
if err := d.extractKernelImages(); err != nil {
381397
return err
382398
}
@@ -594,31 +610,53 @@ func (d *Driver) publicSSHKeyPath() string {
594610
return d.GetSSHKeyPath() + ".pub"
595611
}
596612

597-
func (d *Driver) extractKernelImages() (err error) {
613+
func (d *Driver) extractKernelImages() error {
598614
log.Debugf("Mounting %s", isoFilename)
599615

600616
volumeRootDir := d.ResolveStorePath(isoMountPath)
601-
err = hdiutil("attach", d.ResolveStorePath(isoFilename), "-mountpoint", volumeRootDir)
617+
err := hdiutil("attach", d.ResolveStorePath(isoFilename), "-mountpoint", volumeRootDir)
602618
if err != nil {
603619
return err
604620
}
605621

606-
defer func() {
622+
defer func() error {
607623
log.Debugf("Unmounting %s", isoFilename)
608-
err = hdiutil("detach", volumeRootDir)
624+
return hdiutil("detach", volumeRootDir)
609625
}()
610626

611-
for _, f := range []string{"vmlinux", "vmlinuz64", "vmlinuz", "bzImage", "initrd", "initrd.gz", "initrd.img"} {
612-
p := filepath.Join(volumeRootDir, "boot", f)
613-
dest := d.ResolveStorePath(f)
614-
if vmnet.IsExist(p) {
615-
log.Debugf("Extracting %s into %s", p, dest)
616-
if err := mcnutils.CopyFile(p, dest); err != nil {
617-
return err
627+
if d.BootKernel == "" && d.BootInitrd == "" {
628+
err = filepath.Walk(volumeRootDir, func(path string, f os.FileInfo, err error) error {
629+
if kernelRegexp.MatchString(path) {
630+
d.BootKernel = path
631+
_, d.Vmlinuz = filepath.Split(path)
618632
}
633+
if strings.Contains(path, "initrd") {
634+
d.BootInitrd = path
635+
_, d.Initrd = filepath.Split(path)
636+
}
637+
return nil
638+
})
639+
}
640+
641+
if err != nil {
642+
if err != nil || d.BootKernel == "" || d.BootInitrd == "" {
643+
err = fmt.Errorf("==== Can't extract Kernel and Ramdisk file ====")
644+
return err
619645
}
620646
}
621647

648+
dest := d.ResolveStorePath(d.Vmlinuz)
649+
log.Debugf("Extracting %s into %s", d.BootKernel, dest)
650+
if err := mcnutils.CopyFile(d.BootKernel, dest); err != nil {
651+
return err
652+
}
653+
654+
dest = d.ResolveStorePath(d.Initrd)
655+
log.Debugf("Extracting %s into %s", d.BootInitrd, dest)
656+
if err := mcnutils.CopyFile(d.BootInitrd, dest); err != nil {
657+
return err
658+
}
659+
622660
return nil
623661
}
624662

@@ -915,20 +953,6 @@ func (d *Driver) xhyveArgs() []string {
915953
diskImage = fmt.Sprintf("4:0,ahci-hd,%s", imgPath)
916954
}
917955

918-
switch {
919-
case vmnet.IsExist(d.ResolveStorePath("vmlinuz64")):
920-
d.Vmlinuz = "vmlinuz64"
921-
case vmnet.IsExist(d.ResolveStorePath("bzImage")):
922-
d.Vmlinuz = "bzImage"
923-
}
924-
925-
switch {
926-
case vmnet.IsExist(d.ResolveStorePath("initrd.img")):
927-
d.Initrd = "initrd.img"
928-
case vmnet.IsExist(d.ResolveStorePath("initrd")):
929-
d.Initrd = "initrd"
930-
}
931-
932956
vmlinuz := d.ResolveStorePath(d.Vmlinuz)
933957
initrd := d.ResolveStorePath(d.Initrd)
934958

0 commit comments

Comments
 (0)