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

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:

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 discussed below 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, unless the drive is already configured with LVM, you’re better off choosing any of the other methods described in this series.

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:

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. 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        = [
                  '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/vg3/smbtest,xvda2,w',
              ]

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 {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 logical volume 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   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

Conclusion

Like the first method, the second is also just a standard way of passing any type of 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, shutting down and re-creating the virtual machine, and mounting the drive within the DomU. 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.

The second method also differs from the first 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, the use of LVM makes slicing up the USB drive space easier, 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. 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. The final two parts of this series explore these commands.


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.