How to Attach USB Drive to PV Virtual Machine in Xen Project, Part 2 – LVM

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:

As the first USB pass-through method was discussed in the previous post, we’re now on to the second method.

Using LVM and Passing Through Previously Unformatted Drive

Although passing-through a USB drive to a virtual machine using LVM is overkill in most situations, the configuration described below may be helpful in very specific cases. For example, LVM configuration may be useful when distributing backup storage from a high capacity external USB drive to multiple virtual machines.

If it makes sense to use LVM in your particular setup, it’s important to emphasize that the LVM method provided in this guide is designed for USB drives that do not contain data. If you want to pass-through a drive with data and/or an existing filesystem, I’ve found the first method in this series to be the easiest and safest way.[1]

Caution! Adding a USB drive to LVM with data on it may result in complete loss of that data.

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:

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.

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[2] 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. Configure USB Drive with LVM

In this step, we add the USB drive to LVM using the command pvcreate, then create a volume group and logical volume on the drive using vgcreate and lvcreate. Of course, all this in done in Dom0 because we haven’t passed the drive to the DomU yet.

For more information on LVM, check out A Quick Guide for Configuring LVM.

Using the device name we found in the output of lsblk, above, add the USB drive to LVM as a physical volume:

# pvcreate /dev/sdb

Now we can create a volume group on the physical volume. For the purpose of this tutorial, the volume group name is “vg3”. Choose a volume group name that suits your purposes.

# vgcreate vg3 /dev/sdb

And finally, from the vg3 volume group, we create a 500 megabyte logical volume named “smbtest”:

# lvcreate --name smbtest --size 500M vg3

To check that everything is configured properly, the following commands provide views of LVM physical volumes, volume groups, and logical volumes. All LVM commands are executed with sudo or as root user.

# pvs

# vgs

# lvs

3. Add Filesystem to the Logical Volume

Now we can create a filesystem on the 500 megabyte logical volume created using LVM. If you skip this step, you won’t be able to mount the drive in the DomU, even if it’s successfully passed-through. This command is executed from Dom0:

# mkfs.ext4 /dev/vg3/smbtest

The above command uses the mke2fs utility to create an ext4 filesystem on the block device located in volume group “vg3” with the name “smbtest”. Make sure to change the volume group and logical volume names to match your particular configuration.

4. Update the DomU Configuration File

Now that we’ve created the logical volume to pass to the DomU, we can update the DomU’s configuration file “disk” section to include the logical volume’s 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 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        = [

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        = [

Save and close the configuration file.

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

If the DomU you’re passing the logical volume 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 logical volume 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 logical volume in the resulting output, for example:

xvda1 202:1    0     4G  0 disk /
xvda2 202:2    1   500M  0 disk 

If the passed-through disk 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 + ]”.

6. Mount the Logical Volume in DomU

To test out the logical volume 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


Like the first method, the second is also just a standard way of passing any drive to a DomU virtual machine – but with an added layer of abstraction provided by LVM.

Given the similarity between the two methods, the main drawback of the second method is also all of the manual configuration: configuring the USB drive through LVM, editing the DomU configuration file, creating the virtual machine, and mounting the drive. And it’s worth repeating that the required manual configuration makes this method more of a permanent solution versus the convenient plug-n-play use typically desired from USB drives.

It differs from the first method in a few small ways. First, there’s the added drawback that, unless the drive was previously configured with LVM, the second method is only for empty USB drives. And second, on the positive side, there is a benefit in being able to use LVM to easily slice up the USB drive space, especially if you need to allocate storage to multiple virtual machines.

Finally, the second method comes with the same qualification as provided with the first method. For some Xen Project users, many of these manual configuration steps can be avoided through the use of the xl usbctrl-attach and xl usbdev-attach commands, but only if the required kernel modules are installed. These commands are explored in the third and final part of this series.


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. Otherwise, that might have been the easiest method.

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.