Skip to content

Instantly share code, notes, and snippets.

@bencord0
Created August 10, 2012 03:01
Show Gist options
  • Save bencord0/3310644 to your computer and use it in GitHub Desktop.
Save bencord0/3310644 to your computer and use it in GitHub Desktop.
A neat little script to quickly spinup a Xen PV Gentoo guest (domU).
#!/bin/bash
if [ -z "$1" ]; then
echo "Please provide a name for the new vm"
exit 1
fi
export NAME="$1"
export UU=$(uuidgen)
echo $NAME $UU
echo "Creating control file"
cat << XENU >> /etc/xen/$NAME
kernel = "/usr/lib/xen/boot/pv-grub-x86_64.gz"
extra = '(hd0)/boot/grub/grub.conf'
memory = 512
maxmem = 2048
name = "$NAME"
uuid = "$UU"
cpus = ""
vcpus = 2
vif = [ 'mac=00:00:00:$(echo $UU|tail -c 7|head -c 2):$(echo $UU|tail -c 7|head -c 4|tail -c 2):$(echo $UU|tail -c 7|tail -c 3),bridge=br0' ]
disk = [ 'phy:/dev/vg/$NAME,xvda1,w' ]
XENU
echo "Creating lvm volume"
lvcreate -n $NAME vg -L5G
echo "Formatting volume"
mkfs.ext3 /dev/vg/$NAME
echo "Mounting filesystem"
mkdir -p /mnt/xenu
mount /dev/vg/$NAME /mnt/xenu
echo "Extracting tarball"
pushd /mnt/xenu
cleanup () {
popd 2>&1 > /dev/null
umount /mnt/xenu
exit 1
}
trap 'cleanup' EXIT
du -sh /gentu-x86_64.tar.gz || cleanup
if [ -e "$(which pipebench 2> /dev/null)" ]; then
pipebench < /gentu-x86_64.tar.gz | tar xzpf -
else
tar xzvpf /gentu-x86_64.tar.gz
fi
echo hostname=\"$NAME\" > etc/conf.d/hostname
echo "$NAME vm is now ready"
echo "To run the VM, type"
echo " # xl create -c /etc/xen/$NAME"
@bencord0
Copy link
Author

A few notes about my local setup.
1/. I use LVM. The VG is called 'vg'. That's hardcoded at line 27.
2/. I have a handly stage4 tarball at /gentu-x86_64.tar.gz. It contains
a) /boot/grub/grub.conf (but not the rest of grub)
b) /boot/{vmlinuz,initramfs} for pv-grub. The initramfs isn't used that often.
c) DDNS is available on my network, hence line 51 is crucial.
d) PAM/Kerberos is available on my network, no passwords are stored in the tarball.
e) sshd is available, but no ssh keys. Those are generated on first boot. Security is not hardcoded (see 2d), but derived from the network. The only rescue mechanisms are initramfs shell, or offline mount the PV (see 3)
3/. The entire PV is created as an ext3 volume. ext4 is broken in the current version of pv-grub. Note the mount point inside the VM, xvda1. There is no MBR/BIOS boot. This makes LV maintenance easier, no messing with kpartx. In theory, the LV could be exported and imported into a physical machine's VG.
4/. Line 47 uses app-benchmarks/pipebench. If it isn't available, then a simpler method of tell-the-user-something-is-happening is used.
5/. Not visible in the script, but I use nfsmounts for /usr/portage (and thus binpkgs), and fstab will make use of /swapfile if it exists (off by default).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment