#!/bin/bash # Clone a base virtual machine to create a new node # # Example usage: # vn-clonebase base node1 # uses the virtual machine called "base" to create a (linked) clone called # "node1". # # Assumptions: # The base virtual machine MUST have a snapshot with the same name as the # virtual machine. For example the VM called "base" must also have a snapshot # called "base". It is this snapshot that is cloned. # # Design: # 1. Check the target VM does not already exist # 2. Use VBoxManage to clone the base VM to produce the target VM # # For more info see: http://sandilands.info/sgordon/virtnet # # $Revision$ # $Author$ # $Date$ # $URL$ # Input parameters # Name of the base virtual machine (to clone) basevm=$1 # Name of the target virtual machine (the newly created VM) vmname=$2 # Check if the vm to create already exists; do not clone if it does VBoxManage list vms | cut -d "\"" -f 2 | grep -w ${vmname} > /dev/null vmmatch=$? if [ "${vmmatch}" != "1" ]; then # If a VM with vmname already exists, then rename the old one newname=`date +'%y%m%d%H%M%S'`${vmname} echo "vn: Virtual machine ${vmname} already exists." echo "vn: Will try to rename the old one to ${newname} ..." VBoxManage modifyvm ${vmname} --name ${newname} > /dev/null vmfail=$? if [ "${vmfail}" = "1" ]; then echo "vn: Failed to rename the old ${vmname} to ${newname}" echo "vn: Make sure the old ${vmname} is NOT running and/or rename" echo "vn: it to something other then ${vmname} then try to create" echo "vn: the topology again." echo "vn: ERROR. Exiting..." exit 1 fi echo "vn: Successfully renamed ${vmname} to ${newname}." fi # Check to see if any old directories for past node exists # defaultvmdir=`VBoxManage list systemproperties | grep "Default machine folder" | cut -d ":" -f 2`; # Check to see if the snapshot exists; if not, take it VBoxManage snapshot ${basevm} list 2> /dev/null snmatch=$? if [ "${snmatch}" = "0" ]; # Has snapshots then VBoxManage snapshot ${basevm} list --machinereadable | grep "SnapshotName" | grep -w ${basevm} > /dev/null snmatch=$? if [ "${snmatch}" = "1" ]; # basvm snapshot doesn't exist then # Take snapshot echo "vn: VM ${basevm} does not have snapshot called ${basevm}" echo "vn: Taking snapshot of ${basevm} (snapshot also called ${basevm})" VBoxManage snapshot ${basevm} take ${basevm} else echo "vn: VM ${basevm} already has snapshot ${basevm}" fi else # Take snapshot echo "vn: VM ${basevm} does not have any snapshots" echo "vn: Taking snapshot of ${basevm} (snapshot also called ${basevm})" VBoxManage snapshot ${basevm} take ${basevm} fi # Clone the base vm (specifically the snapshot of the base) VBoxManage clonevm ${basevm} --name ${vmname} --snapshot ${basevm} --options link --register echo "vn: Virtual machine ${basevm} cloned to ${vmname}" exit 0