📜 ⬆️ ⬇️

Forwarding a USB printer to an LXD container

I want to share the found solution on forwarding the HP LaserJet 1000 printer to a container created with LXD.

A bit of background


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

The printer has been reset 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. That's what I want to share.

We start probros


So, Ubuntu 16.04 + LXD is installed, a container based on the same Ubuntu 16.04 is created, proceed to forwarding.
First, find out where the printer is connected to 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
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 are 0664. This information will be useful to us in the future.
Find out what the numeric identifier of the group lp:

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

The lp group has a numeric identifier of 7.
Now we’ll find out who owns and with what permissions 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 group lp with the rights 0660.

Unfortunately, it was not possible to get rid of the USB bus under the spoiler.
The search continues ...
I will leave for history:
UPD 03/07/2017
UPD 03/07/2017 Before forwarding, apply the trick in the form of binding devices with udev.
In my case, the printer is connected one, so the binding is produced in a convenient way for me. If you want to do something similar on your system, keep in mind that everything may be a little different for you, but, as they say, the main thing is to capture the essence.

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

10-printer.ru
 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 break the lines in your rules, as udev interprets your one rule as several rules, and will not work as expected.
(For more detailed information on writing udev rules, I recommend referring here or to the original here )

This rule allows you to create two symbolic links lj1000 and lp0 , which are necessary for further correct transfer of the printer to the container and its normal operation regardless of the connection bus on the host system.


Forward the printer to the container


In fact, everything was not very hard. In my case, the container is called print, and I decided to call the device lj1000. For forwarding you need to perform several 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/07/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 performs forwarding of a unix-char device with rights 0664 and belonging to group 7 (lp), the second command performs forwarding of a device lp0 with rights by default (0660) and belonging to the same group 7.

That's all. Forecast device implemented. 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, just follow the installer instructions. The result should be a printed print test page.

That's all, in this way the printer is thrown into the inside of the container created with LXD. I think that any other device is being forwarded in a similar way.

Thank you all for your attention.

UPD: 09/03/2017 Unfortunately everything turned out to be not so simple and the described udev rules do not allow getting rid of the USB bus. The search continues ...
UPD: 07/03/2017 In the comments there was a question about the binding of the device to the USB bus, in order not to lose the printer if it is accidentally or intentionally reconnected to another connector or reconnected to power. Frankly speaking, for me this question became very topical, so I decided to supplement the article with additional instructions that would allow this trouble to be avoided, as well as correct the commands in the article itself so as not to rewrite everything anew.

Source: https://habr.com/ru/post/306074/


All Articles