[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: udev rule for a kindle



Now working and understood.

On 14/12/13 02:32, Sharon Kimble wrote:
> On Fri, 13 Dec 2013 20:40:26 +1100
> Scott Ferguson <scott.ferguson.debian.user@gmail.com> wrote:
> 
>> On 13/12/13 03:53, Sharon Kimble wrote:
>>> On Fri, 13 Dec 2013 02:02:06 +1100 Scott Ferguson
>>> <scott.ferguson.debian.user@gmail.com> wrote:
>>>
>>>> On 13/12/13 01:30, Sharon Kimble wrote:
>>>>> On Thu, 12 Dec 2013 11:43:32 +1100 Scott Ferguson
>>>>> <scott.ferguson.debian.user@gmail.com> wrote:
>>>>>
>>>>>> On 12/12/13 01:01, Sharon Kimble wrote:
>>>>>>>
>>>>>>> I have a script for backing up my kindle when its first
>>>>>>> mounted, but its not running on mounting, but *does* run when
>>>>>>> invoked manually! When the kindle is mounted it should
>>>>>>> trigger this udev rule -
>>>>>>>
>> <snipped>
>>>>
<snipped>

Here's what worked for me.
NOTE: kindle.sh will only work on KDE machines with a user called scott.

1. Plug in Kindle

2. Find device node
# tail /var/log/messages
grep -v Rej /var/log/messages | tail
Dec 15 22:08:05 vbserver mtp-probe: checking bus 1, device 9:
"/sys/devices/pci0000:00/0000:00:02.2/usb1/1-4"
Dec 15 22:08:05 vbserver mtp-probe: bus: 1, device: 9 was not an MTP device
Dec 15 22:08:06 vbserver kernel: [37231.122454] scsi 8:0:0:0:
Direct-Access     Kindle   Internal Storage 0100 PQ: 0 ANSI: 2
Dec 15 22:08:06 vbserver kernel: [37231.127116] sd 8:0:0:0: Attached
scsi generic sg2 type 0
Dec 15 22:08:06 vbserver kernel: [37231.131048] sd 8:0:0:0: [sdb]
6410688 512-byte logical blocks: (3.28 GB/3.05 GiB)
Dec 15 22:08:07 vbserver kernel: [37231.234688] sd 8:0:0:0: [sdb] Write
Protect is off
Dec 15 22:08:07 vbserver kernel: [37231.344693] sd 8:0:0:0: [sdb] Write
cache: enabled, read cache: enabled, doesn't support DPO or FUA
Dec 15 22:08:07 vbserver kernel: [37231.565991]  sdb: sdb1
Dec 15 22:08:07 vbserver kernel: [37231.844747] sd 8:0:0:0: [sdb]
Attached SCSI removable disk
Dec 15 22:08:08 vbserver kernel: [37232.678814] FAT-fs (sdb1): utf8 is
not a recommended IO charset for FAT filesystems, filesystem will be
case sensitive!

(alternatively you could use # udisks --monitor)

sdb1 is the current device node (the value needed for the next step).

3. Use the KERNEL identification (top of the chain for the block mode,
in this instance /dev/sdb1) so the rule points to the block device
instead of the USB controller. Find identifiers specific to the Kindle.
# udevadm info -a -p $(udevadm info -q path -n /dev/sdb1)

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device
'/devices/pci0000:00/0000:00:02.2/usb1/1-4/1-4:1.0/host8/target8:0:0/8:0:0:0/block/sdb/sdb1':
    KERNEL=="sdb1"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{partition}=="1"
    ATTR{start}=="16"
    ATTR{size}=="6410672"
    ATTR{ro}=="0"
    ATTR{alignment_offset}=="0"
    ATTR{discard_alignment}=="0"
    ATTR{stat}=="     222     3087     4450      732        1        0
      1        0        0      544      732"
    ATTR{inflight}=="       0        0"

  looking at parent device
'/devices/pci0000:00/0000:00:02.2/usb1/1-4/1-4:1.0/host8/target8:0:0/8:0:0:0/block/sdb':
    KERNELS=="sdb"
    SUBSYSTEMS=="block"
    DRIVERS==""
    ATTRS{range}=="16"
    ATTRS{ext_range}=="256"
    ATTRS{removable}=="1"
    ATTRS{ro}=="0"
    ATTRS{size}=="6410688"
    ATTRS{alignment_offset}=="0"
    ATTRS{discard_alignment}=="0"
    ATTRS{capability}=="51"
    ATTRS{stat}=="     228     3087     4498      740        1        0
       1        0        0      552      740"
    ATTRS{inflight}=="       0        0"
    ATTRS{events}=="media_change"
    ATTRS{events_async}==""
    ATTRS{events_poll_msecs}=="-1"

  looking at parent device
'/devices/pci0000:00/0000:00:02.2/usb1/1-4/1-4:1.0/host8/target8:0:0/8:0:0:0':
    KERNELS=="8:0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS=="sd"
    ATTRS{device_blocked}=="0"
    ATTRS{type}=="0"
    ATTRS{scsi_level}=="3"
    ATTRS{vendor}=="Kindle  "
    ATTRS{model}=="Internal Storage"
    ATTRS{rev}=="0100"
    ATTRS{state}=="running"
    ATTRS{timeout}=="30"
    ATTRS{iocounterbits}=="32"
    ATTRS{iorequest_cnt}=="0x12b9"
    ATTRS{iodone_cnt}=="0x12b9"
    ATTRS{ioerr_cnt}=="0x1"
    ATTRS{evt_media_change}=="0"
    ATTRS{queue_depth}=="1"
    ATTRS{queue_type}=="none"
    ATTRS{max_sectors}=="240"

  looking at parent device
'/devices/pci0000:00/0000:00:02.2/usb1/1-4/1-4:1.0/host8/target8:0:0':
    KERNELS=="target8:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS==""

  looking at parent device
'/devices/pci0000:00/0000:00:02.2/usb1/1-4/1-4:1.0/host8':
    KERNELS=="host8"
    SUBSYSTEMS=="scsi"
    DRIVERS==""

  looking at parent device
'/devices/pci0000:00/0000:00:02.2/usb1/1-4/1-4:1.0':
    KERNELS=="1-4:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb-storage"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bNumEndpoints}=="02"
    ATTRS{bInterfaceClass}=="08"
    ATTRS{bInterfaceSubClass}=="06"
    ATTRS{bInterfaceProtocol}=="50"
    ATTRS{supports_autosuspend}=="1"
    ATTRS{interface}=="Mass Storage"

  looking at parent device '/devices/pci0000:00/0000:00:02.2/usb1/1-4':
    KERNELS=="1-4"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{configuration}=="Self-powered"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bmAttributes}=="c0"
    ATTRS{bMaxPower}=="100mA"
    ATTRS{urbnum}=="9867"
    ATTRS{idVendor}=="1949"
    ATTRS{idProduct}=="0004"
    ATTRS{bcdDevice}=="0100"
    ATTRS{bDeviceClass}=="00"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{speed}=="480"
    ATTRS{busnum}=="1"
    ATTRS{devnum}=="9"
    ATTRS{devpath}=="4"
    ATTRS{version}==" 2.00"
    ATTRS{maxchild}=="0"
    ATTRS{quirks}=="0x0"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{authorized}=="1"
    ATTRS{manufacturer}=="Amazon"
    ATTRS{product}=="Amazon Kindle"
    ATTRS{serial}=="B00AD0B11322188J"

  looking at parent device '/devices/pci0000:00/0000:00:02.2/usb1':
    KERNELS=="usb1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{configuration}==""
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bmAttributes}=="e0"
    ATTRS{bMaxPower}=="  0mA"
    ATTRS{urbnum}=="164"
    ATTRS{idVendor}=="1d6b"
    ATTRS{idProduct}=="0002"
    ATTRS{bcdDevice}=="0302"
    ATTRS{bDeviceClass}=="09"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{speed}=="480"
    ATTRS{busnum}=="1"
    ATTRS{devnum}=="1"
    ATTRS{devpath}=="0"
    ATTRS{version}==" 2.00"
    ATTRS{maxchild}=="6"
    ATTRS{quirks}=="0x0"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{authorized}=="1"
    ATTRS{manufacturer}=="Linux 3.2.0-4-686-pae ehci_hcd"
    ATTRS{product}=="EHCI Host Controller"
    ATTRS{serial}=="0000:00:02.2"
    ATTRS{authorized_default}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:02.2':
    KERNELS=="0000:00:02.2"
    SUBSYSTEMS=="pci"
    DRIVERS=="ehci_hcd"
    ATTRS{vendor}=="0x10de"
    ATTRS{device}=="0x0068"
    ATTRS{subsystem_vendor}=="0x1458"
    ATTRS{subsystem_device}=="0x5004"
    ATTRS{class}=="0x0c0320"
    ATTRS{irq}=="20"
    ATTRS{local_cpus}=="ffffffff"
    ATTRS{local_cpulist}=="0-31"
    ATTRS{dma_mask_bits}=="32"
    ATTRS{consistent_dma_mask_bits}=="32"
    ATTRS{enable}=="1"
    ATTRS{broken_parity_status}=="0"
    ATTRS{msi_bus}==""
    ATTRS{companion}==""
    ATTRS{uframe_periodic_max}=="100"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""

The wanted values are:-
(from the device) ATTR{size}=="6410672"
(from the parent device) ATTRS{serial}=="B00AD0B11322188J"
We could use KERNEL=="sd?1" or KERNEL=="sd[a-z]1" to match on the device
node, but the serial number and disk size are sufficiently unique. See
man udev for explanation.


4. The first part of a udev rule is the matching keys. These positively
identify the Kindle (and match no other devices). The second part of the
rule is an action, in this case RUN.
NOTES: only properties from *one* parent of the device and from the
device itself. More than one value can be used. Custom rules should go
in /etc/udev/rules.d not /lib/udev/rules.d. I'm not using a symlink as
another rule already does that when it detects and mounts the Kindle.

====== /etc/udev/rules.d/81-kindle.rules ======
ATTRS{serial}=="B00AD0B11322188J", ATTR{size}=="6410672",
RUN+="/usr/local/bin/kindle.sh"
===============

Rule explanation:-
device serial must equal "B00AD0B11322188J"
device size must equal "6410672"
if so execute /usr/local/bin/kindle.sh
NOTE: RUN is dumb - give a full path or udev assumes the path begins
with /lib/udev.

======= /usr/local/bin/kindle.sh ======
#!/bin/bash
su scott -c 'export DISPLAY=:0 && kdialog --icon calibre-gui --title
"Kindle" --caption "Status" --msgbox "The Kindle is ready"'
su scott -c calibre
=======================================



5. Change logging level
# udevadm control --log-priority=debug

logs to watch:-
/var/log/daemon.log
/var/log/messages


6. Plug in Kindle

Kdialog pops up, then Calibre loads. If you have no such success look at
the logs.



HTH


Kind regards


Reply to: