qemu-init

Some tools to make life with qemu more comfortable

User Tools

Site Tools


Internals

qemu monitor

The qemu monitor is important cause qemu-init requires it for communication. It will be started by default, redirected via tcp at localhost. The first port number is defined in  /etc/qemu/monitor.conf  (default: 30320). For each virtual machine started by  qemu-init  the port will be increased by 1. Make sure that no other programs uses a port inside the range required by  qemu-init . If no valid port could be determined, the monitor will be disabled silently.

The qemu monitor will be bound to  127.0.0.1  (IPv4) by default always.  qemu-init  checks for address  ::1  (IPv6) too. If it is available the qemu monitor will be bound to ::1 additional.

It is a requirement that the monitor could be accessed via a tcp socket. The stop function of  qemu-init  uses a socket to shutdown virtual machines clean. See Stop a VM below for details.

Ports

There are multiple ports used by the monitor. The first one is MONPORT (monitor port), calculated like described above. The MONPORT is used by  qemu-mon  to establish a permanent connection. The second one is the CTLPORT (control port). Its number is MONPORT+100. The CTLPORT is used by  qemu-cmd . Both tools checks for the proper IP version of the socket. The background to have multiple ports is, that there is only one connection to a socket possible at the same time.

Devices

The config file  <name>-qemu.cfg  provides an easy way to define one or multiple hard-disk and ISO images as well network adapters. The variables are DSKx for virtual hard disk images, CDRx for iso images and NICx for network adapters, where x is an consecutively number starting by 1. As long the order of x isn't interrupted devices will be added. The value of DSKx and CDRx is simply the filename of the image.  qemu-init  uses the qemu options  -drive=$DSKx,snapshot=off  and respectively  -cdrom $CDRx  to connect such devices to the virtual machine.

For NICx's the internal processing is a bit more complex. By default qemu-init is using a (public) network bridge - so the host system have to provide this functionality with tun/tap devices.

It is beyond the scope here to explain how to prepare and setup a network bridge - refer to related documentation.

A common name of the first bridge device is br0, thus this device name will be used by default. Otherwise this could be overwritten in  <name>-qemu.cfg  by setting a variable  BRIDGE_IFNAME=“br1” , where the value (here “br1”) have to be the name of the bridge interface. Add it at the bottom of the cfg-file. This have to be set for each virtual machine.

If the prerequisites are met a NIC could be defined easily, e.g. by  NIC1=“virtio-net-pci” . As only a valid qemu network driver is required as parameter, any other like e1000 could be used also. Thus  qemu-init  uses the following parameter for a network adapter:

-device virtio-net-pci,netdev=net0,mac=DE:AD:BE:EF:34:33
-netdev tap,id=net0,script=/etc/qemu/qemu-brup,downscript=/etc/qemu/qemu-brdown

NIC1 will get the  id=net0 , NIC2 will get  id=net1  and so on. The mac address will be calculated by using the variables $NAME, $OS (both from  <name>-qemu.cfg ) and the consecutively number x of NICx. Thus it is - or should be ;-) - unique for each interface. The prefix of the mac address is  DE:AD:BE:EF  always.

The  /etc/qemu/qemu-brup  and  /etc/qemu/qemu-brdown  are links to  qemu-netif , which is part of the qemu-init package.  qemu-netif  is using the  ip  command from the iptools package - only. In the spirit of KISS qemu-init doesn't require  tunctl  nor  brctl  - note that both are marked as deprecated at KVM Networking.

Refer to the custom section of the config file documentation to learn how to use your own custom device definitions and more.

The start/stop process

FIXME For a better understanding let me explain how the tools are designed to work, how things belongs together and some thoughts behind. In general the first argument is  <name> , given as shortname or longname. All shortnames will be resolved through  /etc/qemu/qemu-init.reg .

Start a VM

At start  qemu-init  reads the config file  <name>-qemu.cfg  of the virtual machine. It consolidates the options and assigns free ports to the monitor and to VNC (default: VNC enabled). The first port for the monitor is defined in  /etc/qemu/monitor.conf , the VNC port (screen) will be calculated.

Stop a VM

This is a bit more complex. To shutdown a virtual guest cleanly the qemu monitor command  system_powerdown  is used. This command triggers the ACPI event “power button pressed” at the virtual guest. Thus the guest virtual machine HAVE TO accept this event - most distributions supports it by default. Otherwise the acpid daemon would be an alternative.

In practice, the stop function of  qemu-init  calls  qemu-cmd -c “system_powerdown” <VMID> .

Last modified: 2020/03/30 07:18

Page Tools