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 c9admin
        # 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 20G
        

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 \
            mkpart ESP fat32 1MiB 120MiB \
            mkpart primary ext4 120MiB 720MiB \
            mkpart primary ext4 720MiB 2720MiB \
            mkpart primary ext4 2720MiB 5000MiB \
            set 1 boot on
        
        # kpartx -a -s -l -u /dev/nbd0
        

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

        # mkfs.fat -F 32 $BLK_EFI
        # mkfs.ext4 $BLK_BOOT
        # mkfs.ext4 $BLK_ROOT
        # mkfs.ext4 $BLK_VAR
        

Mount partition;

        # mount $BLK_ROOT $CHROOT
        # mkdir -p $CHROOT/boot
        # mount $BLK_BOOT $CHROOT/boot
        # mkdir -p $CHROOT/boot/efi
        # mount $BLK_EFI $CHROOT/boot/efi
        # mkdir -p $CHROOT/var
        # mount $BLK_VAR $CHROOT/var
        

Before disconnecting image, clean dev mappings;

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

2. 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"
        

2.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
        

2.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=192.168.1.254
        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
        

Guest System

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

runvm/profile/crux

        iso=iso/crux-3.2.iso
        image=img/crux-img.qcow2
        tap="tap1"
        

runvm/runvm.sh

        #!/bin/bash

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

        source profile/$1
        boot=$2

        mac=$(rmac_addr)

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

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

        GRUB_GFXMODE=640x480
        GRUB_GFXPAYLOAD_LINUX=keep
        
Tools Index

This is part of the c9 Manual. Copyright (C) 2016 c9 team. See the file Gnu Free Documentation License for copying conditions.