Customizing cloned Ubuntu VMs
I was playing with creating and cloning Ubuntu virtual machines the other day, and got to the point where I had a nicely setup reference image that I could just copy to fire up additional VMs that would be in a pretty usable state.
There are a few things within a cloned VM that you'd want to change if you were going to keep the new instance around, such as the hostname, SSH host keys, and disk UUIDs. I threw together a simple shell script to take care of these things automatically.
#!/bin/sh # # Updates for cloned Ubuntu VM # # # Some initial settings cloned from the master # ROOT=/dev/vda1 SWAP=/dev/vdb1 LONG_HOSTNAME=ubuntu.local SHORT_HOSTNAME=ubuntu if [ -z $1 ] then echo "Usage: $0 <new-hostname>" exit 1 fi # # Update hostname # shorthost=`echo $1 | cut -d . -f 1` echo $1 >/etc/hostname hostname $1 sed -i -e "s/$LONG_HOSTNAME/$1/g" /etc/hosts sed -i -e "s/$SHORT_HOSTNAME/$shorthost/g" /etc/hosts # # Generate new SSH host keys # rm /etc/ssh/ssh_host_* dpkg-reconfigure openssh-server # # Change root partition UUID # OLD_UUID=`blkid -o value $ROOT | head -n 1` NEW_UUID=`uuidgen` tune2fs -U $NEW_UUID $ROOT sed -i -e "s/$OLD_UUID/$NEW_UUID/g" /etc/fstab /boot/grub/grub.cfg # # Change swap partition UUID # OLD_UUID=`blkid -o value $SWAP | head -n 1` NEW_UUID=`uuidgen` swapoff $SWAP mkswap -U $NEW_UUID $SWAP swapon $SWAP sed -i -e "s/$OLD_UUID/$NEW_UUID/g" /etc/fstab # # Remove udev lines forcing new MAC address to probably show up as eth1 # sed -i -e "/PCI device/d" /etc/udev/rules.d/70-persistent-net.rules sed -i -e "/SUBSYSTEM==/d" /etc/udev/rules.d/70-persistent-net.rules echo "UUID and hostname updated, udev nic lines removed, be sure to reboot"
I'd then run it on the cloned machine with something like
update_clone.sh mynewmachine.foobar.com
This somewhat particular to my specific master VM, in that it's expecting one disk dedicated to root and one disk dedicated to swap, and the VM was created with ubuntu.local
as the hostname. Hopefully though it'll give some ideas about what to look for and how to script those changes.