github.com/containers/podman/v4@v4.9.4/pkg/machine/qemu/command.go (about) 1 package qemu 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/containers/podman/v4/pkg/machine" 8 "github.com/containers/podman/v4/pkg/machine/define" 9 ) 10 11 // QemuCmd is an alias around a string slice to prevent the need to migrate the 12 // MachineVM struct due to changes 13 type QemuCmd []string 14 15 // NewQemuBuilder creates a new QemuCmd object that we will build on top of, 16 // starting with the qemu binary, architecture specific options, and propagated 17 // proxy and SSL settings 18 func NewQemuBuilder(binary string, options []string) QemuCmd { 19 q := QemuCmd{binary} 20 return append(q, options...) 21 } 22 23 // SetMemory adds the specified amount of memory for the machine 24 func (q *QemuCmd) SetMemory(m uint64) { 25 *q = append(*q, "-m", strconv.FormatUint(m, 10)) 26 } 27 28 // SetCPUs adds the number of CPUs the machine will have 29 func (q *QemuCmd) SetCPUs(c uint64) { 30 *q = append(*q, "-smp", strconv.FormatUint(c, 10)) 31 } 32 33 // SetIgnitionFile specifies the machine's ignition file 34 func (q *QemuCmd) SetIgnitionFile(file define.VMFile) { 35 *q = append(*q, "-fw_cfg", "name=opt/com.coreos/config,file="+file.GetPath()) 36 } 37 38 // SetQmpMonitor specifies the machine's qmp socket 39 func (q *QemuCmd) SetQmpMonitor(monitor Monitor) { 40 *q = append(*q, "-qmp", monitor.Network+":"+monitor.Address.GetPath()+",server=on,wait=off") 41 } 42 43 // SetNetwork adds a network device to the machine 44 func (q *QemuCmd) SetNetwork() { 45 // Right now the mac address is hardcoded so that the host networking gives it a specific IP address. This is 46 // why we can only run one vm at a time right now 47 *q = append(*q, "-netdev", "socket,id=vlan,fd=3", "-device", "virtio-net-pci,netdev=vlan,mac=5a:94:ef:e4:0c:ee") 48 } 49 50 // SetNetwork adds a network device to the machine 51 func (q *QemuCmd) SetUSBHostPassthrough(usbs []machine.USBConfig) { 52 if len(usbs) == 0 { 53 return 54 } 55 // Add xhci usb emulation first and then each usb device 56 *q = append(*q, "-device", "qemu-xhci") 57 for _, usb := range usbs { 58 var dev string 59 if usb.Bus != "" && usb.DevNumber != "" { 60 dev = fmt.Sprintf("usb-host,hostbus=%s,hostaddr=%s", usb.Bus, usb.DevNumber) 61 } else { 62 dev = fmt.Sprintf("usb-host,vendorid=%d,productid=%d", usb.Vendor, usb.Product) 63 } 64 *q = append(*q, "-device", dev) 65 } 66 } 67 68 // SetSerialPort adds a serial port to the machine for readiness 69 func (q *QemuCmd) SetSerialPort(readySocket, vmPidFile define.VMFile, name string) { 70 *q = append(*q, 71 "-device", "virtio-serial", 72 // qemu needs to establish the long name; other connections can use the symlink'd 73 // Note both id and chardev start with an extra "a" because qemu requires that it 74 // starts with a letter but users can also use numbers 75 "-chardev", "socket,path="+readySocket.GetPath()+",server=on,wait=off,id=a"+name+"_ready", 76 "-device", "virtserialport,chardev=a"+name+"_ready"+",name=org.fedoraproject.port.0", 77 "-pidfile", vmPidFile.GetPath()) 78 } 79 80 // SetVirtfsMount adds a virtfs mount to the machine 81 func (q *QemuCmd) SetVirtfsMount(source, tag, securityModel string, readonly bool) { 82 virtfsOptions := fmt.Sprintf("local,path=%s,mount_tag=%s,security_model=%s", source, tag, securityModel) 83 if readonly { 84 virtfsOptions += ",readonly" 85 } 86 *q = append(*q, "-virtfs", virtfsOptions) 87 } 88 89 // SetBootableImage specifies the image the machine will use to boot 90 func (q *QemuCmd) SetBootableImage(image string) { 91 *q = append(*q, "-drive", "if=virtio,file="+image) 92 } 93 94 // SetDisplay specifies whether the machine will have a display 95 func (q *QemuCmd) SetDisplay(display string) { 96 *q = append(*q, "-display", display) 97 } 98 99 // SetPropagatedHostEnvs adds options that propagate SSL and proxy settings 100 func (q *QemuCmd) SetPropagatedHostEnvs() { 101 *q = propagateHostEnv(*q) 102 } 103 104 func (q *QemuCmd) Build() []string { 105 return *q 106 }