Tools Index

Qemu

1. Host System

Prepare host system for virtual machines, this includes create new user, loading necessary modules and configure network. Load kvm module, in this example intel module is loaded but depends on host cpu;

        # modprobe -a kvm-intel tun virtio
        

Add users to kvm group;

        # usermod -a -G kvm machine-admin
        # usermod -a -G kvm username
        

2. Disk images

Qemu supports multiple disk images types.

img
Raw disk image, allows dd to a physical device.
raw
Raw disk image, allows dd to a physical device.
qcow2
Qcow disk image file used by qemu.

Create hard disk image, there is different types, this describes how to create a qcow2 type;

        $ qemu-img create -f qcow2 crux-img.qcow2 15G
        

2.1. Mount images

Qemu disk images can be treated as regular disks using qemu disk network block device server;

        $ sudo modprobe nbd
        $ sudo qemu-nbd -c /dev/nbd0 crux-img.qcow2
        

Information about preparing partitions and storage administration. You can use image as a normal disk, example how to use parted to create a gpt system table;

	parted --script ${DEV} \
	     mklabel gpt \
	     unit mib \
	     mkpart primary 2 4 \
	     name 1 grub \
	     mkpart ESP fat32 4 128 \
	     name 2 efi \
	     mkpart primary ext4 128 1128 \
	     name 3 boot \
	     mkpart primary ext4 1128 12128 \
	     name 4 root \
	     mkpart primary ext4 12128 14128 \
	     name 5 var \
	     mkpart primary ext4 14128 100% \
	     name 6 lvm \
	     set 1 bios_grub on \
	     set 2 boot on \
	     set 6 lvm on
        
        # kpartx -a -s -l -u /dev/nbd0
        

Use /dev/mapper/$(name_of_device) to assign correct blocks;

	mkfs.fat -F 32  /dev/mapper/${DEV_NAME}p2
	mkfs.ext4       /dev/mapper/${DEV_NAME}p3
	mkfs.ext4       /dev/mapper/${DEV_NAME}p4
	mkfs.ext4       /dev/mapper/${DEV_NAME}p5
	pvcreate        /dev/mapper/${DEV_NAME}p6
        

Read lvm documentation on how to setup virtual group and logic volumes.

Mount partition;

	mount /dev/mapper/${DEV_NAME}p4 $CHROOT
	mkdir -p $CHROOT/proc
	mkdir -p $CHROOT/sys
	mkdir -p $CHROOT/dev
	mkdir -p $CHROOT/media

	mkdir -p $CHROOT/boot
	mount /dev/mapper/${DEV_NAME}p3 $CHROOT/boot
	mkdir -p $CHROOT/boot/efi
	mount /dev/mapper/${DEV_NAME}p2 $CHROOT/boot/efi
	mkdir -p $CHROOT/var
	mount /dev/mapper/${DEV_NAME}p5 $CHROOT/var
        

Before disconnecting image, clean dev mappings;

        $ sudo kpartx -d /dev/nbd0
        $ sudo qemu-nbd -d /dev/nbd0
        

2.2. Resize images

Verify disk image information;

        $ qemu-img info c1-storage.qcow2
        
	image: c1-storage.qcow2
	file format: qcow2
	virtual size: 10G (10737418240 bytes)
	disk size: 7.6G
	cluster_size: 65536
	Format specific information:
	    compat: 1.1
	    lazy refcounts: false
	    refcount bits: 16
	    corrupt: false
	$
	

In this example is added 25G to the image;

	$ qemu-img resize c1-storage.qcow2 +25G
	

Read lvm resize if image is using lvm, or use resize2fs. If size is not provided to resize2fs, by default it will grow file system to all partition;

        $ sudo qemu-nbd -c /dev/nbd0 /srv/qemu/img/c1-server.qcow2
        
        # kpartx -a -s -l -u /dev/nbd0
        GPT:Primary header thinks Alt. header is not at the end of the disk.
        GPT:Alternate GPT header not at the end of the disk.
        GPT: Use GNU Parted to correct GPT errors.

        # parted /dev/nbd0
        GNU Parted 3.2
        Using /dev/nbd0
        Welcome to GNU Parted! Type 'help' to view a list of commands.
        (parted) print
        Warning: Not all of the space available to /dev/nbd0 appears to be used, you can
        fix the GPT to use all of the space (an extra 16777216 blocks) or continue with
        the current setting?
        Fix/Ignore? Fix

        (parted) resizepart 3 100%
        (parted) quit
        
        # e2fsck /dev/mapper/nbd0p3
        # resize2fs /dev/mapper/nbd0p3
        

3. Network

Network configuration;

slirp
Default virtual NAT'd network.
tun/tap
Good performance to create virtually any type of network topology.
vde
The VDE networking backend.
        KERNEL=="tun", GROUP="kvm", MODE="0660", OPTIONS+="static_node=net/tun"
        

3.1. Routing

Create interface with correct permissions set for kvm group.

        # sysctl -w net.ipv4.ip_forward=1
        # iptables -A INPUT -i br0 -j ACCEPT
        # iptables -A FORWARD -i br0 -j ACCEPT
        # iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -d 10.0.0.0/24 -j ACCEPT
        # iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -j MASQUERADE
        

3.2. Public Bridge

Create bridge, create new tap and add it to bridge;

        DEV="br0"

        ADDR=10.0.0.254
        NET=10.0.0.0
        GW=10.0.0.1
        MASK=24

        # one tap for each cpu core
        NTAPS=$((`/usr/bin/nproc`))

        case $1 in
            start)
                /sbin/ip link add name ${DEV} type bridge
                /sbin/ip addr add ${ADDR}/${MASK} dev ${DEV} broadcast +
                /sbin/ip link set dev ${DEV} up
                /bin/sleep 0.2s

                for i in `/usr/bin/seq $NTAPS`
                do
                    TAP="tap$i"
                    echo "Setting up ${TAP} tap interface."
                    /sbin/ip tuntap add ${TAP} mode tap group kvm
                    /sbin/ip link set ${TAP} up
                    /bin/sleep 0.2s
                    /sbin/ip link set ${TAP} master ${DEV}
                done

                exit 0
                ;;
            stop)

                for i in `/usr/bin/seq $NTAPS`
                do
                    TAP="tap$i"
                    echo "Deleting ${TAP} tap interface."
                    /sbin/ip link del ${TAP}
                done

                /sbin/ip link set dev ${DEV} down
                /sbin/ip route flush dev ${DEV}
                /sbin/ip link del ${DEV}
                exit 0
                ;;
            restart)
                $0 stop
                $0 start
                ;;
            *)
                echo "Usage: $0 [start|stop|restart]"
                ;;
        esac

        # End of file
        

4. Guest System

See scripts/runvm/runvm.sh, as template. Example scripts;

runvm/profile/crux

        mac=$(rmac_addr)
        memory=1024
        boot=d
        tap="tap1"
        iso=iso/crux-3.4.iso
        image=img/crux-standard.qcow2
        other="-vga std -display sdl"
        

runvm/runvm.sh

        function rmac_addr (){
        printf '54:60:BE:EF:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256))
        }

        source profile/$1

        qemu-system-x86_64 \
            -enable-kvm \
            -m ${memory} \
            -boot ${boot} \
            -cdrom ${iso} \
            -hda ${image} \
            -device e1000,netdev=t0,mac=${mac} \
            -netdev tap,id=t0,ifname=${tap},script=no,downscript=no \
            ${other} \
            &
        

Set guests machines to run under the total resolution provided by host system configure grub on the guest with gfxmode;

4.1. Guest Graphics

Get current resolution on host machine;

        $ xrandr --current | fgrep '*'
            1366x768      60.00*+
        

Set grub gfxmod on guest machine, edit /etc/default/grub;

        GRUB_GFXMODE=1366x768
        GRUB_GFXPAYLOAD_LINUX=keep
        

Update grub configuration on guest machine;

        # grub-mkconfig -o /boot/grub/grub.cfg
        

4.2. Guest Sound

Check if DMAR is enable on kernel configuration, Intel and AMD uses different technology. To check on Inter machine run;

	# grep -e DMAR -e IOMMU
	

runvm/profile/crux

        export QEMU_AUDIO_DRV=alsa
        memory=1024
        boot=c
        iso=iso/devuan_jessie_1.0.0_amd64_CD.iso
        image=img/c12-dvd.qcow2
        tap="tap2"
        mac="54:60:be:ef:5c:72"
        other="-soundhw hda -vga std -display sdl"
        

4.3. Guest USB

        # lsusb
        # ls /dev/v4l
        # ls /dev/bus/usb
        
        # chown root:kvm /dev/bus/usb/003/004
        
        export QEMU_AUDIO_DRV=alsa
        memory=1024
        boot=c
        iso=iso/devuan_jessie_1.0.0_amd64_CD.iso
        image=img/c12-dvd.qcow2
        tap="tap2"
        mac="54:60:be:ef:5c:72"
        other="-soundhw hda -vga std -display sdl -usb -device usb-host,vendorid=0x13d3,productid=0x5652"
        

5. Boot iso on usb

        # lsusb
        # ls /dev/bus/usb
        
        # chown root:kvm /dev/bus/usb/003/012
        
        $ qemu-system-x86_64 -m 512 -enable-kvm -vnc :0 -usb  -device usb-host,hostbus=3,hostaddr=12
        
Tools Index

This is part of the Hive System Documentation. Copyright (C) 2018 Hive Team. See the file Gnu Free Documentation License for copying conditions.