How to Attach USB Drive to PV Virtual Machine in Xen Project, Part 1 – .cfg

In this series of posts, we look at four methods of attaching or passing-through a USB drive to a Xen Project paravirtualized (“PV”) virtual machine.

The four methods discussed in this series include:

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

As with any block device listed within Dom0, a USB drive can also be manually passed-through to a DomU via the DomU’s configuration file.

Note that, if a running DomU’s configuration file is manually changed, the virtual machine will need to be shut down and re-created for the changes to take place. Given this requirement, any pass-through method requiring modification of the DomU .cfg file should not be considered a USB drive “hot-plug” solution.

The manual procedure described below will work with drives containing existing filesystems and data as well as with unformatted USB drives. Regardless of the scenario, you’ll need a filesystem on the drive if you plan on mounting it within the DomU.

For more information on creating filesystems and partitions on your USB drive, check out Linux/GNU utilities mkfs and fdisk.

1. Locate USB Drive Information

Before plugging in the USB drive, check to 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 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[1] on the Dom0 used for this tutorial. Make sure to change the commands below to match the block device name found on your computer.

2. Update the DomU Configuration File

In this step, we update the DomU’s configuration file disk section to include the USB drive device name you found in the output of lsblk.

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 Dom0 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 cached version of the DomU configuration file and not check for configuration changes.

# xl shutdown {name-of-domu}

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

# xl create /etc/xen/name-of-domu.cfg -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

The method described above is a standard way of passing any type of drive to a DomU virtual machine by modifying its configuration file. This method is applicable whether the drive is 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 DomU’s configuration file, shutting down and re-creating the virtual machine, and mounting the drive within the DomU. The required manual configuration makes this method more of a permanent solution versus the convenient plug-n-play use typically desired from USB drives. On the other hand, manual configuration enables customization for your individual needs.

Depending on the scenario, some of the above-mentioned manual configuration steps can be avoided through the use of the fstab file, and/or the xl usbdev-attach or xl block-attach commands. These xl tools are explored in the third and fourth methods, whereas the second method examines another configuration file method using LVM to allocate USB drive space.


Notes:

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