mviereck / x11docker

Run GUI applications and desktops in docker and podman containers. Focus on security.
MIT License
5.62k stars 378 forks source link

Support for printer / cups sharing #73

Closed sandrokeil closed 6 years ago

sandrokeil commented 6 years ago

Would be nice if there is a flag to share the printer from host system. I've tried it by sharing of USB device but I guess the printer must be installed in Docker container. Do you have any idea if it is possible to share some directories/services like GPU/audio to be able to print documents e.g. from a Libre Office Docker container?

To determine the USB device (e.g. 04a9:10a5) you have to find out the printer id with lsusb (Arch Linux), then you can use this script to get all related devices. I guess hidraw is not needed for printer, but for other USB devices.

#!/usr/bin/env bash
USB_ID='04a9:10a5'
USB_NAME='Canon'

if lsusb -d $USB_ID > /dev/null; then
  BUS=$(lsusb -d $USB_ID | cut -d' ' -f 2)
  DEVICE=$(lsusb -d $USB_ID | cut -d' ' -f 4 | tr -d ':')
  DOCKER_DEVICE="--device=/dev/bus/usb/$BUS/$DEVICE "

  while read line _; do
    if cat /sys/class/hidraw/$(basename $line)/device/uevent | grep "${USB_NAME}" > /dev/null; then
      DOCKER_DEVICE="${DOCKER_DEVICE} --device=$line"
    fi
  done <<< "$(ls /dev/hidraw*)"
fi

echo $DOCKER_DEVICE

Maybe this script can be simplified too.

mviereck commented 6 years ago

An interesting idea to share printers with containers.

I assume it will be better to use cups server on host instead of sharing the hardware. Printers can be connected on several different ways to host. Printers on USB are only one case. cups on host will already be configured for existing printers on USB, parallel port, WLAN or LAN. Also it provides a virtual PDF printer.

I am not familar with cups, but on a first glance it should be possible to access cups on host over network/IP. The container would only need package cups-client (debian) and x11docker could generate and share a config file. (Similar to pulseaudio over TCP setup).

I will look at this, and maybe you can also find out possibilities to use cups. Is cups the default printer server on arch?

sandrokeil commented 6 years ago

Is cups the default printer server on arch?

I think so, see Arch Wiki CUPS. Yes, sharing via cups server should be easier than via USB/device. I've configured cups and printer on my host system. I've used cups and system-config-printer package. The Arch Wiki describes also how to use avahi or remote cups server.

mviereck commented 6 years ago

I did a first try sharing cups socket with --sharedir /run/cups/cups.sock. At least the virtual PDF printer was already visible, and also my (currently not connected) HP laser printer. /run/cups contains some more files, but maybe all we have to do is sharing that socket.

A bit confusing: man lpstat says:

       -H   Shows the server hostname and port.

But it gives me:

$ lpstat -H
/run/cups/cups.sock

That is nice as it shows me which socket to share. But it also may point to a network server and a port that x11docker may have to provide. The manpage does not mention that lpstat -H can show a socket instead of hostname+port.

sandrokeil commented 6 years ago

Confirmed :+1: using --sharedir /run/cups/cups.sock shows my not connected printer in libre office. Will try tomorrow to print a page ;-)

mviereck commented 6 years ago

I've added a new option --printer. Sharing cups socket already works fine. I still have to check for possible network setups.

$ lpstat -H
/run/cups/cups.sock
$ CUPS_SERVER=localhost lpstat -H
localhost:631
$ CUPS_SERVER=localhost x11docker --printer  # currently fails. IP is translated already.
sandrokeil commented 6 years ago

Thanks a lot. Cups socket sharing works for me. :+1:

mviereck commented 6 years ago

Sometimes it is easier than it seems. :-) I thought it would be much more complicated.

I also figured out how to configure cups to accept network connections. But I assume that will be a rare edge case. Though, it is supported, too.