aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2013-11-23 19:02:34 +0100
committerGuilhem Moulin <guilhem@fripost.org>2015-06-07 04:27:47 +0200
commit0ab14d2764d255b9e2736476309605264721721f (patch)
tree186e696669a5e398abd6e95e348a04328c913195
parent5df4738c72485e6788e70ce021ce266aa556acfa (diff)
Refactored 'virt' shell script.
-rwxr-xr-xvirtualenv/virt58
1 files changed, 30 insertions, 28 deletions
diff --git a/virtualenv/virt b/virtualenv/virt
index 043589f..06b7642 100755
--- a/virtualenv/virt
+++ b/virtualenv/virt
@@ -8,6 +8,7 @@
#
set -ue
+PATH=/usr/bin:/bin
root=$(dirname "$0")
usage() {
@@ -22,22 +23,29 @@ usage() {
exit 1
}
-get_XML_attribute() {
- local domain="$1" object="$2" attribute="$3"
+getXMLattr() {
+ local id="$1" object="$2" attribute="$3" what="${4:-domain}"
- /usr/bin/virsh dumpxml "$domain" \
- | /usr/bin/xmlstarlet sel -t -m /domain/"$object" -v @"$attribute"
+ local cmd
+ case "$what" in
+ domain) cmd=;;
+ network) cmd=net;;
+ esac
+
+ virsh "${cmd:+$cmd-}dumpxml" "$id" \
+ | xmlstarlet sel -t -m "/$what/$object" -n -v @"$attribute" \
+ | sed '/^\s*$/d'
}
add_net_section() {
- local net="${3:-$network}" section="$1" xml="$2"
- /usr/bin/virsh net-update "$net" add "$section" --xml "$xml" --live --config
+ local section="$1" xml="$2" net="${3:-$network}"
+ virsh net-update "$net" add "$section" --xml "$xml" --live --config
}
doesExist() {
- /usr/bin/virsh -r list --all --name | grep -qxF "$1"
+ virsh -r list --all --name | grep -qxF "$1"
}
isActive() {
- /usr/bin/virsh -r list --name | grep -qxF "$1"
+ virsh -r list --name | grep -qxF "$1"
}
install() {
@@ -45,12 +53,13 @@ install() {
local name=$1
local disk=${2:-$root/images/${name}.qcow2}
- local darch version=$(sed -n 's/^VERSION\s*=\s*//p' $root/../preseed/Makefile)
+ local darch
case "${ARCH:=$(/bin/uname -m)}" in
i386|i686) darch=i386;;
x86_64) darch=amd64;;
*) darch=$ARCH;;
esac
+ local version=$(sed -n '/^VERSION\s*=\s*/ {s///p;q}' $root/../preseed/Makefile)
local cdrom="$root/../preseed/dist/$darch/debian-$version-$darch-netinst-preseeded.iso"
if doesExist "$name"; then
@@ -73,45 +82,38 @@ install() {
grep -q '^kvm\s' /proc/modules || echo 'WARN: KVM not available!' >&2
- if ! [ -r "$disk" -a -r "$disk" ]; then
+ if [ ! -f "$disk" ]; then
local size=12G
echo "Creating (sparse) $size disk image '$disk'" >&2
- /usr/bin/qemu-img create -f "${disk##*.}" \
- -o size="$size",preallocation=metadata "$disk"
+ qemu-img create -f "${disk##*.}" -o size="$size",preallocation=metadata "$disk"
fi
# 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 \
+ local network=default ip mac
+ virt-install -q \
--name "$name" \
--arch "$ARCH" \
--ram 128 \
--cdrom "$cdrom" \
--disk "$disk",cache=writeback,bus=sata \
- --network network=$network \
+ --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 )
+ mac=$( getXMLattr $name devices/interface/mac address )
+ ip=$( getXMLattr $network ip/dhcp/host ip network | sort -n | tail -1 )
[ "$ip" ] || \
- ip=$( /usr/bin/virsh net-dumpxml $network \
- | /usr/bin/xmlstarlet sel -t -m /network/ip -v @address )
+ ip=$( getXMLattr $network ip address network )
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, and has been attributed the IP $ip.
+ Domain '$name' has been created, and has been given IP $ip.
As you can see in
virsh list
@@ -138,14 +140,14 @@ view(){
exit 1
fi
- local type=$(get_XML_attribute "$domain" devices/graphics type)
- local port=$(get_XML_attribute "$domain" devices/graphics port) || true
+ local type=$(getXMLattr "$domain" devices/graphics type)
+ local port=$(getXMLattr "$domain" devices/graphics port) || true
if ! [ "$type" = spice -a "$port" ]; then
echo "Error: Could not find a valid Spice server on domain '$domain'." >&2
exit 1
fi
- /usr/bin/spicec -h 127.0.0.1 -p $port
+ spicec -h 127.0.0.1 -p $port
}
[ $# -gt 0 ] || usage