LXD USB Flash Drives

This tutorial is about how to make a USB thumb drive available to a LXD Virtual Machine. I covered how to connect a USB drive to a LXD Container about a year ago in “LXD Containers Mount Host Folders”. This time I mostly focus on how to do this for a LXD Virtual Machine, although I do review how to do this for a LXD container also.

On your LXD Server, list out your USB devices:

lsusb

In my sample listing above, I highlighted an Apacer Technology, Inc. thumb drive. The important information here is that “1005” is the vendor ID for this device and “b113” is the product ID.

On my Ubuntu LXD VM named “ubuntu-vm”, the command to add this thumb drive to my LXD VM is executed from the command line on my LXD Server:

lxc config device add ubuntu-vm flashdrive usb vendorid=1005 productid=b113

The choice of the name “flashdrive” is arbitrary. So, you can pick a name that helps document your use case. The “usb” keyword is necessary.

Next, I connected to the GUI of “ubuntu-vm”:

lxc console ubuntu-vm --type=vga

In the screenshot below you can see the thumb drive displayed.

Back on the LXD Server, you can also remove the device from being associated with the LXD VM:

lxc config device remove ubuntu-vm flashdrive

To associate the flash drive with my Windows 11 LXD VM:

lxc config device add win11 flashdrive usb vendorid=1005 productid=b113

Now I attach to my Windows 11 GUI:

lxc console win11 --type=vga

You can see that the thumb drive is also present in Windows:

Back on the LXD Server, you can see the thumb drive:

df

We can list the thumb drive contents:

ls /media/scott/THUMBDRIVE

LXD Containers are different from LXD Virtual Machines. A LXD Container cannot access the thumb drive as a separate Linux device that can be mounted. I cover accessing USB devices in detail in “LXD Containers Mount Host Folders” and that goes in much more detail for LXD Containers accessing USB devices.

An example of a command to offer our thumb drive as a folder inside of a LXD container:

lxc config device add Test flashdrive disk source=/media/scott/THUMBDRIVE  path=/home/scott/thumbdrive

It’s important to point out that the “/home/scott/thumbdrive” folder was created in advance.

From inside of the LXD Container, I am now able to access the thumb drive:

By default, this folder is read-only as you can see from the touch command below.

image

To learn how to configure your LXD container for “write” access to the USB thumb drive folders, watch my video entitled “LXD Containers Mount Host Folders”.