9

Eject all DVDs from XenServer VMs

http://community.citrix.com

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 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

 

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.

regards, Jens

View Online
|
Add Comment


Read »
thealth's picture
Created by thealth 30 weeks 5 days ago – Made popular 30 weeks 6 days ago
Category: Citrix   Tags: