Best Syzkaller code snippet using qemu.Run
driver_qemu_config_test.go
Source:driver_qemu_config_test.go
...10 normalize := func(s string) string {11 return strings.TrimSpace(indent.ReplaceAllString(s, "$1"))12 }13 runTest := func(expected string, sections []cfgSection) {14 t.Run(expected, func(t *testing.T) {15 actual := normalize(qemuStringifyCfg(sections...).String())16 expected = normalize(expected)17 if actual != expected {18 t.Errorf("Expected: %s. Got: %s", expected, actual)19 }20 })21 }22 t.Run("qemu_base", func(t *testing.T) {23 testCases := []struct {24 opts qemuBaseOpts25 expected string26 }{{27 qemuBaseOpts{"x86_64"},28 `# Machine29 [machine]30 graphics = "off"31 type = "q35"32 accel = "kvm"33 usb = "off"34 [global]35 driver = "ICH9-LPC"36 property = "disable_s3"37 value = "1"38 [global]39 driver = "ICH9-LPC"40 property = "disable_s4"41 value = "1"42 [boot-opts]43 strict = "on"`,44 }, {45 qemuBaseOpts{"aarch64"},46 `# Machine47 [machine]48 graphics = "off"49 type = "virt"50 gic-version = "max"51 accel = "kvm"52 usb = "off"53 [boot-opts]54 strict = "on"`,55 }, {56 qemuBaseOpts{"ppc64le"},57 `# Machine58 [machine]59 graphics = "off"60 type = "pseries"61 cap-large-decr = "off"62 accel = "kvm"63 usb = "off"64 [boot-opts]65 strict = "on"`,66 }, {67 qemuBaseOpts{"s390x"},68 `# Machine69 [machine]70 graphics = "off"71 type = "s390-ccw-virtio"72 accel = "kvm"73 usb = "off"74 [boot-opts]75 strict = "on"`,76 }}77 for _, tc := range testCases {78 runTest(tc.expected, qemuBase(&tc.opts))79 }80 })81 t.Run("qemu_memory", func(t *testing.T) {82 testCases := []struct {83 opts qemuMemoryOpts84 expected string85 }{{86 qemuMemoryOpts{4096},87 `# Memory88 [memory]89 size = "4096M"`,90 }, {91 qemuMemoryOpts{8192},92 `# Memory93 [memory]94 size = "8192M"`,95 }}96 for _, tc := range testCases {97 runTest(tc.expected, qemuMemory(&tc.opts))98 }99 })100 t.Run("qemu_serial", func(t *testing.T) {101 testCases := []struct {102 opts qemuSerialOpts103 expected string104 }{{105 qemuSerialOpts{qemuDevOpts{"pci", "qemu_pcie0", "00.5", false}, "qemu_serial-chardev", 32},106 `# Virtual serial bus107 [device "dev-qemu_serial"]108 driver = "virtio-serial-pci"109 bus = "qemu_pcie0"110 addr = "00.5"111 # LXD serial identifier112 [chardev "qemu_serial-chardev"]113 backend = "ringbuf"114 size = "32B"115 [device "qemu_serial"]116 driver = "virtserialport"117 name = "org.linuxcontainers.lxd"118 chardev = "qemu_serial-chardev"119 bus = "dev-qemu_serial.0"120 # Spice agent121 [chardev "qemu_spice-chardev"]122 backend = "spicevmc"123 name = "vdagent"124 [device "qemu_spice"]125 driver = "virtserialport"126 name = "com.redhat.spice.0"127 chardev = "qemu_spice-chardev"128 bus = "dev-qemu_serial.0"129 # Spice folder130 [chardev "qemu_spicedir-chardev"]131 backend = "spiceport"132 name = "org.spice-space.webdav.0"133 [device "qemu_spicedir"]134 driver = "virtserialport"135 name = "org.spice-space.webdav.0"136 chardev = "qemu_spicedir-chardev"137 bus = "dev-qemu_serial.0"138 `,139 }}140 for _, tc := range testCases {141 runTest(tc.expected, qemuSerial(&tc.opts))142 }143 })144 t.Run("qemu_pcie", func(t *testing.T) {145 testCases := []struct {146 opts qemuPCIeOpts147 expected string148 }{{149 qemuPCIeOpts{"qemu_pcie0", 0, "1.0", true},150 `[device "qemu_pcie0"]151 driver = "pcie-root-port"152 bus = "pcie.0"153 addr = "1.0"154 chassis = "0"155 multifunction = "on"156 `,157 }, {158 qemuPCIeOpts{"qemu_pcie2", 3, "2.0", false},159 `[device "qemu_pcie2"]160 driver = "pcie-root-port"161 bus = "pcie.0"162 addr = "2.0"163 chassis = "3"164 `,165 }}166 for _, tc := range testCases {167 runTest(tc.expected, qemuPCIe(&tc.opts))168 }169 })170 t.Run("qemu_scsi", func(t *testing.T) {171 testCases := []struct {172 opts qemuDevOpts173 expected string174 }{{175 qemuDevOpts{"pci", "qemu_pcie1", "00.0", false},176 `# SCSI controller177 [device "qemu_scsi"]178 driver = "virtio-scsi-pci"179 bus = "qemu_pcie1"180 addr = "00.0"181 `,182 }, {183 qemuDevOpts{"ccw", "qemu_pcie2", "00.2", true},184 `# SCSI controller185 [device "qemu_scsi"]186 driver = "virtio-scsi-ccw"187 multifunction = "on"188 `,189 }}190 for _, tc := range testCases {191 runTest(tc.expected, qemuSCSI(&tc.opts))192 }193 })194 t.Run("qemu_balloon", func(t *testing.T) {195 testCases := []struct {196 opts qemuDevOpts197 expected string198 }{{199 qemuDevOpts{"pcie", "qemu_pcie0", "00.0", true},200 `# Balloon driver201 [device "qemu_balloon"]202 driver = "virtio-balloon-pci"203 bus = "qemu_pcie0"204 addr = "00.0"205 multifunction = "on"206 `,207 }, {208 qemuDevOpts{"ccw", "qemu_pcie0", "00.0", false},209 `# Balloon driver210 [device "qemu_balloon"]211 driver = "virtio-balloon-ccw"212 `,213 }}214 for _, tc := range testCases {215 runTest(tc.expected, qemuBalloon(&tc.opts))216 }217 })218 t.Run("qemu_rng", func(t *testing.T) {219 testCases := []struct {220 opts qemuDevOpts221 expected string222 }{{223 qemuDevOpts{"pci", "qemu_pcie0", "00.1", false},224 `# Random number generator225 [object "qemu_rng"]226 qom-type = "rng-random"227 filename = "/dev/urandom"228 [device "dev-qemu_rng"]229 driver = "virtio-rng-pci"230 bus = "qemu_pcie0"231 addr = "00.1"232 rng = "qemu_rng"233 `,234 }, {235 qemuDevOpts{"ccw", "qemu_pcie0", "00.1", true},236 `# Random number generator237 [object "qemu_rng"]238 qom-type = "rng-random"239 filename = "/dev/urandom"240 [device "dev-qemu_rng"]241 driver = "virtio-rng-ccw"242 multifunction = "on"243 rng = "qemu_rng"244 `,245 }}246 for _, tc := range testCases {247 runTest(tc.expected, qemuRNG(&tc.opts))248 }249 })250 t.Run("qemu_vsock", func(t *testing.T) {251 testCases := []struct {252 opts qemuVsockOpts253 expected string254 }{{255 qemuVsockOpts{qemuDevOpts{"pcie", "qemu_pcie0", "00.4", true}, 14},256 `# Vsock257 [device "qemu_vsock"]258 driver = "vhost-vsock-pci"259 bus = "qemu_pcie0"260 addr = "00.4"261 multifunction = "on"262 guest-cid = "14"263 `,264 }, {265 qemuVsockOpts{qemuDevOpts{"ccw", "qemu_pcie0", "00.4", false}, 3},266 `# Vsock267 [device "qemu_vsock"]268 driver = "vhost-vsock-ccw"269 guest-cid = "3"270 `,271 }}272 for _, tc := range testCases {273 runTest(tc.expected, qemuVsock(&tc.opts))274 }275 })276 t.Run("qemu_gpu", func(t *testing.T) {277 testCases := []struct {278 opts qemuGpuOpts279 expected string280 }{{281 qemuGpuOpts{qemuDevOpts{"pci", "qemu_pcie3", "00.0", true}, "x86_64"},282 `# GPU283 [device "qemu_gpu"]284 driver = "virtio-vga"285 bus = "qemu_pcie3"286 addr = "00.0"287 multifunction = "on"`,288 }, {289 qemuGpuOpts{qemuDevOpts{"pci", "qemu_pci3", "00.1", false}, "otherArch"},290 `# GPU291 [device "qemu_gpu"]292 driver = "virtio-gpu-pci"293 bus = "qemu_pci3"294 addr = "00.1"`,295 }, {296 qemuGpuOpts{qemuDevOpts{"ccw", "devBus", "busAddr", true}, "arch"},297 `# GPU298 [device "qemu_gpu"]299 driver = "virtio-gpu-ccw"300 multifunction = "on"`,301 }, {302 qemuGpuOpts{qemuDevOpts{"ccw", "devBus", "busAddr", false}, "x86_64"},303 `# GPU304 [device "qemu_gpu"]305 driver = "virtio-gpu-ccw"`,306 }}307 for _, tc := range testCases {308 runTest(tc.expected, qemuGPU(&tc.opts))309 }310 })311 t.Run("qemu_keyboard", func(t *testing.T) {312 testCases := []struct {313 opts qemuDevOpts314 expected string315 }{{316 qemuDevOpts{"pci", "qemu_pcie3", "00.0", false},317 `# Input318 [device "qemu_keyboard"]319 driver = "virtio-keyboard-pci"320 bus = "qemu_pcie3"321 addr = "00.0"`,322 }, {323 qemuDevOpts{"pcie", "qemu_pcie3", "00.0", true},324 `# Input325 [device "qemu_keyboard"]326 driver = "virtio-keyboard-pci"327 bus = "qemu_pcie3"328 addr = "00.0"329 multifunction = "on"`,330 }, {331 qemuDevOpts{"ccw", "qemu_pcie3", "00.0", false},332 `# Input333 [device "qemu_keyboard"]334 driver = "virtio-keyboard-ccw"`,335 }, {336 qemuDevOpts{"ccw", "qemu_pcie3", "00.0", true},337 `# Input338 [device "qemu_keyboard"]339 driver = "virtio-keyboard-ccw"340 multifunction = "on"`,341 }}342 for _, tc := range testCases {343 runTest(tc.expected, qemuKeyboard(&tc.opts))344 }345 })346 t.Run("qemu_tablet", func(t *testing.T) {347 testCases := []struct {348 opts qemuDevOpts349 expected string350 }{{351 qemuDevOpts{"pci", "qemu_pcie0", "00.3", true},352 `# Input353 [device "qemu_tablet"]354 driver = "virtio-tablet-pci"355 bus = "qemu_pcie0"356 addr = "00.3"357 multifunction = "on"358 `,359 }, {360 qemuDevOpts{"ccw", "qemu_pcie0", "00.3", true},361 `# Input362 [device "qemu_tablet"]363 driver = "virtio-tablet-ccw"364 multifunction = "on"365 `,366 }}367 for _, tc := range testCases {368 runTest(tc.expected, qemuTablet(&tc.opts))369 }370 })371 t.Run("qemu_cpu", func(t *testing.T) {372 testCases := []struct {373 opts qemuCPUOpts374 expected string375 }{{376 qemuCPUOpts{377 architecture: "x86_64",378 cpuCount: 8,379 cpuSockets: 1,380 cpuCores: 4,381 cpuThreads: 2,382 cpuNumaNodes: []uint64{},383 cpuNumaMapping: []qemuNumaEntry{},384 cpuNumaHostNodes: []uint64{},385 hugepages: "",386 memory: 7629,387 qemuMemObjectFormat: "repeated",388 },389 `# CPU390 [smp-opts]391 cpus = "8"392 sockets = "1"393 cores = "4"394 threads = "2"395 [object "mem0"]396 qom-type = "memory-backend-memfd"397 size = "7629M"398 share = "on"399 [numa]400 type = "node"401 nodeid = "0"402 memdev = "mem0"`,403 }, {404 qemuCPUOpts{405 architecture: "x86_64",406 cpuCount: 2,407 cpuSockets: 1,408 cpuCores: 2,409 cpuThreads: 1,410 cpuNumaNodes: []uint64{4, 5},411 cpuNumaMapping: []qemuNumaEntry{412 {node: 20, socket: 21, core: 22, thread: 23},413 },414 cpuNumaHostNodes: []uint64{8, 9, 10},415 hugepages: "/hugepages/path",416 memory: 12000,417 qemuMemObjectFormat: "indexed",418 },419 `# CPU420 [smp-opts]421 cpus = "2"422 sockets = "1"423 cores = "2"424 threads = "1"425 [object "mem0"]426 qom-type = "memory-backend-file"427 mem-path = "/hugepages/path"428 prealloc = "on"429 discard-data = "on"430 size = "12000M"431 policy = "bind"432 share = "on"433 host-nodes.0 = "8"434 [numa]435 type = "node"436 nodeid = "0"437 memdev = "mem0"438 [object "mem1"]439 qom-type = "memory-backend-file"440 mem-path = "/hugepages/path"441 prealloc = "on"442 discard-data = "on"443 size = "12000M"444 policy = "bind"445 share = "on"446 host-nodes.0 = "9"447 [numa]448 type = "node"449 nodeid = "1"450 memdev = "mem1"451 [object "mem2"]452 qom-type = "memory-backend-file"453 mem-path = "/hugepages/path"454 prealloc = "on"455 discard-data = "on"456 size = "12000M"457 policy = "bind"458 share = "on"459 host-nodes.0 = "10"460 [numa]461 type = "node"462 nodeid = "2"463 memdev = "mem2"464 [numa]465 type = "cpu"466 node-id = "20"467 socket-id = "21"468 core-id = "22"469 thread-id = "23"`,470 }, {471 qemuCPUOpts{472 architecture: "x86_64",473 cpuCount: 2,474 cpuSockets: 1,475 cpuCores: 2,476 cpuThreads: 1,477 cpuNumaNodes: []uint64{4, 5},478 cpuNumaMapping: []qemuNumaEntry{479 {node: 20, socket: 21, core: 22, thread: 23},480 },481 cpuNumaHostNodes: []uint64{8, 9, 10},482 hugepages: "",483 memory: 12000,484 qemuMemObjectFormat: "indexed",485 },486 `# CPU487 [smp-opts]488 cpus = "2"489 sockets = "1"490 cores = "2"491 threads = "1"492 [object "mem0"]493 qom-type = "memory-backend-memfd"494 size = "12000M"495 policy = "bind"496 host-nodes.0 = "8"497 [numa]498 type = "node"499 nodeid = "0"500 memdev = "mem0"501 [object "mem1"]502 qom-type = "memory-backend-memfd"503 size = "12000M"504 policy = "bind"505 host-nodes.0 = "9"506 [numa]507 type = "node"508 nodeid = "1"509 memdev = "mem1"510 [object "mem2"]511 qom-type = "memory-backend-memfd"512 size = "12000M"513 policy = "bind"514 host-nodes.0 = "10"515 [numa]516 type = "node"517 nodeid = "2"518 memdev = "mem2"519 [numa]520 type = "cpu"521 node-id = "20"522 socket-id = "21"523 core-id = "22"524 thread-id = "23"`,525 }, {526 qemuCPUOpts{527 architecture: "x86_64",528 cpuCount: 4,529 cpuSockets: 1,530 cpuCores: 4,531 cpuThreads: 1,532 cpuNumaNodes: []uint64{4, 5, 6},533 cpuNumaMapping: []qemuNumaEntry{534 {node: 11, socket: 12, core: 13, thread: 14},535 {node: 20, socket: 21, core: 22, thread: 23},536 },537 cpuNumaHostNodes: []uint64{8, 9, 10},538 hugepages: "",539 memory: 12000,540 qemuMemObjectFormat: "repeated",541 },542 `# CPU543 [smp-opts]544 cpus = "4"545 sockets = "1"546 cores = "4"547 threads = "1"548 [object "mem0"]549 qom-type = "memory-backend-memfd"550 size = "12000M"551 policy = "bind"552 host-nodes = "8"553 [numa]554 type = "node"555 nodeid = "0"556 memdev = "mem0"557 [object "mem1"]558 qom-type = "memory-backend-memfd"559 size = "12000M"560 policy = "bind"561 host-nodes = "9"562 [numa]563 type = "node"564 nodeid = "1"565 memdev = "mem1"566 [object "mem2"]567 qom-type = "memory-backend-memfd"568 size = "12000M"569 policy = "bind"570 host-nodes = "10"571 [numa]572 type = "node"573 nodeid = "2"574 memdev = "mem2"575 [numa]576 type = "cpu"577 node-id = "11"578 socket-id = "12"579 core-id = "13"580 thread-id = "14"581 [numa]582 type = "cpu"583 node-id = "20"584 socket-id = "21"585 core-id = "22"586 thread-id = "23"`,587 }, {588 qemuCPUOpts{589 architecture: "arm64",590 cpuCount: 4,591 cpuSockets: 1,592 cpuCores: 4,593 cpuThreads: 1,594 cpuNumaNodes: []uint64{4, 5, 6},595 cpuNumaMapping: []qemuNumaEntry{596 {node: 11, socket: 12, core: 13, thread: 14},597 {node: 20, socket: 21, core: 22, thread: 23},598 },599 cpuNumaHostNodes: []uint64{8, 9, 10},600 hugepages: "/hugepages",601 memory: 12000,602 qemuMemObjectFormat: "indexed",603 },604 `# CPU605 [smp-opts]606 cpus = "4"607 sockets = "1"608 cores = "4"609 threads = "1"`,610 }}611 for _, tc := range testCases {612 runTest(tc.expected, qemuCPU(&tc.opts))613 }614 })615 t.Run("qemu_control_socket", func(t *testing.T) {616 testCases := []struct {617 opts qemuControlSocketOpts618 expected string619 }{{620 qemuControlSocketOpts{"/dev/shm/control-socket"},621 `# Qemu control622 [chardev "monitor"]623 backend = "socket"624 path = "/dev/shm/control-socket"625 server = "on"626 wait = "off"627 [mon]628 chardev = "monitor"629 mode = "control"`,630 }}631 for _, tc := range testCases {632 runTest(tc.expected, qemuControlSocket(&tc.opts))633 }634 })635 t.Run("qemu_console", func(t *testing.T) {636 testCases := []struct {637 opts qemuConsoleOpts638 expected string639 }{{640 qemuConsoleOpts{"/dev/shm/console-socket"},641 `# Console642 [chardev "console"]643 backend = "socket"644 path = "/dev/shm/console-socket"645 server = "on"646 wait = "off"`,647 }}648 for _, tc := range testCases {649 runTest(tc.expected, qemuConsole(&tc.opts))650 }651 })652 t.Run("qemu_drive_firmware", func(t *testing.T) {653 testCases := []struct {654 opts qemuDriveFirmwareOpts655 expected string656 }{{657 qemuDriveFirmwareOpts{"/tmp/ovmf.fd", "/tmp/settings.fd"},658 `# Firmware (read only)659 [drive]660 file = "/tmp/ovmf.fd"661 if = "pflash"662 format = "raw"663 unit = "0"664 readonly = "on"665 # Firmware settings (writable)666 [drive]667 file = "/tmp/settings.fd"668 if = "pflash"669 format = "raw"670 unit = "1"`,671 }}672 for _, tc := range testCases {673 runTest(tc.expected, qemuDriveFirmware(&tc.opts))674 }675 })676 t.Run("qemu_drive_config", func(t *testing.T) {677 testCases := []struct {678 opts qemuDriveConfigOpts679 expected string680 }{{681 qemuDriveConfigOpts{682 dev: qemuDevOpts{"pci", "qemu_pcie0", "00.5", false},683 path: "/var/9p",684 protocol: "9p",685 },686 `# Config drive (9p)687 [fsdev "qemu_config"]688 fsdriver = "local"689 security_model = "none"690 readonly = "on"691 path = "/var/9p"692 [device "dev-qemu_config-drive-9p"]693 driver = "virtio-9p-pci"694 bus = "qemu_pcie0"695 addr = "00.5"696 mount_tag = "config"697 fsdev = "qemu_config"`,698 }, {699 qemuDriveConfigOpts{700 dev: qemuDevOpts{"pcie", "qemu_pcie1", "10.2", true},701 path: "/dev/virtio-fs",702 protocol: "virtio-fs",703 },704 `# Config drive (virtio-fs)705 [chardev "qemu_config"]706 backend = "socket"707 path = "/dev/virtio-fs"708 [device "dev-qemu_config-drive-virtio-fs"]709 driver = "vhost-user-fs-pci"710 bus = "qemu_pcie1"711 addr = "10.2"712 multifunction = "on"713 tag = "config"714 chardev = "qemu_config"`,715 }, {716 qemuDriveConfigOpts{717 dev: qemuDevOpts{"ccw", "qemu_pcie0", "00.0", false},718 path: "/var/virtio-fs",719 protocol: "virtio-fs",720 },721 `# Config drive (virtio-fs)722 [chardev "qemu_config"]723 backend = "socket"724 path = "/var/virtio-fs"725 [device "dev-qemu_config-drive-virtio-fs"]726 driver = "vhost-user-fs-ccw"727 tag = "config"728 chardev = "qemu_config"`,729 }, {730 qemuDriveConfigOpts{731 dev: qemuDevOpts{"ccw", "qemu_pcie0", "00.0", true},732 path: "/dev/9p",733 protocol: "9p",734 },735 `# Config drive (9p)736 [fsdev "qemu_config"]737 fsdriver = "local"738 security_model = "none"739 readonly = "on"740 path = "/dev/9p"741 [device "dev-qemu_config-drive-9p"]742 driver = "virtio-9p-ccw"743 multifunction = "on"744 mount_tag = "config"745 fsdev = "qemu_config"`,746 }, {747 qemuDriveConfigOpts{748 dev: qemuDevOpts{"ccw", "qemu_pcie0", "00.0", true},749 path: "/dev/9p",750 protocol: "invalid",751 },752 ``,753 }}754 for _, tc := range testCases {755 runTest(tc.expected, qemuDriveConfig(&tc.opts))756 }757 })758 t.Run("qemu_drive_dir", func(t *testing.T) {759 testCases := []struct {760 opts qemuDriveDirOpts761 expected string762 }{{763 qemuDriveDirOpts{764 dev: qemuDevOpts{"pci", "qemu_pcie0", "00.5", true},765 devName: "stub",766 mountTag: "mtag",767 protocol: "9p",768 readonly: false,769 proxyFD: 5,770 },771 `# stub drive (9p)772 [fsdev "lxd_stub"]773 fsdriver = "proxy"774 sock_fd = "5"775 readonly = "off"776 [device "dev-lxd_stub-9p"]777 driver = "virtio-9p-pci"778 bus = "qemu_pcie0"779 addr = "00.5"780 multifunction = "on"781 mount_tag = "mtag"782 fsdev = "lxd_stub"`,783 }, {784 qemuDriveDirOpts{785 dev: qemuDevOpts{"pcie", "qemu_pcie1", "10.2", false},786 path: "/dev/virtio",787 devName: "vfs",788 mountTag: "vtag",789 protocol: "virtio-fs",790 },791 `# vfs drive (virtio-fs)792 [chardev "lxd_vfs"]793 backend = "socket"794 path = "/dev/virtio"795 [device "dev-lxd_vfs-virtio-fs"]796 driver = "vhost-user-fs-pci"797 bus = "qemu_pcie1"798 addr = "10.2"799 tag = "vtag"800 chardev = "lxd_vfs"`,801 }, {802 qemuDriveDirOpts{803 dev: qemuDevOpts{"ccw", "qemu_pcie0", "00.0", true},804 path: "/dev/vio",805 devName: "vfs",806 mountTag: "vtag",807 protocol: "virtio-fs",808 },809 `# vfs drive (virtio-fs)810 [chardev "lxd_vfs"]811 backend = "socket"812 path = "/dev/vio"813 [device "dev-lxd_vfs-virtio-fs"]814 driver = "vhost-user-fs-ccw"815 multifunction = "on"816 tag = "vtag"817 chardev = "lxd_vfs"`,818 }, {819 qemuDriveDirOpts{820 dev: qemuDevOpts{"ccw", "qemu_pcie0", "00.0", false},821 devName: "stub2",822 mountTag: "mtag2",823 protocol: "9p",824 readonly: true,825 proxyFD: 3,826 },827 `# stub2 drive (9p)828 [fsdev "lxd_stub2"]829 fsdriver = "proxy"830 sock_fd = "3"831 readonly = "on"832 [device "dev-lxd_stub2-9p"]833 driver = "virtio-9p-ccw"834 mount_tag = "mtag2"835 fsdev = "lxd_stub2"`,836 }, {837 qemuDriveDirOpts{838 dev: qemuDevOpts{"ccw", "qemu_pcie0", "00.0", true},839 path: "/dev/9p",840 protocol: "invalid",841 },842 ``,843 }}844 for _, tc := range testCases {845 runTest(tc.expected, qemuDriveDir(&tc.opts))846 }847 })848 t.Run("qemu_pci_physical", func(t *testing.T) {849 testCases := []struct {850 opts qemuPCIPhysicalOpts851 expected string852 }{{853 qemuPCIPhysicalOpts{854 dev: qemuDevOpts{"pci", "qemu_pcie1", "00.0", false},855 devName: "physical-pci-name",856 pciSlotName: "host-slot",857 },858 `# PCI card ("physical-pci-name" device)859 [device "dev-lxd_physical-pci-name"]860 driver = "vfio-pci"861 bus = "qemu_pcie1"862 addr = "00.0"863 host = "host-slot"`,864 }, {865 qemuPCIPhysicalOpts{866 dev: qemuDevOpts{"ccw", "qemu_pcie2", "00.2", true},867 devName: "physical-ccw-name",868 pciSlotName: "host-slot-ccw",869 },870 `# PCI card ("physical-ccw-name" device)871 [device "dev-lxd_physical-ccw-name"]872 driver = "vfio-ccw"873 multifunction = "on"874 host = "host-slot-ccw"`,875 }}876 for _, tc := range testCases {877 runTest(tc.expected, qemuPCIPhysical(&tc.opts))878 }879 })880 t.Run("qemu_gpu_dev_physical", func(t *testing.T) {881 testCases := []struct {882 opts qemuGPUDevPhysicalOpts883 expected string884 }{{885 qemuGPUDevPhysicalOpts{886 dev: qemuDevOpts{"pci", "qemu_pcie1", "00.0", false},887 devName: "gpu-name",888 pciSlotName: "gpu-slot",889 },890 `# GPU card ("gpu-name" device)891 [device "dev-lxd_gpu-name"]892 driver = "vfio-pci"893 bus = "qemu_pcie1"894 addr = "00.0"895 host = "gpu-slot"`,896 }, {897 qemuGPUDevPhysicalOpts{898 dev: qemuDevOpts{"ccw", "qemu_pcie1", "00.0", true},899 devName: "gpu-name",900 pciSlotName: "gpu-slot",901 vga: true,902 },903 `# GPU card ("gpu-name" device)904 [device "dev-lxd_gpu-name"]905 driver = "vfio-ccw"906 multifunction = "on"907 host = "gpu-slot"908 x-vga = "on"`,909 }, {910 qemuGPUDevPhysicalOpts{911 dev: qemuDevOpts{"pci", "qemu_pcie1", "00.0", true},912 devName: "vgpu-name",913 vgpu: "vgpu-dev",914 },915 `# GPU card ("vgpu-name" device)916 [device "dev-lxd_vgpu-name"]917 driver = "vfio-pci"918 bus = "qemu_pcie1"919 addr = "00.0"920 multifunction = "on"921 sysfsdev = "/sys/bus/mdev/devices/vgpu-dev"`,922 }}923 for _, tc := range testCases {924 runTest(tc.expected, qemuGPUDevPhysical(&tc.opts))925 }926 })927 t.Run("qemu_usb", func(t *testing.T) {928 testCases := []struct {929 opts qemuUSBOpts930 expected string931 }{{932 qemuUSBOpts{933 devBus: "qemu_pcie1",934 devAddr: "00.0",935 multifunction: true,936 ports: 3,937 },938 `# USB controller939 [device "qemu_usb"]940 driver = "qemu-xhci"941 bus = "qemu_pcie1"942 addr = "00.0"943 multifunction = "on"944 p2 = "3"945 p3 = "3"946 [chardev "qemu_spice-usb-chardev1"]947 backend = "spicevmc"948 name = "usbredir"949 [device "qemu_spice-usb1"]950 driver = "usb-redir"951 chardev = "qemu_spice-usb-chardev1"952 [chardev "qemu_spice-usb-chardev2"]953 backend = "spicevmc"954 name = "usbredir"955 [device "qemu_spice-usb2"]956 driver = "usb-redir"957 chardev = "qemu_spice-usb-chardev2"958 [chardev "qemu_spice-usb-chardev3"]959 backend = "spicevmc"960 name = "usbredir"961 [device "qemu_spice-usb3"]962 driver = "usb-redir"963 chardev = "qemu_spice-usb-chardev3"`,964 }}965 for _, tc := range testCases {966 runTest(tc.expected, qemuUSB(&tc.opts))967 }968 })969 t.Run("qemu_tpm", func(t *testing.T) {970 testCases := []struct {971 opts qemuTPMOpts972 expected string973 }{{974 qemuTPMOpts{975 devName: "myTpm",976 path: "/dev/my/tpm",977 },978 `[chardev "qemu_tpm-chardev_myTpm"]979 backend = "socket"980 path = "/dev/my/tpm"981 [tpmdev "qemu_tpm-tpmdev_myTpm"]982 type = "emulator"983 chardev = "qemu_tpm-chardev_myTpm"984 [device "dev-lxd_myTpm"]985 driver = "tpm-crb"986 tpmdev = "qemu_tpm-tpmdev_myTpm"`,987 }}988 for _, tc := range testCases {989 runTest(tc.expected, qemuTPM(&tc.opts))990 }991 })992 t.Run("qemu_raw_cfg_override", func(t *testing.T) {993 cfg := []cfgSection{{994 name: "global",995 entries: []cfgEntry{996 {key: "driver", value: "ICH9-LPC"},997 {key: "property", value: "disable_s3"},998 {key: "value", value: "1"},999 },1000 }, {1001 name: "global",1002 entries: []cfgEntry{1003 {key: "driver", value: "ICH9-LPC"},1004 {key: "property", value: "disable_s4"},1005 {key: "value", value: "1"},1006 },1007 }, {1008 name: "memory",1009 entries: []cfgEntry{1010 {key: "size", value: "1024M"},1011 },1012 }, {1013 name: `device "qemu_gpu"`,1014 entries: []cfgEntry{1015 {key: "driver", value: "virtio-gpu-pci"},1016 {key: "bus", value: "qemu_pci3"},1017 {key: "addr", value: "00.0"},1018 },1019 }, {1020 name: `device "qemu_keyboard"`,1021 entries: []cfgEntry{1022 {key: "driver", value: "virtio-keyboard-pci"},1023 {key: "bus", value: "qemu_pci2"},1024 {key: "addr", value: "00.1"},1025 },1026 }}1027 testCases := []struct {1028 cfg []cfgSection1029 overrides map[string]string1030 expected string1031 }{{1032 // unmodified1033 cfg,1034 map[string]string{},1035 `[global]1036 driver = "ICH9-LPC"1037 property = "disable_s3"1038 value = "1"1039 [global]1040 driver = "ICH9-LPC"1041 property = "disable_s4"1042 value = "1"1043 [memory]1044 size = "1024M"1045 [device "qemu_gpu"]1046 driver = "virtio-gpu-pci"1047 bus = "qemu_pci3"1048 addr = "00.0"1049 [device "qemu_keyboard"]1050 driver = "virtio-keyboard-pci"1051 bus = "qemu_pci2"1052 addr = "00.1"`,1053 }, {1054 // override some keys1055 cfg,1056 map[string]string{1057 "raw.qemu.conf": `1058 [memory]1059 size = "4096M"1060 [device "qemu_gpu"]1061 driver = "qxl-vga"`,1062 },1063 `[global]1064 driver = "ICH9-LPC"1065 property = "disable_s3"1066 value = "1"1067 [global]1068 driver = "ICH9-LPC"1069 property = "disable_s4"1070 value = "1"1071 [memory]1072 size = "4096M"1073 [device "qemu_gpu"]1074 driver = "qxl-vga"1075 bus = "qemu_pci3"1076 addr = "00.0"1077 [device "qemu_keyboard"]1078 driver = "virtio-keyboard-pci"1079 bus = "qemu_pci2"1080 addr = "00.1"`,1081 }, {1082 // delete some keys1083 cfg,1084 map[string]string{1085 "raw.qemu.conf": `1086 [device "qemu_keyboard"]1087 driver = ""1088 [device "qemu_gpu"]1089 addr = ""`,1090 },1091 `[global]1092 driver = "ICH9-LPC"1093 property = "disable_s3"1094 value = "1"1095 [global]1096 driver = "ICH9-LPC"1097 property = "disable_s4"1098 value = "1"1099 [memory]1100 size = "1024M"1101 [device "qemu_gpu"]1102 driver = "virtio-gpu-pci"1103 bus = "qemu_pci3"1104 [device "qemu_keyboard"]1105 bus = "qemu_pci2"1106 addr = "00.1"`,1107 }, {1108 // add some keys to existing sections1109 cfg,1110 map[string]string{1111 "raw.qemu.conf": `1112 [memory]1113 somekey = "somevalue"1114 somekey2 = "somevalue2"1115 somekey3 = "somevalue3"1116 somekey4="somevalue4"1117 [device "qemu_keyboard"]1118 multifunction="off"1119 [device "qemu_gpu"]1120 multifunction= "on"`,1121 },1122 `[global]1123 driver = "ICH9-LPC"1124 property = "disable_s3"1125 value = "1"1126 [global]1127 driver = "ICH9-LPC"1128 property = "disable_s4"1129 value = "1"1130 [memory]1131 size = "1024M"1132 somekey = "somevalue"1133 somekey2 = "somevalue2"1134 somekey3 = "somevalue3"1135 somekey4 = "somevalue4"1136 [device "qemu_gpu"]1137 driver = "virtio-gpu-pci"1138 bus = "qemu_pci3"1139 addr = "00.0"1140 multifunction = "on"1141 [device "qemu_keyboard"]1142 driver = "virtio-keyboard-pci"1143 bus = "qemu_pci2"1144 addr = "00.1"1145 multifunction = "off"`,1146 }, {1147 // edit/add/remove1148 cfg,1149 map[string]string{1150 "raw.qemu.conf": `1151 [memory]1152 size = "2048M"1153 [device "qemu_gpu"]1154 multifunction = "on"1155 [device "qemu_keyboard"]1156 addr = ""1157 bus = ""`,1158 },1159 `[global]1160 driver = "ICH9-LPC"1161 property = "disable_s3"1162 value = "1"1163 [global]1164 driver = "ICH9-LPC"1165 property = "disable_s4"1166 value = "1"1167 [memory]1168 size = "2048M"1169 [device "qemu_gpu"]1170 driver = "virtio-gpu-pci"1171 bus = "qemu_pci3"1172 addr = "00.0"1173 multifunction = "on"1174 [device "qemu_keyboard"]1175 driver = "virtio-keyboard-pci"`,1176 }, {1177 // delete sections1178 cfg,1179 map[string]string{1180 "raw.qemu.conf": `1181 [memory]1182 [device "qemu_keyboard"]1183 [global][1]`,1184 },1185 `[global]1186 driver = "ICH9-LPC"1187 property = "disable_s3"1188 value = "1"1189 [device "qemu_gpu"]1190 driver = "virtio-gpu-pci"1191 bus = "qemu_pci3"1192 addr = "00.0"`,1193 }, {1194 // add sections1195 cfg,1196 map[string]string{1197 "raw.qemu.conf": `1198 [object1]1199 key1 = "value1"1200 key2 = "value2"1201 [object "2"]1202 key3 = "value3"1203 [object "3"]1204 key4 = "value4"1205 [object "2"]1206 key5 = "value5"1207 [object1]1208 key6 = "value6"`,1209 },1210 `[global]1211 driver = "ICH9-LPC"1212 property = "disable_s3"1213 value = "1"1214 [global]1215 driver = "ICH9-LPC"1216 property = "disable_s4"1217 value = "1"1218 [memory]1219 size = "1024M"1220 [device "qemu_gpu"]1221 driver = "virtio-gpu-pci"1222 bus = "qemu_pci3"1223 addr = "00.0"1224 [device "qemu_keyboard"]1225 driver = "virtio-keyboard-pci"1226 bus = "qemu_pci2"1227 addr = "00.1"1228 [object "2"]1229 key3 = "value3"1230 key5 = "value5"1231 [object "3"]1232 key4 = "value4"1233 [object1]1234 key1 = "value1"1235 key2 = "value2"1236 key6 = "value6"`,1237 }, {1238 // add/remove sections1239 cfg,1240 map[string]string{1241 "raw.qemu.conf": `1242 [device "qemu_gpu"]1243 [object "2"]1244 key3 = "value3"1245 [object "3"]1246 key4 = "value4"1247 [object "2"]1248 key5 = "value5"`,1249 },1250 `[global]1251 driver = "ICH9-LPC"1252 property = "disable_s3"1253 value = "1"1254 [global]1255 driver = "ICH9-LPC"1256 property = "disable_s4"1257 value = "1"1258 [memory]1259 size = "1024M"1260 [device "qemu_keyboard"]1261 driver = "virtio-keyboard-pci"1262 bus = "qemu_pci2"1263 addr = "00.1"1264 [object "2"]1265 key3 = "value3"1266 key5 = "value5"1267 [object "3"]1268 key4 = "value4"`,1269 }, {1270 // edit keys of repeated sections1271 cfg,1272 map[string]string{1273 "raw.qemu.conf": `1274 [global][1]1275 property ="disable_s1"1276 [global]1277 property ="disable_s5"1278 [global][1]1279 value = ""1280 [global][0]1281 somekey ="somevalue"1282 [global][1]1283 anotherkey = "anothervalue"`,1284 },1285 `[global]1286 driver = "ICH9-LPC"1287 property = "disable_s5"1288 value = "1"1289 somekey = "somevalue"1290 [global]1291 driver = "ICH9-LPC"1292 property = "disable_s1"1293 anotherkey = "anothervalue"1294 [memory]1295 size = "1024M"1296 [device "qemu_gpu"]1297 driver = "virtio-gpu-pci"1298 bus = "qemu_pci3"1299 addr = "00.0"1300 [device "qemu_keyboard"]1301 driver = "virtio-keyboard-pci"1302 bus = "qemu_pci2"1303 addr = "00.1"`,1304 }, {1305 // create multiple sections with same name1306 cfg,1307 // note that for appending new sections, all that matters is that1308 // the index is higher than the existing indexes1309 map[string]string{1310 "raw.qemu.conf": `1311 [global][2]1312 property = "new section"1313 [global][2]1314 value = "new value"1315 [object][3]1316 k1 = "v1"1317 [object][3]1318 k2 = "v2"1319 [object][4]1320 k3 = "v1"1321 [object][4]1322 k2 = "v2"1323 [object][11]1324 k11 = "v11"`,1325 },1326 `[global]1327 driver = "ICH9-LPC"1328 property = "disable_s3"1329 value = "1"1330 [global]1331 driver = "ICH9-LPC"1332 property = "disable_s4"1333 value = "1"1334 [memory]1335 size = "1024M"1336 [device "qemu_gpu"]1337 driver = "virtio-gpu-pci"1338 bus = "qemu_pci3"1339 addr = "00.0"1340 [device "qemu_keyboard"]1341 driver = "virtio-keyboard-pci"1342 bus = "qemu_pci2"1343 addr = "00.1"1344 [global]1345 property = "new section"1346 value = "new value"1347 [object]1348 k1 = "v1"1349 k2 = "v2"1350 [object]1351 k2 = "v2"1352 k3 = "v1"1353 [object]1354 k11 = "v11"`,1355 }, {1356 // mix all operations1357 cfg,1358 map[string]string{1359 "raw.qemu.conf": `1360 [memory]1361 size = "8192M"1362 [device "qemu_keyboard"]1363 multifunction=on1364 bus =1365 [device "qemu_gpu"]1366 [object "3"]1367 key4 = " value4 "1368 [object "2"]1369 key3 = value3 1370 [object "3"]1371 key5 = "value5"`,1372 },1373 `[global]1374 driver = "ICH9-LPC"1375 property = "disable_s3"1376 value = "1"1377 [global]1378 driver = "ICH9-LPC"1379 property = "disable_s4"1380 value = "1"1381 [memory]1382 size = "8192M"1383 [device "qemu_keyboard"]1384 driver = "virtio-keyboard-pci"1385 addr = "00.1"1386 multifunction = "on"1387 [object "2"]1388 key3 = "value3"1389 [object "3"]1390 key4 = " value4 "1391 key5 = "value5"`,1392 }}1393 for _, tc := range testCases {1394 runTest(tc.expected, qemuRawCfgOverride(tc.cfg, tc.overrides))1395 }1396 })1397 t.Run("parse_conf_override", func(t *testing.T) {1398 input := `1399 [global]1400 key1 = "val1"1401 key3 = "val3"1402 [global][0]1403 key2 = "val2"1404 [global][1]1405 key1 = "val3"1406 [global][4]1407 key2 = "val4"1408 [global] 1409 [global][4]1410 [global][5]1411 `...
qemu_test.go
Source:qemu_test.go
...69 store, err := persist.GetDriver()70 assert.NoError(err)71 q := &qemu{72 config: HypervisorConfig{73 VMStorePath: store.RunVMStoragePath(),74 RunStorePath: store.RunStoragePath(),75 },76 }77 sandbox := &Sandbox{78 ctx: context.Background(),79 id: "testSandbox",80 config: &SandboxConfig{81 HypervisorConfig: qemuConfig,82 },83 }84 // Create the hypervisor fake binary85 testQemuPath := filepath.Join(testDir, testHypervisor)86 _, err = os.Create(testQemuPath)87 assert.NoError(err)88 // Create parent dir path for hypervisor.json89 parentDir := filepath.Join(store.RunStoragePath(), sandbox.id)90 assert.NoError(os.MkdirAll(parentDir, DirMode))91 network, err := NewNetwork()92 assert.NoError(err)93 err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig)94 assert.NoError(err)95 assert.NoError(os.RemoveAll(parentDir))96 assert.Exactly(qemuConfig, q.config)97}98func TestQemuCreateVMMissingParentDirFail(t *testing.T) {99 qemuConfig := newQemuConfig()100 assert := assert.New(t)101 store, err := persist.GetDriver()102 assert.NoError(err)103 q := &qemu{104 config: HypervisorConfig{105 VMStorePath: store.RunVMStoragePath(),106 RunStorePath: store.RunStoragePath(),107 },108 }109 sandbox := &Sandbox{110 ctx: context.Background(),111 id: "testSandbox",112 config: &SandboxConfig{113 HypervisorConfig: qemuConfig,114 },115 }116 // Create the hypervisor fake binary117 testQemuPath := filepath.Join(testDir, testHypervisor)118 _, err = os.Create(testQemuPath)119 assert.NoError(err)120 // Ensure parent dir path for hypervisor.json does not exist.121 parentDir := filepath.Join(store.RunStoragePath(), sandbox.id)122 assert.NoError(os.RemoveAll(parentDir))123 network, err := NewNetwork()124 assert.NoError(err)125 err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig)126 assert.NoError(err)127}128func TestQemuCPUTopology(t *testing.T) {129 assert := assert.New(t)130 vcpus := 1131 q := &qemu{132 arch: &qemuArchBase{},133 config: HypervisorConfig{134 NumVCPUs: uint32(vcpus),135 DefaultMaxVCPUs: uint32(vcpus),136 },137 }138 expectedOut := govmmQemu.SMP{139 CPUs: uint32(vcpus),140 Sockets: uint32(vcpus),141 Cores: defaultCores,142 Threads: defaultThreads,143 MaxCPUs: uint32(vcpus),144 }145 smp := q.cpuTopology()146 assert.Exactly(smp, expectedOut)147}148func TestQemuMemoryTopology(t *testing.T) {149 mem := uint32(1000)150 maxMem := memory.TotalMemory() / 1024 / 1024 //MiB151 slots := uint32(8)152 assert := assert.New(t)153 q := &qemu{154 arch: &qemuArchBase{},155 config: HypervisorConfig{156 MemorySize: mem,157 MemSlots: slots,158 DefaultMaxMemorySize: maxMem,159 },160 }161 memMax := fmt.Sprintf("%dM", int(maxMem))162 expectedOut := govmmQemu.Memory{163 Size: fmt.Sprintf("%dM", mem),164 Slots: uint8(slots),165 MaxMem: memMax,166 }167 memory, err := q.memoryTopology()168 assert.NoError(err)169 assert.Exactly(memory, expectedOut)170}171func TestQemuKnobs(t *testing.T) {172 assert := assert.New(t)173 sandbox, err := createQemuSandboxConfig()174 assert.NoError(err)175 q := &qemu{176 config: HypervisorConfig{177 VMStorePath: sandbox.store.RunVMStoragePath(),178 RunStorePath: sandbox.store.RunStoragePath(),179 },180 }181 network, err := NewNetwork()182 assert.NoError(err)183 err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig)184 assert.NoError(err)185 assert.Equal(q.qemuConfig.Knobs.NoUserConfig, true)186 assert.Equal(q.qemuConfig.Knobs.NoDefaults, true)187 assert.Equal(q.qemuConfig.Knobs.NoGraphic, true)188 assert.Equal(q.qemuConfig.Knobs.NoReboot, true)189}190func testQemuAddDevice(t *testing.T, devInfo interface{}, devType DeviceType, expected []govmmQemu.Device) {191 assert := assert.New(t)192 q := &qemu{193 ctx: context.Background(),194 arch: &qemuArchBase{},195 }196 err := q.AddDevice(context.Background(), devInfo, devType)197 assert.NoError(err)198 assert.Exactly(q.qemuConfig.Devices, expected)199}200func TestQemuAddDeviceFsDev(t *testing.T) {201 mountTag := "testMountTag"202 hostPath := "testHostPath"203 expectedOut := []govmmQemu.Device{204 govmmQemu.FSDevice{205 Driver: govmmQemu.Virtio9P,206 FSDriver: govmmQemu.Local,207 ID: fmt.Sprintf("extra-9p-%s", mountTag),208 Path: hostPath,209 MountTag: mountTag,210 SecurityModel: govmmQemu.None,211 Multidev: govmmQemu.Remap,212 },213 }214 volume := types.Volume{215 MountTag: mountTag,216 HostPath: hostPath,217 }218 testQemuAddDevice(t, volume, FsDev, expectedOut)219}220func TestQemuAddDeviceVhostUserBlk(t *testing.T) {221 socketPath := "/test/socket/path"222 devID := "testDevID"223 expectedOut := []govmmQemu.Device{224 govmmQemu.VhostUserDevice{225 SocketPath: socketPath,226 CharDevID: utils.MakeNameID("char", devID, maxDevIDSize),227 VhostUserType: govmmQemu.VhostUserBlk,228 },229 }230 vDevice := config.VhostUserDeviceAttrs{231 DevID: devID,232 SocketPath: socketPath,233 Type: config.VhostUserBlk,234 }235 testQemuAddDevice(t, vDevice, VhostuserDev, expectedOut)236}237func TestQemuAddDeviceSerialPortDev(t *testing.T) {238 deviceID := "channelTest"239 id := "charchTest"240 hostPath := "/tmp/hyper_test.sock"241 name := "sh.hyper.channel.test"242 expectedOut := []govmmQemu.Device{243 govmmQemu.CharDevice{244 Driver: govmmQemu.VirtioSerialPort,245 Backend: govmmQemu.Socket,246 DeviceID: deviceID,247 ID: id,248 Path: hostPath,249 Name: name,250 },251 }252 socket := types.Socket{253 DeviceID: deviceID,254 ID: id,255 HostPath: hostPath,256 Name: name,257 }258 testQemuAddDevice(t, socket, SerialPortDev, expectedOut)259}260func TestQemuAddDeviceKataVSOCK(t *testing.T) {261 assert := assert.New(t)262 dir := t.TempDir()263 vsockFilename := filepath.Join(dir, "vsock")264 contextID := uint64(3)265 port := uint32(1024)266 vsockFile, err := os.Create(vsockFilename)267 assert.NoError(err)268 defer vsockFile.Close()269 expectedOut := []govmmQemu.Device{270 govmmQemu.VSOCKDevice{271 ID: fmt.Sprintf("vsock-%d", contextID),272 ContextID: contextID,273 VHostFD: vsockFile,274 },275 }276 vsock := types.VSock{277 ContextID: contextID,278 Port: port,279 VhostFd: vsockFile,280 }281 testQemuAddDevice(t, vsock, VSockPCIDev, expectedOut)282}283func TestQemuGetSandboxConsole(t *testing.T) {284 assert := assert.New(t)285 store, err := persist.GetDriver()286 assert.NoError(err)287 q := &qemu{288 ctx: context.Background(),289 config: HypervisorConfig{290 VMStorePath: store.RunVMStoragePath(),291 RunStorePath: store.RunStoragePath(),292 },293 }294 sandboxID := "testSandboxID"295 expected := filepath.Join(store.RunVMStoragePath(), sandboxID, consoleSocket)296 proto, result, err := q.GetVMConsole(q.ctx, sandboxID)297 assert.NoError(err)298 assert.Equal(result, expected)299 assert.Equal(proto, consoleProtoUnix)300}301func TestQemuCapabilities(t *testing.T) {302 assert := assert.New(t)303 q := &qemu{304 ctx: context.Background(),305 arch: &qemuArchBase{},306 }307 caps := q.Capabilities(q.ctx)308 assert.True(caps.IsBlockDeviceHotplugSupported())309}310func TestQemuQemuPath(t *testing.T) {311 assert := assert.New(t)312 f, err := os.CreateTemp("", "qemu")313 assert.NoError(err)314 defer func() { _ = f.Close() }()315 defer func() { _ = os.Remove(f.Name()) }()316 expectedPath := f.Name()317 qemuConfig := newQemuConfig()318 qemuConfig.HypervisorPath = expectedPath319 qkvm := &qemuArchBase{320 qemuMachine: govmmQemu.Machine{321 Type: "q35",322 Options: "",323 },324 qemuExePath: expectedPath,325 }326 q := &qemu{327 config: qemuConfig,328 arch: qkvm,329 }330 // get config hypervisor path331 path, err := q.qemuPath()332 assert.NoError(err)333 assert.Equal(path, expectedPath)334 // config hypervisor path does not exist335 q.config.HypervisorPath = "/abc/rgb/123"336 path, err = q.qemuPath()337 assert.Error(err)338 assert.Equal(path, "")339 // get arch hypervisor path340 q.config.HypervisorPath = ""341 path, err = q.qemuPath()342 assert.NoError(err)343 assert.Equal(path, expectedPath)344}345func TestHotplugUnsupportedDeviceType(t *testing.T) {346 assert := assert.New(t)347 qemuConfig := newQemuConfig()348 q := &qemu{349 ctx: context.Background(),350 id: "qemuTest",351 config: qemuConfig,352 }353 _, err := q.HotplugAddDevice(context.Background(), &MemoryDevice{0, 128, uint64(0), false}, FsDev)354 assert.Error(err)355 _, err = q.HotplugRemoveDevice(context.Background(), &MemoryDevice{0, 128, uint64(0), false}, FsDev)356 assert.Error(err)357}358func TestQMPSetupShutdown(t *testing.T) {359 assert := assert.New(t)360 qemuConfig := newQemuConfig()361 q := &qemu{362 config: qemuConfig,363 }364 q.qmpShutdown()365 q.qmpMonitorCh.qmp = &govmmQemu.QMP{}366 err := q.qmpSetup()367 assert.Nil(err)368}369func TestQemuCleanup(t *testing.T) {370 assert := assert.New(t)371 q := &qemu{372 ctx: context.Background(),373 config: newQemuConfig(),374 }375 err := q.Cleanup(q.ctx)376 assert.Nil(err)377}378func TestQemuGrpc(t *testing.T) {379 assert := assert.New(t)380 config := newQemuConfig()381 q := &qemu{382 id: "testqemu",383 config: config,384 }385 json, err := q.toGrpc(context.Background())386 assert.Nil(err)387 var q2 qemu388 err = q2.fromGrpc(context.Background(), &config, json)389 assert.Nil(err)390 assert.True(q.id == q2.id)391}392func TestQemuFileBackedMem(t *testing.T) {393 assert := assert.New(t)394 // Check default Filebackedmem location for virtio-fs395 sandbox, err := createQemuSandboxConfig()396 assert.NoError(err)397 network, err := NewNetwork()398 assert.NoError(err)399 q := &qemu{400 config: HypervisorConfig{401 VMStorePath: sandbox.store.RunVMStoragePath(),402 RunStorePath: sandbox.store.RunStoragePath(),403 },404 }405 sandbox.config.HypervisorConfig.SharedFS = config.VirtioFS406 err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig)407 assert.NoError(err)408 assert.Equal(q.qemuConfig.Knobs.FileBackedMem, true)409 assert.Equal(q.qemuConfig.Knobs.MemShared, true)410 assert.Equal(q.qemuConfig.Memory.Path, fallbackFileBackedMemDir)411 // Check failure for VM templating412 sandbox, err = createQemuSandboxConfig()413 assert.NoError(err)414 q = &qemu{415 config: HypervisorConfig{416 VMStorePath: sandbox.store.RunVMStoragePath(),417 RunStorePath: sandbox.store.RunStoragePath(),418 },419 }420 sandbox.config.HypervisorConfig.BootToBeTemplate = true421 sandbox.config.HypervisorConfig.SharedFS = config.VirtioFS422 sandbox.config.HypervisorConfig.MemoryPath = fallbackFileBackedMemDir423 err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig)424 expectErr := errors.New("VM templating has been enabled with either virtio-fs or file backed memory and this configuration will not work")425 assert.Equal(expectErr.Error(), err.Error())426 // Check Setting of non-existent shared-mem path427 sandbox, err = createQemuSandboxConfig()428 assert.NoError(err)429 q = &qemu{430 config: HypervisorConfig{431 VMStorePath: sandbox.store.RunVMStoragePath(),432 RunStorePath: sandbox.store.RunStoragePath(),433 },434 }435 sandbox.config.HypervisorConfig.FileBackedMemRootDir = "/tmp/xyzabc"436 err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig)437 assert.NoError(err)438 assert.Equal(q.qemuConfig.Knobs.FileBackedMem, false)439 assert.Equal(q.qemuConfig.Knobs.MemShared, false)440 assert.Equal(q.qemuConfig.Memory.Path, "")441 // Check setting vhost-user storage with Hugepages442 sandbox, err = createQemuSandboxConfig()443 assert.NoError(err)444 q = &qemu{445 config: HypervisorConfig{446 VMStorePath: sandbox.store.RunVMStoragePath(),447 RunStorePath: sandbox.store.RunStoragePath(),448 },449 }450 sandbox.config.HypervisorConfig.EnableVhostUserStore = true451 sandbox.config.HypervisorConfig.HugePages = true452 err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig)453 assert.NoError(err)454 assert.Equal(q.qemuConfig.Knobs.MemShared, true)455 // Check failure for vhost-user storage456 sandbox, err = createQemuSandboxConfig()457 assert.NoError(err)458 q = &qemu{459 config: HypervisorConfig{460 VMStorePath: sandbox.store.RunVMStoragePath(),461 RunStorePath: sandbox.store.RunStoragePath(),462 },463 }464 sandbox.config.HypervisorConfig.EnableVhostUserStore = true465 sandbox.config.HypervisorConfig.HugePages = false466 err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig)467 expectErr = errors.New("Vhost-user-blk/scsi is enabled without HugePages. This configuration will not work")468 assert.Equal(expectErr.Error(), err.Error())469}470func createQemuSandboxConfig() (*Sandbox, error) {471 qemuConfig := newQemuConfig()472 sandbox := Sandbox{473 ctx: context.Background(),474 id: "testSandbox",475 config: &SandboxConfig{...
qemu.go
Source:qemu.go
...17}18type QemuNode struct {19 config.NodeConf20}21func (q QemuNode) Run(cfg *config.Topology, dryRun bool, pwMap map[string]*network.PseudoWire) (logrus.Fields, error) {22 var index int // need to find which node we are within the topology23 for i, nd := range cfg.Nodes {24 if nd.Tag == q.Tag {25 index = i26 }27 }28 serialPortBase := 49152 + (cfg.LabID * 32)29 telnetPort := serialPortBase + index30 qemuArgs := []string{31 "--enable-kvm",32 "-name", q.Tag, "-daemonize",33 "-smp", strconv.Itoa(q.Resources.CPU),34 "-pidfile", fmt.Sprintf("/var/run/nlab/%v/%v.pid", cfg.Tag, q.Tag),35 "-monitor", fmt.Sprintf("unix:/var/run/nlab/%v/%v.sock,nowait,server", cfg.Tag, q.Tag),36 "-m", strconv.Itoa(q.Resources.Memory),37 "-display", "none", "-serial", fmt.Sprintf("telnet::%v,nowait,server", telnetPort),38 }39 for _, disk := range q.Resources.Disks {40 cmd := []string{"-drive", fmt.Sprintf("format=%v,file=%v", disk.Format, disk.File)}41 qemuArgs = append(qemuArgs, cmd...)42 }43 if q.Resources.CDROM != "" {44 qemuArgs = append(qemuArgs, "-cdrom "+q.Resources.CDROM)45 }46 virtIO := q.Network.VirtIO // virtio support specified?47 if q.Network.Management == true {48 tapName := netlinux.TapUID(cfg.LabID, cfg.ManagementBridge, q.Tag)49 qemuArgs = append(qemuArgs, linkCmd(cfg.ManagementBridge, tapName, virtIO)...)50 }51 for _, link := range q.Network.Links {52 if cfg.PseudoWire {53 qemuArgs = append(qemuArgs, pwCmd(link, pwMap[link], q.Tag, virtIO)...)54 } else {55 tapName := netlinux.TapUID(cfg.LabID, link, q.Tag)56 qemuArgs = append(qemuArgs, linkCmd(link, tapName, virtIO)...)57 }58 }59 if !dryRun {60 if out, err := exec.Command("qemu-system-x86_64", qemuArgs...).CombinedOutput(); err != nil {61 return nil, errors.New(fmt.Sprintf("%v: %v", err, string(out)))62 } else {63 return logrus.Fields{64 "tag": q.Tag,65 "serial": fmt.Sprintf("telnet://localhost:%v", telnetPort),66 }, nil67 }68 } else {69 fmt.Println("qemu-system-x86_64 " + strings.Join(qemuArgs, " "))70 return nil, nil71 }72}73func (q QemuNode) Stop(cfg *config.Topology) error {...
Run
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4}5import (6func main() {7 cmd := exec.Command("qemu-system-x86_64", "-hda", "C:/Users/Arvind/Desktop/OS/OS2.qcow2")8 err := cmd.Run()9 if err != nil {10 log.Fatal(err)11 }12}13import (14func main() {15 cmd := exec.Command("qemu-system-x86_64", "-hda", "C:/Users/Arvind/Desktop/OS/OS2.qcow2")16 err := cmd.Run()17 if err != nil {18 log.Fatal(err)19 }20}21import (22func main() {23 cmd := exec.Command("qemu-system-x86_64", "-hda", "C:/Users/Arvind/Desktop/OS/OS2.qcow2")24 err := cmd.Run()25 if err != nil {26 log.Fatal(err)27 }28}29import (30func main() {31 cmd := exec.Command("qemu-system-x86_64", "-hda", "C:/Users/Arvind/Desktop/OS/OS2.qcow2")32 err := cmd.Run()33 if err != nil {34 log.Fatal(err)35 }36}37import (38func main() {39 cmd := exec.Command("qemu-system-x86_64", "-hda", "C:/Users/Arvind/Desktop/OS/OS2.qcow2")40 err := cmd.Run()41 if err != nil {42 log.Fatal(err
Run
Using AI Code Generation
1import (2func main() {3 q := qemu.NewQemu("/usr/bin/qemu-system-x86_64", "qemu-system-x86_64")4 q.SetMachine("pc")5 q.SetCpus(2)6 q.SetDrive("/home/abc/Downloads/centos7.qcow2")7 q.SetKernel("/home/abc/Downloads/vmlinuz")8 q.SetInitrd("/home/abc/Downloads/initrd.img")9 q.SetAppend("console=ttyS0")10 if err := q.Run(); err != nil {11 fmt.Println(err)12 }13}
Run
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("qemu-system-x86_64", "-enable-kvm", "-m", "2048", "-smp", "2", "-cpu", "host", "-net", "nic,model=virtio", "-net", "user", "-drive", "file=debian.qcow2,if=virtio", "-vnc", ":0")4 err := cmd.Start()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println("QEMU process started")9 err = cmd.Wait()10 fmt.Println("QEMU process finished")11}12import (13func main() {14 cmd := exec.Command("qemu-system-x86_64", "-enable-kvm", "-m", "2048", "-smp", "2", "-cpu", "host", "-net", "nic,model=virtio", "-net", "user", "-drive", "file=debian.qcow2,if=virtio", "-vnc", ":0")15 err := cmd.Run()16 if err != nil {17 fmt.Println(err)18 }19 fmt.Println("QEMU process started")20 fmt.Println("QEMU process finished")21}
Run
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("qemu-system-x86_64", "-m", "2G", "-hda", "ubuntu.qcow2", "-boot", "c", "-cdrom", "ubuntu.iso", "-net", "nic,model=virtio", "-net", "user", "-enable-kvm", "-cpu", "host", "-smp", "2")4 err := cmd.Start()5 if err != nil {6 fmt.Println(err)7 os.Exit(1)8 }9 err = cmd.Wait()10 if err != nil {11 fmt.Println(err)12 os.Exit(1)13 }14 fmt.Println("Command finished successfully")15}
Run
Using AI Code Generation
1import (2func main() {3 table := tablewriter.NewWriter(os.Stdout)4 table.SetHeader([]string{"Name", "Status", "Uptime"})5 table.SetAlignment(tablewriter.ALIGN_LEFT)6 qemu := new(qemu)7 qemu.Run()8 vmdata := qemu.Data()9 if len(vmdata) == 0 {10 fmt.Println("No VMs Found")11 } else {12 for _, v := range vmdata {13 table.Append([]string{v["name"], v["status"], v["uptime"]})14 }15 table.Render()16 }17}18type qemu struct {19}20func (q *qemu) Run() {21 out, err := exec.Command("virsh", "list", "--all").Output()22 if err != nil {23 fmt.Println(err)24 }25 lines := strings.Split(string(out), "26 for i, v := range lines {27 if i > 1 {28 cols := strings.Fields(v)29 if len(cols) > 0 {30 q.data[name] = map[string]string{31 }32 }33 }34 }35}36func (q *qemu
Run
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("/home/rohit/Desktop/2")4 cmd.SysProcAttr = &syscall.SysProcAttr{}5 cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(1000), Gid: uint32(1000)}6 cmd.Env = []string{"PS1=-[qemu]- # "}7 if err := cmd.Run(); err != nil {8 fmt.Println("Error:", err)9 os.Exit(1)10 }11}12import (13func main() {14 cmd := exec.Command("/bin/bash")15 cmd.SysProcAttr = &syscall.SysProcAttr{}16 cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(1000), Gid: uint32(1000)}17 cmd.Env = []string{"PS1=-[qemu]- # "}18 if err := cmd.Run(); err != nil {19 fmt.Println("Error:", err)20 os.Exit(1)21 }22}23dpkg: error processing archive /home/username/Downloads/program_1.0.0-1_amd64.deb (--install):
Run
Using AI Code Generation
1import (2func main() {3 qemu := qemu.NewQemu()4 qemu.Run()5 fmt.Println("Qemu Output: ", qemu.Output)6}7Qemu Output: QEMU 2.11.0 monitor - type 'help' for more information (qemu)8import (9func main() {10 qemu := qemu.NewQemu()11 qemu.RunInBackground()12 fmt.Println("Qemu Output: ", qemu.Output)13 fmt.Println("Qemu PID: ", qemu.PID)14}15Qemu Output: QEMU 2.11.0 monitor - type 'help' for more information (qemu)16import (17func main() {18 qemu := qemu.NewQemu()19 qemu.RunInBackground()20 fmt.Println("Qemu Output: ", qemu.Output)21 fmt.Println("Qemu PID: ", qemu.PID)22 qemu.Kill()23}24Qemu Output: QEMU 2.11.0 monitor - type 'help' for more information (qemu)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!