User:Simon/Trim4/prototypes/bootleg library setup

From XPUB & Lens-Based wiki

Initial RPi setup

The bootleg library runs on a Raspberry Pi (RPi) single-board computer. The operating system of the RPi is stored on an SD card, that must be "flashed" with an "image" of the operating system in order to boot the computer.

There are various distributions you can choose from, depending on how you want to use the computer. This page from the Raspberry Pi documentation gives a good guide on how to install an operating system image on an SD card using Raspberry Pi Imager, or manually. For the bootleg library, I initially installed Buster-lite, and then eventually switched to a plain-text environment because it was lighter.

I used software called balena etcher, but you can also attempt to install a distribution using the dreaded dd command to convert and copy the exact image on to the SD card. dd is sometimes referred to as the "disk destroyer", so use with caution.

Auto-mount an external drive on boot

Step 1 – Plug In The Device

Step 2 – Identify The Device's Unique ID

In order to find the unique reference (UUID) for your drive run the following command in the terminal :

ls -l /dev/disk/by-uuid/

This will give you an output that should list your drive :

Bootleg uuid.png

The line will usually refer to “/sda” and in this example it is “sda1”. My ID is “989B-E900”. Note down yours. You would need to repeat this step if you wanted to use a different device as the UUID would be different.

Step 3 – Create a Mount Point

A mount point is a directory that will point to the contents of your flash drive. Create a suitable folder :

sudo mkdir /media/usb

I’m using “usb” but you can give it whatever name you like. Keep it short as it saves typing later on. Now we need to make sure the Pi user owns this folder :

sudo chown -R pi:pi /media/usb

You will only need to do this step once.

Step 4 – Manually Mount The Drive

To manually mount the drive use the following command :

sudo mount /dev/sda1 /media/usb -o uid=pi,gid=pi


This will mount the drive so that the ordinary Pi user can write to it. Omitting the “-o uid=pi,gid=pi” would mean you could only write to it using “sudo”. Now you can read, write and delete files using “/media/usb” as a destination or source without needing to use sudo.

Step 5 – Un-mounting The Drive

You don’t need to manually un-mount if you shutdown your Pi but if you need to remove the drive at any other time you should un-mount it first. Only the user that mounted the drive can un-mount it.

umount /media/usb

If you used the fstab file to auto-mount it you will need to use :

sudo umount /media/usb

If you are paying attention you will notice the command is “umount” NOT “unmount”!

Step 6 – Auto Mount

When you restart your Pi your mounts will be lost and you will need to repeat Step 4. If you want your USB drive to be mounted when the system starts you can edit the fstab file :

sudo nano /etc/fstab

Then add the following line at the end :

UUID=989B-E900 /media/usb vfat auto,nofail,noatime,users,rw,uid=pi,gid=pi 0 0

The “nofail” option allows the boot process to proceed if the drive is not plugged in. The “noatime” option stops the file access time being updated every time a file is read from the USB stick. This helps improve performance. This is what my fstab file looks like:

Bootleg fstab.png

Make sure you set the correct UUID. Use CTRL-X followed by Y to save and exit the nano editor.

Now reboot :

sudo reboot

Your USB drive should be auto-mounted and available as “/media/usb”.

An Extra Note About File Systems

In the examples above I specified “vfat” as the file system of the device as it was formatted as FAT32. If you need to change the file system replace references of “vfat” with “ntfs-3g”, “ext3” or “ext4”. If you are using NTFS you will also need to install the following package :

sudo apt-get install ntfs-3g

Final Thoughts

This technique suits my applications but the main disadvantage is that it is specific to a known USB device given we are using the device ID. However if you created a few mount points in advance you could manually mount a new device to a spare mount point without worrying about updating the fstab file.

Configuring the web server

Creating a unit file

You can easily create a unit file for calibre to run on boot on a modern (systemd) based Linux system. This means it will also restart after a crash. Create the file /etc/systemd/system/calibre-server.service with the contents shown below:

[Unit]
Description=Calibre.
After=syslog.target network.target

[Service]
Type=simple
User=pi
Group=pi
WorkingDirectory=/home/pi/calibre-web
ExecStart=python cps.py
Restart=always

[Install]
WantedBy=multi-user.target

The User and Group should be the same ones that own the files in the calibre library directory. It's generally not a good idea to run the server as root. Also change the path to the calibre library directory to suit your system.

Starting the calibre-server service

Run:

sudo systemctl start calibre-server

to start the server.

Stopping the calibre-server service

Stopping the service is as easy as running:

sudo systemctl stop calibre-server

Checking the status of the calibre-server service

Check its status with:

sudo systemctl status calibre-server

Enabling the calibre-server service to start at boot

To make it start at boot, run:

sudo systemctl enable calibre-server

Note

The calibre server does not need a running X server, but it does need the X libraries installed as some components it uses link against them.

The calibre server also supports systemd socket activation, so you can use that, if needed, as well.

calibre-web configuration

This is the configuration for calibre-web to listen on localhost:

--- a/cps/server.py
+++ b/cps/server.py
@@ -152,7 +152,7 @@ class WebServer(object):
        http_server = HTTPServer(WSGIContainer(self.app),
                                 max_buffer_size=209700000,
                                 ssl_options=self.ssl_args)
-        http_server.listen(self.listen_port, self.listen_address)
+        http_server.listen(self.listen_port, address="127.0.0.1")
        self.wsgiserver = IOLoop.instance()
        self.wsgiserver.start()
        # wait for stop signal

nginx configuration

nginx is a web server software that the bootleg library runs on. This configuration allows maximum uploads of 100mb:

server {
 listen 80 default_server;
 listen [::]:80 default_server;
 server_name _;  
 location /bootleglibrary {

   auth_basic           "ヽ(°〇°)ノ";
   auth_basic_user_file /etc/nginx/htpasswd;

   client_max_body_size 100M;

   proxy_bind         $server_addr;
   proxy_pass         http://127.0.0.1:20190;
   proxy_set_header   Host                $http_host;
   proxy_set_header   X-Forwarded-For     $proxy_add_x_forwarded_for;
   proxy_set_header   X-Scheme            $scheme;
   proxy_set_header   X-Script-Name       /bootleglibrary;

   }
}

nginx service file

To enable, start, stop, or check the status of the nginx web server, a service file is needed. To create one in a systemd Linux distribution, make this file:

/lib/systemd/system/nginx.service

with the following contents:

# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target


On Debian/Ubuntu/RHEL/CentOS Linux

Use the following command: # /etc/init.d/nginx restart

OR

# /etc/init.d/nginx reload

OR

# service nginx restart

OR

# service nginx reload

OR if you are using systemd based Linux distro:

$ sudo systemctl restart nginx

OR

$ sudo systemctl reload nginx

To view status:

# service nginx status

OR

$ sudo systemctl status nginx

However, the recommend way is as follows. This should work with any Linux distributions or Unix-like operating systems:

# nginx -s reload

OR

# /path/to/full/nginx -s reload

If nginx is compiled and installed from the source code

If nginx binary is installed at /usr/local/nginx/sbin/nginx, enter:

# /usr/local/nginx/sbin/nginx -s reload xvm uses these settings:

location /bootleglibrary {
 client_max_body_size 100M;
 proxy_pass http://10.0.0.103/bootleglibrary;
}