User:Egberts/Drafts/QEMU/Linux guest

From Gentoo Wiki
< User:Egberts‎ | Drafts‎ | QEMU
Jump to:navigation Jump to:search
This article is a stub. Please help out by expanding it - how to get started.
ROUGH DRAFT: Partially seeded by QEMU/Linux guest page
Needs kernel configuration settings needed for various scenarios: CONFIG_EMBEDDED, different CPU architectures, disk-less, swap-less, initramfs-free.

This article describes the creation of the Gentoo Linux OS in a QEMU VM..



Guest setup

We could take this entire 'Guest setup' section and put it under a new QEMU/guest wiki group but a trend has already been established as QEMU/Linux guest, QEMU/Windows guest, ...

Kernel configuration

As a demonstration of completeness of configuration settings for the Linux kernel, the kernel configuration file will be initialized to tinyconfig which is the smallest buildable Linux kernel (but not necessarily the bootable or functional one).

Backup the/usr/src/linux/.config, if needed!

Now rewrite the .config with the smallest default setting possible, execute:

root #cd /usr/src/linux make tinyconfig

Using the guest OS/VM

Manual use

Use from Gentoo init system

Use from Gentoo systemd

Accessing the guest



Accessing the container with sshd

Filesystem layout

Template scripts


Automatic setup: QEMU standard Gentoo template script

Automatic setup: qemu-gentoo

Other distributions

Headless server

When running on a headless server, the settings will need tweaked a bit:

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" \

When prompted at boot time to select the kernel, input the following to obtain a TTY:

boot: gentoo console=ttyS0


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.


When using genkernel do not build the VirtIO drivers as modules, compile them into the kernel.

KERNEL Enable built-in VirtIO support
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

By using SLAB over SLUB, the level of kernel effort during lookaside page translation is reduced by 2-level of pointer indirections for each page read; SLAB is generally faster for a guest OS but at the expense of introducing CPU-jitter at application level.

KERNEL Choose SLAB allocator (CONFIG_SLAB)
General setup  --->
  Choose SLAB allocator (SLAB)  --->
    (X) SLAB
    ( ) SLUB (Unqueued Allocator)

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

(chroot) livecd /usr/src/linux #make defconfig
(chroot) livecd /usr/src/linux #make kvm_guest.config


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 the following lines:

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

Finally, uncomment the following:

FILE /etc/inittab
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


Expose images to LAN

Sometimes it is required that the image should get a proper IP address on the LAN network to allow other peers to access it.

Such a configuration is possible by using an existing network bridge and telling the machine to use it.

Assuming that there exists a bridge called br0 on the machine, the following configuration exposes the image to the LAN.

exec qemu-system-x86_64 -enable-kvm \
        -cpu host \
        -drive file=Gentoo-VM.img,if=virtio \
        -netdev bridge,id=net0,br=br0 \
        -device virtio-net-pci,netdev=net0 \
        -device virtio-rng-pci \
        -m 512M \
        -smp 2 \
        -nographic \
        -name "Gentoo VM" \
root #./ -boot d -cdrom install-amd64-minimal-20120621.iso

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


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)


   [   12.667558] random: crng init done

about 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) kernel configuration in the guest VM 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 kernel configuration inside the guest VM. However, there are security concerns with this approach.

VM shutdown problems

Host control scripts may send a system_powerdown message to the virtual machine in order to shut it down.

For this to work properly, ACPI functionality on the guest is necessary. Also, ACPI daemon sys-power/acpid should be installed and running on the guest.

root #emerge --ask sys-power/acpid

Firmware hang

If the following lines were the last breath of a failed Gentoo boot:

[  0.95951] Loading firmware: regulatory.db
[  0.95980] platform regulatory.0: Direct firmware load for regulatory.db

then the next line (that is missing) is your video driver bootup failed.

Workaround: Go back to the virt-manager View->Detail submenu and change to Video QXL option from Video Virtio.

See also