-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmclone-maker
executable file
·94 lines (74 loc) · 3.44 KB
/
vmclone-maker
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh
mem_size="2048" # in MB
vram_size="18" # in MB
vm_home="$HOME/VirtualBox-VMs"
image_disk_base="$vm_home/$HOME/VirtualBox-VMs/ubuntu-16.04LTS-image/ubuntu-16.04LTS-image.vdi"
clone_disk_base="$vm_home/cloned_ubuntu-16.04lts-image.vdi"
controller_type="sata"
controller="sata1"
help="Usage: `basename $0` new-vm-name
Options: none
This script will create a VM based off $clone_disk_base
but any changes made in the machine subsequent to creation will
be local to this clone.
FYI: the command 'VBoxManage unregister --delete <vmname>' will unregister
the specified <vmname> and delete all the associated files.
"
# For future info, here are some convenient Virtualbox Commands:
# VBoxManage list [vms, hdds] list the current VMs or HDDs
# VBoxManage unregister [--delete] removes VM from VB interface, --delete deletes all the files as well
# VBoxManage closemedium disk <path> [--delete] removes HD medium from VB interface, --delete deletes file
case $# in
0)
echo 1>&2 "?Usage: $help"
exit 1
;;
1)
vm_name="$1"
;;
*)
echo 1>&2 "?Too many arguments. Usage: $help"
exit 2
;;
esac
target_disk="$vm_name/$vm_name.vdi"
echo "At the start, these VMs exist:"
echo "----------"
VBoxManage list vms
echo "----------"
set -x
# create a clone machine
# --register causes the machine to be visible to the Virtualbox installation
# --ostype is typically Ubuntu_64 for Ubuntu. "VBoxManage list ostypes" for more.
VBoxManage createvm --name "$vm_name" --ostype Ubuntu_64 --register || exit 5
# Adjust default settings. See https://www.virtualbox.org/manual/ch08.html#vboxmanage-modifyvm for more.
# These appear to be the key parameters to set.
VBoxManage modifyvm "$vm_name" --memory=$mem_size --vram=$vram_size || exit 5
VBoxManage modifyvm "$vm_name" --description="vmclone-maker Ubuntu machine based off disk file $clone_disk_base" || exit 5
# Add a SATA controller to the VM clone
VBoxManage storagectl "$vm_name" --name=$controller --add $controller_type || exit 5
# add our Shared Folder to the machine
VBoxManage sharedfolder add "$vm_name" --name="sharename" --hostpath="$vm_home/shared_folder"
# Enable Mouse Integration
VBoxManage modifyvm "$vm_name" --mouse=usbtablet
VBoxManage modifyvm "$vm_name" --mouse=bidirectional
# Optionally, add a local networking NIC (Use VirtualBox Preferences to add vboxnet0 to Network Settings)
# VBoxManage modifyvm "$vm_name" --nic1=hostonly --hostonlyadapter1="vboxnet0"
VBoxManage modifyvm "$vm_name" --nic1=bridged --bridgeadapter1=en3
# Optionally, Add port forwarding to allow ssh into the box
VBoxManage modifyvm "$vm_name" --natpf1="ssh,tcp,,3022,,22"
# Clone the base disk, if needed
test -f $clone_disk_base || VBoxManage clonehd "$image_disk_base" "$clone_disk_base"
# Optionally, change the UUID of the disk
#VBoxManage internalcommands sethduuid "$target_disk" || exit 5
# Attach the clone drive to the machine.
# Note 'multiattach' is key for copy-on-write. If the type is 'immutable', then any writes are reset upon reboot.
# See https://www.virtualbox.org/manual/ch05.html#hdimagewrites for more.
VBoxManage storageattach "$vm_name" --storagectl=$controller --port=0 --device=0 --type=hdd --medium=$clone_disk_base --mtype=multiattach || exit 5
# Add a DVD/CD drive
VBoxManage storageattach "$vm_name" --storagectl=$controller --port=1 --device=0 --type=dvddrive --medium=emptydrive || exit 5
set +x
echo "At the end, these VMs exist:"
echo "----------"
VBoxManage list vms
echo "----------"