From eb7f5815272a56e3e489835d01eeed7bd21b4c32 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Tue, 29 Oct 2013 21:51:18 +0100 Subject: Use fixed IPs for the new virtual hosts. Each new VM is attributed a fresh static IPv4 (uniqueness is guaranteed by merely incrementing the /24), and DNS entries are added libvirt's dnsmasq instance. --- virtualenv/virt | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/virtualenv/virt b/virtualenv/virt index 46c56dc..b88d04e 100755 --- a/virtualenv/virt +++ b/virtualenv/virt @@ -28,6 +28,10 @@ get_XML_attribute() { /usr/bin/virsh dumpxml "$domain" \ | /usr/bin/xmlstarlet sel -t -m /domain/"$object" -v @"$attribute" } +add_net_section() { + local net="${3:-$network}" section="$1" xml="$2" + /usr/bin/virsh net-update "$net" add "$section" --xml "$xml" --live --config +} doesExist() { /usr/bin/virsh -r list --all --name | grep -qxF "$1" @@ -78,17 +82,37 @@ install() { # TODO: the bus should be chosen at random among sata,ide,scsi,usb # TODO: test EFI: http://www.linux-kvm.org/page/OVMF + local network=default /usr/bin/virt-install -q \ --name "$name" \ --arch "$ARCH" \ --ram 128 \ --cdrom "$cdrom" \ --disk "$disk",cache=writeback,bus=sata \ - --network network=default \ + --network network=$network \ --graphics spice + # Attribute the next available IP. You may also want to add a domain + # section, e.g., , and prepend + # the DNS IP to /etc/resolv.conf. + # Cf. http://libvirt.org/formatnetwork.html + local mac ip + mac=$(get_XML_attribute "$name" devices/interface/mac address) + ip=$( /usr/bin/virsh net-dumpxml $network \ + | /usr/bin/xmlstarlet sel -t -m /network/ip/dhcp/host -n -v @ip \ + | sed '/^\s*$/d' \ + | sort -n \ + | tail -1 ) + [ -n "$ip" ] || \ + ip=$( /usr/bin/virsh net-dumpxml $network \ + | /usr/bin/xmlstarlet sel -t -m /network/ip -v @address ) + ip=${ip%.*}.$(( 1 + ${ip##*.} )) + add_net_section ip-dhcp-host "" + add_net_section dns-host "$name" + cat >&2 <<- EOF - Domain '$name' has been created. As you can see in + Domain '$name' has been created, and has been attributed the IP $ip. + As you can see in virsh list -- cgit v1.2.3