QEMU/Linux guest

From Gentoo Wiki
< QEMU
Jump to: navigation, search

This article describes the setup of a Gentoo Linux guest using QEMU.

Configuration

Host

To create a disk image for the virtual machine, run:

user $qemu-img create -f qcow2 Gentoo-VM.img 15G

Download a minimal Gentoo LiveCD from here.

Since QEMU requires a lot of options, it would be a good idea to put them into a shell script, e.g.:

FILE start_Gentoo_VM.sh
#!/bin/bash
exec qemu-system-x86_64 -enable-kvm \
        -cpu host \
        -drive file=Gentoo-VM.img,if=virtio \
        -netdev user,id=vmnic,hostname=Gentoo-VM \
        -device virtio-net,netdev=vmnic \
        -device virtio-rng-pci \
        -m 512M \
        -smp 2 \
        -monitor stdio \
        -name "Gentoo VM" \
        $@

Change the path to your disk image Gentoo-VM.img in the script. You can add more options when calling the script. To boot the disk image, run:

user $./start_Gentoo_VM.sh -boot d -cdrom install-amd64-minimal-20120621.iso

Install the guest per the Gentoo Handbook. See the guest section for optimum support. After the installation start the script without the additional options.

Headless server

If running on a headless server, you will need to tweak the settings a bit

FILE start_Gentoo_VM.sh
#!/bin/bash
exec qemu-system-x86_64 -enable-kvm \
        -cpu host \
        -drive file=Gentoo-VM.img,if=virtio \
        -netdev user,id=vmnic,hostname=Gentoo-VM \
        -device virtio-net,netdev=vmnic \
        -device virtio-rng-pci \
        -m 512M \
        -smp 2 \
        -nographic \
        -name "Gentoo VM" \
        $@

and when prompted at boot time to select the kernel, you should input

FILE start_Gentoo_VM.sh
boot> gentoo console=ttyS0

Guest

Hard drive

The VirtIO hard drive is mapped to /dev/vda. Where the handbook refers to /dev/sdaX, always use /dev/vdaX when configuring the guest.

Kernel

If you use genkernel do not build the VirtIO drivers as modules, compile them into the kernel.

KERNEL
Processor type and features  --->
    [*] Linux guest support --->
        [*] Enable Paravirtualization code
        [*] KVM Guest support (including kvmclock)
Device Drivers  --->
    [*] Virtio drivers  --->
        <*> PCI driver for virtio devices
    [*] Block devices  --->
        <*> Virtio block driver
    SCSI device support  --->
        [*] SCSI low-level drivers  --->
            [*] virtio-scsi support
    [*] Network device support  --->
        [*] Network core driver support
            <*> Virtio network driver
    Graphics support  --->
        <*> Virtio GPU driver
    Character devices ---> 
       <*>   Hardware Random Number Generator Core support --->
           <*>   VirtIO Random Number Generator support

As an alternative, use these commands after emerging the kernel sources:

(chroot) livecd / #cd /usr/src/linux
(chroot) livecd /usr/src/linux #make defconfig
(chroot) livecd /usr/src/linux #make kvmconfig

GRUB

For a minimal grub BIOS install:

(chroot) livecd / #echo 'GRUB_PLATFORMS="pc"' >> /etc/portage/make.conf
(chroot) livecd / #echo 'sys-boot/grub -fonts -nls -themes' > /etc/portage/package.use/grub
(chroot) livecd / #emerge --ask sys-boot/grub:2

Optional: to make the guest work in the headless mode, add these lines:

FILE /etc/default/grub
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0"
GRUB_TERMINAL=console

and uncomment the following:

FILE /etc/inittab
# SERIAL CONSOLES
s0:12345:respawn:/sbin/agetty -L 115200 ttyS0 vt100

Install grub on the guest disk:

(chroot) livecd / #grub-install /dev/vda
Installing for i386-pc platform.
Installation finished. No error reported.

Configure grub for the kernel build earlier:

(chroot) livecd / #grub-mkconfig -o /boot/grub/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-4.9.16-gentoo
done

Advanced

Optional post install guest IPv6 setup

For IPv6 networking see the IPv6 subarticle.

Run images as service

To conveniently configure, start and stop a Linux (or any other) guest, check out this great init script.

Mount guest image

To access the guest disk from the host (and e.g. chroot into the guest), use a "Network Block Device":

root # modprobe nbd max_part=16
root # qemu-nbd -c /dev/nbd0 Gentoo-VM.img
root # mount /dev/nbd0p4 /mnt/gentoo

Make any changes required and clean up:

root # umount /mnt/gentoo
root # qemu-nbd -d /dev/nbd0

Troubleshooting

Boot hangs at syslog-ng

If the guest boots slow, or if the boot hangs on * Checking your configfile (/etc/syslog-ng/syslog-ng.conf) or there are syslog messages like [ 1.264763] random: dbus-deamon: uninitialized urandom read (12 bytes read) or [ 12.667558] random: crng init done (12 seconds after booting), this is likely due to the lack of entropy. A way to fix this is to enable the "VirtIO Random Number Generator support" (HW_RANDOM_VIRTIO=y) in the guest kernel and boot with the QEMU virtio-rng-pci device.

Another way to solve this is to enable "Trust the CPU manufacturer to initialize Linux's CRNG" (RANDOM_TRUST_CPU=y) in the guest kernel. However, there are security concerns with this approach.