PHONIEBOX_ReadOnly - MiczFlor/RPi-Jukebox-RFID GitHub Wiki

Phoniebox with read-only Filesystem

In order to make the Phoniebox more resilient against sudden power loss a mostly read-only filesystem helps against filesystem corruption.

Using a read-only filesystem makes changes to the Phoniebox non-permanent or impossible.
This includes changes to folders where audio files are stored for example. Additionally settings, like the maximum volume allowed cannot be changed anymore.
Since this is not desired, this guide shows method to get a read-only filesystem with exception for folders that need to be writable to keep the features of the Phoniebox vital.

There are two methods available. Please choose which one fits your needs.

With either method: Backup your system before applying changes.
Both methods are for intermediate Linux users.

Method 1: Using overlay filesystem and bind mount

This method needs a separate partition or disk (USB stick for example) which is used for reading and writing.

The raspberry operating system has the function to make the filesystem read-only built in, using an overlay filesystem.
That function makes the whole filesystem read-only.
Exceptions from that can be made using co called bind mounts.

Overview

  • Main filesystem is read-only
  • settings and audiofolders are stored on a separate partition or disk
  • settings folders and audiofolders are bind mounted read-write at their original places (at /home/pi/RPi-Jukebox-RFID/settings/ and /home/pi/RPi-Jukebox-RFID/shared/)

Implementation

This is just an overview of the things to do. It assumes that the Phoniebox has an SD-Card only.
Please check your situation and adopt these instructions to your needs.

There are guides for this on the internet - alternatively ask an AI chatbot on how to do it

Create alternative location and copy existing settings and audiofolders

  1. Backup your Phoniebox
  2. Insert a USB drive or create a separate partition on your SD-Card and format either one using an ext4 filesystem

    A USB drive is most likely the easier method

  3. Create a folder to mount the partition or drive, like sudo mkdir /mnt/USB-disk
  4. Mount the new filesystem at the newly created folder (using mount /dev/sda1 /mnt/USB-disk)
  5. Copy audiofolders and settings to partition/drive like
    • cp --archive ~/RPi-Jukebox-RFID/settings/ /mnt/USB-disk/settings
    • cp --archive ~/RPi-Jukebox-RFID/shared/ /mnt/USB-disk/shared

    audiofolders are located in shared - shared contains some additional things that should be read-write, too
    double check that the old folders and the new folders have the same permission (check with ls -la ~/RPi-Jukebox-RFID/ and ls -la /mnt/USB-disk Potential output (identical for both, old and new folders)

    drwxrwxr-x  2 pi http     4096 Dec 25  2022 settings
    drwxrwxr-x  4 pi http     4096 Dec 25  2022 shared
    

Setup new locations on startup (boot)

  1. Setup autmatic mount of the usb stick on startup (boot) of the Phonibox in the file /etc/fstab like this:

    add a line like this - remember to adopt it to your needs, if needed. Check if your partition is different from /dev/sda1
    Don't change existing text, unless you exactly know what you're doing

    /dev/sda1       /mnt/USB-disk/   ext4    defaults        0       0
    
  2. Setup bind mounts to mount the settings and shared folder at startup at their old locations, but with read-write permissions in the file /etc/fstab like this:
    /media/pi/PHOBOX_STORAGE/shared/      /home/pi/RPi-Jukebox-RFID/shared/  none    bind    0       0
    /media/pi/PHOBOX_STORAGE/settings/      /home/pi/RPi-Jukebox-RFID/settings/  none    bind    0       0
    

    You can use the editor nano for this with sudo nano /etc/fstab. Read about how this editor works before.
    These folders are mounted back to their "old" locations to keep the Phoniebox software working. It assumes those folders are at /home/pi/RPi-Jukebox-RFID/. The bind mount "links" them to the USB stick now - this is invisible to the Phoniebox software, so they are just where they need to be from the software's perspective.

Enable read-only filesystem in raspi-config

  1. Open a terminal and enter sudo raspi-config
  2. In the following menu go to Advanced Options, then chose Overlay FS. Confirm to anable the overlay fs (read-only filesystem) by chosing yes
  3. Reboot

After reboot you can still make changes everywhere. Those changes are gone after another reboot.
To make permanent changes again:

  • Disable Overlay FS (read-only filesystem), then
  • reboot
  • Make your desired changes
  • Enable read-only filesystem again and
  • reboot

If you have issues with this guide, create an issue here

Method 2: Using mount and tmpfs

First we need to mount a read-write Partition to /home/pi/RPi-Jukebox-RFID/shared/. This can bei either a further partition on the SD card or an USB drive connected to the Pi. If you want to store the shared partition on the sd card you need to first resize the main partition. In a normal raspbian install there is one small partition and one that fills the rest of the card. There is a good HOWTO on how to resize the partition here: https://www.howtoforge.com/partitioning_with_gparted

Please resize the partition AFTER the first boot of teh systtem since Raspbian tries to resize the partition on the sd card to fill the whole card this process seems to fail if there is a partition in the way.

Before the new partition can be mounted the original shared directory with its contents needs to be moved out of the way:

cd ~/RPi-Jukebox-RFID/
mv shared shared.old
mkdir shared

Then we need to find out the PARTUUID of the newly created partition with the command blkid. It should produce the following output:

pi@JukeboxJan:~/RPi-Jukebox-RFID $ blkid
/dev/mmcblk0p1: LABEL="boot" UUID="9304-D9FD" TYPE="vfat" PARTUUID="9282f822-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="29075e46-f0d4-44e2-a9e7-55ac02d6e6cc" TYPE="ext4" PARTUUID="9282f822-02"
/dev/mmcblk0p3: LABEL="shared" UUID="821fcedf-b705-4515-a5e8-2e99beefc1bf" TYPE="ext4" PARTUUID="9282f822-03"

In order to mount the rw partition to ~/RPi-Jukebox-RFID/shared/ on bootup you need to add the following line to /etc/fstab Replace the PARTUUID with teh one from your output. (Don't forget you need to sudo vi /etc/fstab to edit the file)

PARTUUID="9282f822-03" /home/pi/RPi-Jukebox-RFID/shared ext4 defaults,noatime 0 2 

sudo mount -a will mount the new partition to its mountpoint.

Now adjust the ownership and permissions of the new shared directory:

sudo chown pi:www-data shared
sudo chmod 775 shared

Rsync the contents of shared.old into share:

rsync -av shared.old/* shared/

Some settings need to persist between boots, others are not necessarily persistend. Those that need to be persistent are symlinked to a new directory shared/settings, the others are symlinked to files in /var/tmp that will reside on a ramdisk.

ln -sf /var/tmp/Latest_RFID ~/RPi-Jukebox-RFID/settings
ln -sf /var/tmp/Latest_Folder_Played ~/RPi-Jukebox-RFID/settings
ln -sf /var/tmp/Latest_Playlist_Played ~/RPi-Jukebox-RFID/settings
ln -sf /var/tmp/latestID.txt ~/RPi-Jukebox-RFID/shared/

mkdir ~/RPi-Jukebox-RFID/shared/settings
mv Audio_Folders_Path Audio_Volume_Change_Step  Idle_Time_Before_Shutdown Max_Volume_Limit Playlists_Folders_Path Second_Swipe ShowCover ../shared/settings/
ln -sf ~/RPi-Jukebox-RFID/shared/settings/* ~/RPi-Jukebox-RFID/settings/

Add the following lines to the fstab:

sudo vi /etc/fstab

tmpfs           /var/log        tmpfs   nodev,nosuid          0 0
tmpfs           /var/lib/samba  tmpfs   nodev,nosuid          0 0
tmpfs           /var/lib/mpd  tmpfs   nodev,nosuid          0 0
tmpfs           /var/cache/samba  tmpfs   nodev,nosuid          0 0
tmpfs           /var/tmp        tmpfs   nodev,nosuid          0 0
tmpfs           /tmp            tmpfs   nodev,nosuid          0 0

If you have not yet created a password for the Samba User pi, do it now, before moving the files away:

sudo smbpasswd pi

Move Samba files away and create mount point for a tmpfs:

sudo mv /var/lib/samba /var/lib/samba.save
sudo mkdir /var/lib/samba

sudo vi /etc/tmpfiles.d/smb.conf

paste

#Type Path        Mode UID  GID  Age Argument
C     /var/lib/samba/private  - - - - /var/lib/samba.save/private
C     /var/lib/samba/account_policy.tdb - - - - /var/lib/samba.save/account_policy.tdb
C     /var/lib/samba/group_mapping.tdb - - - - /var/lib/samba.save/group_mapping.tdb
C     /var/lib/samba/printers - - - - /var/lib/samba.save/printers
C     /var/lib/samba/registry.tdb - - - - /var/lib/samba.save/registry.tdb
C     /var/lib/samba/share_info.tdb - - - - /var/lib/samba.save/share_info.tdb
C     /var/lib/samba/usershares - - - - /var/lib/samba.save/usershares
C     /var/lib/samba/wins.dat - - - - /var/lib/samba.save/wins.dat
C     /var/lib/samba/wins.tdb - - - - /var/lib/samba.save/wins.tdb
d     /var/log/samba  0750 root adm - -

sudo vi /etc/tmpfiles.d/lighttpd.conf

paste

#Type Path        Mode UID  GID  Age Argument
d     /var/log/lighttpd  0755 www-data www-data - -

Change settings/Playlist_Folders_Path to shared/playlists

Edit /etc/mpd.conf and change log_file from /var/log/mpd/mpd.log to /var/log/mpd.log

If you need to make changes to your root filesystem in the future you need to remount it in read-write mode.

This can be done with the command mount -o remount,rw / and reversed by mount -oremount,to /

Sources: