Implements initrd support
This commit is contained in:
		| @@ -57,8 +57,9 @@ const ( | |||||||
|  |  | ||||||
| // Kernel describe kernel parameters for qemu | // Kernel describe kernel parameters for qemu | ||||||
| type Kernel struct { | type Kernel struct { | ||||||
| 	Name string | 	Name       string | ||||||
| 	Path string | 	KernelPath string | ||||||
|  | 	InitrdPath string | ||||||
| } | } | ||||||
|  |  | ||||||
| // QemuSystem describe qemu parameters and runned process | // 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 = &QemuSystem{} | ||||||
| 	q.arch = arch | 	q.arch = arch | ||||||
|  |  | ||||||
| 	if _, err = os.Stat(kernel.Path); err != nil { | 	if _, err = os.Stat(kernel.KernelPath); err != nil { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	q.kernel = kernel | 	q.kernel = kernel | ||||||
| @@ -163,7 +164,7 @@ func (q *QemuSystem) Start() (err error) { | |||||||
| 	hostfwd := fmt.Sprintf("hostfwd=tcp:%s-:22", q.sshAddrPort) | 	hostfwd := fmt.Sprintf("hostfwd=tcp:%s-:22", q.sshAddrPort) | ||||||
| 	qemuArgs := []string{"-snapshot", "-nographic", | 	qemuArgs := []string{"-snapshot", "-nographic", | ||||||
| 		"-hda", q.drivePath, | 		"-hda", q.drivePath, | ||||||
| 		"-kernel", q.kernel.Path, | 		"-kernel", q.kernel.KernelPath, | ||||||
| 		"-append", "root=/dev/sda ignore_loglevel console=ttyS0 rw", | 		"-append", "root=/dev/sda ignore_loglevel console=ttyS0 rw", | ||||||
| 		"-smp", fmt.Sprintf("%d", q.Cpus), | 		"-smp", fmt.Sprintf("%d", q.Cpus), | ||||||
| 		"-m", fmt.Sprintf("%d", q.Memory), | 		"-m", fmt.Sprintf("%d", q.Memory), | ||||||
| @@ -171,6 +172,10 @@ func (q *QemuSystem) Start() (err error) { | |||||||
| 		"-netdev", "user,id=n1," + hostfwd, | 		"-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() { | 	if (q.arch == X86_64 || q.arch == I386) && kvmExists() { | ||||||
| 		qemuArgs = append(qemuArgs, "-enable-kvm") | 		qemuArgs = append(qemuArgs, "-enable-kvm") | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func TestQemuSystemNew_InvalidKernelPath(t *testing.T) { | 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 { | 	if _, err := NewQemuSystem(X86_64, kernel, "/bin/sh"); err == nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| @@ -20,7 +20,7 @@ func TestQemuSystemNew_InvalidKernelPath(t *testing.T) { | |||||||
|  |  | ||||||
| func TestQemuSystemNew_InvalidQemuArch(t *testing.T) { | func TestQemuSystemNew_InvalidQemuArch(t *testing.T) { | ||||||
| 	// FIXME put kernel image to path not just "any valid path" | 	// 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 { | 	if _, err := NewQemuSystem(unsupported, kernel, "/bin/sh"); err == nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| @@ -28,7 +28,7 @@ func TestQemuSystemNew_InvalidQemuArch(t *testing.T) { | |||||||
|  |  | ||||||
| func TestQemuSystemNew_InvalidQemuDrivePath(t *testing.T) { | func TestQemuSystemNew_InvalidQemuDrivePath(t *testing.T) { | ||||||
| 	// FIXME put kernel image to path not just "any valid path" | 	// 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 { | 	if _, err := NewQemuSystem(X86_64, kernel, "/invalid/path"); err == nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| @@ -36,14 +36,14 @@ func TestQemuSystemNew_InvalidQemuDrivePath(t *testing.T) { | |||||||
|  |  | ||||||
| func TestQemuSystemNew(t *testing.T) { | func TestQemuSystemNew(t *testing.T) { | ||||||
| 	// FIXME put kernel image to path not just "any valid path" | 	// 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 { | 	if _, err := NewQemuSystem(X86_64, kernel, "/bin/sh"); err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestQemuSystemStart(t *testing.T) { | 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") | 	qemu, err := NewQemuSystem(X86_64, kernel, "/bin/sh") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| @@ -57,7 +57,7 @@ func TestQemuSystemStart(t *testing.T) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func TestQemuSystemStart_Timeout(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") | 	qemu, err := NewQemuSystem(X86_64, kernel, "/bin/sh") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| @@ -90,7 +90,11 @@ func TestGetFreeAddrPort(t *testing.T) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func startTestQemu() (q *QemuSystem, err error) { | 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) | 	q, err = NewQemuSystem(X86_64, kernel, testConfigRootfs) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return | 		return | ||||||
| @@ -107,6 +111,9 @@ func startTestQemu() (q *QemuSystem, err error) { | |||||||
|  |  | ||||||
| func TestQemuSystemCommand(t *testing.T) { | func TestQemuSystemCommand(t *testing.T) { | ||||||
| 	qemu, err := startTestQemu() | 	qemu, err := startTestQemu() | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
| 	defer qemu.Stop() | 	defer qemu.Stop() | ||||||
|  |  | ||||||
| 	output, err := qemu.Command("root", "cat /etc/shadow") | 	output, err := qemu.Command("root", "cat /etc/shadow") | ||||||
|   | |||||||
| @@ -5,4 +5,5 @@ | |||||||
| package qemukernel | package qemukernel | ||||||
|  |  | ||||||
| const testConfigVmlinuz = "tools/qemu-debian-img/vmlinuz-bionic" | const testConfigVmlinuz = "tools/qemu-debian-img/vmlinuz-bionic" | ||||||
|  | const testConfigInitrd = "tools/qemu-debian-img/initrd-bionic" | ||||||
| const testConfigRootfs = "tools/qemu-debian-img/bionic.img" | 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 | *.img | ||||||
| *.log | *.log | ||||||
| vmlinuz* | 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 |     qemu-debian-img generate --repository="http://archive.ubuntu.com/ubuntu" --release="bionic" bionic.img | ||||||
|     cp bionic.img /vagrant/ |     cp bionic.img /vagrant/ | ||||||
|     cp /boot/vmlinuz-* /vagrant/vmlinuz-bionic |     cp /boot/vmlinuz-* /vagrant/vmlinuz-bionic | ||||||
|  |     cp /boot/initrd-* /vagrant/vmlinuz-bionic | ||||||
|   SHELL |   SHELL | ||||||
| end | end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user