summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-05-18 19:06:43 +1200
committerLars Wirzenius <liw@liw.fi>2010-05-18 19:06:43 +1200
commit4fa9035c5b80b6765cc29330fa7842940bd89d96 (patch)
tree27faf81ee03876fa5dcd6551fd349e2904743375
parent532ae9f01cb8fce08016742cda857a34b15a256a (diff)
parent1dba93e66954c540bea80767b585580e6a39c227 (diff)
downloadliw-automation-4fa9035c5b80b6765cc29330fa7842940bd89d96.tar.gz
Merged changes from work.
-rwxr-xr-xliw-vm-clone87
1 files changed, 87 insertions, 0 deletions
diff --git a/liw-vm-clone b/liw-vm-clone
new file mode 100755
index 0000000..fc5b40d
--- /dev/null
+++ b/liw-vm-clone
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+set -e
+
+die()
+{
+ printf -- "%s\n" "$*" 1>&2
+ exit 1
+}
+
+
+clone()
+{
+ local oldname="$1"
+ local newname="$2"
+ local img="$3"
+
+ if [ -e "$img" ]
+ then
+ echo "Image file $img already exists. Skipping clone."
+ else
+ echo "Cloning $oldname to $newname"
+ virt-clone --connect qemu:///system -o "$oldname" -n "$newname" \
+ -f "$img"
+ chmod 770 "$img"
+ fi
+}
+
+
+fixup()
+{
+ local oldname="$1"
+ local mountpt="$2"
+
+ if grep -x -e "$oldname" "$mountpt/etc/hostname" > /dev/null
+ then
+ echo "fixing up /etc/hostname on $oldname"
+ sed -i "s/$oldname/$newname/g" "$mountpt/etc/hostname"
+
+ echo "fixing up /etc/hosts on $oldname"
+ sed -i "s/$oldname/$newname/g" "$mountpt/etc/hosts"
+
+ echo "removing udev's persistent net rule"
+ rm -f "$mountpt/etc/udev/rules.d/70-persistent-net.rules"
+ else
+ echo "EEEK! /etc/hostname does not match $oldname"
+ echo "Not touching this filesystem."
+ echo "/etc/hostname is:"
+ cat $mountpt/etc/hostname
+ fi
+}
+
+
+[ $# = 2 ] || die "Usage: $0 oldname newname"
+oldname="$1"
+newname="$2"
+img="/srv/vm/$newname.img"
+
+clone "$oldname" "$newname" "$img"
+
+mountpt=$(mktemp -d)
+tempfile=$(mktemp)
+kpartx -av "$img" > "$tempfile"
+sleep 2 # FIXME: for some reasons things are not immediate
+
+sed 's/.*(\(.*\)).*/\1/' "$tempfile" |
+while read pair
+do
+ dev="/dev/block/$pair"
+ if pvdisplay "$dev" > /dev/null 2>&1
+ then
+ vg=$(pvdisplay "$dev" | awk '/^ VG Name/ { print $NF }')
+ vgchange -ay "$vg" > /dev/null
+ for lv in "/dev/$vg"/*
+ do
+ if mount "$lv" "$mountpt" 2> /dev/null && [ -e "$mountpt/etc" ]
+ then
+ fixup "$oldname" "$mountpt"
+ umount "$mountpt"
+ fi
+ done
+ vgchange -an "$vg" > /dev/null
+ fi
+done
+kpartx -d "$img" > /dev/null
+rm -f "$tempfile"
+rmdir "$mountpt"