| |

Attach USB disks to XenServer VM Guest

There is a very nice script for Windows dealing with attaching XenServer USB disk to a guest. It can be found here.

This script has several problems, as I see it. The first – this is a Windows batch script, which is a very limited language, and it can handle only a single VDI disk in the SR group called “Removable Storage”.

As I am a *nix guy, and can hardly handle Windows batch scripts, I have rewritten this script to run from Linux CLI (focused on running from the XenServer Domain0), and allowed it to handle multiple USB disks. My assumption is that running this script will map/unmap *all* local USB disks to the VM.

Following downloading this script, you should make sure it is executable, and run it with the arguments “attach” or “detach”, per your needs.

And here it is:

# This script will map USB devices to a specific VM
# Written by Ez-Aton, http://run.tournament.org.il , with the concepts
# taken from http://jamesscanlonitkb.wordpress.com/2012/03/11/xenserver-mount-usb-from-host/
# and http://support.citrix.com/article/CTX118198

# Variables
# Need to change them to match your own!
DEVICE_NAMES="hdc hde" # Local disk mapping for the VM

function attach() {
        # Here we attach the disks
        # Check if storage is attached to VBD
        VBDS=`$XE vdi-list sr-uuid=${REMOVABLE_SR_UUID} params=vbd-uuids --minimal | tr , ' '`
        if [ `echo $VBDS | wc -w` -ne 0 ]
                echo "Disks are allready attached. Check VBD $VBDS for details"
                exit 1
        # Get devices!
        VDIS=`$XE vdi-list sr-uuid=${REMOVABLE_SR_UUID} --minimal | tr , ' '`
        for i in $VDIS
                VBD=`$XE vbd-create vm-uuid=${VM_UUID} device=${DEVICE_NAMES[$INDEX]} vdi-uuid=${i}`
                if [ $? -ne 0 ]
                        echo "Failed to connect $i to ${DEVICE_NAMES[$INDEX]}"
                        exit 2
                $XE vbd-plug uuid=$VBD
                if [ $? -ne 0 ]
                        echo "Failed to plug $VBD"
                        exit 3
                let INDEX++

function detach() {
        # Here we detach the disks
        VBDS=`$XE vdi-list sr-uuid=${REMOVABLE_SR_UUID} params=vbd-uuids --minimal | tr , ' '`
        for i in $VBDS
                $XE vbd-unplug uuid=${i}
                $XE vbd-destroy uuid=${i}
        echo "Storage Detached from VM"
case "$1" in
        attach) attach
        detach) detach
        *)      echo "Usage: $0 [attach|detach]"
                exit 1



Similar Posts


  1. Thanks very much for posting this – I have several Xenservers on which I have external USB drives used to back up VMs, both Linux and Windows hosts and I’m working on the exact same scenario you describe.
    I’ve got my Xenserver 6 hosts autostarting the vms I want to autostart – just setting the parameters did not work, had to start them using a command in /etc/rc.d/rc.local

    I’ve tried your script, but even when the USB is detached I’m getting “Disks are allready attached. Check VBD vbd-uuids (SRO) : for details. I’m not a complete neophyte with bash scripting but I’m not at your level.

    The script wants to run but I’m not sure what to use for variable DEVICE_NAMES
    The local disk mapping – is that the local device for the Xenserver host or the vm?
    On my Xenserver, the device name of the USB drive is /dev/sdb and the partition (formatted on the vm guest) is /dev/sdb1
    On the vm guest, which is an Ubuntu 10.04 server, the device is /dev/xvdc and the partition is /dev/xvdc1

    What values should I be using for DEVICE_NAMES ?
    This is a very helpful and timely post. Grazie!

    What should be the device names on my

  2. I got it working. Now I understand I needed to put the disk ID (xvdc) on the vm into the script.
    It works perfectly. Kudos and many thanks!

  3. Apologies for a third post in such a short time. I thought I’d describe the scenario I’ve been dealing with on my Xenservers.
    We have a number of Xenservers with external USB drives connected to guest vms – the external USB drives are used to back up data from the VM guest attached.
    One thing I’ve noticed is that when there is a power failure or the Xenserver goes down, several issues occurred:

    1. Guest vms did not autostart on Xenserver 6.
    2. External USB drives were not seen by the Xenserver.
    3. The External USB drive was not attached to the guest VM when the Xenserver rebooted.

    Problem 1 we attempted to solve by adding autostart to the vm parameters. This was unsuccessful. We had to add a start script:
    export PATH=/opt/xensource/bin
    sleep 50
    xe vm-start uuid=

    Problem 2 was solved by adding commands to /etc/rc.d/rc.local to unload and reload the usb storage drivers:
    modprobe -r usb_storage
    sleep 7
    modprobe usb_storage

    Problem 3 was solved by adding your script to /etc/rc.d/rc.local with the attach argument

    Note that the modprobe commands and your script need to be in rc.local before the script to start the vm.

    With attribution and a link to your post, I’ll be posting this to Citrix’s Xenserver forums.

    Many thanks for your script – it was an important piece of the solution.

    Problem 2
    sleep 5
    modprobe usb_storage

  4. One quick question – if the guest is a Windows guest, would the DEVICE_NAME variable be set to hdx, where x is the actual hard disk device reported by the Windows guest?

  5. Sorry for the delay in my approval of the comments and the answers. You seem to have made quite a good job yourself 🙂

    In XenServer 6, there is a different method of autostarting the VMs. I have no idea as to why the USB device driver did not detect the disks, but I did not test it just yet.
    about the guest VMs in Windows – yes. You should use “hdb”, “hdc” and so on. Skip “hdd” as it is used for the cdrom (if exists) of the VM.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.