cloneVM.bash

 1 #!/bin/bash
 2 # cloneVM.bash - This script clones the "CentOS" VirtualBox VM, it can be made 
 3 #                to clone other VMs with a very simple configuration change,
 4 #                see below.
 5 #
 6 # Copyright 2007 Barak Korren
 7 #
 8 #    This program is free software: you can redistribute it and/or modify
 9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 
22 # --------------------------- Configuration Variables -------------------------
23 VBOXMANAGE="/usr/bin/vboxmanage"
24 
25 # Where to put the VMs, this is the default location for a non-root user
26 VBBASE="$HOME/.VirtualBox"
27 VMDIR="$VBBASE/Machines"
28 VDIDIR="$VBBASE/VDI"
29 
30 # The name of the VM we're duplicating
31 ORIGVM="CentOS"
32 ORIGVDI="$VDIDIR/$ORIGVM.vdi"
33 
34 # Unless you want to improve my script you 
35 # needn't change anything below this line
36 # ------------------------------------------------------------------------------
37 usage() {
38         cat 1>&2 <<EOF
39 Usage:
40   $(basename $0) VMNAME
41 Creates a new clone of the $ORIGVM VM and names it with he given name.
42 EOF
43         exit 1
44 }
45 
46 # Just enough input processing to make it not explode in your face...
47 [[ $# -ne 1 ]] && usage
48 NEWVM="$1"
49 shift
50 
51 if [[ "$NEWVM" == "$ORIGVM" ]]; then
52         echo "New VM name must be different then \"$ORIGVM\"." 1>&2
53         usage
54 fi
55 
56 NEWVDI="$VDIDIR/$NEWVM.vdi"
57 
58 # This is how you actually clone the VM:
59 # 1st you clone the virtual disk
60 $VBOXMANAGE clonevdi "$ORIGVDI" "$NEWVDI"
61 # Then you register the new disk with VirtualBox
62 $VBOXMANAGE registerimage disk "$NEWVDI"
63 # When you're done with the disk you create a new VM
64 $VBOXMANAGE createvm -name "$NEWVM" -register
65 # Finally configure the VM to your liking, including adding the cloned disk
66 # TODO: Parametrise everything here
67 $VBOXMANAGE modifyvm "$NEWVM" \
68         -ostype "linux26" \
69         -memory "256M" \
70         -boot1 "disk" -boot2 "dvd" -boot3 "floppy" \
71         -hda "$NEWVDI" \
72         -nic1 "nat" \
73         -audio none 
74 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s