Linux.com

Home Linux Community Community Blogs Business (or Enterprise) Clone a Virtual Machine from the shell (The Script)

Clone a Virtual Machine from the shell (The Script)

After few comments on my previous blog related on how to manually clone a Virtual Machine from the shell I've decided to write a simple script to do everything automatically. Maybe this could be useful for newbies but basically it reproduces all the information reported on my latest blog.

There's no rocket science here and I've tried to keep the script simple and hackable for anyone, it required me some time (less than 1h) due to my poor sed knowledge, I've taken it as an exercise to improve my sed capabilities.

As in open source feel free to improve or modify it as you wish, send me an updated copy so I can publish your best version as well, error checking it's quite simple now. You may input absolute or relative paths but there're few limitations around.

 

Basic Usage:

VMCopy <old name> <new name>
oldname is the name of the directory with original VMWare files
newname
is the name of the directory with newly created VMWare files

simple, isn't it ?

 

Here's the script:

#!/bin/bash
#
# @name VMCopy - Copy/Clone a VMWARE Virtual machine with a new name
#
# @author Andrea Benini (Ben)
# @since 2011-02
# @website http://www.linux.com
# @email andrea benini (at domain name) gmail [DoT] com
# @package Use it to get a physical copy of an existing machine, no snapshots or
# VMWare tools involved in this operation, it's a plain text bash script
# @require This tool should be portable to many UNIX platforms, it just requires:
# sed, dirname, basename, md5sum, $RANDOM (shell variable) and few more
# shell builtins commands
#
# @license GPL v2 AND The Beer-ware License
# See GPL details from http://www.gnu.org/licenses/gpl-2.0.html
# "THE BEER-WARE LICENSE" (Revision 43)
# Andrea Benini wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If you make modification on
# the file please leave author notes on it, if you improve/alter/modify
# it please send me an updated copy by email. If we meet some day, and
# you think this stuff is worth it, you can buy me a beer in return.
# Andrea Benini
#
SOURCEPATH=$(dirname "$1")
TARGETPATH=$(dirname "$2")
SOURCEMACHINE=$(basename "$1")
TARGETMACHINE=$(basename "$2")

if [[ $# -ne 2 ]]; then
echo -e "$0 "
echo -e " Copies a VMWare virtual machine"
echo " and are names"
echo " of the machine you'd like to copy and the new destination name"
echo ""
exit
fi

exec 2> /dev/null
echo "VMCopy - VMWare Virtual Machines cloner"
echo " - Copying source machine '$SOURCEMACHINE' with the new name '$TARGETMACHINE'..."
rm -rf "$TARGETPATH/$TARGETMACHINE"
cp -R "$SOURCEPATH/$SOURCEMACHINE" "$TARGETPATH/$TARGETMACHINE"

echo " - Removing unnecessary files for '$TARGETMACHINE'"
rm -f "$TARGETPATH/$TARGETMACHINE"/*.log

echo " - Renaming files for '$TARGETMACHINE'"
for OLDNAME in "$TARGETPATH/$TARGETMACHINE"/*; do
NEWNAME=${OLDNAME/$SOURCEMACHINE/$TARGETMACHINE}
mv -f "$OLDNAME" "$NEWNAME"
done

echo " - Remapping Hard Disks for the new machine"
ls "$TARGETPATH/$TARGETMACHINE"/*.vmdk | grep -v -e "-s....vmdk" | while read DISKNAME; do
sed -i "s/$SOURCEMACHINE/$TARGETMACHINE/g" "${DISKNAME}"
done

echo " - Changing resource files (if any)"
if [[ -f "$TARGETPATH/$TARGETMACHINE/$TARGETMACHINE.vmxf" ]]; then
sed -i "s/$SOURCEMACHINE/$TARGETMACHINE/g" "$TARGETPATH/$TARGETMACHINE/$TARGETMACHINE.vmxf"
fi

echo " - Changing $TARGETMACHINE.vmx file"
# Massive character substitutions
sed -i "s/$SOURCEMACHINE/$TARGETMACHINE/g" "$TARGETPATH/$TARGETMACHINE/$TARGETMACHINE.vmx"
# Change ethernet mac addresses
MACADDRESSES=`cat "$TARGETPATH/$TARGETMACHINE/$TARGETMACHINE.vmx"|grep "generatedAddress ="| sed -e "s/.*=."//" -e "s/"//"`
REGEXP="[0-9 A-Z a-z][0-9 A-Z a-z]"
for OLDMAC in $MACADDRESSES; do
NEWMAC=$(echo $RANDOM$RANDOM |md5sum| sed -r 's/(..)/1:/g; s/^(.{17}).*$/1/;')
sed -i "s/$OLDMAC/$NEWMAC/" "$TARGETPATH/$TARGETMACHINE/$TARGETMACHINE.vmx"
done

echo -e " - Operation Complete, '$TARGETMACHINE' cloned successfully"

 

 

Share your ideas

If you find errors or you'd like to change some parts let me know, share your ideas to improve the script, I'll always post here the improved version

 

 

Comments

Subscribe to Comments Feed

Upcoming Linux Foundation Courses

  1. LFD411 Embedded Linux Development
    25 Aug » 29 Aug - Santa Clara, CA
    Details
  2. LFS422 High Availability Linux Architecture
    08 Sep » 11 Sep - Raleigh, NC
    Details
  3. LFS426 Linux Performance Tuning
    08 Sep » 11 Sep - New York
    Details

View All Upcoming Courses


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board