KVM Host Setup
Ubuntu 20.04 and newer
Install packages
apt install qemu qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils vlan virtinst libguestfs-tools
For the rest follow steps from Ubuntu 18.04 section
Ubuntu 18.04
Install packages
apt install qemu qemu-kvm libvirt-bin bridge-utils vlan virtinst libguestfs-tools
Networking
If you need interface level vlan support do
sudo modprobe 8021q
Edit /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: eno1: ###this can be named something else on other servers, check the available interface(s) with "ip addr" command dhcp4: no dhcp6: no vlans: eno1.98: id: 98 link: eno1 bridges: br0: interfaces: [eno1] addresses: [ 10.0.0.172/16 ] gateway4: 10.0.100.40 nameservers: addresses: [10.0.10.70] parameters: stp: false forward-delay: 9 hello-time: 2 max-age: 12 extbr98: interfaces: [eno1.98] dhcp4: no dhcp6: no parameters: stp: false forward-delay: 9 hello-time: 2 max-age: 12
If needed, a bond can be configured like this:
bonds: bond0: interfaces: [eno1,enp3s0f1] dhcp4: no dhcp6: no parameters: mii-monitor-interval: 100 mode: 802.3ad down-delay: 200 up-delay: 200 lacp-rate: fast
If you are using bonds, you need to change the config above and make the relevant bridge/vlan use the bond instead of an interface.
Netplan doesn't bring UP interfaces that do not have an IP address (extbr98 bridge for example), so we need to create a simple configuration file to force this. Note: this might be fixed in the future so worth checking at a later time.
Note: this doesn't seem to be needed in Ubuntu 20.04 anymore
#vi /etc/systemd/network/extbr98.network [Match] Name=extbr98 [Network] LinkLocalAddressing=no IPv6AcceptRA=no
Apply the changes
netplan apply
Zabbix Monitoring
For zabbix to be able to monitor if VMs have autostart, we need to do the following steps when building a new KVM host.
NOTE: after installing zabbix-agent
Add zabbix user to libvirt group
adduser zabbix libvirt
Add the following lines at the end of file /etc/zabbix/zabbix_agentd.conf
UserParameter=vm.running.discovery,l=$(virsh -c qemu:///system list | grep running | awk '{print $2}');echo -n '{"data":[';for i in ${l};do echo -n "{\"{#VMNAME}\": \"$i\"},";done|sed -e 's:\},$:\}:';echo -n ']}'; UserParameter=vm.running.check.autostart[*],virsh -c qemu:///system list --all --autostart | grep running | awk '{print $ 2}' | grep -Fxq $1 && echo 1 || echo 0 UserParameter=vm.shutdown.discovery,l=$(virsh -c qemu:///system list --all | grep "shut off" | awk '{print $2}');echo -n '{"data":[';for i in ${l};do echo -n "{\"{#VMNAME}\": \"$i\"},";done|sed -e 's:\},$:\}:';echo -n ']}'; UserParameter=vm.shutdown.check.autostart[*],virsh -c qemu:///system list --all --autostart | grep "shut off" | awk '{print $ 2}' | grep -Fxq $1 && echo 1 || echo 0
Restart the zabbix agent
systemctl restart zabbix-agent
Building VMs
For how to build a VM visit this page: Cloud-Init
Ubuntu 16.04 or older
Basic install
If your machine was built with a package set to support virtualisation this step may not be necessary.
apt-get install kvm libvirt-bin bridge-utils vlan python-vm-builder libguestfs-tools
Networking
If need interface level vlan support do
sudo modprobe 8021q
We use bridging for the network interfaces. There can be several interfaces: e.g. a "main" interface, one for a storage network and/or dedicated connection, one for guests on an SSN. Adapt IP addressing and interface definitions as necessary. Different setups will have very different requirements.
- Edit /etc/network/interfaces as needed, bridging interfaces to suit:
# Main interface auto br0 iface br0 inet static address 10.0.0.162 netmask 255.255.0.0 gateway 10.0.0.153 bridge_ports eth0 [CHECK what your particular interface is called - it may be eth0, but may be em1 for instance] bridge_fd 9 bridge_hello 2 bridge_maxage 12 bridge_stp off # SAN auto br1 iface br1 inet static address 192.168.20.49 netmask 255.255.255.224 bridge_ports eth1 bridge_fd 9 bridge_hello 2 bridge_maxage 12 bridge_stp off # SSN auto eth0.98 iface eth0.98 inet manual auto extbr98 iface extbr98 inet manual bridge_ports eth0.98 bridge_fd 9 bridge_hello 2 bridge_maxage 12 bridge_stp off
The interfaces should have the same name on all hosts, so that we can move VMs from one host to the other without having to change the configuration every time.
Modifying default virtual bridge
By default libvirt creates a virtual bridge named virbr0 (can be checked with ifconfig command). If you want to modify this interface for another subnet you can do this by first shutting down the interface:
virsh net-destroy default
Modify the interface
virsh net-edit default
And start it again
virsh net-start default
Mods for vmbuilder on 16.04 (awaiting fork) - DEPRECATED
If you run into an error from dpkg, related to the script giving the wrong response to a "should I overwrite this conf file" question, edit the related Python lib and rerun:
vi /usr/lib/python2.7/dist-packages/VMBuilder/plugins/ubuntu/dapper.py
Find the line containing "dist-upgrade" and change "-y" to "-s" (changing the command to test mode only)
self.run_in_target('apt-get', '-s', '--force-yes', 'dist-upgrade',
or
self.install_from_template('/etc/sudoers', 'sudoers')
If building 16.04 VMs as normal, you should modify the template file to use the new style Ethernet device naming:
vi /etc/vmbuilder/ubuntu/interfaces.tmpl
and change every instance of eth0 to ens3 (assumes no other changes - as the interfaces now named in order of enumeration, so an additional PCI device could change which is the one associated with a network device. Simpler, in general, to build the basic machine, gain access to it via SSH and modify it and its hardware definition from that point on.
Use the following line to create a new 16.04 VM on a 16.04 host, modifying as necessary to suit your network environment etc.:
vmbuilder kvm ubuntu --libvirt qemu:///system --debug -v --tmpfs=1280 --bridge=br0 --arch=amd64 --suite=xenial --flavour=virtual --mirror='http://gb.archive.ubuntu.com/ubuntu/' --components=main --rootpass=<yourinitialrootpass> -d /var/lib/libvirt/images/<machinename> -m 1024 --cpus=2 --rootsize=9182 --swapsize=4096 --hostname=<machinename> --ip=ip.add.re.ss --mask=255.255.255.0 --gw=gateway.ip.add.ress --dns=internal.dns.ser.ver --addpkg=vim --addpkg=openssh-server --addpkg=curl --addpkg=linux-image-generic
Note you cannot create a newer VM on an older host i.e. to create a 16.04 you must build on a 16.04 host. You can build older releases on a later host, and in general an older host will work with any compatible VM regardless of installed OS.