Categories
Kindle Linux Posts Ubuntu

Auto Sync Kindle in Ubuntu

For Christmas I got a Kindle 3 and it was instantly filled with Free eBooks (http://www.gutenberg.org/wiki/Main_Page) and research papers (PDFs). I found myself wanting to be able to automatically synchronize it with a folder on my desktop when it was plugged in and found no one else had posted online how to do this, so I began researching into udev and rsync to get something working.

Setting up udev rules to run a script when the Kindle is plugged in.

Firstly, we need to find some parameters of the Kindle which are unique to it so that udev can identify that it is the Kindle being plugged in and not another USB mass storage device. We’ll need to find out where the device is mounted in /dev/ which can be a little tricky because this folder is pretty full.

An easier way to do this is to first, without the Kindle plugged in do the following command (list the directory and pipe the output to a file called dev1 in the home dir):

ls /dev/ > ~/dev1

Now plug the Kindle in and redo the command, changing the destination output:

ls /dev/ > ~/dev2

The difference between the two files will show what has changed in the /dev/ directory:

 sdiff ~/dev1 ~/dev2

The output for mine (show in the image below) shows that 3 things changed when I plugged in the Kindle, sdb, sdb1 and sg2 directorys were added (shown by the > sign to the left). The folder I am looking for is the sdb1, this is where the mass storage device is found.

Write down where it’s found in /dev/ and then you can clean up those two files you previously made:

rm ~/dev1 | rm ~/dev2

The next thing we want to do is use udevadm to collect some useful information about the device which we can do by the following command (I have chosen to pipe the output to a file called kindle in the home dir, because the output was pretty big. If you don’t want to remove the > ~/kindle from the end of the command):

udevadm info -a -p $(udevadm info -q path -n /dev/sdb1) > ~/kindle

WARNING: This can look pretty daunting but, (in the words of Douglas Adams) Don’t Panic.

  looking at device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/host21/target21:0:0/21:0:0:0/block/sdb/sdb1':
    KERNEL=="sdb1"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{partition}=="1"
    ATTR{start}=="16"
    ATTR{size}=="6410672"
    ATTR{alignment_offset}=="0"
    ATTR{discard_alignment}=="4294959104"
    ATTR{stat}=="     151     3389     4239     3776        0        0        0        0        0     2424     3776"
    ATTR{inflight}=="       0        0"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/host21/target21:0:0/21: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}=="     156     3389     4279     3808        0        0        0        0        0     2456     3808"
    ATTRS{inflight}=="       0        0"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/host21/target21:0:0/21:0:0:0':
    KERNELS=="21: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}=="0xe1"
    ATTRS{iodone_cnt}=="0xe1"
    ATTRS{ioerr_cnt}=="0x1"
    ATTRS{modalias}=="scsi:t-0x00"
    ATTRS{evt_media_change}=="0"
    ATTRS{dh_state}=="detached"
    ATTRS{queue_depth}=="1"
    ATTRS{queue_type}=="none"
    ATTRS{max_sectors}=="240"

While this many look daunting, what you want to do if find some attributes from the output which mean only your Kindle will be found when looking for them all, the problem I have found is that, with udev rules you cannot go too far down in the parent tree which stops me being able to use the Kindles serial to identify it. This could mean that my computer will sync any Kindle that’s plugged into it but I don’t have 2 to check. The following are the attributes I chose:

KERNEL=="sd?", ATTRS{vendor}=="Kindle  ", ATTRS{modalias}=="scsi:t-0x00"

Now you’ll want to write the udev rule in /etc/udev/rules.d/

cd /etc/udev/rules.d/

According to the readme the numbers represent the priority of the rule (higher overriding lower) followed by a descriptive name and it must end in .rules eg (xx-description.rules).

sudo gedit 81-kindle-sync.rules

The following is the whole line I used for the udev rule, the only addition to the above is the RUN+=”/home/alex/.scripts/kindlesync.sh” which will run the script in that directory, this script contains the rsync command.

KERNEL=="sd?", ATTRS{vendor}=="Kindle  ", ATTRS{modalias}=="scsi:t-0x00", RUN+="/home/alex/.scripts/./kindlesync.sh"

Save and close the file and then restart the udev service:

sudo service udev restart

Writing a script to sync a folder on the Desktop with a folder on the Kindle using rsync:
Change the first directory from “/home/alex/ebooks/kindle_sync” to the directory on your computer where you want to keep the files that will be synced onto your Kindle and the second from “/media/Kindle/documents/sync/” to the location on your mounted Kindle where you want the files to be stored, save this file as kindlesync.sh. This is the script you call from the udev rules so for mine it’s saved as “/home/alex/.scripts/kindlesync.sh”.

#!/bin/bash

#Sync
rsync -av /home/alex/eBooks/kindle_sync/ /media/Kindle/documents/sync/

Go to the location of the script and make it executable with the following command:

chmod +x kindlesync.sh

Now try plugging in your Kindle and make sure it syncs up!

Problems

In the udev rule, ACTION==”add” can be used to specify when the Kindle is first plugged in, however when trying to use this Ubuntu’s auto-mounting service mounts the drive after the script is ran so the sync becomes useless. Without this it runs the script 3 times, twice before it has mounted and then once it has mounted – this is currently the best I have found but it’s extremely messy.