Linux – How to make a USB visible in LXD Container

containerlinuxlxdusb

I am fairly new to LXD Conatiners. I have a Host ubuntu 16.04 machine running which has a single container :

root@ubuntu-blade1:/etc/udev/rules.d# lxc list
+-------------+---------+----------------------+------+------------+-----------+
|    NAME     |  STATE  |         IPV4         | IPV6 |    TYPE    | SNAPSHOTS |
+-------------+---------+----------------------+------+------------+-----------+
| nish-ubuntu | RUNNING | 10.20.186.185 (eth0) |      | PERSISTENT | 0         |
+-------------+---------+----------------------+------+------------+-----------+
root@ubuntu-blade1:/etc/udev/rules.d# 

I have a attached USB device to the main host system :

Disk /dev/sde: 14.7 GiB, 15795748864 bytes, 30851072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0527cd14

The lsusb output is as follows :

root@ubuntu-blade1:/etc/udev/rules.d# lsusb
Bus 002 Device 005: ID 8564:1000 Transcend Information, Inc. JetFlash
Bus 002 Device 004: ID 0424:2512 Standard Microsystems Corp. USB 2.0 Hub
Bus 002 Device 003: ID 0424:2512 Standard Microsystems Corp. USB 2.0 Hub
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 005: ID 0624:0251 Avocent Corp. Virtual Mass Storage
Bus 001 Device 004: ID 0624:0249 Avocent Corp. Virtual Keyboard/Mouse
Bus 001 Device 003: ID 0624:0248 Avocent Corp. Virtual Hub
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Now i need to detect/attach the usb (/dev/sde ) inside the container. I am having a little context of udev and seen a couple of stack question on same issue but none have a clear procedure to do so. Can someone please guide ?

Best Answer

This question is quite old, but I stumbled upon the same use case today and I found a solution so easy that probably it wasn't available 7 months ago.

At this link one can find more information, but essentially it is this way:

let's say we have a modem device (AT commands) on /dev/ttyUSB2 and a container "xenial1", one can add ttyUSB2 to the container with the following command:

$ lxc config device add xenial1 ttyUSB2 unix-char path=/dev/ttyUSB2

it does not matter whether the container is running or not

This configuration is permanently saved in the container's default profile:

$ lxc config show xenial1
architecture: x86_64
config:
  security.privileged: "true"
  volatile.base_image: <numbers>
  volatile.eth0.hwaddr: xx:xx:xx:xx:xx:xx
  volatile.idmap.base: "0"
  volatile.idmap.next: '[]'
  volatile.last_state.idmap: '[]'
  volatile.last_state.power: RUNNING
devices:
  root:
    path: /
    type: disk
  ttyUSB1:
    path: /dev/ttyUSB1
    type: unix-char
  ttyUSB2:
    path: /dev/ttyUSB2
    type: unix-char
ephemeral: false
profiles:
- default

In order to delete a device, use "remove" command in place of "add".

NOTE: my container is "privileged", so this solution might not work on unpriviledged containers

Related Question