As a best practive in virtual environments you should leave the DVD-Drive empty if you don’t have to use it because it can prevent you from doing a live migration e.g. if the local DVD drive from a XenServer host is mapped or to start a VM on another host if the local DVD drive of the host is mapped. If your XenServer Pool is very small it is very easy to take a fast look where a drive is mapped or not. In a bigger environment this is not so easy.

Therefore I have created a small script for this:

 #!/bin/bash

#####################################################################################
##                                                                                 ##
##                                                                                 ##
##  file :          all-dvd-eject.sh                                               ##
##  description :   ejects all DVDs from VMs running in a XenServer pool           ##
##                                                                                 ##
##  parameter :     eject|dry                                                      ##
##                  - eject will <span class="code-keyword">throw</span> out all mapped DVD drives                   ##
##                  - <span class="code-quote">"dry"</span> 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 <span class="code-quote">"Control"</span>            ##
##                                                                                 ##
##    Use <span class="code-keyword">this</span> 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 <span class="code-keyword">if</span> you want to use it be sure           ##
##  to <span class="code-keyword">do</span> intensive tests <span class="code-keyword">for</span> your environment.                                    ##
##                                                                                 ##
##  copyright (c) Jens Brunsen, Citrix Systems                                     ##
#####################################################################################

echo

<span class="code-keyword">if</span> [ $# -gt 0 ] ; then

    VMLIST=`xe vm-list | grep <span class="code-quote">"uuid ( RO) "</span> | awk '{print $5}'`
        
    <span class="code-keyword">for</span> VM in $VMLIST
    <span class="code-keyword">do</span>
        VMNAME=`xe vm-list uuid=$VM | grep <span class="code-quote">"name-label ( RW)"</span> | awk '{print $4}'`
        <span class="code-keyword">if</span> [ <span class="code-quote">"$VMNAME"</span> != <span class="code-quote">"Control"</span> ] ; then
            DVDSTATE=`xe vm-cd-list uuid=$VM | grep <span class="code-quote">"empty ( RO)"</span> | awk '{print $4}'`

            <span class="code-keyword">if</span> [ <span class="code-quote">"$1"</span> = <span class="code-quote">"eject"</span> ] ; then
                <span class="code-keyword">if</span> [ <span class="code-quote">"$DVDSTATE"</span> = <span class="code-quote">"<span class="code-keyword">false</span>"</span> ] ; then
                    xe vm-cd-eject uuid=$VM
                    echo <span class="code-quote">"$VMNAME : DVD/ISO ejected"</span>
                fi
            fi
        
            <span class="code-keyword">if</span> [ <span class="code-quote">"$1"</span> = <span class="code-quote">"dry"</span> ] ; then
                <span class="code-keyword">if</span> [ <span class="code-quote">"$DVDSTATE"</span> = <span class="code-quote">"<span class="code-keyword">false</span>"</span> ] ; then
                    echo <span class="code-quote">"$VMNAME : DVD/ISO attached"</span>
                <span class="code-keyword">else</span>
                    echo <span class="code-quote">"$VMNAME : empty"</span>
                fi
            fi
        fi
    done
    
<span class="code-keyword">else</span>
    echo all-dvd-eject.sh:
    echo <span class="code-quote">"  error: Missing parameter."</span>
    echo <span class="code-quote">"  usage: all_dvd_eject [eject|dry]"</span>
fi

echo


The script can be run with two different options: dry or eject.

 


Dry-Option:

    ./all-dvd-eject.sh dry

If you run the script with the dry option it will list all virtual machines in a pool and show you which vm has mounted something at the DVD drive. It doesn’t matter if there is mapped the local DVD drive or a ISO file from a iso repository.

The output will look like:

[root@jbxs01 ~]# ./all-dvd-eject.sh dry

Test-VM1 : empty
essentials : DVD/ISO attached
dc2 : empty
VLA_Router_v20090209 : empty
datacore01 : empty
xenapp2 : empty
ddc : empty
xenapp3 : empty
Nexenta : empty
xenapp1 : empty
Master : empty
Ubuntu : empty
WindowsXP-1 : empty
NetApp : empty
pvs01 : empty
NFS-Server : empty
Test-VM2 : empty
WindowsXP-1 : empty
jbpc02 : empty
NetScaler : DVD/ISO attached

[root@jbxs01 ~]#

 

Eject-Option:
    ./all-dvd-eject.sh eject

If you run the script with the eject option it will walk trough all virtual machines in a pool and look if there is mounted something at the DVD drive. If so the script ejects the mounted DVD/ISO

The output will look like:

[root@jbxs01 ~]# ./all-dvd-eject.sh eject

essentials : DVD/ISO ejected
NetScaler : DVD/ISO ejected

[root@jbxs01 ~]#






I hope this script will help anyone managing his environment. Therefore that this is just a small example how to manage the DVD drives feel free to take this and change it to your own way. Please keep in mind that you have to test everything before you will use it in a productive environment.

Have fun…

          Jens