VMware Server 2 Cloning and Permissions


VMWare server is great - but you can't clone from the menu. Use this script instead by copying it to  "/var/lib/vmware/Virtual Machines" and executing it as root

Notes about how it works

This script is adapted from an online source, and updated to handle the different virtual disk modes that correspond to the two options you have when you create a machine. You options are;
  • allocate all disk space now
  • split disk space into 2G chunks
The first option is the most interesting as it influeneces the actual virtual disk file name on your system.

If you select nothing, you get a small single file that grows as you use it, called a "monolithic sparse file". This is a binary file and looks like SomeName.vmdk

If you select to allocate all space now, you get two files;  SomeName.vmdk and SomeName-flat.vmdk. Unlike in the default choice, the SomeName.vmdk becomes a text file which points to the actual binary disk file, SomeName-flat.vmdk

Clone Script


echo `renice 19 $PID`

if [ $# != 2 ]; then
        echo "\nUsage: $0 <original VM name> <new VM name>\n"
        exit 127


echo "SOURCE is \"$SOURCE\""
echo "DEST is \"$DEST\""

if [ "`ls "$SOURCE" | grep "\.lck$"`" != "" ]; then
        echo "Shut down the VM \"$SOURCE\" first"
        exit 127

if [ "`ls | grep "$DEST"`" != "" ]; then
        echo "Destination VM already exists - pick a new name or delete first"
        exit 127

echo "\nCopying:"
cp -Rv "$SOURCE" "$DEST"

echo "\nRenaming files in $DEST:"
ls "$DEST" | grep "^$SOURCE" | while read FILE
        mv -v "$DEST/$FILE" "$DEST/`echo $FILE | sed -e \"s/^$SOURCE/$DEST/\"`"

echo "\nReplacing \"$SOURCE\" with \"$DEST\" in text files:"
ls "$DEST" | grep -v "\(\.nvram$\|\.vmdk$\|\.log$\)" | while read FILE
        echo "Doing \"$DEST/$FILE\", writing to \"$DEST/$FILE.temp\""
        sed -e "s/$SOURCE/$DEST/g" "$DEST/$FILE" > "$DEST/$FILE.temp"

# test to see if there is a -flat.vmdk file and if so, edit the .vmdk file to refer to it
ls "$DEST" | grep -e "-flat\.vmdk$" | while read FILE
        FILE=`echo $FILE | sed "s/\-flat//"`
        echo "Doing \"$DEST/$FILE\", writing to \"$DEST/$FILE.temp\""   
        sed -e "s/$SOURCE/$DEST/g" "$DEST/$FILE" > "$DEST/$FILE.temp"

echo "\nRenaming temp files to get rid of \".temp\":"
ls "$DEST" | grep "\.temp$" | while read FILE
        mv -vf "$DEST/$FILE" "$DEST/`echo $FILE | sed -e \"s/\.temp//\"`"

echo "\n(Re)Setting permissions:"
chown -Rv root:root "$DEST"
chmod -v 755 "$DEST"
chmod -v 600 "$DEST"/*
chmod -v +rx "$DEST"/*.vmx
chmod -v +r "$DEST"/*.log


It's also useful to note that the permissions determine who can do what with the VMs

The VM Directory
drwxr-xr-x  4 root   root   4096 2009-03-20 11:09 Windows7_Test

And the contents of that directory
-rwxr-xr-x  1 root   root       2118 2009-03-20 11:09 Windows7_Test.vmx
-rw-------  1 root   root 7080443904 2009-03-20 14:26 Windows7_Test.vmdk

Excerpt from the web:

File permissions
File permissions are of the utmost importance when it comes to VMware Server on Linux -- specifically the permissions on the VMs configuration file, the vmx file. A VMs vmx file is located at "/var/lib/vmware/vms/VM_NAME/VM_NAME.vmx". There are two areas where the permissions on this file come into play. They are user access and process ownership.

User access
What level of access a user has to a VM depends on the file permissions that are set on the VM's vmx file. Here is a list of possible file permissions and their effects:

rwx - A user has full control of this VM, and they will also be able to log into the VMware Server MUI.

r-x - A user can control a VM's power states (on/off/suspend), and they will also be able to log into the VMware Server MUI.

rw- - A user will be able to change a VM's configuration but will not able to alter its power states. A user without execute privileges on any VM will not be able to log into the VMware Server MUI.

A user will also only see VMs in the MUI that the user has access to.