+ if node[u"port"] != 22:
+ self._vm_info[u"host_port"] = node[u"port"]
+ self._vm_info[u"host_username"] = node[u"username"]
+ self._vm_info[u"host_password"] = node[u"password"]
+ # Input Options.
+ self._opt[u"qemu_id"] = qemu_id
+ self._opt[u"mem"] = int(mem)
+ self._opt[u"smp"] = int(smp)
+ self._opt[u"img"] = img
+ self._opt[u"vnf"] = vnf
+ # Temporary files.
+ self._temp = dict()
+ self._temp[u"pidfile"] = f"/run/qemu_{qemu_id}.pid"
+ if img == Constants.QEMU_VM_IMAGE:
+ self._opt[u"vm_type"] = u"nestedvm"
+ self._temp[u"qmp"] = f"/run/qmp_{qemu_id}.sock"
+ self._temp[u"qga"] = f"/run/qga_{qemu_id}.sock"
+ elif img == Constants.QEMU_VM_KERNEL:
+ self._opt[u"img"], _ = exec_cmd_no_error(
+ node, f"ls -1 {Constants.QEMU_VM_KERNEL}* | tail -1",
+ message=u"Qemu Kernel VM image not found!"
+ )
+ self._opt[u"vm_type"] = u"kernelvm"
+ self._temp[u"log"] = f"/tmp/serial_{qemu_id}.log"
+ self._temp[u"ini"] = f"/etc/vm_init_{qemu_id}.conf"
+ self._opt[u"initrd"], _ = exec_cmd_no_error(
+ node, f"ls -1 {Constants.QEMU_VM_KERNEL_INITRD}* | tail -1",
+ message=u"Qemu Kernel initrd image not found!"
+ )
+ else:
+ raise RuntimeError(f"QEMU: Unknown VM image option: {img}")
+ # Computed parameters for QEMU command line.
+ self._params = OptionString(prefix=u"-")
+ self.add_params()
+
+ def add_params(self):
+ """Set QEMU command line parameters."""
+ self.add_default_params()
+ if self._opt.get(u"vm_type", u"") == u"nestedvm":
+ self.add_nestedvm_params()
+ elif self._opt.get(u"vm_type", u"") == u"kernelvm":
+ self.add_kernelvm_params()
+ else:
+ raise RuntimeError(u"QEMU: Unsupported VM type!")
+
+ def add_default_params(self):
+ """Set default QEMU command line parameters."""
+ self._params.add(u"daemonize")
+ self._params.add(u"nodefaults")
+ self._params.add_with_value(
+ u"name", f"vnf{self._opt.get(u'qemu_id')},debug-threads=on"
+ )
+ self._params.add(u"no-user-config")
+ self._params.add_with_value(u"monitor", u"none")
+ self._params.add_with_value(u"display", u"none")
+ self._params.add_with_value(u"vga", u"none")
+ self._params.add(u"enable-kvm")
+ self._params.add_with_value(u"pidfile", self._temp.get(u"pidfile"))
+ self._params.add_with_value(u"cpu", u"host")
+
+ self._params.add_with_value(u"machine", self._opt.get(u"machine_args"))
+ self._params.add_with_value(
+ u"smp", f"{self._opt.get(u'smp')},sockets=1,"
+ f"cores={self._opt.get(u'smp')},threads=1"
+ )
+ self._params.add_with_value(
+ u"object", f"memory-backend-file,id=mem,"
+ f"size={self._opt.get(u'mem')}M,mem-path=/dev/hugepages,share=on"
+ )
+ self._params.add_with_value(u"m", f"{self._opt.get(u'mem')}M")
+ self._params.add_with_value(u"numa", u"node,memdev=mem")
+ self._params.add_with_value(u"balloon", u"none")
+
+ def add_nestedvm_params(self):
+ """Set NestedVM QEMU parameters."""
+ self._params.add_with_value(
+ u"net",
+ f"nic,macaddr=52:54:00:00:{self._opt.get(u'qemu_id'):02x}:ff"
+ )
+ self._params.add_with_value(
+ u"net", f"user,hostfwd=tcp::{self._vm_info[u'port']}-:22"
+ )
+ locking = u",file.locking=off"
+ self._params.add_with_value(
+ u"drive", f"file={self._opt.get(u'img')},"
+ f"format=raw,cache=none,if=virtio{locking}"
+ )
+ self._params.add_with_value(
+ u"qmp", f"unix:{self._temp.get(u'qmp')},server,nowait"
+ )
+ self._params.add_with_value(
+ u"chardev", f"socket,host=127.0.0.1,"
+ f"port={self._vm_info[u'serial']},id=gnc0,server,nowait")
+ self._params.add_with_value(u"device", u"isa-serial,chardev=gnc0")
+ self._params.add_with_value(
+ u"chardev", f"socket,path={self._temp.get(u'qga')},"
+ f"server,nowait,id=qga0"
+ )
+ self._params.add_with_value(u"device", u"isa-serial,chardev=qga0")
+
+ def add_kernelvm_params(self):
+ """Set KernelVM QEMU parameters."""
+ self._params.add_with_value(
+ u"serial", f"file:{self._temp.get(u'log')}"
+ )
+ self._params.add_with_value(
+ u"fsdev", u"local,id=root9p,path=/,security_model=none"
+ )
+ self._params.add_with_value(
+ u"device", u"virtio-9p-pci,fsdev=root9p,mount_tag=virtioroot"
+ )
+ self._params.add_with_value(u"kernel", f"{self._opt.get(u'img')}")
+ self._params.add_with_value(u"initrd", f"{self._opt.get(u'initrd')}")
+ self._params.add_with_value(
+ u"append", f"'ro rootfstype=9p rootflags=trans=virtio "
+ f"root=virtioroot console={self._opt.get(u'console')} "
+ f"tsc=reliable hugepages=256 "
+ f"init={self._temp.get(u'ini')} fastboot'"
+ )
+
+ def create_kernelvm_config_vpp(self, **kwargs):
+ """Create QEMU VPP config files.
+
+ :param kwargs: Key-value pairs to replace content of VPP configuration
+ file.
+ :type kwargs: dict