Back to Home

Forward USB printer to LXD container

LXD · LXC · Linux · Ubuntu

Forward USB printer to LXD container

I want to share the solution I found for sending the HP LaserJet 1000 printer to a container created using LXD.

A little background


There is a home server based on the old Acer Aspire 5520G laptop, which is used for all kinds of experiments. Ubuntu 14.04 was installed on it and several containers were created using LXC, one of which was used as a print server.

The printer was forwarded by adding the following lines to the guest configuration file:

lxc.cgroup.devices.allow = c 189:* rwm
lxc.mount.entry = /dev/bus/usb/003 dev/bus/usb/003 none bind,optional,create=dir
lxc.mount.entry = /dev/usb/lp0 dev/usb/lp0 none bind,optional,create=file

Everything worked fine, but I wanted to upgrade to Ubuntu 16.04 and try LXD.

It turned out that the old configuration files do not work and you need to look for a new solution. Here I also want to share it.

Getting to Forward


So, Ubuntu 16.04 + LXD are installed, a container based on the same Ubuntu 16.04 is created, proceed to forwarding.
First, find out where the printer is connected on the host:

root@aspire-5520g:~# lsusb
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 004: ID 0bda:8197 Realtek Semiconductor Corp. RTL8187B Wireless Adapter
Bus 001 Device 003: ID 5986:0102 Acer, Inc Crystal Eye Webcam
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 002: ID 03f0:0517 Hewlett-Packard LaserJet 1000
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

The printer is connected to Bus 003 Device 002, in other words, it corresponds to the unix-char file / dev / bus / usb / 003/002
Let’s find out who owns it and with what rights:

root@aspire-5520g:~# ls -l /dev/bus/usb/003/002
crw-rw-r-- 1 root lp 189, 257 Июл 15 16:02 /dev/bus/usb/003/002

The file belongs to the root user and the lp group, the rights to the file 0664. This information will be useful to us in the future.
Find out what the numerical identifier of the lp group is:

root@aspire-5520g:~# cat /etc/group | egrep lp
lp:x:7:

The lp group has the numeric identifier 7.
Now we find out who owns and with what rights, the lp0 file:

root@aspire-5520g:~# ls -l /dev/usb/lp0
crw-rw---- 1 root lp 180, 0 Июл 15 16:02 /dev/usb/lp0

The file belongs to the root user and the lp group with the rights 0660.

Unfortunately, I couldn’t get rid of the USB bus using the spoiler.
The search continues ... I
'll leave it for the story:
UPD 03/03/2017
UPD 03/03/2017 Before forwarding, we apply the trick in the form of binding devices using udev.
In my case, the printer is connected alone, so I’ll bind it in a convenient way for me. If you want to do this on your system, keep in mind that everything can be a little different for you, but as they say, the main thing is to catch the point.

To perform the forwarding, regardless of the connection bus, on the host machine ( not in the container! ) We will create a file: of the /etc/udev/rules.d/10-printer.rulesfollowing content:

10-printer.rules
SUBSYSTEM=="usb", ATTR{manufacturer}=="Hewlett-Packard", ATTR{product}=="hp LaserJet 1000", SYMLINK+="lj1000"
KERNEL=="lp[0-9]", SUBSYSTEM=="usbmisc", SYMLINK+="%k"


Remember that udev does not support line wrapping in any form. Do not allow line breaks in your rules, as udev interprets your single rule as multiple rules, and will not work as expected.
(For more detailed information on writing udev rules, I recommend contacting here or to the original here )

This rule allows you to create two symbolic links lj1000and lp0are necessary for further correct forwarding of the printer to the container and its normal operation regardless of the connection bus on the host system.


We will forward the printer to the container


In fact, everything turned out to be not strong and complicated. In my case, the container is called print, and I decided to call the device lj1000. To forward, you need to perform a few simple manipulations:

root@aspire-5520g:~# lxc config device add print lj1000 unix-char path=/dev/bus/usb/003/002 mode=0664 gid=7
Устройство lj1000 добавляется к print
root@aspire-5520g:~# lxc config device add print lp0 unix-char path=/dev/usb/lp0 gid=7
Устройство lp0 добавляется к print

UPD 03/03/2017
root@aspire-5520g:~# lxc config device add print lj1000 unix-char path=/dev/lj1000 mode=0664 gid=7
Устройство lj1000 добавляется к print
root@aspire-5520g:~# lxc config device add print lp0 unix-char path=/dev/lp0 gid=7
Устройство lp0 добавляется к print


The first command forwards the unix-char device with the rights 0664 and belonging to group 7 (lp), the second command performs the forwarding of the lp0 device with default rights (0660) and belonging to the same group 7.

That's all. The device is forwarded. Next, you need to install the printer in the container:

root@aspire-5520g:~# lxc exec print -- apt update && apt upgrade -y && apt install hplip -y
root@aspire-5520g:~# lxc exec print -- hp-setup -i

Here we simply follow the instructions of the installer. The result should be a printed test print page.

That's all, that's how the printer is forwarded inside the container created using LXD. I think that any other device is forwarded in a similar way.

Thank you all for your attention.

UPD: 03/09/2017 Unfortunately everything turned out to be not so simple and the described udev rules do not allow to get rid of the USB bus. The search continues ...
UPD: 03/07/2017In the comments, the question arose about tying the device to the USB bus, in order not to lose the printer if it was accidentally or intentionally reconnected to another connector or reconnected by power. To be honest, for me this issue has become very relevant, so I decided to supplement the article with additional instructions that will help to avoid this trouble, as well as correct the commands in the article itself so as not to rewrite everything again.

Read Next