Implements initrd support
This commit is contained in:
		| @@ -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") | ||||
| 	} | ||||
|   | ||||
| @@ -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") | ||||
|   | ||||
| @@ -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" | ||||
|   | ||||
							
								
								
									
										1
									
								
								tools/qemu-debian-img/.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								tools/qemu-debian-img/.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,3 +1,4 @@ | ||||
| *.img | ||||
| *.log | ||||
| vmlinuz* | ||||
| initrd* | ||||
|   | ||||
							
								
								
									
										1
									
								
								tools/qemu-debian-img/Vagrantfile
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								tools/qemu-debian-img/Vagrantfile
									
									
									
									
										vendored
									
									
								
							| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user