David Moreau Simard

2 minute read

I was preparing an update for a Xenserver pool with over 100 VMs on it and I forgot that when you apply patches on Xenserver nodes, you need to eject images/ISO/DVDs from the virtual machines.

I start looking through some of the VMs, eject a few manually through Xencenter and think “There must be an easier way..".
Thankfully, the folks at Citrix made a simple and efficient bash script that loops through the VMs, verifies if there is a mounted media and allows you to eject it.

Enjoy !

Source: http://blogs.citrix.com/2010/01/28/eject-all-dvds-from-xenserver-vms/

#!/bin/bash
 
#####################################################################################
##                                                                                 ##
##                                                                                 ##
##  file :          all-dvd-eject.sh                                               ##
##  description :   ejects all DVDs from VMs running in a XenServer pool           ##
##                                                                                 ##
##  parameter :     eject|dry                                                      ##
##                  - eject will throw out all mapped DVD drives                   ##
##                  - "dry" will show you which VM has mapped a DVD                ##
##                                                                                 ##
##  example:        ./all-dvd-eject.sh eject                                       ##
##                  ./all-dvd-eject.sh dry                                         ##
##                                                                                 ##
##    known issues:   - The script doesn't work on VMs namend "Control"            ##
##                                                                                 ##
##    Use this script in your own risk and be careful to use it in productive      ##
##  environments. This script was tested in a very simple way and just on          ##
##  XenServer 5.5 with update 1. Therefore if you want to use it be sure           ##
##  to do intensive tests for your environment.                                    ##
##                                                                                 ##
##  copyright (c) Jens Brunsen, Citrix Systems                                     ##
#####################################################################################
 
echo
 
if [ $# -gt 0 ] ; then
 
    VMLIST=`xe vm-list | grep "uuid ( RO) " | awk '{print $5}'`
         
    for VM in $VMLIST
    do
        VMNAME=`xe vm-list uuid=$VM | grep "name-label ( RW)" | awk '{print $4}'`
        if [ "$VMNAME" != "Control" ] ; then
            DVDSTATE=`xe vm-cd-list uuid=$VM | grep "empty ( RO)" | awk '{print $4}'`
 
            if [ "$1" = "eject" ] ; then
                if [ "$DVDSTATE" = "false" ] ; then
                    xe vm-cd-eject uuid=$VM
                    echo "$VMNAME : DVD/ISO ejected"
                fi
            fi
         
            if [ "$1" = "dry" ] ; then
                if [ "$DVDSTATE" = "false" ] ; then
                    echo "$VMNAME : DVD/ISO attached"
                else
                    echo "$VMNAME : empty"
                fi
            fi
        fi
    done
     
else
    echo all-dvd-eject.sh:
    echo "  error: Missing parameter."
    echo "  usage: all_dvd_eject [eject|dry]"
fi
 
echo