HOWTO Connect a USB drive to a Xen Guest (DomU)
From Consultancy.EdVoncken.NET
Contents |
In Xen, hardware devices are usually managed by the privileged domain, Dom0.
You can connect a USB drive to a Xen guest (DomU) using the "xm block-attach" feature. Inside the DomU, the device will appear as a standard (non-USB, pluggable) device. You need to take special care when disconnecting the USB drive.
How to connect the USB drive
- You must be root on both the Dom0 and the DomU, or it won't work
- Connect the USB drive to your computer as usual (in this example, a factory-formatted USB drive)
- Do NOT mount the drive on Dom0! If you are running a graphical desktop on Dom0, make sure the drive is not automatically mounted. We will be mounting the drive from DomU only!
Attach the device to your DomU
- Log on to your Dom0 and look at the dmesg or fdisk -l output. Determine the device name given to your USB drive (in this example, /dev/sde).
[root@dom0]# fdisk -l
...
Disk /dev/sde: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sde1 1 121601 976760001 c W95 FAT32 (LBA)
- Choose an unused device name on the Xen guest (in this case, I'll choose /dev/sda on Xen guest "colossus")
- Issue the following command to connect the device to your Xen guest (DomU):
[root@dom0]# xm block-attach colossus phy:sde /dev/sda w
This will attach the USB drive in Read/Write mode. For Read Only mode, specify "r" instead of "w" on the commandline.
Mount the device in your DomU
- Log on to your DomU and look at the dmesg or fdisk -l output to verify that the device is available.
[root@domU]# fdisk -l
...
Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 1 121601 976760001 c W95 FAT32 (LBA)
- Mount the device on your DomU (in this example, on /mnt):
[root@domU]# mount /dev/sda1 /mnt
How to disconnect the USB drive
Before physically disconnecting the USB device from Dom0, you need to detach it from the DomU first. Otherwise, you may lose your data!
Unmount the device in your DomU
Log on to your DomU and unmount the device:
[root@domU]# umount /mnt
Verify that the device has been unmounted properly before proceeding.
Detach the device from your Dom0
You would probably expect to run an "xm block-detach" command similar to the "xm block-attach" command above. Unfortunately, the "block-detach" command expects a device ID instead of the friendly device name (phy:sde).
According to the manpage, you are expected to run the "xm block-list" command to retrieve the device ID - but it doesn't show you the device name!
[root@dom0]# xm block-list colossus Vdev BE handle state evt-ch ring-ref BE-path 51712 0 0 4 11 8 /local/domain/0/backend/vbd/1/51712 2048 0 0 4 14 1421 /local/domain/0/backend/vbd/1/2048
It turns out that it is far easier to just grep the Xen logfile for the information:
[root@dom0]# grep sde /var/log/xen/xend.log
[2009-05-15 17:27:51 xend 5105] DEBUG (DevController:112) DevController: writing {'domain': 'colossus',
'frontend': '/local/domain/1/device/vbd/2048', 'format': 'raw', 'dev': '/dev/sda', 'state': '1',
'params': 'sde', 'mode': 'w', 'online': '1', 'frontend-id': '1', 'type': 'phy'}
to /local/domain/0/backend/vbd/1/2048.
OK, we finally have the device ID (2048 in this example) - we can detach the device from DomU:
[root@dom0]# xm block-detach colossus 2048
Now, you can safely disconnect the USB drive from your computer.