From bb8a0a013b979837ffb8e5fa57bd378372c08b3b Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sat, 22 Sep 2018 10:34:43 +0000 Subject: [PATCH] Implements initrd support --- qemu-kernel.go | 13 +++++++++---- qemu-kernel_test.go | 21 ++++++++++++++------- test.config.go | 1 + tools/qemu-debian-img/.gitignore | 1 + tools/qemu-debian-img/Vagrantfile | 1 + 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/qemu-kernel.go b/qemu-kernel.go index 2b69080..98b4be2 100644 --- a/qemu-kernel.go +++ b/qemu-kernel.go @@ -57,8 +57,9 @@ const ( // Kernel describe kernel parameters for qemu type Kernel struct { - Name string - Path string + Name string + KernelPath string + InitrdPath string } // QemuSystem describe qemu parameters and runned process @@ -98,7 +99,7 @@ func NewQemuSystem(arch arch, kernel Kernel, drivePath string) (q *QemuSystem, e q = &QemuSystem{} q.arch = arch - if _, err = os.Stat(kernel.Path); err != nil { + if _, err = os.Stat(kernel.KernelPath); err != nil { return } q.kernel = kernel @@ -163,7 +164,7 @@ func (q *QemuSystem) Start() (err error) { hostfwd := fmt.Sprintf("hostfwd=tcp:%s-:22", q.sshAddrPort) qemuArgs := []string{"-snapshot", "-nographic", "-hda", q.drivePath, - "-kernel", q.kernel.Path, + "-kernel", q.kernel.KernelPath, "-append", "root=/dev/sda ignore_loglevel console=ttyS0 rw", "-smp", fmt.Sprintf("%d", q.Cpus), "-m", fmt.Sprintf("%d", q.Memory), @@ -171,6 +172,10 @@ func (q *QemuSystem) Start() (err error) { "-netdev", "user,id=n1," + hostfwd, } + if q.kernel.InitrdPath != "" { + qemuArgs = append(qemuArgs, "-initrd", q.kernel.InitrdPath) + } + if (q.arch == X86_64 || q.arch == I386) && kvmExists() { qemuArgs = append(qemuArgs, "-enable-kvm") } diff --git a/qemu-kernel_test.go b/qemu-kernel_test.go index 71a25ef..e950e7a 100644 --- a/qemu-kernel_test.go +++ b/qemu-kernel_test.go @@ -12,7 +12,7 @@ import ( ) func TestQemuSystemNew_InvalidKernelPath(t *testing.T) { - kernel := Kernel{Name: "Invalid", Path: "/invalid/path"} + kernel := Kernel{Name: "Invalid", KernelPath: "/invalid/path"} if _, err := NewQemuSystem(X86_64, kernel, "/bin/sh"); err == nil { t.Fatal(err) } @@ -20,7 +20,7 @@ func TestQemuSystemNew_InvalidKernelPath(t *testing.T) { func TestQemuSystemNew_InvalidQemuArch(t *testing.T) { // FIXME put kernel image to path not just "any valid path" - kernel := Kernel{Name: "Valid path", Path: "/bin/sh"} + kernel := Kernel{Name: "Valid path", KernelPath: "/bin/sh"} if _, err := NewQemuSystem(unsupported, kernel, "/bin/sh"); err == nil { t.Fatal(err) } @@ -28,7 +28,7 @@ func TestQemuSystemNew_InvalidQemuArch(t *testing.T) { func TestQemuSystemNew_InvalidQemuDrivePath(t *testing.T) { // FIXME put kernel image to path not just "any valid path" - kernel := Kernel{Name: "Valid path", Path: "/bin/sh"} + kernel := Kernel{Name: "Valid path", KernelPath: "/bin/sh"} if _, err := NewQemuSystem(X86_64, kernel, "/invalid/path"); err == nil { t.Fatal(err) } @@ -36,14 +36,14 @@ func TestQemuSystemNew_InvalidQemuDrivePath(t *testing.T) { func TestQemuSystemNew(t *testing.T) { // FIXME put kernel image to path not just "any valid path" - kernel := Kernel{Name: "Valid path", Path: "/bin/sh"} + kernel := Kernel{Name: "Valid path", KernelPath: "/bin/sh"} if _, err := NewQemuSystem(X86_64, kernel, "/bin/sh"); err != nil { t.Fatal(err) } } func TestQemuSystemStart(t *testing.T) { - kernel := Kernel{Name: "Host kernel", Path: testConfigVmlinuz} + kernel := Kernel{Name: "Test kernel", KernelPath: testConfigVmlinuz} qemu, err := NewQemuSystem(X86_64, kernel, "/bin/sh") if err != nil { t.Fatal(err) @@ -57,7 +57,7 @@ func TestQemuSystemStart(t *testing.T) { } func TestQemuSystemStart_Timeout(t *testing.T) { - kernel := Kernel{Name: "Host kernel", Path: testConfigVmlinuz} + kernel := Kernel{Name: "Test kernel", KernelPath: testConfigVmlinuz} qemu, err := NewQemuSystem(X86_64, kernel, "/bin/sh") if err != nil { t.Fatal(err) @@ -90,7 +90,11 @@ func TestGetFreeAddrPort(t *testing.T) { } func startTestQemu() (q *QemuSystem, err error) { - kernel := Kernel{Name: "Host kernel", Path: testConfigVmlinuz} + kernel := Kernel{ + Name: "Test kernel", + KernelPath: testConfigVmlinuz, + InitrdPath: testConfigInitrd, + } q, err = NewQemuSystem(X86_64, kernel, testConfigRootfs) if err != nil { return @@ -107,6 +111,9 @@ func startTestQemu() (q *QemuSystem, err error) { func TestQemuSystemCommand(t *testing.T) { qemu, err := startTestQemu() + if err != nil { + t.Fatal(err) + } defer qemu.Stop() output, err := qemu.Command("root", "cat /etc/shadow") diff --git a/test.config.go b/test.config.go index 036d9c3..69f0f66 100644 --- a/test.config.go +++ b/test.config.go @@ -5,4 +5,5 @@ package qemukernel const testConfigVmlinuz = "tools/qemu-debian-img/vmlinuz-bionic" +const testConfigInitrd = "tools/qemu-debian-img/initrd-bionic" const testConfigRootfs = "tools/qemu-debian-img/bionic.img" diff --git a/tools/qemu-debian-img/.gitignore b/tools/qemu-debian-img/.gitignore index 9dabb3e..d7d027b 100644 --- a/tools/qemu-debian-img/.gitignore +++ b/tools/qemu-debian-img/.gitignore @@ -1,3 +1,4 @@ *.img *.log vmlinuz* +initrd* diff --git a/tools/qemu-debian-img/Vagrantfile b/tools/qemu-debian-img/Vagrantfile index 67844d5..7e84d1e 100644 --- a/tools/qemu-debian-img/Vagrantfile +++ b/tools/qemu-debian-img/Vagrantfile @@ -13,5 +13,6 @@ Vagrant.configure("2") do |config| qemu-debian-img generate --repository="http://archive.ubuntu.com/ubuntu" --release="bionic" bionic.img cp bionic.img /vagrant/ cp /boot/vmlinuz-* /vagrant/vmlinuz-bionic + cp /boot/initrd-* /vagrant/vmlinuz-bionic SHELL end