The Kernel Virtual Machine is a hypervisor for Linux on hardware with virtualization extensions (Intel VT or AMD-V). It is deployed as a loadable kernel modules, kvm.ko
, and either kvm-intel.ko
or kvm-amd.ko
.
The KVM Debian Wiki rocks, and provides details on the basics including a great performance tuning section.
Install
Easy instructions to get QEMU/KVM and virt-manager up and running on Arch.
- Check CPU supports virtualisation
grep -E "(vmx|svm)" --color=always /proc/cpuinfo
- Make sure VT CPU extension is enabled in BIOS.
- User access to
/dev/kvm
is required, so add users into kvm(78) group withsudo gpasswd -a USER_NAME kvm
- Loading kernel modules
kvm_intel
orkvm_amd
depend on your CPU, Add module name in/etc/modules-load.d/kvm.conf
eitherkvm_intel
orkvm_amd
- Install
qemu
,virt-manager
,dnsmasq
andiptables
withsudo pacman -S qemu virt-manager dnsmasq iptables ebtables dnsmasq
- Run and enable boot up start
libvirtd
daemon withsystemctl start libvirtd
andsystemctl enable libvirtd
- Use PolicyKit authorization create
/etc/polkit-1/rules.d/50-libvirt.rules
with the example policy below. - Create the
libvirt
group and add users withgroupadd libvirt
thensudo gpasswd -a USER_NAME libvirt
- Check network interface status
sudo virsh net-list --all
. If it isinactive
start it usingsudo virsh net-start default
- Now you can use
virt-manager
GUI to build some VMs.
PolicyKit that allows the kvm
group to manage libvirt:
/* Allow users in kvm group to manage the libvirt
daemon without authentication */
polkit.addRule(function(action, subject) {
if (action.id == "org.libvirt.unix.manage" &&
subject.isInGroup("kvm")) {
return polkit.Result.YES;
}
});
Administration tasks
User specific vs system wide VMs
virsh
when not run as root, will default the connection to libvirt using qemu://session
, sandboxing the view of VMs per user.
To manage system (i.e. root) level VMs either run virsh
as root, or run virsh
with a custom connect string virsh --connect qemu:///system list --all
.
To change the default bind string can set the LIBVIRT_DEFAULT_URI
env var.
$ export LIBVIRT_DEFAULT_URI='qemu:///system'
List VMs
List user specific VMs:
$ virsh list --all
Or system wide VMs:
$ virsh --connect qemu:///system list --all
Or:
# virsh list --all
Start VM
# virsh start ARCHBOX
Shutdown VM
# virsh shutdown ARCHBOX
Murder (hung) VM
# virsh destroy UBUNTUBOX
Autostart default NATed bridged network
sudo virsh net-autostart default
What if the default
network interface is not listed
If virsh net-list
is not listing any network interface just reinitialize it with:
sudo virsh net-define /usr/share/libvirt/networks/default.xml
How to extend / increase a partition
- Shutdown the VM
virsh shutdown hostname
- Increase the qcow2 image. Find the qcow2 file of the VM and take a backup (just in case).
cp hostname.qcow2 hostname.qcow2.backup
andqemu-img resize hostname.qcow2 +100GB
- Start the VM
virsh start hostname
- Extend the partition in Window
Use network ISO source for new VMs
Using the awesome --location
option:
virt-install --virt-type kvm --name buster-amd64 \
--location http://deb.debian.org/debian/dists/buster/main/installer-amd64/ \
--os-variant debian10 \
--disk size=10 --memory 1000
Windows VM disk driver
Check and install fast IO driver virtio-win
on guest Windows VM.
Create new VM guest with below configuration: IDE storage for Windows OS container
WINDOWS.qcow2
, IDE CDROM attach Windows OS ISO.Start VM guest and install the Windows OS as usual
Shutdown VM guest
Reconfigure VM guest: Add a dummy VirtIO / VirtIO SCSI storage with 100MB size, e.g.
DUMMY.qcow2
, then attach VirtIO driver CD ISO to the IDE CDROM.Restart VM guest
Install the VirtIO driver from the IDE CDROM when Windows prompt for new hardware driver
For VM guest of Windows 10 and above, run “cmd” as Administrator and run:
bcdedit /set {current} safeboot minimal
Shutdown VM guest
Reconfigure VM guest with below configuration: Remove IDE storage for Windows OS DONT delete
WINDOWS.qcow2
, remove VirtIO storage for dummy storage you can deleteDUMMY.qcow2
, remove IDE storage for CD ROM, then add a new VirtIO/VirtIO SCSI storage and attachWINDOWS.qcow2
to it.Restart the VM guest
For VM guest of Windows 10 and above, run command:
bcdedit /deletevalue {current} safeboot