Squash File System over OverlayFS
Forked from: https://wiki.psuter.ch/doku.php?id=solve_raspbian_sd_card_corruption_issues_with_read-only_mounted_root_partition
/src
├── overlay
│ └── overlayRoot.sh # Original script v1.1 Created by Pascal Suter
│
└── overlay_sfs # Custom resources
├── boot # Contain boot files
│ ├── cmdline-no_overlay.txt # Boot Raspberry without OverlayFS
│ └── cmdline-overlay.txt # Boot Raspberry with OverlayFS
└── sbin # Script to create OverlayFS and SquashFS
└── overlayRoot.sh # SquashFS over OverlayFS script v1.4
NOTE: This script has been tested with Raspbian OS (32-bit) Lite (Debian Buster)
-
First of all, create the folder structure:
/lib/live/ ├── mounted │ ├── ro # Where Lower overlay folder is located │ ├── rw # Where Upper and Work overlay folder is located │ └── squashed # Where .sfs file is unsquashed └── squashfs # Where .sfs file is located
To do that:
sudo -s mkdir /lib/live mkdir /lib/live/mounted mkdir /lib/live/mounted/ro mkdir /lib/live/mounted/rw mkdir /lib/live/mounted/squashed mkdir /lib/live/squahfs
-
Install Squasfs
sudo apt-get install squashfs-tools -y
-
Copy overlayRoot.sh script into /sbin/
- I usually copy from my computer to Raspberry /home/user path using:
# From computer terminal sudo scp -r /pc_overlayRoot rsp_user@rsp_ip_address:/destination_path
- After copy the script, simply move to /sbin path runing:
sudo mv /home/user/overlayRoot.sh /sbin/
- Make it executable:
chmod +x /sbin/overlayRoot.sh
- I usually copy from my computer to Raspberry /home/user path using:
-
Disable swap
dphys-swapfile swapoff dphys-swapfile uninstall update-rc.d dphys-swapfile remove systemctl stop dphys-swapfile systemctl disable dphys-swapfile
-
Change your boot parameter in /boot/cmdline.txt. Adding
init=/sbin/overlayRoot.sh
:- cmdline.txt with OverlayFS
console=serial0,115200 console=tty1 root=PARTUUID=97652995-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait init=/sbin/overlayRoot.sh
- cmdline.txt without OverlayFS
console=serial0,115200 console=tty1 root=PARTUUID=97652995-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
NOTE: Can copy cmdline-overlay.txt and cmdline-no_overlay.txt from this repository and paste in your system
-
Reboot time }: )
reboot
Copy cmdline-no_overlay.txt and cmdline-overlay.txt into /boot/ path.
After runing the script /boot/ partition is mounted as a RW. To Enable or Disable OverlayFS:
- Enable OverlayFS:
cp /boot/cmdline-overlay.txt /boot/cmdline.txt
- Disable OverlayFS:
cp /boot/cmdline-no_overlay.txt /boot/cmdline.txt
- After modify cmdline.txt file reboot Rapsberry
sudo reboot
For this example I've created a 3 test files which will be located in /home and /etc folders after mount OverlayFS.
- Create a temp folder in /home/user (for exemple):
sudo -s mkdir /home/user/squahsfs_tmp
- Inside the squash file we will add a file which will be located inside /etc/ and /home/ folders (just for testing), so we should create 2 new folders inside squashfs_tmp:
cd /home/user/squahsfs_tmp mkdir etc mkdir home
- Create some test files inside this folders:
touch etc/hello_etc touch home/home_file_01 touch home/home_file_01
- We've implemented the changes so is time to create a SFS file:
This command generate a sfs file call file_system.sfs based in squahfs_tmp folder content.
cd .. # We're located in /home/user path right now squashfs squahsfs_tmp file_system.sfs -comp xz # Creating SFS File }:)
- In the script the /lib/live/squahsfs directory is defined as the location where the file will be found the squash files so we should move the file.
mv /home/user/squahsfs_tmp /lib/live/squashfs
- Remove squasfs_tmp folder
rm -rf /home/user/squashfs_tmp
- Enable overlay
cp /boot/cmdline-overlay.txt /boot/cmdline.txt
- Reboot time }: )
reboot
- If everything has worked as we expected we will see:
ls /home ├── home_file_01 ├── home_file_02 └── other /home files... ls /etc ├── hello_etc └── other /etc files...
Update squashs file adding or removing some file. To do that:
- Reboot system without OverlayFS
sudo -s cp /boot/cmdline-no_overlay.txt /boot/cmdline.txt reboot
- Go to squashfs default path
sudo -s cd /lib/live/squashfs
- Unsquash file_system.sfs
After run this command a folder squashfs-root will appear.
unsquashfs -f file_system.sfs
/lib/live/squashfs ├── file_sytem.sfs └── squashfs-root
- Modify squashfs-root content removing home_file_02 (for exemple)
rm -rf squashfs-root/home/home_file_02
- Remove old file_system.sfs
rm -rf file_system.sfs
- Create the new sfs file
squashfs squashfs-root file_system.sfs -comp xz
- Now modify cmdline.txt to mount overlayFS after reboot and apply the sfs changes
cp /boot/cmdline-overlay.txt /boot/cmdline.txt
- Rebot time }: )
reboot
The other way to modify squashfs:
- Remount RO partition
sudo -s mount -o remount,rw /lib/live/mounted/ro
- Go to squashfs folder
cd /lib/live/mounted/ro/lib/live/squashfs
- Repeat steps from 3 until 8 indicated in the previous section
- Twitter. @0xDA_bit
- Github. OxDAbit
- Mail. oxdabit@protonmail.com