KVM

1 minute read

Install KVM

Check HVM cpu compatibility

egrep '(vmx|svm)' --color=always /proc/cpuinfo

If it display vmx or svm in color, you’re good to go.

Install Packages

This is the selection of packages to install KVM :

apt-get update
apt get-install qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils

To allow a regular user to manage Vms, run :

adduser <youruser> libvirt
adduser <youruser> libvirt-qemu

Setup the virtual bridge :

nano /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet manual

# The Virtual Bridge network interface
auto peth0
iface peth0 inet static
        address 192.168.1.2/24
        gateway 192.168.1.1
        bridge_ports eth0
        bridge_stp off

Manage KVM

Command Line Interface

List VMs state :

virsh list --all
virsh list --inactive

create a VM :

1) create the LV

lvcreate -V 26G --thin -n YOURVM-hvm pool1/thinpool1 #DISK
lvcreate -V 2G --thin -n YOURVM-swap pool1/thinpool1 #SWAP

2) Create the VM

virt-install --name=mid-server --vcpus=6 --memory=8192 --cdrom=/mnt/datatemp/ubuntu-16.04.5-server-amd64.iso --disk=/dev/rootvg/mid-server-disk

3) Create the VM from xml

nano /etc/kvm/YOURVM.xml
<domain type='kvm'>
  <name>YOUR NAME</name>
  <memory unit='KiB'>11534336</memory>
  <currentMemory unit='KiB'>11534336</currentMemory>
  <vcpu placement='static'>12</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-2.8'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <vmport state='off'/>
  </features>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='none' io='native'/>
      <source dev='/dev/rootvg/YOURLV'/>
      <target dev='vda' bus='virtio'/>
    </disk>
   <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='none' io='native'/>
      <source dev='/dev/pool1/osiris-swap'/>
      <target dev='vdb' bus='virtio'/>
    </disk>
    <interface type='bridge'>
      <mac address='12:43:56:78:90:ab'/>
      <source bridge='peth0'/>
      <model type='virtio'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='tablet' bus='usb'>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
  </devices>
</domain>

4) Run virsh define YOURVM.xml to register the domain on the system. The invert of this command is virsh undefine

Start/Stop commands :

virsh start       #START
virsh shutdown    #STOP
virsh destroy     #INTERRUPT
virsh reboot      #RESTART

Enable autostart on a VM :

virsh autostart VMNAME

Disable autostart for a VM :

virsh autostart VMNAME --disable

Snapshot :

lvcreate -s -n MYVM-snap pool1/MYVM   #Create the snapshot
lvchange -ay -Ky pool1/MYVM-snap      #Enable the snapshot

Clone a VM :

virt-clone --original vmbase --name vm-server3 --file /dev/datavg/vm-server3 --force

Access to a VM via console :

virsh console VMNAME

Debug :

virsh domiflist VMNAME        #Show Virtual Interfaces
virsh dominfo                 #Show Basic Informations about the vm

Leave a comment

Leave a comment