diff options
author | Guilhem Moulin <guilhem@fripost.org> | 2013-10-29 21:51:18 +0100 |
---|---|---|
committer | Guilhem Moulin <guilhem@fripost.org> | 2015-06-07 04:27:43 +0200 |
commit | eb7f5815272a56e3e489835d01eeed7bd21b4c32 (patch) | |
tree | 2af74060e6bdda1574d1adb1a05253d8ccaa106b | |
parent | e596091daf51443248a0cb427832be62552eaf27 (diff) |
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.
-rwxr-xr-x | virtualenv/virt | 28 |
1 files 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., <domain name='libvirt.example.org'/>, 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 "<host mac='$mac' name='$name' ip='$ip'/>" + add_net_section dns-host "<host ip='$ip'><hostname>$name</hostname></host>" + 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 |