Smokes your problems, coughs fresh air.

Tag: KVM

Debian network bridge setup for kvm/qemu

When you run virtual machines, the most convenient network setup is having a bridge between the virtual machine and your normal ethernet network. In Debian, I use the following config in /etc/network/interfaces to have such a bridge:

# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
 
# The loopback interface
auto lo
iface lo inet loopback
 
# The first network card - this entry was created during the Debian installation
# (network, broadcast and gateway are optional)
auto eth1
iface eth1 inet manual
 
auto br0
iface br0 inet static
    address 192.168.1.101
    network 192.168.1.0
    broadcast 192.168.1.255
    netmask 255.255.255.0
    gateway 192.168.1.100
    bridge_ports eth1
    bridge_fd 9
    bridge_hello 2
    bridge_maxage 12
    bridge_stp off

In this setup, the br0 gets the IP address that eth1 had before. Intuitively, I would have created a bridge which would connect eth1 and new interfaces, but when I did that, I could not get it to work.

I then used a command similar to this to install the virtual machine:

virt-install --connect qemu:///system -n vm10 -r 512 --vcpus=2 -f ~/vm10.qcow2 -s 12 -c ~/debian-500-amd64-netinst.iso --vnc --noautoconsole --os linux --os-variant debianLenny --accelerate --network=bridge:br0 --hvm

You can adjust where necessary. What’s important, is the –network=bridge:br0. This makes sure that kvm is run with:

kvm [more options] -net nic,macaddr=54:52:00:52:1c:7c,vlan=0,model=virtio -net tap,fd=7,script=,vlan=0,ifname=vnet0

The network setup looks like:

br0       Link encap:Ethernet  HWaddr 00:15:17:23:83:67
          inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::215:17ff:fe23:8367/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1866633 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1678602 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:892540330 (851.1 MiB)  TX bytes:971936093 (926.9 MiB)
 
eth1      Link encap:Ethernet  HWaddr 00:15:17:23:83:67
          inet6 addr: fe80::215:17ff:fe23:8367/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13371212 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12703312 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:4234365756 (3.9 GiB)  TX bytes:201738186 (192.3 MiB)
          Memory:88180000-881a0000
 
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:2672817 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2672817 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:493599313 (470.7 MiB)  TX bytes:493599313 (470.7 MiB)
 
vnet0     Link encap:Ethernet  HWaddr 00:ff:2d:a0:76:34
          inet6 addr: fe80::2ff:2dff:fea0:7634/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:159072 errors:0 dropped:0 overruns:0 frame:0
          TX packets:310906 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:500
          RX bytes:10860687 (10.3 MiB)  TX bytes:465526222 (443.9 MiB)

source for all this.

Making sure virtual machine guests shut down properly on a Debian host

I just installed libvirt and installed a vritual machine with virt-install. I also used virsh to configure it to autostart on machine start. This way, it integrates quite nicely with the host operating system.

However, I found out that libvirt provides no way to properly shut the machines down when the host goes down. It is completely beyond me that they didn’t implement this. When you shut down libvirtd, the guests just die…

Googling yielded almost nothing, except a bug report from 17 Mar 2009 with a request for this. The report still wasn’t handled, but the guy said that he wrote a crude script to do it. I mailed him to ask for it, and here it is:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          kvm_domains
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     
# Default-Stop:      0 6
# Short-Description: Shutdown kvm domains
# Description:       Shutdown kvm domains on reboot/poweroff.
#                    Forcefully kill instances that still run after a
#                    given timeout.
### END INIT INFO
 
# Author: Michael Biebl <biebl@teco.edu>
#
 
# Do NOT "set -e"
 
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="KVM domains"
VIRSH=/usr/bin/virsh
SCRIPTNAME=/etc/init.d/kvm_domains
TIMEOUT=60
 
# Exit if the package is not installed
[ -x "$VIRSH" ] || 0
 
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
 
#
# Function that stops the daemon/service
#
do_stop()
{
	domains=$($VIRSH -q list | awk '/running/ {print $2}')
 [ -z "$domains" ] ;
		log_progress_msg "none"
	 0
 
 d $domains ;
		$VIRSH -q shutdown $d > /dev/null
		log_progress_msg $d
 
 
	i=0
 [ $i -lt $TIMEOUT ] ;
	 $VIRSH -q list | grep -q running ;
			i=$(($i+1))
			log_progress_msg "."
			sleep 1
	
		 0
	
 
	# forcefully kill the remaining kvm instances
	killall -9 kvm
 1 
}
 
 "$1"
  start)
	;;
  stop)
	log_begin_msg "Stopping $DESC:"
	do_stop
 "$?"
		0) log_end_msg 0 ;;
		1) log_end_msg 1 ;;
 
	;;
  restart|force-reload)
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
 "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
 3
	;;
 
:

It depends on text output by virsh, so it’s not the most robust, but it works.

Run this to put in it in the proper runlevels (the 10 on my system makes sure it executes before kvm and libvirt):

update-rc.d kvm_domains stop 10 0 6 .

© 2024 BigSmoke

Theme by Anders NorenUp ↑