mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-23 11:00:25 +00:00
Merge pull request #1873 from justincormack/qemu-cleanup
Clean up of Qemu run
This commit is contained in:
commit
95a1491151
@ -19,7 +19,7 @@ const QemuImg = "linuxkit/qemu:17f052263d63c8a2b641ad91c589edcbb8a18c82"
|
|||||||
|
|
||||||
// QemuConfig contains the config for Qemu
|
// QemuConfig contains the config for Qemu
|
||||||
type QemuConfig struct {
|
type QemuConfig struct {
|
||||||
Prefix string
|
Path string
|
||||||
ISO bool
|
ISO bool
|
||||||
UEFI bool
|
UEFI bool
|
||||||
Kernel bool
|
Kernel bool
|
||||||
@ -42,18 +42,20 @@ func runQemu(args []string) {
|
|||||||
invoked := filepath.Base(os.Args[0])
|
invoked := filepath.Base(os.Args[0])
|
||||||
flags := flag.NewFlagSet("qemu", flag.ExitOnError)
|
flags := flag.NewFlagSet("qemu", flag.ExitOnError)
|
||||||
flags.Usage = func() {
|
flags.Usage = func() {
|
||||||
fmt.Printf("USAGE: %s run qemu [options] prefix\n\n", invoked)
|
fmt.Printf("USAGE: %s run qemu [options] path\n\n", invoked)
|
||||||
fmt.Printf("'prefix' specifies the path to the VM image.\n")
|
fmt.Printf("'path' specifies the path to the VM image.\n")
|
||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
fmt.Printf("Options:\n")
|
fmt.Printf("Options:\n")
|
||||||
flags.PrintDefaults()
|
flags.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine Flags
|
// Display flags
|
||||||
enableGUI := flags.Bool("gui", false, "Set qemu to use video output instead of stdio")
|
enableGUI := flags.Bool("gui", false, "Set qemu to use video output instead of stdio")
|
||||||
uefiBoot := flags.Bool("uefi", false, "Set UEFI boot from 'prefix'-efi.iso")
|
|
||||||
isoBoot := flags.Bool("iso", false, "Set Legacy BIOS boot from 'prefix'.iso")
|
// Boot type; we try to determine automatically
|
||||||
kernelBoot := flags.Bool("kernel", true, "Set boot using 'prefix'-kernel/-initrd/-cmdline")
|
uefiBoot := flags.Bool("uefi", false, "Use UEFI boot")
|
||||||
|
isoBoot := flags.Bool("iso", false, "Boot image is an ISO")
|
||||||
|
kernelBoot := flags.Bool("kernel", false, "Boot image is kernel+initrd+cmdline 'path'-kernel/-initrd/-cmdline")
|
||||||
|
|
||||||
// Paths and settings for disks
|
// Paths and settings for disks
|
||||||
disk := flags.String("disk", "", "Path to disk image to use")
|
disk := flags.String("disk", "", "Path to disk image to use")
|
||||||
@ -80,19 +82,45 @@ func runQemu(args []string) {
|
|||||||
remArgs := flags.Args()
|
remArgs := flags.Args()
|
||||||
|
|
||||||
if len(remArgs) == 0 {
|
if len(remArgs) == 0 {
|
||||||
fmt.Println("Please specify the prefix to the image to boot")
|
fmt.Println("Please specify the path to the image to boot")
|
||||||
flags.Usage()
|
flags.Usage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
prefix := remArgs[0]
|
path := remArgs[0]
|
||||||
|
|
||||||
// Print warning if conflicting UEFI and ISO flags are set
|
_, err := os.Stat(path)
|
||||||
if *uefiBoot && *isoBoot {
|
stat := err == nil
|
||||||
log.Warnf("Both -iso and -uefi have been used")
|
|
||||||
|
// if the path does not exist, must be trying to do a kernel boot
|
||||||
|
if !stat {
|
||||||
|
_, err = os.Stat(path + "-kernel")
|
||||||
|
statKernel := err == nil
|
||||||
|
if statKernel {
|
||||||
|
*kernelBoot = true
|
||||||
|
}
|
||||||
|
// we will error out later if neither found
|
||||||
|
} else {
|
||||||
|
// if path ends in .iso they meant an ISO
|
||||||
|
if strings.HasSuffix(path, ".iso") {
|
||||||
|
*isoBoot = true
|
||||||
|
}
|
||||||
|
// autodetect EFI ISO from our default naming
|
||||||
|
if strings.HasSuffix(path, "-efi.iso") {
|
||||||
|
*uefiBoot = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// user not trying to boot off ISO or kernel, so assume booting from a disk image
|
||||||
|
if !*kernelBoot && !*isoBoot {
|
||||||
|
if *disk != "" {
|
||||||
|
// Need to add multiple disk support to do this
|
||||||
|
log.Fatalf("Cannot boot from disk and specify a disk as well at present")
|
||||||
|
}
|
||||||
|
*disk = path
|
||||||
}
|
}
|
||||||
|
|
||||||
config := QemuConfig{
|
config := QemuConfig{
|
||||||
Prefix: prefix,
|
Path: path,
|
||||||
ISO: *isoBoot,
|
ISO: *isoBoot,
|
||||||
UEFI: *uefiBoot,
|
UEFI: *uefiBoot,
|
||||||
Kernel: *kernelBoot,
|
Kernel: *kernelBoot,
|
||||||
@ -110,7 +138,6 @@ func runQemu(args []string) {
|
|||||||
|
|
||||||
config = discoverBackend(config)
|
config = discoverBackend(config)
|
||||||
|
|
||||||
var err error
|
|
||||||
if config.Containerized {
|
if config.Containerized {
|
||||||
err = runQemuContainer(config)
|
err = runQemuContainer(config)
|
||||||
} else {
|
} else {
|
||||||
@ -169,10 +196,10 @@ func runQemuLocal(config QemuConfig) error {
|
|||||||
|
|
||||||
func runQemuContainer(config QemuConfig) error {
|
func runQemuContainer(config QemuConfig) error {
|
||||||
var wd string
|
var wd string
|
||||||
if filepath.IsAbs(config.Prefix) {
|
if filepath.IsAbs(config.Path) {
|
||||||
// Split the path
|
// Split the path
|
||||||
wd, config.Prefix = filepath.Split(config.Prefix)
|
wd, config.Path = filepath.Split(config.Path)
|
||||||
log.Debugf("Prefix: %s", config.Prefix)
|
log.Debugf("Path: %s", config.Path)
|
||||||
} else {
|
} else {
|
||||||
var err error
|
var err error
|
||||||
wd, err = os.Getwd()
|
wd, err = os.Getwd()
|
||||||
@ -267,33 +294,26 @@ func buildQemuCmdline(config QemuConfig) (QemuConfig, []string) {
|
|||||||
qemuArgs = append(qemuArgs, "-drive", "file="+config.DiskPath+",format="+config.DiskFormat+",index=0,media=disk")
|
qemuArgs = append(qemuArgs, "-drive", "file="+config.DiskPath+",format="+config.DiskFormat+",index=0,media=disk")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check flags for iso/uefi boot and if so disable kernel boot
|
|
||||||
if config.ISO {
|
if config.ISO {
|
||||||
config.Kernel = false
|
qemuArgs = append(qemuArgs, "-cdrom", config.Path)
|
||||||
qemuIsoPath := buildPath(config.Prefix, ".iso")
|
qemuArgs = append(qemuArgs, "-boot", "d")
|
||||||
qemuArgs = append(qemuArgs, "-cdrom", qemuIsoPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.UEFI {
|
if config.UEFI {
|
||||||
config.Kernel = false
|
|
||||||
qemuIsoPath := buildPath(config.Prefix, "-efi.iso")
|
|
||||||
qemuArgs = append(qemuArgs, "-pflash", config.FWPath)
|
qemuArgs = append(qemuArgs, "-pflash", config.FWPath)
|
||||||
qemuArgs = append(qemuArgs, "-cdrom", qemuIsoPath)
|
|
||||||
qemuArgs = append(qemuArgs, "-boot", "d")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// build kernel boot config from kernel/initrd/cmdline
|
// build kernel boot config from kernel/initrd/cmdline
|
||||||
if config.Kernel {
|
if config.Kernel {
|
||||||
qemuKernelPath := buildPath(config.Prefix, "-kernel")
|
qemuKernelPath := buildPath(config.Path, "-kernel")
|
||||||
qemuInitrdPath := buildPath(config.Prefix, "-initrd.img")
|
qemuInitrdPath := buildPath(config.Path, "-initrd.img")
|
||||||
qemuArgs = append(qemuArgs, "-kernel", qemuKernelPath)
|
qemuArgs = append(qemuArgs, "-kernel", qemuKernelPath)
|
||||||
qemuArgs = append(qemuArgs, "-initrd", qemuInitrdPath)
|
qemuArgs = append(qemuArgs, "-initrd", qemuInitrdPath)
|
||||||
consoleString, err := ioutil.ReadFile(config.Prefix + "-cmdline")
|
cmdlineString, err := ioutil.ReadFile(config.Path + "-cmdline")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Infof(" %s\n defaulting to console output", err.Error())
|
log.Errorf("Cannot open cmdline file: %v", err)
|
||||||
qemuArgs = append(qemuArgs, "-append", "console=ttyS0 console=tty0 page_poison=1")
|
|
||||||
} else {
|
} else {
|
||||||
qemuArgs = append(qemuArgs, "-append", string(consoleString))
|
qemuArgs = append(qemuArgs, "-append", string(cmdlineString))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,5 +23,5 @@ moby build -name "${NAME}" test.yml
|
|||||||
[ -f "${NAME}-kernel" ] || exit 1
|
[ -f "${NAME}-kernel" ] || exit 1
|
||||||
[ -f "${NAME}-initrd.img" ] || exit 1
|
[ -f "${NAME}-initrd.img" ] || exit 1
|
||||||
[ -f "${NAME}-cmdline" ]|| exit 1
|
[ -f "${NAME}-cmdline" ]|| exit 1
|
||||||
linuxkit run qemu "${NAME}" | grep -q "Welcome to LinuxKit"
|
linuxkit run qemu -kernel "${NAME}" | grep -q "Welcome to LinuxKit"
|
||||||
exit 0
|
exit 0
|
||||||
|
@ -20,5 +20,5 @@ trap clean_up EXIT
|
|||||||
|
|
||||||
moby build -name "${NAME}" test.yml
|
moby build -name "${NAME}" test.yml
|
||||||
[ -f "${NAME}.iso" ] || exit 1
|
[ -f "${NAME}.iso" ] || exit 1
|
||||||
linuxkit run qemu -iso "${NAME}" | grep -q "Welcome to LinuxKit"
|
linuxkit run qemu -iso "${NAME}.iso" | grep -q "Welcome to LinuxKit"
|
||||||
exit 0
|
exit 0
|
||||||
|
@ -27,5 +27,5 @@ fi
|
|||||||
|
|
||||||
moby build -name "${NAME}" test.yml
|
moby build -name "${NAME}" test.yml
|
||||||
[ -f "${NAME}-efi.iso" ] || exit 1
|
[ -f "${NAME}-efi.iso" ] || exit 1
|
||||||
linuxkit run qemu -uefi "${NAME}" | grep -q "Welcome to LinuxKit"
|
linuxkit run qemu -iso -uefi "${NAME}-efi.iso" | grep -q "Welcome to LinuxKit"
|
||||||
exit 0
|
exit 0
|
||||||
|
25
test/cases/010_platforms/000_qemu/030_run_qcow/test.sh
Normal file
25
test/cases/010_platforms/000_qemu/030_run_qcow/test.sh
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# SUMMARY: Check that qcow2 image boots in qemu
|
||||||
|
# LABELS:
|
||||||
|
# AUTHOR: Dave Tucker <dt@docker.com>
|
||||||
|
# AUTHOR: Justin Cormack <justin.cormack@docker.com>
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Source libraries. Uncomment if needed/defined
|
||||||
|
#. "${RT_LIB}"
|
||||||
|
. "${RT_PROJECT_ROOT}/_lib/lib.sh"
|
||||||
|
|
||||||
|
NAME=qemu-qcow2
|
||||||
|
|
||||||
|
clean_up() {
|
||||||
|
# remove any files, containers, images etc
|
||||||
|
rm -rf ${NAME}* || true
|
||||||
|
}
|
||||||
|
|
||||||
|
trap clean_up EXIT
|
||||||
|
|
||||||
|
moby build -name "${NAME}" test.yml
|
||||||
|
[ -f "${NAME}.qcow2" ] || exit 1
|
||||||
|
linuxkit run qemu -disk-format qcow2 "${NAME}.qcow2" | grep -q "Welcome to LinuxKit"
|
||||||
|
exit 0
|
16
test/cases/010_platforms/000_qemu/030_run_qcow/test.yml
Normal file
16
test/cases/010_platforms/000_qemu/030_run_qcow/test.yml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
kernel:
|
||||||
|
image: "linuxkit/kernel:4.9.x"
|
||||||
|
cmdline: "console=ttyS0 console=tty0 page_poison=1"
|
||||||
|
init:
|
||||||
|
- linuxkit/init:f71c3b30ac1ba4ef16c160c89610fa4976f9752f
|
||||||
|
- linuxkit/runc:b0fb122e10dbb7e4e45115177a61a3f8d68c19a9
|
||||||
|
- linuxkit/containerd:60e2486a74c665ba4df57e561729aec20758daed
|
||||||
|
onboot:
|
||||||
|
- name: poweroff
|
||||||
|
image: "linuxkit/poweroff:a8f1e4ad8d459f1fdaad9e4b007512cb3b504ae8"
|
||||||
|
command: ["/bin/sh", "/poweroff.sh", "10"]
|
||||||
|
trust:
|
||||||
|
image:
|
||||||
|
- linuxkit/kernel
|
||||||
|
outputs:
|
||||||
|
- format: qcow2
|
@ -17,7 +17,7 @@ trap clean_up EXIT
|
|||||||
|
|
||||||
# Test code goes here
|
# Test code goes here
|
||||||
moby build test-kernel-config
|
moby build test-kernel-config
|
||||||
RESULT="$(linuxkit run qemu test-kernel-config)"
|
RESULT="$(linuxkit run qemu -kernel test-kernel-config)"
|
||||||
echo "${RESULT}" | grep -q "suite PASSED"
|
echo "${RESULT}" | grep -q "suite PASSED"
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
@ -17,7 +17,7 @@ trap clean_up EXIT
|
|||||||
|
|
||||||
# Test code goes here
|
# Test code goes here
|
||||||
moby build test-sysctl
|
moby build test-sysctl
|
||||||
RESULT="$(linuxkit run qemu test-sysctl)"
|
RESULT="$(linuxkit run qemu -kernel test-sysctl)"
|
||||||
echo "${RESULT}" | grep -q "suite PASSED"
|
echo "${RESULT}" | grep -q "suite PASSED"
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
Loading…
Reference in New Issue
Block a user