GeekFunkLabs / squishbox

Software and design files for a Raspberry Pi sound module enclosure
20 stars 3 forks source link

Make read-only filesystem an option for easier shutdown #11

Open albedozero opened 6 months ago

albedozero commented 6 months ago

Also (but this connects with another big topic so happy to have it somewhere else than here), I have seen another project with the system in read only which makes it easier to shutdown (no way to corrupt a file so no need a command to power it off). If this sounds interesting to you then I would suggest to create another partition (rw) and install SquishBox files there while keeping the system partition as read only.

Originally posted by @cpu150 in https://github.com/GeekFunkLabs/squishbox/issues/7#issuecomment-2119235838

albedozero commented 6 months ago

Locking up the user's file system is a bit too drastic I think. My philosophy is that the Pi is a computer, so if you want to unplug it without shutting down properly you accept the risks. In practice you can just unplug it most of the time with no ill effects - as long as the SD card isn't being written, which is indicated by the green LED and only happens when loading or saving a bank, there's little chance of corruption.

This is suggested/requested often, so maybe it's worth putting the procedure in the user manual as an option for power users. I'm planning some updates/revisions to the manual anyway (see #2)

cpu150 commented 5 months ago

@albedozero I am documenting what I did in case it could help.

The command to boot the system as read only: sudo raspi-config nonint do_overlayfs 0 (you revert with: sudo raspi-config nonint do_overlayfs 1) Reboot to apply the change: sudo reboot

As the project is today it seems working fine as soon as there is no bank change. (Probably because fluidpatcher tries to write the config)

This is where it starts to get tricky. To solve this issue you could install SquishBox RW partition, which could be a USB key. This is the most straightforward solution I can think about but I guess it misses the point of the project which tend to have something small and all in a box.

The solution I came across (more portable than the above one but more complicated) is to create another partition on the SD card. I am running Bookworm distrib. During the first boot the Raspberry Pi will resize the root partition to make it fill entirely the SD card size. I did not find a way to avoid that so I had to shrink back the root partition. I have looked at resizing the root partition while the system is running but did not have luck. I found that I can use initramfs to resize the partition before the root partition get mounted (I think this what it is used to expand the root partition at the first boot). I tried few things unfortunately I never got it running while booting... So I got another Linux and plugged the SD card as USB key.

Some useful commands: uname -r - Prints the kernel release (useful if while using initramfs) df -h - Checks disk usage

WARNING: The following could end up by loosing everything if it fails. It worth backing up everything before. Shrink the root partition:

  1. Get partition using lsblk (in my case the SD card is /dev/sda and the partition I want to resize is /dev/sda2)
  2. Clean filesystem up: sudo e2fsck -yf /dev/sda2
  3. Resize filesystem: sudo resize2fs /dev/sda2 6G (in my case I shrink it down to 6 GB) If getting this message: resize2fs: New size smaller than minimum (1616971) Then it means it is too small
  4. List details of each partitions using sudo parted /dev/sda print Output example: Number Start End Size Type File system Flags 1 4194kB 541MB 537MB primary fat32 lba 2 541MB 62.3GB 61.7GB primary ext4
  5. Resize partition: sudo parted /dev/sda resizepart 2 6541MB 2 is the partition number 6542MB is where I want the partition to end: Start of the partition (in this case 541MB) + size I want (in my case 6GB == 6000MB) + 1MB
  6. Clean it up again: sudo e2fsck -f /dev/sda2 I got asked: Abort<y>? I answered no When I got asked one of theses: Ignore error<y>? Force rewrite<y>? Fix<y>? It failed for me. (not booting anymore)
  7. And finally: sudo resize2fs -f /dev/sda2

If you are going with the initramfs solution then you want to execute the steps 2 and 3 in your initramfs script.

Create a new partition: (I want another 6GB == 6000MB partition)

  1. sudo parted /dev/sda mkpart p ext4 6542MB 12542MB 6542MB End of Prev Partition 12542MB End of partition: Start (6542MB) + 6000MB
  2. Format partition as ext4 sudo mkfs.ext4 /dev/sda3
  3. Boot on the SD card
  4. mkdir ~/rw_disk
  5. Get the new partition using lsblk (in my case /dev/mmcblk0p3)
  6. Edit /etc/fstab and add: /dev/mmcblk0p3 /home/pi/rw_disk ext4 defaults,noatime 0 0 Change /dev/mmcblk0p3 and /home/pi/rw_disk as per your setup
  7. sudo reboot
  8. sudo mkdir ~/rw_disk/SquishBox and sudo chown pi:pi -R ~/rw_disk/SquishBox
  9. Install SquishBox to ~/rw_disk/SquishBox directory
  10. Setup the system as read only: sudo raspi-config nonint do_overlayfs 0
  11. sudo reboot