#!/bin/bash echo "#####################################################" echo "# Xen Clone (v1.0) - Glen Davis #" echo "# Clone a SLES10 VM, change mac, hostname and ip #" echo "#####################################################" # Creates a new SLES10 DomU (VM) from an existing SLES10 DomU (VM), the new VM will be cloned from the old VM, but given a new host name, mac address and ipaddress. Thus you can create several "unique" servers from one installed SLES10 VM. #Once finished the new DomU (VM) should boot without any additional configuration. #Disclaimer -- Currently works with a single NIC, and basic bridge setup. # Tested with cloning a SLES10 VM, where the VM was created from the SLES10 YaST Virtual Machine Management module. This script is known to work where the source VM has a static ipaddress and using traditional networking (IE not Network Manager). When finished the new DomU (VM) should appear in the YaST Xen Management screen with the other existing VMs. #Config file and Image file locations (respectively) conf_loc=/etc/xen/vm/ image_loc=/var/lib/xen/images/ #image_loc=/data1/ args=$@ id_check() { if [ `id -u` -ne 0 ] then echo "You must be root to run this script!" echo ID_check succeeded! exit 65 fi } id_check usage() { echo Usage: xenclone SourceVM NewVM new_ip echo Example - xenclone vm1 vm2 10.0.0.2 echo exit 1 } if test -e "$conf_loc""$1" then echo else echo "$conf_loc""$1" does not exist, in that path. If needed edit xenclone conf_loc and/or image_loc paths usage fi if test -e "$image_loc""$1""/hda" then echo else echo "$image_loc""$1""/hda" does not exist, in that path. If needed edit xenclone image_loc path. usage fi # test if vm is running vmtest=`xm list | grep $1 | awk '{ print $1 }'` if test "$vmtest" = "$1" then echo DomU $1 is currently running on Xen, please shutdown before cloning. echo Exiting... exit 1 else echo fi # Make sure the user specifies arguments error_check() { total=`echo $args | awk 'END { print NF }'` if [ $total -lt 3 ]; then usage fi } error_check random_num() { # Get random numbers to specify Mac address SPOTS=10 # Modulo 6 gives range 0 - 9. # num1=0 # num2=0 # num3=0 # num4=0 # num5=0 # num6=0 # num7=0 # num8=0 let "num1 = $RANDOM % $SPOTS" let "num2 = $RANDOM % $SPOTS" let "num3 = $RANDOM % $SPOTS" let "num4 = $RANDOM % $SPOTS" let "num5 = $RANDOM % $SPOTS" let "num6 = $RANDOM % $SPOTS" let "num7 = $RANDOM % $SPOTS" let "num8 = $RANDOM % $SPOTS" } random_num echo -n Copying config cp -v "$conf_loc""$1" "$conf_loc""$2" echo # create directory if it does not exist mkdir -p "$image_loc""$2" echo -n Copying image #cp -v "$image_loc""$1""/hda" "$image_loc""$2" cp -v $image_loc""$1""/* "$image_loc""$2" echo Copy Finished! echo echo "Editing config file, assigning random Mac address, and editing DomU name." # set some variables, if "type" is found consider it FV fvtrue=`cat "$conf_loc"tmp.txt | grep type` pvmac=`cat "$conf_loc"tmp.txt | grep mac` sed "s/$1/$2/g" "$conf_loc""$2" > "$conf_loc"tmp.txt sleep 1 if cat "$conf_loc"tmp.txt | grep type then mac1=`echo $fvtrue | awk -F "," '{ print $2 }'` sleep 1 sed s/$mac1/"mac=00:16:3E:$num3$num4:$num5$num6:$num7$num8"/ "$conf_loc"tmp.txt > "$conf_loc""$2" echo echo "Your new HWaddr 00:16:3E:$num3$num4:$num5$num6:$num7$num8" fi if ! cat "$conf_loc"tmp.txt | grep type then mac1=`echo $pvmac | awk -F "'" '{ print $2 }'` sleep 1 echo "Your new assigned HWaddr 00:16:3E:$num3$num4:$num5$num6:$num7$num8" sed s/$mac1/"mac=00:16:3E:$num3$num4:$num5$num6:$num7$num8"/ "$conf_loc"tmp.txt > "$conf_loc""$2" fi # old code - didn't work for FV guests #sed "s/$1/$2/g" "$conf_loc""$2" > "$conf_loc"/tmp.txt #sed /vif/d "$conf_loc"/tmp.txt > "$conf_loc""$2" #echo vif = [ "'"mac=00:16:$num1$num2:$num3$num4:$num5$num6:$num7$num8"'" ] >> "$conf_loc""$2" #rm "$conf_loc"/tmp.txt # mount image loop back and change ipaddress, host and networking config mkdir -p /xentmp lomount -diskimage "$image_loc""$2"/hda -partition 2 /xentmp echo Changing Network settings. # new code sleep 1 cd /xentmp/etc/sysconfig/network/ sleep 1 oldethid=`ls | grep ifcfg-eth-id` #if -n $oldethid if ls | grep ifcfg-eth-id then cp $oldethid ifcfg-eth0 mkdir -p oldether mv ifcfg-eth-id* ./oldether/ fi sed /IPADDR/d ./ifcfg-eth0 > ./ifcfg-tmp sleep 1 echo IPADDR="'"$3"'" >> ./ifcfg-tmp mv ifcfg-tmp ifcfg-eth0 # rm ./ifcfg-tmp cd /xentmp/etc/udev/rules.d/ cp 30-net_persistent_names.rules 30-net_persistent_names.old sed /SUBSYSTEM/d 30-net_persistent_names.rules > 30.new mv 30.new 30-net_persistent_names.rules # end new code # This is the original - redoing this # sleep 1 # cd /xentmp/etc/sysconfig/network/ # sleep 1 # oldeth=`ls | grep ifcfg-eth | tail -1` # oldethid=`ls | grep ifcfg-eth-id` # #echo oldnum is $oldethid # sleep 1 # cd /xentmp/etc/sysconfig/network/ # cp ./$oldeth ./ifcfg-tmp # sleep 1 # # if [ -f $oldethid ] # then # mkdir -p oldether # mv ifcfg-eth-id* ./oldether/ # fi #ethnumm=ls | grep ifcfg-eth | tail -1 | awk -F"ifcfg-eth" '{ print $2 }' #ethnumm=$(($ethnumm+1)) # sed /IPADDR/d ./ifcfg-tmp > ./ifcfg-eth$ethnumm # # sleep 1 # rm ./ifcfg-tmp # echo IPADDR="'"$3"'" >> ./ifcfg-eth$ethnumm echo Changing HOSTNAME file. cd /xentmp/etc oldhost=`cat /xentmp/etc/HOSTNAME | awk -F"." '{ print $1 }'` sleep 1 sed "s/$oldhost/$2/g" ./HOSTNAME > ./Hnew sleep 1 mv ./Hnew ./HOSTNAME sleep 1 echo Changing hosts file. sed "s/$oldhost/$2/g" ./hosts > ./Hnew1 sleep 1 oldip=`tail -1 /xentmp/etc/Hnew1 | awk '{ print $1 }'` sleep 1 sed "s/$oldip/$3/g" ./Hnew1 > ./hosts sleep 1 rm ./Hnew1 sleep 1 cd / sleep 1 umount /xentmp sleep 1 echo Clone is complete. New VM "$conf_loc""$2" is ready to start! exit