QEMU Guide

From ParabolaWiki
Jump to: navigation, search
Summary
KVM
QEMU

1 About the article

This article covers some doubts how to create and run QEMU virtual machines (VMs) from Parabola and get more perfomance during the using of our VMs. In our case we are using a machine with dualcore cpu and kvm support.

Note: qemu-system-* binaries (for example qemu-system-i386 or qemu-system-x86_64, depending on emulated architecture) are used to run the virtualized system.

2 Some necessaries tips to know before using QEMU

The -cpu host option was used to make QEMU emulate the host's exact CPU. If you don't do this, it may be trying to emulate a more generic CPU. If the host machine has multiple CPUs, assign the guest more CPUs using the -smp option, on our case -smp option is 2, which is according to the number of cores which we are using. Make sure you have assigned the virtual machine enough memory. By default, QEMU only assigns 128MiB of memory to each virtual machine. Use the -m option to assign more memory. For example, -m 512 runs a virtual machine with 512MiB of memory which is our case. See here for more details.

Note: Read the following steps before begin to know if your machine has kvm support

3 How to create the image (example: 10 Gbytes)

$ qemu-img create -f raw distro_image.img 10G
Note: Read the following steps here for more details.

4 To install a distro from a image file

$ qemu-system-i386 -enable-kvm -machine type=pc,accel=kvm -cpu host -smp 2 -m 512 -vga std -soundhw all -usb -usbdevice tablet -net nic -net user -hda distro_image.img -cdrom ~/distro.iso -boot d

5 Using virtual machines (user-mode networking)

Here there is a example how to open a VM:

$ qemu-system-i386 -enable-kvm -machine type=pc,accel=kvm -cpu host -smp 2 -m 512 -vga std -soundhw all -usb -usbdevice tablet -net nic -net user -hda distro_image.img

However, if you would specify the guest-visible address of the host to use static address on your VM, the -net user flag have some important options for it. The net option set IP network address the guest will see. Optionally specify the netmask, either in the form a.b.c.d or as number of valid top-most bits. Default is 10.0.2.0/24. host option specify the guest-visible address of the host. Default is the 2nd IP in the guest network, i.e. x.x.x.2. dns option specify the guest-visible address of the virtual nameserver. The address must be different from the host address. Default is the 3rd IP in the guest network, i.e. x.x.x.3.

Here there is a example about it:

$ qemu-system-i386 -enable-kvm -machine type=pc,accel=kvm -cpu host -smp 2 -m 512 -vga std -soundhw all -usb -usbdevice tablet -net nic -net user,net=192.168.0.0/24,host=192.168.0.2,dns=192.168.0.1 -hda distro_image.img
Note: The netmask in -net user flag can be different to your netmask on your machine. i.e: real machine could be 192.168.0.0/24 and guest 10.0.2.0/24. See the QEMU documentation on the -net user flag for more details.

6 Using virtual machines for use servers from OS guests (bridge-mode networking using qemu-bridge-helper)

The following describes how to bridge a virtual machine to a host interface such as eth0, which is probably the most common configuration. This configuration makes it appear that the virtual machine is located directly on the external network, on the same Ethernet segment as the physical host machine.

A bridge is a piece of software used to unite two or more network segments. A bridge behaves like a virtual network switch, working transparently (the other machines don't need to know or care about its existence). Any real devices (e.g. eth0) and virtual devices (e.g. tap0) can be connected to it.

This article explains how to create a bridge that contains at least an ethernet device.

6.1 Installation

Install the netctl package from the official repositories.

Install the bridge-utils package from the official repositories.

6.2 Configuration (Parabola host)

  • Load the tun module and configure it to be loaded on boot. See Kernel modules for details.
  • Add kvm group for your user to get the permission to use bridge mode.
# gpasswd -a your_user kvm
  • Create the tap. Remember to name your bridge as tap0, or change the scripts below to your tap's name.
# cp -a /etc/netctl/examples/tuntap /etc/netctl/examples/tap0
  • Create the bridge. Remember to name your bridge as br0, or change the scripts below to your bridge's name.
# cp -a /etc/netctl/examples/bridge /etc/netctl/examples/br0
  • On this example, we edit our eth0, create a bridge called br0 which has real Ethernet adapter eth0 and a tap device tap0 connected to it. Even we create tap manually from netctl too.
/etc/netctl/eth0
Connection=ethernet
Description='Ethernet connection'
Interface=eth0
IP=no
IP6=no
/etc/netctl/tap0
Description='Tuntap connection'
Interface=tap0
Connection=tuntap
Mode='tap'
User='nobody'
Group='nobody'
/etc/netctl/br0
Description="Static bridge connection"
Interface=br0
Connection=bridge
BindsToInterfaces=(eth0 tap0)
IP=static
IP6=static
Address='192.168.0.100/24'
Address6='fd06:dc06:1ab2::100/48'
Gateway='192.168.0.1'
Gateway6='fd06:dc06:1ab2::1'
Hostname=your_hostname
DNS=('192.168.0.1' 'fd06:dc06:1ab2::1')
DNSDomain=your_domain_name.org
DNSSearch=your_domain_name.org

Even, how we use static IP, so must also edit /etc/resolv.conf.

/etc/resolv.conf
# Generated by resolvconf
domain your_domain_name.org
nameserver 192.168.0.1
nameserver fd06:dc06:1ab2::1
Note: Domain name is optional, not a requirement
  • You can bridge any combination of network devices editing BindsToInterfaces option.
  • If any of the bridged devices (e.g. eth0, tap0) had dhcpcd enabled, disable and stop the dhcpcd@eth0.service daemon. Or set IP=no to the netctl profiles.
  • Restart /etc/netctl/eth0, and enable and start /etc/netctl/tap0 and /etc/netctl/br0, respectively.
  • Copy /etc/qemu/bridge.conf.sample to /etc/qemu/bridge.conf. Now modify /etc/qemu/bridge.conf to contain the names of all bridges to be used by QEMU:
/etc/qemu/bridge.conf
allow br0
  • Finally, type the following command to use QEMU on bridge mode.
$ qemu-system-i386 -enable-kvm -machine type=pc,accel=kvm -cpu host -smp 2 -m 512 -vga std -soundhw all -usb -usbdevice tablet -net nic -net bridge -hda distro_image.img

6.3 Configuration (Parabola guest)

After enter on our Parabola guest, we edit eth0

/etc/netctl/eth0
Connection=ethernet
Description='Ethernet connection'
Interface=eth0
IP=static
IP6=static
Address='192.168.0.101/24'
Address6='fd06:dc06:1ab2::101/48'
Gateway='192.168.0.1'
Gateway6='fd06:dc06:1ab2::1'
Hostname=your_hostname_guest
DNS=('192.168.0.1' 'fd06:dc06:1ab2::1')
DNSDomain=your_domain_name.org
DNSSearch=your_domain_name.org

Even, how we use static IP on the guest too, so must also edit /etc/resolv.conf.

/etc/resolv.conf
# Generated by resolvconf
domain your_domain_name.org
nameserver 192.168.0.1
nameserver fd06:dc06:1ab2::1

7 Virtual machine with vmware drivers support

The following example is useful if you would get better performance on the video.

Note: Read the following steps on [1] and [2] to get more info about vmware drivers
$ export SDL_VIDEO_X11_DGAMOUSE=0 && qemu-system-i386 -enable-kvm -machine type=pc,accel=kvm -cpu host -smp 2 -m 512 -vga vmware -soundhw all -usb -usbdevice tablet -net nic,model=virtio -net user -hda distro_image.img

7.1 Monitor settings for vmware

Note: This step is OPTIONAL and should not be done unless you know what you are doing.

First, create a new config file on the guest VM, such as /etc/X11/xorg.conf.d/10-monitor.conf.

Insert the following code into the config file mentioned above:

/etc/X11/xorg.conf.d/
Section "Monitor"
    Identifier    "Monitor0"
    HorizSync 1.0 - 10000.0
    VertRefresh 1.0 - 10000.0
EndSection

Section "Device"
    Identifier    "Device0"
    Driver        "vmware" #Choose the driver used for this monitor
EndSection

Section "Screen"
    Identifier    "Screen0"  #Collapse Monitor and Device section to Screen section
    Device        "Device0"
    Monitor       "Monitor0"
    DefaultDepth  24 #Choose the depth (16||24)
    SubSection "Display"
        Viewport 0 0
        Depth       24
        Modes     "1600x1200" #Choose the resolution
    EndSubSection
EndSection 

8 Virtual machine with vmware drivers and virtio support

Using the virtio drivers, you will provide better performance and lower overhead on your VMs.

$ export SDL_VIDEO_X11_DGAMOUSE=0 && qemu-system-i386 -enable-kvm -machine type=pc,accel=kvm -cpu host -smp 2 -m 512 -vga vmware -soundhw all -usb -usbdevice tablet -net nic,model=virtio -net user -boot order=c -drive file=distro_image.img,if=virtio
Warning: Read the steps related to vmware on the previous note and even the following steps here before use it

9 How to enable a USB device on guest VMs

Before enable it, you need know the USB device's ID, in our case is a USB key:

$ lsusb
Bus 005 Device 002: ID 0951:1607 Kingston Technology DataTraveler 100
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

Our ID is 0951:1607, so the -usbdevice parameter should be:

-usbdevice host:0951:1607

Here there is a example how to put it:

$ export SDL_VIDEO_X11_DGAMOUSE=0 && qemu-system-i386 -enable-kvm -machine type=pc,accel=kvm -cpu host -smp 2 -m 512 -vga vmware -soundhw all -usb -usbdevice table,host:0951:1607 -net nic,model=virtio -net user -boot order=c -drive file=distro_image.img,if=virtio

10 Tips during emulation

The following keys are useful to use toggling on qemu:

 ctrl-alt-f      toggle full screen
 ctrl-alt-n      switch to virtual console 'n'
 ctrl-alt        toggle mouse and keyboard grab