Archive for May, 2012

Mapping internal (SATA, SAS, RAID, etc) disks from XenServer host to VM

Thursday, May 17th, 2012

In my post here, I have explained (actually – created a shell script) to map USB disks to VMs directly. While this is easy and simple, it becomes more challenging when you want to map internal SATA disks. They are not attached to the “Removable Storage” SR, and thus, behave differently.

The solution is to make them part of the “Removable Storage” group. This can be performed by adding the following two lines at the bottom of the XenServer’s /etc/udev/rules.d/50-udev.rules


ACTION=="add", KERNEL=="sdb", SYMLINK+="xapi/block/%k", RUN+="/bin/sh -c '/opt/xensource/libexec/local-device-change %k 2>&1 >/dev/null&'"
ACTION=="remove", KERNEL=="sdb", RUN+="/bin/sh -c '/opt/xensource/libexec/local-device-change %k 2>&1 >/dev/null&'"

Replace sdb with the device, as can be found using `cat /proc/partitions` (that way you can get the exact size, and compare it to what you expect to see). In this particular case, the device ‘sdb’ will be added to the “Removable Storage” group and then it’s all easy – just like I have described in my previous post.

I have had a great reference from here

Attach USB disks to XenServer VM Guest

Saturday, May 5th, 2012

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, , with the concepts
# taken from
# and

# 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