====== KVM ======
===== Installation =====
* {{Package|direct=yes|app-emulation/qemu-kvm}}
* {{Package|direct=yes|app-emulation/libvirt}} {{USEFlag|{{EnableFlag|avahi}} {{EnableFlag|libvirtd}} {{EnableFlag|lvm}} {{EnableFlag|network}} {{EnableFlag|nfs}} {{EnableFlag|qemu}} {{EnableFlag|udev}} {{DisableFlag|iscsi}} {{DisableFlag|openvz}} {{DisableFlag|virtualbox}} {{DisableFlag|xen}} }}
===== Building a Host Kernel =====
{{Root|
genkernel \
--no-clean \
--no-mrproper \
--iscsi \
--disklabel \
--lvm \
--luks \
--unionfs \
--splash \
--splash=natural-gentoo \
--splash-res=1280x800 \
--initramfs-overlay=/net/universe/mnt/pool/kvm/kernels/build/initramfs-overlay/ \
all
}}
===== Building a Guest Kernel =====
The initrd doesn't have a udev implementation, so won't create the device nodes for the vda block devices automatically. We have to manually create these, and arrange for them to be available in /dev inside the initrd. ''/net/universe/mnt/pool/kvm/kernels/build/initramfs-overlay'' contains a /dev directory, and device nodes copied from an live cd.
The kvm kernel configs also build the virtio device drivers as modules, so these need to be loaded into the initramfs before the main system can boot. Genkernel does not currently include these modules by default, so the default initramfs scripts need to be updated to add support.
Edit ''module_load'' to have the modules included in the initramfs:
{{File|/usr/share/genkernel/arch/x86_64/module_load|
- Virtual hardware drivers
MODULES_VIRTIO="virtio virtio_balloon virtio_console virtio_net virtio_blk virtio_pci virtio_ring"
}}
Then edit ''initrd.default'' to recognise the new VIRTIO module group and have the modules modprobe'd during the ''/linuxrc''>
{{File|/usr/share/genkernel/default/initrd.default|
HWOPTS='keymap cache modules pata sata scsi usb firewire waitscan slowusb evms lvm dmraid mdadm fs net virtio'
MY_HWOPTS='modules pata sata scsi usb firewire waitscan dmraid mdadm fs net iscsi virtio'
}}
{{Warning|These files are not protected and will be replaced each time {{Package|direct=yes|sys-kernel/genkernel}} is updated.}}
Now build the kernel, initrd, and modules packages with the following command line:
{{Root|
genkernel \
--no-clean \
--no-mrproper \
--kernname=kvm-sabayon \
--kerneldir=/usr/src/linux-2.6.35-sabayon \
--kernel-config=/net/universe/mnt/pool/kvm/kernels/build/config-x86_64-2.6.35-kvm-sabayon-r6 \
--iscsi \
--disklabel \
--lvm \
--luks \
--unionfs \
--splash \
--splash=natural-gentoo \
--splash-res=1280x800 \
--initramfs-overlay=/net/universe/mnt/pool/kvm/kernels/build/initramfs-overlay/ \
--minkernpackage=/net/universe/mnt/pool/kvm/kernels/build/kernel-x86_64-2.6.35-kvm-sabayon-r6.tar.bz2 \
--modulespackage=/net/universe/mnt/pool/kvm/kernels/build/modules-x86_64-2.6.35-kvm-sabayon-r6.tar.bz2 \
--no-install \
all
}}
This will build a bzip'd tarball of the kernel files, and another package containing all the modules. Extract the kernel files from the archive and put them in the right place. The generated files wont have the correct ''kernname'' either, so fix that at the same time.
{{Root|
cd /net/universe/mnt/pool/kvm/kernels/
tar -xjf kernel-x86_6x-2.6.35-kvm-sabayon-r6.tar.bz2
mv kernel-2.6.35-sabayon kernel-x86_64-2.6.35-kvm-sabayon-r6
mv config-x86_64-2.6.34-{sabayon,kvm-sabayon}-r6
mv initramfs-x86_64-2.6.34-{sabayon,kvm-sabayon}-r6
}}
The modules package needs to be copied into the guest, and extracted into the root filesystem. Finally, edit the domain config to use the new kernel and initramfs, then reboot the guest and hope for the best.
===== Provision a new VM using the command line =====
{{Root|
virt-install \
--connect qemu+ssh://root@$VMHOST/system \
--name $NAME \
--ram $RAM \
--arch=$ARCH \
--description "$DESCRIPTION" \
--cdrom /var/lib/libvirt/images/kvm-iso/$INSTALLISO \
--os-type none \
--os-variant none \
--disk pool=kvm-images,device=disk,bus=virtio,size=$DISKSIZE,format=raw \
--network bridge=br0,model=virtio \
--network bridge=br1,model=virtio \
--hvm \
--virt-type kvm \
--soundhw ac97 \
--video vga \
--vnc \
--noreboot \
--autostart
}}
''$ARCH'' should be one of ''x86_64'' or ''i686''. ''$RAM'' is the amount of memory in MB, and ''$DISKSIZE'' is the size of the vritual disk drive in GB.
===== Creating a new instance of a template =====
{{Root|
virt-clone \
--connect qemu+ssh://root@$VMHOST/system \
--original=$TEMPLATE \
--name=$NAME \
--file=/var/lib/libvirt/images/$NAME.img
}}
===== Sending SysRq keys =====
You can use virsh to send SysRq key presses to guests; handy if things get stuck and you want to force a slighly more graceful reboot than pulling the virtual power.
{{Root|
- where $key is one of REISUB
virsh send-key $domain KEY_LEFTALT KEY_SYSRQ KEY_${key}
}}
Credit for this tip to [[http://dustymabe.com/2012/04/21/send-magic-sysrq-to-a-kvm-guest-using-virsh/|Dusty Mabe]] for this.