Linux Blog

Virtual Box Clone Script

Filed under: Shell Script Sundays — TheLinuxBlog.com at 8:30 am on Sunday, February 27, 2011

I was in need for a way to clone virtual box vms so I wrote a quick bash script to clone them. After writing it, I realized that I could just export a VM, and then import it. There are limitations such as no snapshot support and probably a bunch I’ve never thought about. Either way, hit the jump to see the outcome.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
#usage function
usage () {
echo -e "Usage: $0 Source Name [Mac address]"
echo -e "Source (Include .vdi)"
echo -e "Name (Exclude .vdi)"
echo -e "Mac not implemented yet"
}
if [ -z "$1" ]; then
usage && exit 1
fi;
 
# Name to clone from
INPUTSOURCE="$1"
 
# Name for the VM
NAME="$2"
MAC="$3"
 
SATANAME="SATA Controller"
SATATYPE="IntelAHCI"
 
echo $INPUTSOURCE, $NAME, $MAC
 
VBOXPATH=~/.VirtualBox/HardDisks
 
# TODO: Check for ending in .vdi (for source)
 
# Setup Source and Dest Files
SOURCE=$VBOXPATH/$INPUTSOURCE
DEST=$VBOXPATH/$NAME.vdi
 
# Clone the HD
VBoxManage clonehd $SOURCE $DEST
 
# Create the VM
VBoxManage createvm --name "$NAME" --register
 
# Create the controller
VBoxManage storagectl "$NAME" --add sata --name "$SATANAME"  --controller "$SATATYPE"
 
# Attach the cloned disk
VBoxManage storageattach "$NAME" --storagectl "$SATANAME" --port 0 --device 0 --type hdd --medium "$DEST"

Conclusion
It can be downloaded here. Use as you wish, if you extend it or use it for something really cool let me know!





Random Man Pages:
utf8
unicode
ldap
stime

2 Comments »

Comment by Angeli

December 29, 2015 @ 5:48 pm

I came across this page, and your site, when I was tiyrng to find a command built into vbox for renaming drive images. What I have found is; after you release the drive if you rename the file, and then reattach everything works fine. Cloning disks just takes soooo long.Thanks for pointing me in the right direction.

Comment by Owen

December 30, 2015 @ 12:07 am

Glad it helped, thanks for the comment. Cloning is kind of a pain, now there are better ways to do it. I’ll write something up using more modern tools.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>