Difference between revisions of "USB Printing in VE"

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search
 
(9 intermediate revisions by one other user not shown)
Line 3: Line 3:
 
Please note that ownership/permission/devices are referred to my system and you have to adapt to YOUR configuration: they are reported only as an EXAMPLE.
 
Please note that ownership/permission/devices are referred to my system and you have to adapt to YOUR configuration: they are reported only as an EXAMPLE.
  
* Install usbutils in both HN and VE.  
+
* Install '''usbutils''' in both HN and VE.  
 
* In a shell on HN type '''lsusb''' and you'll get the list fo the usb devices on your system:
 
* In a shell on HN type '''lsusb''' and you'll get the list fo the usb devices on your system:
 
   Bus 002 Device 006: ID 0dda:2026 Integrated Circuit Solution, Inc. USB2.0 Card Reader
 
   Bus 002 Device 006: ID 0dda:2026 Integrated Circuit Solution, Inc. USB2.0 Card Reader
Line 27: Line 27:
 
** a '''printer inside''' the /dev/usb directory: <code>crw-rw---- 1 root lp 180, 0 Nov 28 14:14 lp0</code>
 
** a '''printer inside''' the /dev/usb directory: <code>crw-rw---- 1 root lp 180, 0 Nov 28 14:14 lp0</code>
 
** a '''bus/usb''' directory
 
** a '''bus/usb''' directory
** '''many directories''' in bus/usb: named with the bus numer that you get with the first lsusb command. In this example the relevant one is '''008'''
+
** '''many directories''' in bus/usb: named with the bus number that you got with the first lsusb command. In this example the relevant one is '''008'''
 
** in the numbered subdirectory '''1 file for every usb device attached to this bus''', the first one is the usb port:
 
** in the numbered subdirectory '''1 file for every usb device attached to this bus''', the first one is the usb port:
 
***<code>crw-rw-r--  1 root usb  189, 896 Nov 24 16:16 001</code>
 
***<code>crw-rw-r--  1 root usb  189, 896 Nov 24 16:16 001</code>
***<code>crw-rw-r--  1 root usb  189, 902 Nov 28 14:14 007</code>
+
***<code>crw-rw-r--  1 root usb  189, 900 Nov 28 14:14 005</code>
* Now that you have an idea fo the general structure of your usb devices you have to replicate them on your VE.
+
* Now that you have an idea of the general structure of your usb devices you have to replicate them on your VE.
 +
** enter your VE: <code>vzctl enter VE_NUMBER</code>
 
** create in /dev (if not present) the usb directory
 
** create in /dev (if not present) the usb directory
 
** create in /dev/usb the printer device typing: <code>mknod lp0 c 180 0</code>
 
** create in /dev/usb the printer device typing: <code>mknod lp0 c 180 0</code>
Line 37: Line 38:
 
**** <code>chown root:lp lp0</code>
 
**** <code>chown root:lp lp0</code>
 
**** <code>chmod 660 lp0 </code>
 
**** <code>chmod 660 lp0 </code>
**** check with <code>ls -l lp0</code> if the new device is '''IDENTICAL''' to the one in HN --> <code>crw-rw---- 1 root lp 180, 0 Nov 28 14:14 lp0</code>
+
**** check with <code>ls -l lp0</code> if the new device is '''IDENTICAL''' to the one in HN --> <code>crw-rw---- 1 root lp 180, 0 Nov 27 13:51 lp0</code>
 +
** go back to /dev and create a symlink to the printer device: <code>ln -s /dev/usb/lp0 /dev/usblp0</code>
 +
** create all the needed directories:
 +
*** <code>mkdir /dev/bus</code>
 +
*** <code>mkdir /dev/bus/usb</code>
 +
*** <code>mkdir /dev/bus/usb/008</code>
 +
** switch to /dev/bus/usb/008 directory and create there the needed devices: one for the usb port and one for the printer
 +
*** <code>mknod 001 c 189 896</code>
 +
*** <code>mknod 005 c 189 900</code>
 +
** assign ownership/permissions:
 +
*** <code>chown root:usb *</code>
 +
*** <code>chmod 664 * </code>
 +
*** check everything: <code>ls -l</code> should give the same results as in HN:
 +
***<code>crw-rw-r--  1 root usb  189, 896 Nov 27 13:29 001</code>
 +
***<code>crw-rw-r--  1 root usb  189, 900 Nov 27 13:30 005</code>
 +
* '''Doublecheck everything!'''
 +
* Now edit your VE_NUMBER.conf adding this line: <code>DEVICES="c:180:0:rw c:189:896:rw c:189:900:rw"</code>
 +
** These are the devices replicated in the VE with mknod.
 +
* Almost there: restart your VE and enter it.
 +
* In VE type: <code>lsusb</code> and you should see the two devices created before:
 +
  Bus 008 Device 005: ID 04b8:0005 Seiko Epson Corp. Stylus Printer
 +
  Bus 008 Device 001: ID 1d6b:0001
 +
* If not '''RECHECK''' everything.
 +
* Start cups and access it via your browser
 +
* You should be able to see the printer attached to the usb port
 +
 
 +
== Sample bash shell script for compute vzctl command for dynamically assigning some devices to one VZ ==
 +
Be care, the command <code>vzctl set xxx --devices "dddd" --devnodes "nnnn" --save</code> will reassign
 +
the entire list of devices shared to one VZ, even suppressing some already presents.
 +
<code>
 +
  #!/bin/bash
 +
  VZ=1234
 +
  DEVICES="[bB]luetooth|[pP]rinter"
 +
 
 +
  VZDEV=""
 +
  VZNOD=""
 +
 
 +
  while read A B C D E;do
 +
    if [[ "$E" =~ (${DEVICES}) ]] ;then
 +
        BUS=${B#0} DEV=${D%:}
 +
        BUS=${B#0} DEV=${DEV#0}
 +
        DEV=${DEV#0}
 +
        VZDEV="$VZDEV c:189:$(((BUS-1)*128+DEV-1)):rw"
 +
        VZNOD="$VZNOD bus/usb/$B/${D}rw"
 +
        fi
 +
    done < <(lsusb)
 +
 
 +
  printf "DEVICES=\"%s\"\n" "${VZDEV# }"
 +
  printf "DEVNODES=\"%s\"\n" "${VZNOD# }"
 +
  printf "vzctl set %d --devices '%s' --devnodes '%s' --save\n" $VZ "${VZDEV# }" "${VZNOD# }"
 +
</code>
 +
 
 +
[[Category:HOWTO]]

Latest revision as of 16:51, 17 April 2012

This howto tries to cover the "device" configuration for printing with a USB printer in a Container. Please refer to your specific distro for required printing packages. Please note that ownership/permission/devices are referred to my system and you have to adapt to YOUR configuration: they are reported only as an EXAMPLE.

  • Install usbutils in both HN and VE.
  • In a shell on HN type lsusb and you'll get the list fo the usb devices on your system:
 Bus 002 Device 006: ID 0dda:2026 Integrated Circuit Solution, Inc. USB2.0 Card Reader
 Bus 002 Device 005: ID 05e3:0608 Genesys Logic, Inc. USB-2.0 4-Port HUB
 Bus 002 Device 003: ID 05e3:0608 Genesys Logic, Inc. USB-2.0 4-Port HUB
 Bus 002 Device 001: ID 1d6b:0002  
 Bus 008 Device 005: ID 04b8:0005 Seiko Epson Corp. Stylus Printer
 Bus 008 Device 001: ID 1d6b:0001  
 Bus 007 Device 002: ID 046d:c044 Logitech, Inc. 
 Bus 007 Device 001: ID 1d6b:0001  
 Bus 006 Device 002: ID 051d:0002 American Power Conversion Uninterruptible Power Supply 
 Bus 006 Device 001: ID 1d6b:0001  
 Bus 001 Device 001: ID 1d6b:0002  
 Bus 005 Device 001: ID 1d6b:0001  
 Bus 004 Device 001: ID 1d6b:0001  
 Bus 003 Device 001: ID 1d6b:0001 
  • Now examine the output of your command and you'll see two important lines: one for your printer and one for the usb port at which is attached, in my case:
 Bus 008 Device 005: ID 04b8:0005 Seiko Epson Corp. Stylus Printer
 Bus 008 Device 001: ID 1d6b:0001
  • In /dev directory in HN you should have:
    • a symlink to your usb printer: lrwxrwxrwx 1 root root 7 Nov 28 14:14 usblp0 -> usb/lp0
    • a usb directory
    • a printer inside the /dev/usb directory: crw-rw---- 1 root lp 180, 0 Nov 28 14:14 lp0
    • a bus/usb directory
    • many directories in bus/usb: named with the bus number that you got with the first lsusb command. In this example the relevant one is 008
    • in the numbered subdirectory 1 file for every usb device attached to this bus, the first one is the usb port:
      • crw-rw-r-- 1 root usb 189, 896 Nov 24 16:16 001
      • crw-rw-r-- 1 root usb 189, 900 Nov 28 14:14 005
  • Now that you have an idea of the general structure of your usb devices you have to replicate them on your VE.
    • enter your VE: vzctl enter VE_NUMBER
    • create in /dev (if not present) the usb directory
    • create in /dev/usb the printer device typing: mknod lp0 c 180 0
      • give the correct ownership/permission to the new created device typing:
        • chown root:lp lp0
        • chmod 660 lp0
        • check with ls -l lp0 if the new device is IDENTICAL to the one in HN --> crw-rw---- 1 root lp 180, 0 Nov 27 13:51 lp0
    • go back to /dev and create a symlink to the printer device: ln -s /dev/usb/lp0 /dev/usblp0
    • create all the needed directories:
      • mkdir /dev/bus
      • mkdir /dev/bus/usb
      • mkdir /dev/bus/usb/008
    • switch to /dev/bus/usb/008 directory and create there the needed devices: one for the usb port and one for the printer
      • mknod 001 c 189 896
      • mknod 005 c 189 900
    • assign ownership/permissions:
      • chown root:usb *
      • chmod 664 *
      • check everything: ls -l should give the same results as in HN:
      • crw-rw-r-- 1 root usb 189, 896 Nov 27 13:29 001
      • crw-rw-r-- 1 root usb 189, 900 Nov 27 13:30 005
  • Doublecheck everything!
  • Now edit your VE_NUMBER.conf adding this line: DEVICES="c:180:0:rw c:189:896:rw c:189:900:rw"
    • These are the devices replicated in the VE with mknod.
  • Almost there: restart your VE and enter it.
  • In VE type: lsusb and you should see the two devices created before:
 Bus 008 Device 005: ID 04b8:0005 Seiko Epson Corp. Stylus Printer
 Bus 008 Device 001: ID 1d6b:0001
  • If not RECHECK everything.
  • Start cups and access it via your browser
  • You should be able to see the printer attached to the usb port

Sample bash shell script for compute vzctl command for dynamically assigning some devices to one VZ[edit]

Be care, the command vzctl set xxx --devices "dddd" --devnodes "nnnn" --save will reassign the entire list of devices shared to one VZ, even suppressing some already presents.

 #!/bin/bash
 VZ=1234
 DEVICES="[bB]luetooth|[pP]rinter"
 
 VZDEV=""
 VZNOD=""
 
 while read A B C D E;do
   if [[ "$E" =~ (${DEVICES}) ]] ;then
       BUS=${B#0} DEV=${D%:}
       BUS=${B#0} DEV=${DEV#0}
       DEV=${DEV#0}
       VZDEV="$VZDEV c:189:$(((BUS-1)*128+DEV-1)):rw"
       VZNOD="$VZNOD bus/usb/$B/${D}rw"
       fi
   done < <(lsusb)
 
 printf "DEVICES=\"%s\"\n" "${VZDEV# }"
 printf "DEVNODES=\"%s\"\n" "${VZNOD# }"
 printf "vzctl set %d --devices '%s' --devnodes '%s' --save\n" $VZ "${VZDEV# }" "${VZNOD# }"