Install KVM

How to install and setup KVM

Install KVM packages

apt install qemu-kvm libvirt-clients libvirt-daemon-system virtinst libguestfs-tools

Configure NAT for Network Access

The network interface is already there, but it is not started. Configure autostart like this:

virsh net-autostart default
virsh net-start default

Prepare Automatic Installation for Debian

Create a script preseed.cfg which is used to automate Debian installation:


Create a new Virtual Machine

virt-install --virt-type kvm --name vm2 --vcpus 2 --ram 1024 --disk size=8 --network bridge=virbr0 --graphics none --console pty,target_type=serial --location --os-variant debian9 --extra-args "auto=true language=en country=DE locale=en_US.UTF-8 hostname=vm2 console=ttyS0,115200n8 serial" --initrd-inject=preseed.cfg

Management of Virtual Machines

virsh list
virsh list --all
virsh start vm1 --console
virsh shutdown vm1
virsh destroy vm1
virsh undefine vm1

Accessing Data from VM

You can mount the file system of a VM like this:

guestmount -d vm1 -m /dev/sda1 /mnt

Configure Console

To see the console of a virtual machine, it has to be forwarded. To do that, you need to modify the boot parameters inside the VM and add the console in /boot/grub/grub.cfg:

linux<->/boot/vmlinuz-4.9.0-4-amd64 root=/dev/vda1 ro  quiet console=ttyS0

Configure VM

Change memory settings:

virsh setmaxmem vm1 1G --config
virsh setmem vm1 1G --config