Tag Archives: Bash Script

XenServer GrubConf.py fix script

In a previous article (Fixing XenServer error “Unable to find partition containing kernel”) I described how to fix a recurring problem after patching XenServer 6.2 installations. While the fix is known from years it’s never been adopted, and different distros (such as Ubuntu LTS 14.04) fail to boot properly when the GrubConf.py (on dom0) gets reset to its default state.

Being the lazy person that I am I decided to set up a script to do the work for me, after all we’re admins, not monkeys.

#!/bin/bash
GRUBCONF="/usr/lib/python2.4/site-packages/grub/GrubConf.py"
PATCHED=$(grep "_entry" $GRUBCONF | wc -l)
if [ $PATCHED -eq 2 ]; then
  echo "GrubConf.py is already patched"
else
  echo "Patching GrubConf.py to fix boot..."
  sed -i 's/_entry}":/_entry}":\n                        arg = "0"\n                    elif arg.strip() == "${next_entry}":/' $GRUBCONF
  PATCHED=$(grep "_entry" $GRUBCONF | wc -l)
  if [ $PATCHED -eq 2 ]; then
    echo "- Patch was applied successfully."
  else
    echo "- There was a problem while applying the patch."
  fi;
fi;
echo

This does just what I/we used to do manually: detects if GrubConf.py has been reverted and, if not, patches it up. Supplementary tests added for paranoia 🙂

XenServer VMs and easy autostart

One of the most tedious tasks I find myself doing on XS installation is switching VMs’ autostart off and on. While no big deal it got boring real fast. I thus crafted a couple bash scripts to be run on the XS host that speed the task up.

#!/bin/bash
pool=$(xe pool-list | sed -n "s/.*\uuid.*: \(.*\)/\1/p" | awk '{ system("xe pool-param-get param-name=other-config param-key=auto_poweron uuid="$0" 2>&1") }' | sed 's/Error: Key auto_poweron not found in map/false/g' | sed 's/false/INACTIVE/g' | sed 's/true/ACTIVE/g')
list=$(xe vm-list | sed -n "s/.*\(uuid\|name-label\).*)[ ]*: \(.*\)/\2/p" | awk '{ print $0; } NR%2==1 { system("xe vm-param-get param-name=other-config param-key=auto_poweron uuid="$0" 2>&1" ) }' | sed 's/Error: Key auto_poweron not found in map/false/g' | awk '{id=$0; getline; atrun=$0; getline; print atrun " " $0 " [" id "]"; }' | sort -k 2 | grep -v "Transfer VM for VDI" | grep -v "Control domain on host" )
active=$(echo "$list" | sed -n "s/true \(.*\)/\1/p")
inactive=$(echo "$list" | sed -n "s/false \(.*\)/\1/p")
echo -e "\nAuto restart in the pool is $pool\n\n"
if [ "$active" != "" ]; then
  echo -e "*** VIRTUAL MACHINES AUTO RESTARTING ***************\n\n$active\n\n"
fi
if [ "$inactive" != "" ]; then
  echo -e "*** VIRTUAL MACHINES _NOT_ AUTO RESTARTING *********\n\n$inactive\n\n"
fi

This script will parse all the VMs while skipping a few system instances, and will clearly show which ones are starting automatically, together with the corresponding UUID, so no guesswork or matching is needed.

# ./autostart_status.sh

Auto restart in the pool is ACTIVE


*** VIRTUAL MACHINES AUTO RESTARTING ***************

VM1 [UUID1]


*** VIRTUAL MACHINES _NOT_ AUTO RESTARTING *********

VM2 [UUID2]
VM3 [UUID3]

With our UUID in hand, we can now enable/disable it through this tiny script:

#!/bin/bash
if [ "$2" == "true" -o "$2" == "false" ]; then
        xe vm-param-set other-config:auto_poweron=$2 uuid=$1
else
        echo Correct syntax: $0 \ \
fi

With these two scripts the task becomes checking the VM name and switching it on/off in a matter of seconds. Mission complete.