User:Egberts/Drafts/QEMU/Linux guest
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..
Installation
Configuration
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
qemu-console
qemu-attach
Accessing the container with sshd
Filesystem layout
Template scripts
Gentoo
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:
#!/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" \
${@}
When prompted at boot time to select the kernel, input the following to obtain a TTY:
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
When using genkernel do not build the VirtIO drivers as modules, compile them into the 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
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.
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
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 the following lines:
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0"
GRUB_TERMINAL=console
Finally, uncomment the following:
# 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
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.
#!/bin/bash
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 #
./start_Gentoo_VM.sh -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
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
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
- User:Egberts/Drafts/Virtualization — the concept and technique that permits running software in an environment separate from a computer operating system.
- User:Egberts/Drafts/QEMU — a generic, open source hardware emulator and virtualization suite.
- User:Egberts/Drafts/QEMU/Front-ends
- User:Egberts/Drafts/Libvirt — a virtualization management toolkit.
- User:Egberts/Drafts/Libvirt/QEMU_networking — details the setup of Gentoo networking by Libvirt for use by guest containers and QEMU-based virtual machines.
- User:Egberts/Drafts/Libvirt/QEMU_guest — covers libvirt and its creation of a virtual machine (VM) for use under the soft-emulation mode QEMU hypervisor Type-2, notably using only CLI tools such as virt-install and virt-xml-edit command.
- User:Egberts/Drafts/Virt-manager — desktop user interface for management of virtual machines and containers through the libvirt library
- User:Egberts/Drafts/Virt-manager/QEMU_guest — QEMU creation of a guest (VM or container)
- User:Egberts/Drafts/QEMU/Linux guest — describes the creation of the Gentoo Linux OS in a QEMU VM.