/
vmclone
229 lines (204 loc) · 5.67 KB
/
vmclone
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/sh
# --------------------------------------------------------------------
#
# vmclone - a bash script for cloning vmware v2.x virtual machines
#
# --------------------------------------------------------------------
# author: (c) Artur Martins, arturmartins@gmail.com
# version: 1.0 - 02-Jul-2009 - All rights reserved.
# --------------------------------------------------------------------
#
# DESCRIPTION:
#
# vmclone is a bash script that was written to create a complete
# duplicate of a Virtual Machine for VMWare Server version 2.x.
# --------------------------------------------------------------------
#
# WARRANTY:
#
# NO WARRANTY IS IMPLIED OR GIVEN - use at your own risk.
# If you screw it up, it is not my fault. Use of this tool constitutes
# your acceptance of these terms. Advice: keep a backup first!
# --------------------------------------------------------------------
#
# LICENSE:
#
# This software is Donationware - if you feel you have benefited from
# the use of this tool then please consider a donation. The value of
# which is entirely left up to your discretion. I accept PayPal :)
# --------------------------------------------------------------------
#
# INSTALLATION:
#
# Installation: Copy this file to /usr/local/bin/vmclone, make it
# executable. An example could be:
# cp vmclone /usr/local/bin/
# chown root.root /usr/local/bin/vmclone
# chmod 555 /usr/local/bin/vmclone
#
# If you can want a more secure enviroment for this tool, give access
# only to the root user:
# chown root.root /usr/local/bin/vmclone
# chmod 500 /usr/local/bin/vmclone
#
# If you need to change or customize this script, you should give
# write permission to the file first:
# chmod u+w /usr/local/bin/vmclone
# --------------------------------------------------------------------
#
# RUN:
#
# Run the command: vmclone "source dir" "clone dir".
# Type "vmclone" on its own for usage guidelines.
# --------------------------------------------------------------------
#
# FINAL THOUGHTS:
#
# Please send any feedback, opinion , suggestion about this tool to
# my email.
#
# I hope that you have fun with it!
#
# Thank you,
# Artur Martins
# --------------------------------------------------------------------
#
# Configuration
#
VMWARE_CFG='/etc/vmware/config'
VMWARE_KEY='vmdir'
#
# error_msg function
#
function error_msg
{
echo "-------------------------------------------------------"
echo -e '\E[31m'"\033[1m ERROR: " $1 "\033[0m"
echo "-------------------------------------------------------"
}
#
# usage function
#
function usage
{
echo "Usage: "
echo " "$0" <source_vm_name> <clone_vm_name>"
echo ""
echo " source_vm_name - source virtual machine name. It must "
echo " exist and it should be a vmware vm directory"
echo " clone_vm_name - clone virtual machine name. It should not exist"
echo ""
echo ""
}
# check amount of arguments
if [ $# -lt 2 ]; then
error_msg "USAGE ERROR"
usage
exit 1
fi
#
# check if the VMWare config file exists to
# determine where is the VMWare Virtual Machines
# root directory
#
if [ -f $VMWARE_CFG ]; then
VM_ROOT_DIR=`grep $VMWARE_KEY $VMWARE_CFG | awk '{ print $3 }' | cut -f2 -d\"`
echo -e '\E[37m'"\033[1m -- Configuration: --\033[0m"
echo "Using $VMWARE_CFG"
echo "VMWare Root directory located at $VM_ROOT_DIR."
echo ""
else
error_msg "Could not determine VMWare root directory. "
exit 1
fi
# define variables
SOURCE=$1
CLONE=$2
SOURCE_DIR=$VM_ROOT_DIR'/'$SOURCE
CLONE_DIR=$VM_ROOT_DIR'/'$CLONE
#check if root and clone are not the same
if [ $SOURCE == $CLONE ]; then
error_msg "Clone dir has to be DIFERENT from the source dir."
usage
exit 1
fi
#check if SOURCE_DIR exists
if [ -d $SOURCE_DIR ]; then
# check if there is an vmware vmxf file
if [ -f $SOURCE_DIR'/'$SOURCE'.vmx' ] && [ -f $SOURCE_DIR'/'$SOURCE'.vmxf' ]; then
echo "source directory: "$SOURCE_DIR
else
error_msg "Source directory DOES NOT HAVE a vmware virtual machine."
exit 1
fi
else
error_msg "Source directory DOES NOT exists."
exit 1
fi
#check if CLONE_DIR exists
if [ -d $CLONE_DIR ]; then
error_msg "Clone directory ALREADY exists."
else
echo "clone directory: "$CLONE_DIR
fi
#
# step 1 - copy files
#
echo -e '\E[37m'"\033[1m Step 1:\033[0m"
echo " --- Copying files :"
time cp -axv $SOURCE_DIR $CLONE_DIR
echo " --- finished."
echo ""
#
# step 2 - copy files
#
echo -e '\E[37m'"\033[1m Step 2:\033[0m"
echo " --- Renaming files:"
actual_dir=`pwd`
cd $CLONE_DIR
ls -1 | grep $SOURCE | while read VMFILE;
do
NEWFILE=`echo $VMFILE | sed -e s/$SOURCE/$CLONE/`
echo $VMFILE " >> " $NEWFILE
mv $VMFILE $NEWFILE
done
echo " --- finished."
echo ""
#
# step 3 - fix the vmx and vmxf files with the new clone name
#
echo -e '\E[37m'"\033[1m Step 3:\033[0m"
echo " --- Fixing name on vmx and vmxf files:"
cp $CLONE.vmx $CLONE.vmx.bak
cp $CLONE.vmxf $CLONE.vmxf.bak
sed -e s/$SOURCE/$CLONE/g $CLONE.vmx > cloned.vmx
sed -e s/$SOURCE/$CLONE/g $CLONE.vmxf > cloned.vmxf
cp -f cloned.vmx $CLONE.vmx
cp -f cloned.vmxf $CLONE.vmxf
rm cloned.vmx
rm cloned.vmxf
echo " --- finished."
echo ""
#
# step 4 - cleaning log files
#
echo -e '\E[37m'"\033[1m Step 4:\033[0m"
echo " --- Cleaning logs"
rm -v $CLONE_DIR/vmware*.log
echo " --- finished."
echo ""
#
# step 5 - delete lock files if any
#
echo -e '\E[37m'"\033[1m Step 5:\033[0m"
echo " --- Deleting locks files, if any"
rm -v $CLONE_DIR/*.lck/*.lck -f
echo " --- done!"
echo ""
echo -e '\E[32m'"\033[1m Operation terminated successfully.\033[0m"
# listing files
echo ""
echo -e '\E[37m'"\033[1m Listing $CLONE:\033[0m"
/bin/ls -l
#go to the previous directory
cd $actual_dir