Virtual machines with kvm

From Andreida

https://wiki.debian.org/KVM

Install and config

  • Install it
apt-get install qemu-kvm libvirt-bin virtinst virt-manager


network host

/etc/network/interfaces of the host with ip 192.168.1.130

allow-hotplug eth0
  iface eth0 inet static
  address 192.168.1.130
  network 192.168.1.0
  netmask 255.255.255.0
  broadcast 192.168.1.255
  gateway 192.168.1.1
  
auto br0
  iface br0 inet static
  address 192.168.1.130
  network 192.168.1.0
  netmask 255.255.255.0
  broadcast 192.168.1.255
  gateway 192.168.1.1
  bridge_ports eth0


network guest

  • when creating a new virtual machine, give it as network br0

/etc/network/interfaces of the client (VM) with the ip 192.168.1.132

source /etc/network/interfaces.d/*

auto lo
iface lo inet loopback

allow-hotplug eth0
iface eth0 inet static
        address 192.168.1.132
        network 192.168.1.0
        netmask 255.255.255.0
        broadcast 192.168.1.255
        gateway 192.168.1.1

console

in /etc/default/grub of the VM (client) change

GRUB_CMDLINE_LINUX_DEFAULT="quiet"

to

GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0,115200n8 serial"

inside the guest call

systemctl enable serial-getty@ttyS0.service
systemctl start serial-getty@ttyS0.service

so you have a chance that "virsh console <id>" works at all. If it works, now you can do

virsh list
virsh console <number>
<enter>

firewall firehol

Be careful with firewall. If you don't understand what is happening, consider turning off the firewall (lan only) to find out if the problem is there.

/etc/firehol/firehol.conf

#!/sbin/firehol

interface any lan src "192.168.1.0/24" dst 192.168.1.130
    policy drop
    protection strong
    server ssh accept
    server dns accept
    #server custom if_my_ssh tcp/3018 any accept
    #server all accept
    client all accept

router myrouter src "192.168.1.0/24"
    policy drop
    protection strong
    server ssh accept
    server dns accept
    #server all accept
    client all accept

Use

http://qemu-buch.de/de/index.php/QEMU-KVM-Buch/_Anhang/_libvirt

virsh list --all
virsh start <machine>
virsh shutdown <machine>

Clone your kvm-vm

If you want to clone to a new name and don't care about the file location:

virt-clone --original yourVm --name yourVm-2 --auto-clone

IF you change the file location, check the format of the created file with "file" before you do anything else. Sometimes the format is a different one and not bootable by KVM.

If it is a linux machine, don't forget to

  • change /etc/hostname
  • modify /etc/hosts
  • modify /etc/mailname
  • change in /etc/network/interfaces the ip

delete a kvm-vm

virsh shutdown <name>
virsh undefine <name>
virsh vol-list default
virsh vol-delete <name-with-path>

edit a kvm-vm

virsh edit <name>

If you want to change the memory, use MB * 1024. Example: 128 MB => 131072, 2 GB => 2 * 1024 * 1024 => 2097152 KiB

Have the vm autostart:

virsh autostart <name>

Clone a linux machine

Use the the context menu on the source machine and clone it.

  • rename the machine
  • rename the file ! (Clone / Details)
  • boot into the new clone
    • change /etc/hostname
    • change /etc/mailname
    • change in /etc/network/interfaces the ip
    • delete in /etc/udev/rules.d/70-persistent-net.rules the old network card and change the name of the new one to eth0 (from eth1)
    • modify /etc/hosts
    • change perhaps /etc/munin/munin-node.conf (if you use munin)

Clone a Windows 2000/W2k machine

  • rename the machine
  • rename the file ! (Clone / Details)
  • boot into the new clone
  • MyComputer / Properties / Network Identification
    • (Network ID)
    • Properties -> Computer name
  • MyNetworkPlaces / Properties / Local Area Connection / Properties / Internet Protocol / Properties
    • Set the IP

Automatic suspend/resume of guests when the host goes down/up

There exists a ready script to do it for you

cd /etc/init.d
cp /usr/share/doc/libvirt-bin/examples/libvirt-suspendonreboot virsh-suspend
chmod 755 virsh-suspend
mkdir /var/lib/libvirt/autosuspend

now make the script being executed: http://wiki.andreas-duffner.de/index.php/Debian%2C_start_something_at_system_start


Automatic shutdown of running guests when the host goes down

Suspend is nice, but there is more work to be done for it to work correctly. First thing which comes to mind is the date. With the above method to resume a VM you will have a VM running with the wrong date. So if you want to do it the simple way, just shutdown the running VMs. And let the host care about which one to start when it itself starts. Changing the script from above a bit we get

#! /bin/sh
### BEGIN INIT INFO
# Provides:          virsh-shutdown
# Required-Start:    libvirt-bin
# Required-Stop:     libvirt-bin $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: shutdown all vms on shutdown of host
### END INIT INFO
# (c) Andreas Duffner
# original: (c) Andi Barth <aba@not.so.argh.org> 2008
# Distributable under the terms of the GNU GPL version 2.
#
# copy to /etc/init.d/virsh-shutdown and use
# update-rc.d virsh-shutdown defaults 29 71
# to enable

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

case "$1" in
  start)
    ;;
  stop)
    for domain in /etc/libvirt/qemu/*xml; do
        domain=$(basename $domain .xml)
        state=$(virsh domstate $domain)
        if [ "$state" = "running" ]; then
            echo "shutting down $domain ..."
            virsh shutdown ${domain}
        fi
    done
    sleep 10
    ;;
  reload|force-reload|restart)
    # No action, nothing to reload
    ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
        exit 1
esac

now make the script being executed: http://wiki.andreas-duffner.de/index.php/Debian%2C_start_something_at_system_start


Adding a partition

  • stop the virtual machine
  • use virt-manager to add a partition
    • add hardware
    • storage
  • start the vm
  • show existing drives
fdisk -l
  • use fdisk for the new drive, for example:
fdisk /dev/sda
  • show that there are no partitions on that new drive
p
  • create a new partition
n
e
(default)
(default)
(default)
  • verify there is a new partition if you keep it
p
  • save and exit
w
  • enter fdisk again
fdisk /dev/sda
  • create a logical partition
n
l
(default)
(default)
  • save and exit
w
  • find out the exact "name" of the new partition, look for "Linux", not "Extended"
fdisk -l
  • format the new partition(s), for example:
mkfs.ext4 /dev/sda5
  • create a mount point
mkdir /mnt/funny_disk
  • mount the drive
mount /dev/sda5 /mnt/funny_disk
  • check the space
df -hT
  • find the automatic mount data
mount | grep disk
  • add this temporary data to /etc/fstab

if it looks like

/dev/sda5 on /mnt/rep_disk type ext4 (rw,relatime,data=ordered)

you add something like

/dev/sda5 /mnt/rep_disk ext4 rw,relatime,data=ordered

to the /etc/fstab file

  • unmount
/umount /mnt/funny_disk
  • mount via /etc/fstab
mount /mnt/funny_disk

Now it should be working.

Errors

Unable to initialize GTK: could not open display

  • change the script to ask for the root password
vi `which virt-manager`
exec gksudo python "/usr/share/virt-manager/virt-manager.py" "$@"
  • start the virt-manager as normal user