VMware Communities
matze502
Contributor
Contributor

Shell Script to automatically clone a vm "template"

Hi all,

I just started to create a script to automatically clone an already created VM template and it works fine so far, but I'm wondering if there is any way to change the RAM of the created machine on the command-line.

Here it is if you are interested in using it. I've also created a github repository for it and some other scripts in the future. https://github.com/matze502/scripts/tree/master/bash/vmware/fusion

Regards Matze

#!/bin/bash

#

# Clone from Template

#

# be sure to put "/Applications/VMware Fusion.app/Contents/Library" in your PATH variable

# For this script I created a template manually and installed my ssh public key for root in it.

# If you don't do that you have to enter the root password of the cloned VM  at the end of the script.

# Folder holding all VM's, use the full path here, no ~

VMDIR="/Users/matthias/Documents/Virtual Machines.localized"

# Get already created VM's

SOURCE=($(ls -1 "$VMDIR"))

# Default VM to clone

VM="$VMDIR/centos7_template.vmwarevm/centos7_template.vmx"

# Domain

DOMAIN="fritz.box"

if [ -z $SOURCE ]; then

  echo "No virtual machine to clone"

  exit 1

fi

while true; do

  while true; do

    echo "Default                : $VM"

    read -p "use default [y/n] ? " CHOICE

  case $CHOICE in

  y)

  QUELLE=$VM

  break

  ;;

  n)

  echo

  echo "Following machines found :"

  echo

  for i in `seq 0 50`; do

   if [ -z ${SOURCE[$i]} ]; then

     break

   else

     echo -e "$i\t$(echo ${SOURCE[$i]}| cut -d. -f1)"

   fi

  done

  read -p "Choose VM Number to clone : " VM

  QUELLE="$VMDIR/${SOURCE[$VM]}/$(echo ${SOURCE[VM]}| cut -d. -f1).vmx"

  break

  ;;

  *)

  echo

  echo "press y or n ..."

  ;;

  esac

  done

  echo

  read -p "Enter clone name               : " KLONNAME

  echo

  echo    "Source                         : $QUELLE"

  echo    "Destination                    : $VMDIR/$KLONNAME.vmwarevm/$KLONNAME.vmx"

  echo    "Clone-Name                     : $KLONNAME"

  read -p "All correct [y/n]? " CHOICE

  case $CHOICE in

    y)

        break

  ;;

    n)

  ;;

    *)

        echo

  echo "press y or no ..."

  ;;

  esac

done

mkdir "$VMDIR/$KLONNAME.vmwarevm"

echo

echo "Create Dir $VMDIR/$KLONNAME.vmwarevm ..."

echo "Cloning VM ..."

vmrun -T fusion clone "$QUELLE" "$VMDIR/$KLONNAME.vmwarevm/$KLONNAME.vmx" full -cloneName=$KLONNAME

if [ $? -eq 0 ]; then

  echo "VM cloned."

else

  echo "Error cloning the VM. Exiting..."

  exit 1

fi

while true; do

read -p "Turn the Clone on [y/n]? " CHOICE2

case $CHOICE2 in

  y)

    echo

    vmrun -T fusion start "$VMDIR/$KLONNAME.vmwarevm/$KLONNAME.vmx"

    break

    ;;

  n)

    echo

    echo "To register the new VM to Fusion you have to start the VM manually with the following command:"

    echo "vmrun -T fusion start \"$VMDIR/$KLONNAME.vmwarevm/$KLONNAME.vmx\""

    echo

    break

    ;;

  *)

    echo

    echo "press y or no ..."

    ;;

esac

done

echo "Wait a minute or go make a coffee..."

while true; do

  GUESTIP=$(vmrun getGuestIPAddress "$VMDIR/$KLONNAME.vmwarevm/$KLONNAME.vmx")

  echo $GUESTIP |grep -e "192.168."

  if [ $? -eq 0 ]; then

  break

  fi

done

echo "Guest IP is: $GUESTIP"

ssh root@$GUESTIP -oStrictHostKeyChecking=no "hostname $KLONNAME"

echo "Set Hostname $KLONNAME ..."

ssh root@$GUESTIP -oStrictHostKeyChecking=no "echo \"$GUESTIP  $KLONNAME.$DOMAIN $KLONNAME\" >> /etc/hosts"

ssh root@$GUESTIP -oStrictHostKeyChecking=no "echo \"$KLONNAME\" > /etc/hostname"

echo "Edit Hosts-File ..."

ssh root@$GUESTIP -oStrictHostKeyChecking=no 'echo "send host-name = git" >> /etc/dhcp/dhclient.conf'

echo "Set DHCP settings..."

ssh root@$GUESTIP -oStrictHostKeyChecking=no "systemctl restart network"

echo "Network-Service restart..."

exit 0

Tags (3)
Reply
0 Kudos
2 Replies
Mikero
Community Manager
Community Manager

Awesome stuff, thanks for sharing!

You can use vmrun to pass parameters to the .vmx file using the writeVariable command, so you could specify new RAM and CPU values that way, but I don't think there's a way to do it for a running VM.

What's your use case behind this, I'm curious?

Just seeing if you can do it, or it is part of a bigger workflow?

-
Michael Roy - Product Marketing Engineer: VCF
Reply
0 Kudos
matze502
Contributor
Contributor

Hi Mikero,

thanks. I just sometimes need to create a machine for some tests and after I saw that vagrant wants money for the vmware deployment I started to write this.

Its very annoying when you want to test something, for example build a mariadb master/slave, and have to create a fresh machine from scratch first.

WriteVariable should work. I will try to use memsize for this later. I also saw a line below this: mem.hotadd = "TRUE" . Maybe it will work in running state..

By the way I only used centOS 7 for the template but it should be no problem at the end to let the script look for the os and write in the proper files I think.

Reply
0 Kudos