Libvirt

From Segfault
(Redirected from KVM)
Jump to navigation Jump to search

Installation

Debian

sudo apt-get install qemu-kvm libvirt-bin
sudo usermod -a -G kvm,libvirt alice

If necessary:[1][2]

sudo deb-systemd-invoke start virtlockd.socket virtlockd.service

Fedora

sudo dnf install virt-manager qemu-kvm libvirt                                          # Fedora, RHEL Note: libvirt seems to be optional
sudo usermod -a -G libvirt alice

Permissions

For already existing VMs or disk images, grant access to everyone in the libvirt group:

sudo find /opt/vm/ -type d -exec setfacl -m u:libvirt-qemu:rwx -m g:libvirt:rwx '{}' +
sudo find /opt/vm/ -type f -exec setfacl -m u:libvirt-qemu:rw  -m g:libvirt:rw  '{}' +

Postinstall

Networking should already be enabled these days:

$ sudo virsh net-edit default
<network>
  <name>default</name>
  <uuid>cc3cf216-c5b0-4e0f-bb5c-94420b5054f6</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='12:34:00:aa:bb:cc'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>

$ sudo systemctl restart libvirtd.service

After KVM has been installed, let's setup a Debian virtual machine:

virt-install --name debian --description "Debian GNU/Linux" --ram 4096 --vcpus 2 \
   --disk path=/opt/vm/debian.qcow,size=4 --os-variant debian9 \                               # Use osinfo-query os for all --os-variant options.
   --network bridge=virbr0 --graphics vnc,listen=127.0.0.1,port=5910 \
   --cdrom ../debian.iso

As Fedora comes with SELinux enabled, we may have to grant extra permissions[3][4] to the locations above:

sudo setsebool -P virt_use_nfs 1
sudo chcon -R --type=virt_image_t /opt/vm/kvm
sudo setfacl -m u:$USER:rwx /opt/vm/kvm

Usage

The newly installed virtual machine can be controlled with virsh[5].

List virtual machines:

$ sudo virsh list 
Id    Name                           State
----------------------------------------------------
2     debian                         running

Connect the virtual machine's serial console:

$ sudo virsh console debian                                                                    # The guest needs to enable its Serial Console
Connected to domain debian
Escape character is ^]

Shutdown/reboot/start a virtual machine:

sudo virsh shutdown debian
sudo virsh reboot debian
sudo virsh start debian

Core dump a virtual machine:

sudo virsh dump debian --file debian.core                                                      # May not work due to non-migratable devices

Bugs

  • Redhat #678555 - systemd should not purge application created cgroups, even if they contain no processes
  • Redhat #452422 - qemu: could not open disk image
  • Redhat #527736 - Storage driver can't create storage pool volumes on a FAT32 hard disk

Links

References

  1. Debian #747568 - Unknown lvalue 'ControlGroup' in section 'Service'
  2. Debian #758688 - Unit virtlockd.service cannot be reloaded because it is inactive
  3. VirtManager: could not open disk image
  4. OpenNebula – KVM QEMU could not open disk image disk.0: Permission denied
  5. Virsh Command Reference