cmd: Add support for kernel+squashfs to the qemu runner

Unlike the hyperkit runner, the qemu runner already had better
support for auto-detecting the boot method so the changes
are less invasive (and backward compatible).

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2018-04-16 17:48:41 +01:00
parent 0e3c88d47c
commit 0b5ea3fcc3

View File

@ -29,6 +29,7 @@ type QemuConfig struct {
Path string Path string
ISOBoot bool ISOBoot bool
UEFI bool UEFI bool
SquashFS bool
Kernel bool Kernel bool
GUI bool GUI bool
Disks Disks Disks Disks
@ -139,6 +140,7 @@ func runQemu(args []string) {
// Boot type; we try to determine automatically // Boot type; we try to determine automatically
uefiBoot := flags.Bool("uefi", false, "Use UEFI boot") uefiBoot := flags.Bool("uefi", false, "Use UEFI boot")
isoBoot := flags.Bool("iso", false, "Boot image is an ISO") isoBoot := flags.Bool("iso", false, "Boot image is an ISO")
squashFSBoot := flags.Bool("squashfs", false, "Boot image is a kernel+squashfs+cmdline")
kernelBoot := flags.Bool("kernel", false, "Boot image is kernel+initrd+cmdline 'path'-kernel/-initrd/-cmdline") kernelBoot := flags.Bool("kernel", false, "Boot image is kernel+initrd+cmdline 'path'-kernel/-initrd/-cmdline")
// State flags // State flags
@ -199,12 +201,18 @@ func runQemu(args []string) {
_, err := os.Stat(path) _, err := os.Stat(path)
stat := err == nil stat := err == nil
// if the path does not exist, must be trying to do a kernel boot // if the path does not exist, must be trying to do a kernel+initrd or kernel+squashfs boot
if !stat { if !stat {
_, err = os.Stat(path + "-kernel") _, err = os.Stat(path + "-kernel")
statKernel := err == nil statKernel := err == nil
if statKernel { if statKernel {
*kernelBoot = true _, err = os.Stat(path + "-squashfs.img")
statSquashFS := err == nil
if statSquashFS {
*squashFSBoot = true
} else {
*kernelBoot = true
}
} }
// we will error out later if neither found // we will error out later if neither found
} else { } else {
@ -252,13 +260,19 @@ func runQemu(args []string) {
disks[i] = d disks[i] = d
} }
// user not trying to boot off ISO or kernel, so assume booting from a disk image // user not trying to boot off ISO or kernel+initrd, so assume booting from a disk image or kernel+squashfs
if !*kernelBoot && !*isoBoot { if !*kernelBoot && !*isoBoot {
if _, err := os.Stat(path); err != nil { var diskPath string
log.Fatalf("Boot disk image %s does not exist", path) if *squashFSBoot {
diskPath = path + "-squashfs.img"
} else {
if _, err := os.Stat(path); err != nil {
log.Fatalf("Boot disk image %s does not exist", path)
}
diskPath = path
} }
// currently no way to set format, but autodetect probably works // currently no way to set format, but autodetect probably works
d := Disks{DiskConfig{Path: path}} d := Disks{DiskConfig{Path: diskPath}}
disks = append(d, disks...) disks = append(d, disks...)
} }
@ -301,6 +315,7 @@ func runQemu(args []string) {
Path: path, Path: path,
ISOBoot: *isoBoot, ISOBoot: *isoBoot,
UEFI: *uefiBoot, UEFI: *uefiBoot,
SquashFS: *squashFSBoot,
Kernel: *kernelBoot, Kernel: *kernelBoot,
GUI: *enableGUI, GUI: *enableGUI,
Disks: disks, Disks: disks,
@ -603,16 +618,28 @@ func buildQemuCmdline(config QemuConfig) (QemuConfig, []string) {
} }
// build kernel boot config from kernel/initrd/cmdline // build kernel boot config from kernel/initrd/cmdline
if config.Kernel { switch {
case config.Kernel:
qemuKernelPath := config.Path + "-kernel" qemuKernelPath := config.Path + "-kernel"
qemuInitrdPath := config.Path + "-initrd.img" qemuInitrdPath := config.Path + "-initrd.img"
qemuArgs = append(qemuArgs, "-kernel", qemuKernelPath) qemuArgs = append(qemuArgs, "-kernel", qemuKernelPath)
qemuArgs = append(qemuArgs, "-initrd", qemuInitrdPath) qemuArgs = append(qemuArgs, "-initrd", qemuInitrdPath)
cmdlineString, err := ioutil.ReadFile(config.Path + "-cmdline") cmdlineBytes, err := ioutil.ReadFile(config.Path + "-cmdline")
if err != nil { if err != nil {
log.Errorf("Cannot open cmdline file: %v", err) log.Errorf("Cannot open cmdline file: %v", err)
} else { } else {
qemuArgs = append(qemuArgs, "-append", string(cmdlineString)) qemuArgs = append(qemuArgs, "-append", string(cmdlineBytes))
}
case config.SquashFS:
qemuKernelPath := config.Path + "-kernel"
qemuArgs = append(qemuArgs, "-kernel", qemuKernelPath)
cmdlineBytes, err := ioutil.ReadFile(config.Path + "-cmdline")
if err != nil {
log.Errorf("Cannot open cmdline file: %v", err)
} else {
cmdline := string(cmdlineBytes)
cmdline += " root=/dev/sda"
qemuArgs = append(qemuArgs, "-append", cmdline)
} }
} }