Jump to content

Raspberry Pi configuration

From ChemWiki

This page records the process of configuration of the Raspberry Pi computers (presently version 2 model B) used on the Measurement Science labs in 1st and 2nd year.

The goal is to setup an operating system image which will be made available via an NFS server, and all Pi computers will run this identical image.

The process starts by booting a Raspbian Buster Lite image from an SD card.

Most of the process is done on the command prompt with command lines starting with an '$' . Many of the commands require extended privileges to run and therefore need to be preceded by the command sudo.

Changing default password

The default configuration includes the user with login 'pi' and password 'raspberry'. We will keep this local user but change the default password. This can be done by running the utility raspi-config and choosing 'Change user password'.


Installing packages

Change archive mirror

This is not strictly necessary, but a much greater bandwidth is accessible by downloading packages from the University of Oxford mirror. In /etc/apt/sources.list change the line starting with deb to read

deb http://mirror.ox.ac.uk/sites/archive.raspbian.org/archive/raspbian/ buster main contrib non-free rpi

To start with an updated system run the following commands

$ apt update
$ apt upgrade

Trimming down the image

This is again not necessary, but while the Raspbian Lite image is rather small, still it contains bits of software which are not needed and can be trimmed further. For example, the Raspberry Pi 2 board does not have wireless or bluetooth capability, and these are not needed for the labs, therefore related packages can be removed

$ apt purge pi-bluetooth bluez wireless-tools wpa_supplicant iw crda

The different firmware packages (firmeware-xxx) installed can also be removed. (But do not remove the firmware files in the /boot directory).

For our purposes all Python 2 (but not Python 3) packages can be removed.

There are several daemons that are running in the default installation and that are not needed. This includes the ssh server and the triggerhappy deamon.

$ apt purge openssh-server triggerhappy rng-tools

Installing a graphical environment

For ease of use we want to install a graphical environment.

$ apt install --no-install-recommends raspberrypi-ui-mod lxde pi-greeter pishutdown raspberrypi-artwork rpi-icons rpi-wallpaper xserver-xorg-video-fbturbo

Let us add to that a web-browser and a pdf reader

$ apt install --no-install-recommends epiphany-browser xpdf

Also an office productivity suite:

$ apt install --no-install-recommends libreoffice-calc libreoffice-writer libreoffice-math libreoffice-draw

ImageJ

ImageJ is an imaging processing software that is used in some experiments. Installing will also install the necessary Java machinery.

$ apt install --no-install-recommends imagej

To install the contact angle plugin, we download it from the website and move it to the relevant directory where it can be found by the program

$ wget https://imagej.nih.gov/ij/plugins/download/jars/Contact_Angle.jar
$ mv Contact_Angle.jar /usr/share/imagej/plugins/

IPython and Jupyter notebooks

To get an ipython shell and a useful scientific python environment

$ apt install --no-install-recommends ipython3 python3-numpy python3-matplotlib python3-tk python3-scipy python3-pandas python3-sympy

The last two packages are not critical, but can still be useful.

Additionally, to get Jupyter notebooks running you need a Javascript able web-browser and to install the following packages

$ apt install --no-install-recommends jupyter-notebook jupyter-nbformat jupyter-nbconvert

There is no menu item on the menu to launch jupyter notebooks. To add one to this effect, create the file /usr/share/applications/jupyter.desktop with the following content:

[Desktop Entry]
Name=Jupyter
Comment=Launch Jupyter notebooks
Exec=/usr/bin/jupyter-notebook --browser='x-www-browser'
Icon=/usr/lib/python3/dist-packages/notebook/static/base/images/favicon.ico
Terminal=false
Type=Application
Categories=Development;Science;Programming;

Installing some chemistry software

None of the current experiments require these program. Still, it might be good to have some structure viewer and drawing software.

$ apt install --no-install-recommends gperiodic easychem jmol


Installing the ADC chip

I2C bus

First the I2C bus needs to be enabled. For that effect run the raspi-config utility, select option '5 Interfacing Options', and then 'P5 I2C'.

Now we need to change the permissions to allow all users to access the bus by editing file /lib/udev/rules.d/60-i2c-tools.rules and changing to read MODE="0666".

It may also be needed to change the permissions set in file /etc/udev/rules.d/99-com.rules changing MODE in the following line to read:

SUBSYSTEM=="i2c-dev", GROUP="i2c", MODE="0666"


Installing Adafruit modules

The rest of this section describes the installation of the Analog to Digital Converter (ADC) ADS1x15 from Adafruit. The relevant python code can be installed using the PIP.

First we install the PIP installation system and the RPi.GPIO library used to interact with the GPIO pins on the Pi.

$ apt install --no-install-recommends python3-pip python3-rpi.gpio

Then use PIP to install the Adafruit code

$ pip3 install adafruit-ads1x15

which installs the code in /usr/local/lib/python3.7/dist-packages.

Experiment scripts

Copy the following files to the /usr/local/bin directory

  • pi_spectrsocopy
  • pi_fluorescence
  • dark_count.py
  • continuous_read_adc.py
  • fluo_continuous_read_adc.py

The first 2 are bash scripts and need to be made executable

$ chmod +x /usr/local/bin/pi_*

The last 2 python scrips are inspired by Adafruit examples.


Change permissions to access camera

Gaining access to the camera can in principle be done by adding the users to the local video group at login. A better solution might be to simply relax the permissions on access to this hardware through udev rules.

To get the camera working, change the /lib/udev/rules.d/10-local-rpi.rules file, change all permissions to MODE="0666". It should read

SUBSYSTEM=="vchiq", GROUP="video", MODE="0666"
SUBSYSTEM=="vc-sm", GROUP="video", MODE="0666"
SUBSYSTEM=="bcm2708_vcio", GROUP="video", MODE="0666"


Login with College credentials

We want to setup the system to use the College LDAP directory such that users can login using the College login and password. Also the home directory should be the College H: drive share and not a local directory on the computer or the NFS image.

Login setup

First we need to install some packages

$ apt install --no-install-recommends libpam-ldapd libnss-ldapd nscd ldap-utils

A prompt will show up asking for the LDAP server URI, use

ldap://unixldap.cc.ic.ac.uk/

In the following prompt about the name search base, use

ou=everyone,dc=ic,dc=ac,dc=uk

When asked about the services for which to use LDAP lookups, select 'passwd', 'group' and 'shadow'.

The default display manager (lightdm) does not play along very well with the large College LDAP directory, so we will change to use the old 'xdm' instead:

$ apt install --no-install-recommends xdm

By default, xdm spawns an ssh-agent for each user that logs in. This is not needed. To prevent this from happening, modify the following line in /etc/X11/Xsession.options.

no-use-ssh-agent

The file /etc/X11/xdm/Xresources can be edited to make the login screen prettier.

Mount H: drive as home directory

This will create a directory /home/'username' and mount the H: drive as a CIFS (aka Samba) file system there. First we need to install some packages:

$ apt install --no-install-recommends libpam-mount libpam-mount-bin cifs-utils hxtools

Users' H: drives are spread across differently named servers. On which server is a given user's H: drive directory can be determined with an LDAP query (see script in /usr/local/sbin/ldapserver), but I haven't found a way to pass on this information to the relevant XML configuration file /etc/security/pam_mount.conf.xml. The strategy is this to try all servers, this will fail on all but one sever, but failures will be transparent to the user and the result will be a mounted home directory.

Add the following lines to the file /etc/security/pam_mount.conf.xml.

<!-- Volume definitions -->
<volume fstype="cifs" server="icnas1.cc.ic.ac.uk" path="%(DOMAIN_USER)" mountpoint="/home/%(DOMAIN_USER)" options="sec=ntlmssp"> <not> <uid>1000</uid> </not> </volume>
<volume fstype="cifs" server="icnas2.cc.ic.ac.uk" path="%(DOMAIN_USER)" mountpoint="/home/%(DOMAIN_USER)" options="sec=ntlmssp"> <not> <uid>1000</uid> </not> </volume>
<volume fstype="cifs" server="icnas3.cc.ic.ac.uk" path="%(DOMAIN_USER)" mountpoint="/home/%(DOMAIN_USER)" options="sec=ntlmssp"> <not> <uid>1000</uid> </not> </volume>
<volume fstype="cifs" server="icnas4.cc.ic.ac.uk" path="%(DOMAIN_USER)" mountpoint="/home/%(DOMAIN_USER)" options="sec=ntlmssp"> <not> <uid>1000</uid> </not> </volume>

Also edit the following line on the same file such that all processes and disc activity is terminated upon logout.

<logout wait="100000" hup="yes" term="yes" kill="yes" />


Booting from NFS

Here we describe how to transfer the system image from the SD card to an NFS server and to boot the Pi from there. The NFS server is jbe-nfsserver.ch.ic.ac.uk (155.198.225.243). The final configuration will be a root file system (/) over NFS, a /boot remaining on the SD card, /home is on the NFS drive but a sub-directory will be created for each user and mounted over CIFS.

Disabling swap

The default swap system dphys-swapfile, does not play nicely with an root NFS so we need to remove it first.

$ dphys-swapfile swapoff
$ apt purge dphys-swapfile

Configure the server

We create a new directory on the server (jbe-nfsserver.ch.ic.ac.uk) where we will place the Pi image. On the server command line:

$ mkdir -p /nfsexports_5/rpi

Edit /etc/exports to configure this folder as an NFS export by adding the line

/nfsexports_5/rpi       *(rw,async,no_root_squash,no_subtree_check)

The previous line allows any machine on the network to connect to this image. The async option means that communication with the client and writing to disk on the server are not synchronised, making the response faster but there might be data loss if the server crashes.

Restart the NFS daemon. On the installed Ubuntu system this is done by

$ /etc/init.d/nfs-kernel-server restart

Copy the system image to the server

Back in the Pi, we mount the NFS directory

$ mount jbe-nfsserver.ch,ic.ac.uk:/nfsexports_5/rpi /mnt/

Now copy the full system to the server

$ cp -axv /. /mnt/
$ cp -axv /dev/. /mnt/dev/

Now comment out the line on /mnt/etc/fstab which mounts / on the SD card. This looks something like:

# PARTUUID=b78cea42-02  /               ext4    defaults,noatime  0       1

We also add a new line to the same file to mount /tmp on RAM.

tmpfs    /tmp    tmpfs   defaults,noatime,nosuid,nodev,noexec,mode=1777    0       0

Adjusting boot

Edit /boot/cmdline.txt to read like:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/nfs rootfstype=nfs nfsroot=155.198.225.243:/nfsexports_5/rpi rw smsc95xx.turbo_mode=N ip=dhcp rootwait elevator=deadline

The system can now be rebooted and upon it will use the NFS server as a root partition.

Re-enabling swap

The system can be run without swap, but since the amount of RAM memory of the Pi is not enormous and there might be a chance of the occasional memory intensive process, we can add some swap space to the configuration. We will delete the system partition on the SD card (which is no longer needed as the Pi is using the NFS server), and use that space for a swap partition.

There should be 2 partitions on the SD card: partition 1 contains the /boot mount (we will keep this); partition 2 contains the old root (/) mount (we no longer need it). Use the fdisk utility to delete the partition 2. Create a new partition in the free space created and label it as a 'Linux Swap' partition. Write the partition table to the SD card.

Now we need to setup the swap area and activate it by running

$ mkswap /dev/mmcblk0p2
$ swapon /dev/mmcblk0p2

The SD card is not an excellent swap medium as it deteriorates with the number of writes. We therefore want to use swap the least possible and only when it is really needed. We therefore change the swappiness of the system, by creating the file /etc/sysctl.d/99-swappiness.conf with content:

vm.swappiness=10

For the swap partition to be activated at boot time, edit the /etc/fstab by adding the following line

PARTUUID=b78cea42-02  none               swap    defaults  0       0


Other configurations

Configure College time servers

In order to keep time accurate we can configure the system to synchronized the clock with the College time servers. Edit the file /etc/systemd/timesyncd.conf to include the lines

[time]
NTP=time1.ic.ac.uk time2.ic.ac.uk time3.ic.ac.uk


Configuring printing

The server jbe-nfssever.ch.ic.ac.uk has a configured print server that the Pis can point to. To do this, install

$ apt install --no-install-recommends cups-client

Create the file /etc/cups/client.conf, with the following line in it

ServerName jbe-nfssever.ch.ic.ac.uk