Continuing my series on provisioning VMs on workstation pro outside of the GUI, I’ve got a functional script that will create a linked clone using the vmrun command.
It allows you to specify the following options on the command line:
- Username in the provisioned VM
- Password in the provisioned VM
- Source Template
- Snapshot used for linked clone
- Name of new VM
It completes the following tasks
- Clones virtual machine
- Modifies the new VM’s VMX file to show the correct name
- Powers on the new VM (which also registers it to Workstation)
- Copies a script from the host to the VM as soon as VMware tools is available
- Copies output from the script from the VM to the host machine and prints output to the screen
- Prints the IP of the VM as soon as it’s available
The output looks like this:
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
|
$ ./workstation-clone-vm.sh --user root --pass [obfuscated] \
--template /storage/virtual_machines/Ubuntu_1810_Template/Ubuntu_1810_Template.vmx \
--snapshot base-4 --name test1
Parameters are:
User=root
Pass=[obfuscated]
Template Name=/storage/virtual_machines/Ubuntu_1810_Template/Ubuntu_1810_Template.vmx
Snapshot Name=base-4
New VM Name=test1
Cloning Ubuntu_1810_Template to test1 and using the snapshot: base-4
Powering on test1
Waiting for VMware Tools to start
..
Copying script to change the hostname from the host to the guest
Running custom script in test1 to set the hostname and rebooting
Waiting for VMware Tools to start
Copying output to local machine
Below is the output from the OS Customization:
Current hostname is ubuntu_1810_template. Hostname will be changed to test1
Waiting for the VM to return an IP Address
......
VM is running and has the IP Address: 192.168.102.131
Script took 77 seconds to execute
|
You can find the script source code below and on github:
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
#Original Locations:
#Blog: http://www.virtjunkie.com/thin-provision-vms-on-workstation-pro-using-the-vmrun-command/
#Github: https://github.com/jonhowe/Virtjunkie.com/blob/master/workstation-clone-vm.sh
#Example:
#testgetopt.sh --user root --pass [pass] --template /storage/virtual_machines/Ubuntu_1810_Template/Ubuntu_1810_Template.vmx --snapshot base-4 --name test1
TEMP=`getopt -o u:,p:,n:,t:,s: --long user:,pass:,name:,template:,snapshot: -n 'clone_workstation_vm' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
VMRUN=/usr/bin/vmrun
#Do not edit below this line
SOURCEVM=
SOURCESNAPSHOT=
DESTINATIONVMNAME=
GUESTUSER=
GUESTPASS=
echo "Parameters are:"
while true; do
case "$1" in
-u | --user )
GUESTUSER=$2;echo "User=$GUESTUSER"; shift 2 ;;
-p | --pass )
GUESTPASS=$2;echo "Pass=$GUESTPASS"; shift 2 ;;
-n | --name )
DESTINATIONVMNAME=$2;echo "New VM Name=$DESTINATIONVMNAME"; shift 2 ;;
-s | --snapshot )
SOURCESNAPSHOT=$2;echo "Snapshot Name=$SOURCESNAPSHOT"; shift 2 ;;
-t | --template )
SOURCEVM=$2;echo "Template Name=$SOURCEVM"; shift 2 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
echo
wait_for_vmware_tools() {
CMD=$1
USER=$2
PASS=$3
VM=$4
echo "Waiting for VMware Tools to start"
until ($CMD -T ws -gu $USER -gp $PASS CheckToolsState $VM | grep -q "running"); do
printf '.'
sleep 5;
done
sleep 10
echo
}
wait_for_vm_ip() {
CMD=$1
USER=$2
PASS=$3
VM=$4
echo "Waiting for the VM to return an IP Address"
until ($CMD -t ws -gu $USER -gp $PASS getGuestIPAddress $VM | grep -q -v "Error: Unable to get the IP address"); do
printf "."
sleep 5
done
sleep 10
echo
IP="$($CMD -t ws -gu $USER -gp $PASS getGuestIPAddress $VM)"
echo "VM is running and has the IP Address: $IP"
echo
}
clone_workstation_vm() {
SOURCE=$1
DESTINATION=$2
SNAPSHOT=$3
USER=$4
PASS=$5
SOURCEVMX="${SOURCE##*/}"
SOURCEVMNAME="${SOURCEVMX%%.*}"
NEWVM=/storage/virtual_machines/$DESTINATION/$DESTINATION.vmx
echo "Cloning $SOURCEVMNAME to $DESTINATION and using the snapshot: $SNAPSHOT"
$VMRUN -T ws -gu $USER -gp $PASS clone $SOURCE $NEWVM linked $SNAPSHOT
sed -i "s/displayName = \"Clone of $SOURCEVMNAME\"/displayName = \"$DESTINATION\"/g" $NEWVM
echo "Powering on $DESTINATION"
$VMRUN -t ws -gu $USER -gp $PASS start $NEWVM
wait_for_vmware_tools $VMRUN $USER $PASS $NEWVM
echo "Copying script to change the hostname from the host to the guest"
currentDir="${0%/*}"
$VMRUN -t ws -gu $USER -gp $PASS copyFileFromHostToGuest $NEWVM $currentDir"/JH01_Set-Hostname.sh" /tmp/JH01_Set-Hostname.sh
echo "Running custom script in $DESTINATION to set the hostname and rebooting"
$VMRUN -t ws -gu $USER -gp $PASS runProgramInGuest $NEWVM /tmp/JH01_Set-Hostname.sh "$DESTINATION"
wait_for_vmware_tools $VMRUN $USER $PASS $NEWVM
echo "Copying output to local machine"
$VMRUN -t ws -gu $USER -gp $PASS copyFileFromGuestToHost $NEWVM /var/log/oscustomization.log $currentDir"/"$DESTINATION"_Customization.log"
echo
echo "Below is the output from the OS Customization:"
cat $currentDir"/"$DESTINATION"_Customization.log"
wait_for_vm_ip $VMRUN $USER $PASS $NEWVM
}
start=`date +%s`
clone_workstation_vm $SOURCEVM $DESTINATIONVMNAME $SOURCESNAPSHOT $GUESTUSER $GUESTPASS
end=`date +%s`
runtime=$((end-start))
echo "Script took $runtime seconds to execute"
|