Saturday, September 7, 2013

It's been a while... but there's new stuff!

So, my last post was made on december 2012, it's been nine months since I haven't posted anything. It's not because I didn't want to, but because nothing new happened.

On my last post I said that I had bought a 500GB USB HDD to use as the rootfs of my Arch Linux ARM RaspberryPi (RPI), but around february of 2013 I bought a 2 GB one. So I replaced the old HDD with the 2 GB one and kept the 500 GB HDD as my personal backup media.

That's the only new thing that had happened, until today.

I always loved reading books. I have probably around 200 (or more) books at home. I never thought this day would come but recently I got interested in getting myself an ereader and start going in the direction of ebooks. I always thought that turning a page was something unreplaceble, but I'm reading my first ebook on my android phone and I have to say I was wrong. It's quite comfortable. I wonder how comfortable it actually is read to on an ereader, certainly better. I plan on buying an ereader, but there are other financial priorities right now.

Anyway, surfing the internet I found out about calibre. I had actually heard about it before, but since I was not into ebooks, I never gave it much attention. I installed it on my desktop and it's actually pretty cool. It enables you to manage your ebook collection and it syncs ebooks with a lot of different ereaders/tablets.

So I thought: would it be possible to keep my ebooks on my raspberry pi and access them from the web? It turns out the answer is yes, and the best part, it's very simple. I'll describe what I did to turn my RPI that's running Arch Linux ARM into an ebook server.

The first thing was to create a database on the RPI using calibre. I actually ran calibre on my desktop, created a database and then copied it to my RPI.

One could run calibre with it's heavy GUI, but it's not a good idea. RPI's CPU is not strong enough, it would be a pain to run calibre like this. So what I did was use a command line tool that is shipped with calibre called calibre-server. What this tool does is run a web server where you can search and download the ebooks that are on it's database. The confirguration is very simple (check this link), but I wanted it to run everytime the RPI booted, and for that I had to create a .service file for systemd. I looked around the net and found two pages: this one and this one. So here's my final calibre.service file. Just save it on /lib/systemd/system/.

[Unit]
Description=Calibre-Server
After=network.target

[Service]
Type=forking
PIDFile=/run/calibre-server.pid
ExecStart=/usr/bin/calibre-server \
        --daemonize \
        --port=portnumber \
        --pidfile=/run/calibre-server.pid \
        --with-library=/path/to/library/ \
Restart=on-abort

[Install]
WantedBy=multi-user.target

# Useful options for calibre-server
#--password=passy
#--username=user

Edit the text in italic. A few notes on these settings:

1) Username and password are optional. With the current Arch Linux packages, setting a username and password will result in a non working server because of a bug on cherrypy 3.2.3 (web framework used by calibre). I got this info from this post. I looked for the cherrypy 3.2.2 Arch Linux ARM package but I could not find it. Appearantly the binary package provided by the official calibre site doesn't have this bug but I'm kinda lazy, I prefer using regular Arch packages. Anyway, this is not THAT much of a problem, since if a unauthorized person finds my server, the only thing he/she'll be able to do is download my books.
2) Run this server on a unsual port. As I mentioned on a earlier post, when I first started running a SSH server, it kept getting hammered (I did use ssh keys, which is a good thing). Once I changed the port number, all the hammering was gone. Now, I use unusual port numbers for every server I run on my home.

Now, to start the server simply type:

sudo systemctl start calibre.service

To stop the server:

sudo systemctl stop calibre.service

To run the server on every boot:

sudo systemctl enable calibre.service

That's it!

Also, while I was watching a movie on the RPI, I used my phone to access the web server and make some queries, the movie did not strutter. I didn't download any files, but the CPU managed the extra load.

There's a small problem with this setup: how to manage the database if running calibre GUI on the RPI is insanely slow (for adding books, for example)? One can do it with the command line tools calibre has but I reckon it'd be a pain. I did it by running calibre on my desktop and mounting the RPI filesystem on the desktop using NFS. Calibre devs say that running the database on a network drive is not supported but I did not encounter any problems. Just for the sake of safety, I did make a backup of the database before changing it.

Well, that's it. I hope this post ends being useful to anyone.