VMware ESX patch management

Before VMware released ESX Server 3.0.2 (and, for the time being, removed the need for me to patch it), I was looking for a solution to patching ESX that didn’t involve rebooting after every patch, and didn’t involve a lot of manual intervention. I came up with a script that copies the tarballs from the base directory to another spot, extracts them, puts the host in maintenance mode, and then patches sequentially until it’s all done. The trick was to get the patches in the right order, or you’d be left with a bit of cleaning up to do (refer to http://kb.vmware.com/kb/10230 for more on that). To that end, instead of importing a listing of the extracted directories, I use text files and break it into phases. It’s ugly, but it kind of works. I’m sure I won’t ever have to resort to something so cludgy once version 3.5 is released. VMware have also announced, incidentally, that version 3.0.2 esxupdate now has the ability to cope with patches being installed in non-chronological order. Which negates the need for a lot of what I did with this script, and will instead make it easier to use something like ls to give you the output you need. I couldn’t find a nice way to exit maintenance mode once the host rebooted, and tried a few things with rc.local that never really worked out. Life goes on. Copy the script, the two phase files, and the tarballs to a nominated directory on the host.
This is the script itself. I like to call mine barry.sh. Make sure it’s executable (chmod +x barry.sh).

[barry.sh]

#!/bin/bash

# Set some variables
dir=`pwd`

# Logging is for whimps

# Check to see if the update directory exists, if not, make it
echo “Creating updates directory”
if [ -d /var/updates ]
then
echo “Updates directory exists”
rm -rf /var/updates/*
else
mkdir /var/updates
echo “Created updates directory”
fi

# Copy the patches to the update directory
echo “Copying patches to updates directory”
cp –reply=y *.tgz /var/updates/
echo “Copied patches to updates directory”

# Get a list of patch files to extract
echo “Locating patch files to extract”
ls /var/updates >$dir/input.file
echo “Found ’em”

# Extract the files
echo “Extracting the patch files”
for i in `cat input.file`; do
cd /var/updates
tar xzvf $i
cd $dir
done
echo “Extracted patch files”

# Remove the tarballs
echo “Removing the source tarballs”
rm -rf /var/updates/*.tgz
echo “Tarballs gone”

# Enter maintenance mode
echo “Entering Maintenance Mode”
vimsh -n -e /hostsvc/maintenance_mode_enter
echo “vi3 Host now in Maintenance mode”

# Patch ’em – I like to call this Phase 1
echo “Running patches in Phase 1”
for i in `cat $dir/phase1`; do
cd /var/updates/
cd $i
esxupdate -n update
done

# This one’s nasty – also known as a phasebreaker
cd /var/updates/ESX-6431040/
./install_patches << FOO
all
y
n
FOO

# Patch ’em – I like to call this Phase 2
echo “Running patches in Phase 2”
for i in `cat $dir/phase2`; do
cd /var/updates/
cd $i
esxupdate -n update
done

reboot

The next two files are called, respectively, phase1 and phase2. They need to be in the same directory as barry.sh to be useful.

[phase1]

ESX-1006511
ESX-1410076
ESX-2158032
ESX-2066306
ESX-6921838
ESX-8173580
ESX-9986131
ESX-1271657
ESX-1917602
ESX-2031037
ESX-2092658
ESX-3996003
ESX-5497987
ESX-6075798
ESX-3199476
ESX-5031800
ESX-5885387
ESX-6050503
ESX-6856573
ESX-9865995
ESX-1541239
ESX-2257739
ESX-2559638

[phase2]

ESX-9916286
ESX-4825991
ESX-5095559
ESX-5140477
ESX-6657345
ESX-6704314
ESX-7281356
ESX-7302867
ESX-7408807
ESX-7557441
ESX-1000073
ESX-1000070
ESX-1000039

Thanks for reading …