XenServer – Apply patches using bash script

With the release of XenServer 6.2, Citrix has provided best & easy to use virtulization option for free. Only limitation with free xenserver is you can’t use xencenter to apply patches. Which anyways many Linux Admins must not be using. To patch free xenserver only option is to use cli, you can patch xenserver using cli or by using below mentioned script.

PL TEST THIS SCRIPT THOROUGHLY BEFORE USING FOR PRODUCTION.

Limitations,
This script can not be used to patch diff ver of xenservers at once.

#!/bin/bash
#dpthakar@gmail.com
#http://www.linuxreaders.com
 
#with the citrix xenserver 6.2 release, free xencenter can not be used to patch servers. this script can be used on any of server to patch itself & rest of the xenservers, this script is only tested with stanalone servers, no pool etc.
#pl make sure updates stored under PATCHDIR is for SERVERS mentioned here, e.g patches stored for 6.2 only. Also do not mix diff ver of patches & servers.
NFSSHARE=192.168.1.24:/storage/xen_back
MOUNTDIR=/mnt/back
PATCHDIR=/mnt/back/up
#mention list of the xenserver ip
SERVERS="192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4"
SUSER=root
SPASS=redhat
 
#check for backup / mount dir.
if [ -d $MOUNTDIR ]
        then
                echo "Mount Dir Exists"
        else
                echo "Mount Dir does not exists, Creating"
                mkdir -p $MOUNTDIR
fi
 
#mount nfs share if not already mounted.
MOUNT=`mount|grep $NFSSHARE -c`
if [ $MOUNT = "1" ]
        then
                echo "NFS already mouted"
        else
                echo "Mounting NFS share"
                mount -t nfs $NFSSHARE $MOUNTDIR
fi
 
 
#list patches for patchdir
PLIST=`ls $PATCHDIR/*.xsupdate | wc -l`
PCOUNT=0
PLINE=1	
ls $PATCHDIR/*.xsupdate > /tmp/patch_list
until [ "$PCOUNT" = "$PLIST" ]	
	do
	PATCH=`gawk FNR==$PLINE /tmp/patch_list`
 
	#upload patch file to all mentioned servers
	echo $SERVERS | sed 's/,/\n/g' > /tmp/patch_server_list
	SLIST=`cat /tmp/patch_server_list | wc -l`
	COUNT=0
	LINE=1
	until [ "$COUNT" = "$SLIST" ]
        	do
			SERVER=`gawk FNR==$LINE /tmp/patch_server_list`
			#upload patch to server
			PATCHUUID=`xe -s $SERVER -u $SUSER -pw $SPASS patch-upload file-name=$PATCH`
			#find server uuid
			HOSTUUID=`xe -s $SERVER -u $SUSER -pw $SPASS host-list params=uuid | gawk -F: '{ print $2 }' | sed 's/ //g' | sed '/^$/d'`
			#apply patch
			echo "Patching $PATCH to Server $SERVER"
			xe -s $SERVER -u $SUSER -pw $SPASS patch-apply uuid=$PATCHUUID host-uuid=$HOSTUUID
			#clean applied patch file from local storage to save space
			xe patch-clean uuid=$PATCHUUID 
			COUNT=`expr $COUNT + 1`
			LINE=`expr $LINE + 1`
	        done
	PCOUNT=`expr $PCOUNT + 1`
	PLINE=`expr $PLINE + 1`
	done
if you find any missing point in here, please let us know in comment section or tweet us at @linuxreaders. To get more articles like this, subscribe to our RSS feeds / Mails.

4 Comments

  1. profileuser says:

    Hi Dhaval, First of all thank you very much for this credible script you have made available for automated patch install for xenservers. On trying to run the script, I ran through some syntax errors, and just wondering whether you could give me some pointers? Error response below: ################## [root@xen01 ~]# /var/tmp/posts-v2.sh : No such file or directory1: #!/bin/bash : command not foundh: line 16:  /var/tmp/posts-v2.sh: line 66: syntax error near unexpected token `done’ /var/tmp/posts-v2.sh: line 66: ` done’ [root@xen01 ~]# #################### First of all I’m new to bash scripting and as such I don’t know where to customise the script to adapt it to our environment. The only portion I changed are as below and the rest I left alone. The error above popped up NFSSHARE=192.168.10.9:/Public SERVERS=”192.168.10.15″ SUSER=root SPASS=Password1 Another question is how to the save the file: I pasted your code above into Notepad, saved it as “xx.sh” and selected Encoding UTF-8, is this the right way for unix / linux ? Please assist where ever you can.. Anthony

    • dpthakar says:

      hi, download bash script from following link. https://www.dropbox.com/s/h0l2z9h3aiisgs4/xen_patch.sh run following command as root & modify as per your requirement using vim / gedit. chdmo 755 xen_patch.sh

      • profileuser says:

        Hi Again, dpthakar, I came across another script however using FTP as a source for xsupates. This was derived from the pdf document from Citrix. I have been trying for 1.5 weeks to troubleshoot this script below: Please help me. Xenserver 6.2 ##################################### chmod 755 ./postscripts.sh [root@xen01 ~]# ./postscripts.sh ./postscripts.sh: line 11: echo Downloading updates…: command not found –2014-04-02 14:10:16– ftp://192.168.10.5/FTP/xen62/*.xsupdate => `.listing’ Connecting to 192.168.10.5:21… connected. Logging in as anonymous … Logged in! ==> SYST … done. ==> PWD … done. ==> TYPE I … done. ==> CWD /FTP/xen62 … done. ==> PASV … done. ==> LIST … done. [ ] 951 –.-K/s in 0.06s 2014-04-02 14:10:17 (14.9 KB/s) – `.listing’ saved [951] Removed `.listing’. –2014-04-02 14:10:17– ftp://192.168.10.5/FTP/xen62/xs62E001.xsupdate => `xs62E001.xsupdate’ ==> CWD not required. ==> PASV … done. ==> RETR xs62E001.xsupdate … done. Length: 6625641 (6.3M) 100%[======================================>] 6,625,641 –.-K/s in 0.1s 2014-04-02 14:10:17 (55.2 MB/s) – `xs62E001.xsupdate’ saved [6625641] –2014-04-02 14:10:17– ftp://192.168.10.5/FTP/xen62/xs62E002.xsupdate => `xs62E002.xsupdate’ ==> CWD not required. ==> PASV … done. ==> RETR xs62E002.xsupdate … done. Length: 1743501 (1.7M) 100%[======================================>] 1,743,501 –.-K/s in 0.07s 2014-04-02 14:10:17 (24.1 MB/s) – `xs62E002.xsupdate’ saved [1743501] –2014-04-02 14:10:17– ftp://192.168.10.5/FTP/xen62/xs62E004.xsupdate => `xs62E004.xsupdate’ ==> CWD not required. ==> PASV … done. ==> RETR xs62E004.xsupdate … done. Length: 35766769 (34M) 100%[======================================>] 35,766,769 66.3M/s in 0.5s 2014-04-02 14:10:18 (66.3 MB/s) – `xs62E004.xsupdate’ saved [35766769] –2014-04-02 14:10:18– ftp://192.168.10.5/FTP/xen62/xs62E005.xsupdate => `xs62E005.xsupdate’ ==> CWD not required. ==> PASV … done. ==> RETR xs62E005.xsupdate … done. Length: 9315631 (8.9M) 100%[======================================>] 9,315,631 24.7M/s in 0.4s 2014-04-02 14:10:18 (24.7 MB/s) – `xs62E005.xsupdate’ saved [9315631] –2014-04-02 14:10:18– ftp://192.168.10.5/FTP/xen62/xs62E010.xsupdate => `xs62E010.xsupdate’ ==> CWD not required. ==> PASV … done. ==> RETR xs62E010.xsupdate … done. Length: 252927 (247K) 100%[======================================>] 252,927 –.-K/s in 0.02s 2014-04-02 14:10:18 (10.5 MB/s) – `xs62E010.xsupdate’ saved [252927] –2014-04-02 14:10:18– ftp://192.168.10.5/FTP/xen62/xs62E011.xsupdate => `xs62E011.xsupdate’ ==> CWD not required. ==> PASV … done. ==> RETR xs62E011.xsupdate … done. Length: 845698 (826K) 100%[======================================>] 845,698 –.-K/s in 0.06s 2014-04-02 14:10:18 (14.2 MB/s) – `xs62E011.xsupdate’ saved [845698] –2014-04-02 14:10:18– ftp://192.168.10.5/FTP/xen62/xs62E012.xsupdate => `xs62E012.xsupdate’ ==> CWD not required. ==> PASV … done. ==> RETR xs62E012.xsupdate … done. Length: 35767767 (34M) 100%[======================================>] 35,767,767 55.4M/s in 0.6s 2014-04-02 14:10:19 (55.4 MB/s) – `xs62E012.xsupdate’ saved [35767767] –2014-04-02 14:10:19– ftp://192.168.10.5/FTP/xen62/xs62E014.xsupdate => `xs62E014.xsupdate’ ==> CWD not required. ==> PASV … done. ==> RETR xs62E014.xsupdate … done. Length: 1745433 (1.7M) 100%[======================================>] 1,745,433 –.-K/s in 0.03s 2014-04-02 14:10:19 (53.2 MB/s) – `xs62E014.xsupdate’ saved [1745433] –2014-04-02 14:10:19– ftp://192.168.10.5/FTP/xen62/xs62ESP1002.xsupdate => `xs62ESP1002.xsupdate’ ==> CWD not required. ==> PASV … done. ==> RETR xs62ESP1002.xsupdate … done. Length: 1744985 (1.7M) 100%[======================================>] 1,744,985 –.-K/s in 0.04s 2014-04-02 14:10:19 (41.5 MB/s) – `xs62ESP1002.xsupdate’ saved [1744985] sleeping in 60 secs Uploading Update xs62E001.xsupdate… Operation failed. Error: file ‘/tmp/Updates/xs62E001.xsupdate’ does not exist installing Update xs62E001.xsupdate… The uuid you supplied was invalid. type: pool_patch uuid: sleeping in 60 secs Uploading Update xs62E002.xsupdate… Operation failed. Error: file ‘/tmp/Updates/xs62E002.xsupdate’ does not exist installing Update xs62E002.xsupdate… The uuid you supplied was invalid. type: pool_patch uuid: sleeping in 60 secs Uploading Update xs62E004.xsupdate… Operation failed. Error: file ‘/tmp/Updates/xs62E004.xsupdate’ does not exist installing Update xs62E004.xsupdate… The uuid you supplied was invalid. type: pool_patch uuid: sleeping in 60 secs Uploading Update xs62E005.xsupdate… Operation failed. Error: file ‘/tmp/Updates/xs62E005.xsupdate’ does not exist installing Update xs62E005.xsupdate… The uuid you supplied was invalid. type: pool_patch uuid: sleeping in 60 secs Uploading Update xs62E010.xsupdate… Operation failed. Error: file ‘/tmp/Updates/xs62E010.xsupdate’ does not exist installing Update xs62E010.xsupdate… The uuid you supplied was invalid. type: pool_patch uuid: sleeping in 60 secs Uploading Update xs62E011.xsupdate… Operation failed. Error: file ‘/tmp/Updates/xs62E011.xsupdate’ does not exist installing Update xs62E011.xsupdate… The uuid you supplied was invalid. type: pool_patch uuid: sleeping in 60 secs Uploading Update xs62E012.xsupdate… Operation failed. Error: file ‘/tmp/Updates/xs62E012.xsupdate’ does not exist installing Update xs62E012.xsupdate… The uuid you supplied was invalid. type: pool_patch uuid: sleeping in 60 secs Uploading Update xs62E014.xsupdate… Operation failed. Error: file ‘/tmp/Updates/xs62E014.xsupdate’ does not exist installing Update xs62E014.xsupdate… The uuid you supplied was invalid. type: pool_patch uuid: sleeping in 60 secs Uploading Update xs62ESP1002.xsupdate… Operation failed. Error: file ‘/tmp/Updates/xs62ESP1002.xsupdate’ does not exist installing Update xs62ESP1002.xsupdate… The uuid you supplied was invalid. type: pool_patch uuid: Broadcast message from root (pts/1) (Wed Apr 2 14:28:20 2014): The system is going down for reboot NOW! ##################################### The script itself is as listed below: ################################## #!/bin/bash # Install XenServer Updates HOSTUUID=$(xe host-list name-label=$HOSTNAME –minimal) cd /tmp if [ -a /tmp/secondboot ] then echo “Secondboot” else mkdir updates cd updates echo” Downloading updates…” wget ftp://192.168.10.5/FTP/xen62/*.xsupdate cd /tmp touch /tmp/secondboot fi for updatefile in `ls /tmp/updates`;do echo sleeping in 60 secs sleep 60 echo “Uploading Update $updatefile…” echo “Uploading Update $Updatefile…”>> /var/log/messages PATCHUUID=$(xe patch-upload file-name=/tmp/Updates/$updatefile) sleep 60 echo “installing Update $updatefile…” echo “installing update $updatefile…”>> /var/log/messages xe patch-pool-apply host-uuid=$HOSTUUID uuid=$PATCHUUID rm -f /tmp/updates/$updatefile PATCHACTION=$(xe patch-list uuid=$PATCHUUID params=after-apply-guidance –minimal) if [ "$PATCHACTION" == "restartXAPI" ] then /opt/xensource/bin/xe-toolstack-restart sleep 60 elif [ "$PATCHACTION" == "restartHost" ]; then reboot; sleep 60 fi done # Disable first boot for subsequent reboots rm -f /etc/rc3.d/S99zzpostinstall rm -f /tmp/secondboot # Final Reboot reboot ################################## Thanks for your help in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trending Posts