Skip to content

Instantly share code, notes, and snippets.

@fpytloun
Created January 4, 2019 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fpytloun/108288076e4088a36ae826ab0561454f to your computer and use it in GitHub Desktop.
Save fpytloun/108288076e4088a36ae826ab0561454f to your computer and use it in GitHub Desktop.
Libvirt and KVM

Using Libvirt and KVM

Installation

Install required packages
apt-get install qemu-kvm libvirt-bin bridge-utils virtinst
Setup /etc/network/interfaces
iface eth0 inet manual

auto br0
iface br0 inet static
    bridge_interfaces eth0
    bridge_stp off
    bridge_maxwait 5
    .. address, netmask, etc.

Try to put br0 interface up or reboot

brctl addbr br0

Deploy instance

Now you have more options

  • deploy from custom image
  • deploy clean Ubuntu using ubuntu-vm-builder

Ubuntu VM builder

Attention

Untested

apt-get install ubuntu-vm-builder
ubuntu-vm-builder kvm trusty --rootsize=10000 --domain cfg01 --dest cfg01
--hostname cfg01 --arch amd64 --mem 4096 --ip 172.19.5.30 --mask 255.255.255.0
--net 172.19.0.0 --gw 172.19.5.0 --dns 172.19.0.1 --libvirt qemu:///system
--addpkg openssh-server --addpkg acpid --addpkg vim-nox --components
"main,universe"

Boot from image

apt-get install virtinst
Get image and prepare image for guest (you can use overlay image instead)
cd /var/lib/libvirt/images
wget http://apt.cloudlab.cz/images/ubuntu-14-04-x64-1437659147.qcow2
cp ubuntu-14-04-x64-1437659147.qcow2 cfg01.qcow2
Install guest
virt-install --name cfg01 \
    --ram 4096 --vcpus=2 \
    --accelerate --network bridge:br0,model=virtio \
    --disk path=/var/lib/libvirt/images/cfg01.qcow2,bus=virtio,cache=none \
    --boot hd --vnc --noreboot --autostart

Using overlay image

To save disk space, you can use overlay image instead of copying original image for each instance.

qemu-img create -f qcow2 -b ubuntu-14-04-x64-1437659147.qcow2 cfg01.ovl

Then simply use *.ovl for --disk parameter of virt-install command.

Attention

Don't ever touch original image

Cloud-init configuration

To use cloud-init with libvirt, you need to provide meta-data and user-data somehow. There are multiple data sources that cloud-init expects, the simplest way is probably to generate configuration iso and attach it to instance cdrom.

Install dependencies
apt-get install genisoimage
curl -q https://raw.githubusercontent.com/larsks/virt-utils/master/create-config-drive | sed s,/bin/sh,/bin/bash,g > create-config-drive
chmod +x create-config-drive
Prepare userdata file, eg. userdata.sh
#!/bin/bash

sed -i '/iface\ eth0\ inet\ dhcp/d' /etc/network/interfaces
cat << EOF >> /etc/network/interfaces
iface eth0 inet static
  address 172.19.5.101
  netmask 255.255.255.0
  gateway 172.19.5.254
  dns-nameservers 172.19.0.1
EOF

ifdown eth0; ifup eth0

echo "id: test1.int.cloudlab.cz" >> /etc/salt/minion.d/minion.conf
echo "master: 172.19.5.30" >> /etc/salt/minion.d/minion.conf
service salt-minion restart

salt-call state.highstate
Generate configuration iso image
./create-config-drive -k ~/.ssh/id_rsa.pub -u userdata.sh -h test1 test1-config.iso
Finally you can create domain
virt-install --name test1 \
    --ram 4096 --vcpus=2 \
    --accelerate --network bridge:br0,model=virtio \
    --disk path=/var/lib/libvirt/images/test1.qcow2,bus=virtio,cache=none
    --disk path=test1-config.iso,device=cdrom \
    --boot hd --vnc --noreboot --autostart \

Manage instance

List instances
virsh list --all
Stop instance gracefuly
virsh shutdown cfg01
Stop instance by force in case it's hanged
virsh destroy cfg01
Delete instance
virsh undefine cfg01

Resize disk

Attention

Shut down instance first, this is important

You can resize both overlay image or normal image. But don't ever resize image that is source for overlay images.

qemu-img resize cfg01.ovl +10G

On reboot, cloud-init should automatically grow root filesystem.

Extend memory

First you need to redefine max memory for guest:

virsh setmaxmem cfg01 16G --config

After that you need to destroy domain and start it again.

Finally you can set memory onlin with following command:

virsh setmem cfg01 16G --config --live

Parameter --config will update domain XML so changes will preserve boot.

Live migration without shared storage on native KVM

Live migration without shared storage

  • create same disk in target kvm host in /var/lib/libvirt/ (scp)
virsh migrate --verbose --undefinesource --persistent --live --copy-storage-all <vm_name> qemu+ssh://<target_kvm>/system

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment