How to Attach USB Drive to PV Virtual Machine in Xen Project

In this series of posts, we look at three methods of attaching or passing-through a USB drive to a Xen Project hypervisor PV virtual machine.

The three methods discussed in this series include:

  • passing through a formatted USB drive containing an existing filesystem;
  • using LVM and passing through a previously unformatted drive; and,
  • using the xl usbctrl-attach and xl usbdev-attach commands.

The first method is discussed below, while the others are presented in subsequent posts (linked above).

Passing Through Formatted USB Drive Containing an Existing Filesystem

With my particular setup of Xen Project hypervisor on Debian, the method described below is the easiest and safest way to attach a USB drive containing an existing filesystem.[1]

Note that this method also works using an unformatted USB drive. However, you’ll likely need a filesystem on the drive if you plan on using it within the DomU. Check out Linux/GNU utilities mkfs for creating filesystems, and fdisk for creating partitions.

1. Locate USB Drive Information

Before we plug in the USB drive, let’s see what block devices are available on the Dom0 virtual machine using the lsblk command:

$ lsblk

You should see output similar to the following, depending on your computer’s configuration:

NAME                  MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda                     8:0    0 119.2G  0 disk  
--sda1                  8:1    0   243M  0 part  /boot
--sda2                  8:2    0     1K  0 part  
--sda5                  8:5    0   119G  0 part  
  --sda5_vg0          254:0    0   119G  0 lvm 
    --vidi--vg-root   254:1    0 111.4G  0 lvm   /
    --vidi--vg-swap_1 254:2    0   7.6G  0 lvm   [SWAP]
sr0                    11:0    1  1024M  0 rom   

Now we’ll plug in the USB drive and run lsblk again to get the drive’s device name. You should see the output now includes a new device.

NAME                  MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda                     8:0    0 119.2G  0 disk  
--sda1                  8:1    0   243M  0 part  /boot
--sda2                  8:2    0     1K  0 part  
--sda5                  8:5    0   119G  0 part  
  --sda5_vg0          254:0    0   119G  0 lvm 
    --vidi--vg-root   254:1    0 111.4G  0 lvm   /
    --vidi--vg-swap_1 254:2    0   7.6G  0 lvm   [SWAP]
sdb                     8:16   1   980M  0 disk
sr0                    11:0    1  1024M  0 rom

Note that the new device “sdb” appeared on the Dom0 used for this tutorial.[2] Make sure to change the commands below to match the block device name found on your computer.

2. Update the DomU Configuration File

Next, we’ll update the DomU’s configuration file “disk” section to include the USB drive’s device name. If you don’t have a DomU configured yet, have a look at the tutorial Using xen-tools to Create DomU Virtual Machines in Xen Project Hypervisor.

Assuming that you have a DomU ready to go, and that the configuration file is located in the standard directory “/etc/xen/”, open the DomU’s “.cfg” file using the nano editor.

# nano /etc/xen/name-of-domu.cfg

Here’s an example of the “disk” section before:

root        = '/dev/xvda1 ro'
disk        = [
                  'phy:/dev/vg0/name-of-domu-disk,xvda1,w',
              ]

And here’s how it looks after the USB drive configuration information is added (don’t forget the trailing comma):

root        = '/dev/xvda1 ro'
disk        = [
                  'phy:/dev/vg0/name-of-domu-disk,xvda1,w',
                  'phy:/dev/sdb,xvda2,w',
              ]

Save and close the configuration file.

3. Shutdown/Create the DomU and Check for USB Drive

If the DomU you’re passing the USB drive to is currently running, use the xl shutdown command to shut it down.

Note that I tried running the xl reboot command, instead of xl shutdown, but the USB drive was not successfully passed through to the DomU. Using xl reboot may use a cashed version of the DomU configuration file and not check for configuration changes.

# xl shutdown {DomU-name}

Now we’ll start the DomU virtual machine, and pass the console to the DomU with the “-c” option:

# xl create {DomU-name} -c

Login to the DomU virtual machine when prompted.

Now run the lsblk command while logged in to the DomU. You should see the USB drive in the resulting output, for example:

NAME  MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
xvda1 202:1    0     4G  0 disk /
xvda2 202:2    1   980M  0 disk 

If the USB drive shows up in the DomU, you should be good to go. If not, make sure you actually shut down the DomU after you edit its configuration file, and verify that the “disk” section is properly filled out.

Quick Tip: If you’re in DomU and want to pass the terminal back to Dom0, use the key combination “Ctrl + ]”.

4. Mount the USB Drive in DomU

To test out the USB drive using the DomU virtual machine, you can either temporarily mount the drive to the DomU’s “/mnt/” directory using the following command, or mount it permanently within the DomU by editing the “/etc/fstab” file:

# mount /dev/xvda2 /mnt

Conclusion

That’s about it for this method. It’s really just a standard way of passing any drive to a DomU virtual machine whether it’s an internal hard drive, detachable USB drive, or abstracted drive like LVM.

In my opinion, the main drawback of this particular method is all of the manual configuration: editing the configuration file, creating the virtual machine, and mounting the drive. The required manual configuration makes this method more of a permanent solution versus the convenient plug-n-play use typically desired from USB drives.

For some Xen Project users, these manual configuration steps may be avoided through the use of the xl usbctrl-attach and xl usbdev-attach commands, but only if the required kernel modules are installed.


Notes:

1. However, as you’ll see in the last method discussed, there’s a caveat: the xl commands usbctrl-attach and usbdev-attach did not work with my current Xen Project setup due to missing kernel modules.

2. Note that my Dom0 did not recognize the USB drive when I first plugged it in: the USB drive didn’t show up when I ran lsblk. After some unfruitful research, I shutdown and restarted Dom0 with the USB drive plugged in and it magically fixed the problem.