solve_raspbian_sd_card_corruption_issues_with_read-only_mounted_root_partition

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
solve_raspbian_sd_card_corruption_issues_with_read-only_mounted_root_partition [30.01.2017 14:46] Pascal Sutersolve_raspbian_sd_card_corruption_issues_with_read-only_mounted_root_partition [25.10.2018 08:12] (current) – [The Script] Pascal Suter
Line 14: Line 14:
 sudo dphys-swapfile uninstall sudo dphys-swapfile uninstall
 sudo update-rc.d dphys-swapfile remove</code> sudo update-rc.d dphys-swapfile remove</code>
 +  * after you have set up and configured your raspberry pi the way you want it, simply activate the overlayRoot script as alternate init script by adding this entry to the end of your cmdline.txt file in the boot partition of the raspbian sd card: <code>init=/sbin/overlayRoot.sh</code> then reboot 
 +  * to make changes, simply remove the entry above and reboot and your raspberry is writeable again. then re-enable and reboot for it to be readonly again. 
  
 ===== The Script ===== ===== The Script =====
Line 19: Line 21:
 #!/bin/sh #!/bin/sh
 #  Read-only Root-FS for Raspian using overlayfs #  Read-only Root-FS for Raspian using overlayfs
-#  Version 1.0+#  Version 1.1 
 +
 +#  Version History: 
 +#  1.0: initial release 
 +#  1.1: adopted new fstab style with PARTUUID. the script will now look for a /dev/xyz definiton first  
 +#       (old raspbian), if that is not found, it will look for a partition with LABEL=rootfs, if that 
 +#       is not found it look for a PARTUUID string in fstab for / and convert that to a device name 
 +#       using the blkid command. 
 # #
 #  Created 2017 by Pascal Suter @ DALCO AG, Switzerland to work on Raspian as custom init script #  Created 2017 by Pascal Suter @ DALCO AG, Switzerland to work on Raspian as custom init script
Line 39: Line 48:
 # #
 # #
-#  Tested with Raspbian mini, 2017-01-11+#  Tested with Raspbian mini, 2018-10-09
 # #
 #  This script will mount the root filesystem read-only and overlay it with a temporary tempfs  #  This script will mount the root filesystem read-only and overlay it with a temporary tempfs 
Line 50: Line 59:
 # #
 #  Install:  #  Install: 
-#  copy this script to /sbin/overlayRoot.sh and add "init=/sbin/overlayRoot.sh" to the cmdline.txt  +#  copy this script to /sbin/overlayRoot.sh, make it executable and add "init=/sbin/overlayRoot.sh" to the  
-#  file in the raspbian image's boot partition. +#  cmdline.txt file in the raspbian image's boot partition. 
 #  I strongly recommend to disable swapping before using this. it will work with swap but that just does  #  I strongly recommend to disable swapping before using this. it will work with swap but that just does 
 #  not make sens as the swap file will be stored in the tempfs which again resides in the ram. #  not make sens as the swap file will be stored in the tempfs which again resides in the ram.
Line 95: Line 104:
 rootMountOpt=`awk '$2 == "/" {print $4}' /etc/fstab` rootMountOpt=`awk '$2 == "/" {print $4}' /etc/fstab`
 rootFsType=`awk '$2 == "/" {print $3}' /etc/fstab` rootFsType=`awk '$2 == "/" {print $3}' /etc/fstab`
 +echo "check if we can locate the root device based on fstab"
 +blkid $rootDev
 +if [ $? -gt 0 ]; then
 +    echo "no success, try if a filesystem with label 'rootfs' is avaialble"
 +    rootDevFstab=$rootDev
 +    rootDev=`blkid -L "rootfs"`
 +    if [ $? -gt 0 ]; then
 +        echo "no luck either, try to further parse fstab's root device definition"
 +        echo "try if fstab contains a PARTUUID definition"
 +        echo "$rootDevFstab" | grep 'PARTUUID=\(.*\)-\([0-9]\{2\}\)'
 +        if [ $? -gt 0 ]; then 
 +     fail "could not find a root filesystem device in fstab. Make sure that fstab contains a device definition or a PARTUUID entry for / or that the root filesystem has a label 'rootfs' assigned to it"
 +        fi
 +        device=""
 +        partition=""
 +        eval `echo "$rootDevFstab" | sed -e 's/PARTUUID=\(.*\)-\([0-9]\{2\}\)/device=\1;partition=\2/'`
 +        rootDev=`blkid -t "PTUUID=$device" | awk -F : '{print $1}'`p$(($partition))
 +        blkid $rootDev
 +        if [ $? -gt 0 ]; then
 +     fail "The PARTUUID entry in fstab could not be converted into a valid device name. Make sure that fstab contains a device definition or a PARTUUID entry for / or that the root filesystem has a label 'rootfs' assigned to it"
 +        fi
 +    fi
 +fi
 mount -t ${rootFsType} -o ${rootMountOpt},ro ${rootDev} /mnt/lower mount -t ${rootFsType} -o ${rootMountOpt},ro ${rootDev} /mnt/lower
 if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
  • solve_raspbian_sd_card_corruption_issues_with_read-only_mounted_root_partition.txt
  • Last modified: 25.10.2018 08:12
  • by Pascal Suter