Projet de remplacement du "RPiPasserelle" d'Otec.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

read-only-fs.sh 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/bin/bash
  2. # CREDIT TO THESE TUTORIALS:
  3. # petr.io/en/blog/2015/11/09/read-only-raspberry-pi-with-jessie
  4. # hallard.me/raspberry-pi-read-only
  5. # k3a.me/how-to-make-raspberrypi-truly-read-only-reliable-and-trouble-free
  6. if [ $(id -u) -ne 0 ]; then
  7. echo "Installer must be run as root."
  8. echo "Try 'sudo bash $0'"
  9. exit 1
  10. fi
  11. clear
  12. echo "This script configures a Raspberry Pi"
  13. echo "SD card to boot into read-only mode,"
  14. echo "obviating need for clean shutdown."
  15. echo "NO FILES ON THE CARD CAN BE CHANGED"
  16. echo "WHEN PI IS BOOTED IN THIS STATE. Either"
  17. echo "the filesystems must be remounted in"
  18. echo "read/write mode, card must be mounted"
  19. echo "R/W on another system, or an optional"
  20. echo "jumper can be used to enable read/write"
  21. echo "on boot."
  22. echo
  23. echo "Links to original tutorials are in"
  24. echo "script source. THIS IS A ONE-WAY"
  25. echo "OPERATION. THERE IS NO SCRIPT TO"
  26. echo "REVERSE THIS SETUP! ALL other system"
  27. echo "config should be complete before using"
  28. echo "this script. MAKE A BACKUP FIRST."
  29. echo
  30. echo "Run time ~5 minutes. Reboot required."
  31. echo
  32. echo -n "CONTINUE? [y/N] "
  33. read
  34. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  35. echo "Canceled."
  36. exit 0
  37. fi
  38. # FEATURE PROMPTS ----------------------------------------------------------
  39. # Installation doesn't begin until after all user input is taken.
  40. INSTALL_RW_JUMPER=0
  41. INSTALL_HALT=0
  42. INSTALL_WATCHDOG=0
  43. # Given a list of strings representing options, display each option
  44. # preceded by a number (1 to N), display a prompt, check input until
  45. # a valid number within the selection range is entered.
  46. selectN() {
  47. for ((i=1; i<=$#; i++)); do
  48. echo $i. ${!i}
  49. done
  50. echo
  51. REPLY=""
  52. while :
  53. do
  54. echo -n "SELECT 1-$#: "
  55. read
  56. if [[ $REPLY -ge 1 ]] && [[ $REPLY -le $# ]]; then
  57. return $REPLY
  58. fi
  59. done
  60. }
  61. SYS_TYPES=(Pi\ 3\ /\ Pi\ Zero\ W All\ other\ models)
  62. WATCHDOG_MODULES=(bcm2835_wdog bcm2708_wdog)
  63. OPTION_NAMES=(NO YES)
  64. # echo -n "Enable boot-time read/write jumper? [y/N] "
  65. # read
  66. # if [[ "$REPLY" =~ (yes|y|Y)$ ]]; then
  67. # INSTALL_RW_JUMPER=1
  68. # echo -n "GPIO pin for R/W jumper: "
  69. # read
  70. # RW_PIN=$REPLY
  71. # fi
  72. #
  73. # echo -n "Install GPIO-halt utility? [y/N] "
  74. # read
  75. # if [[ "$REPLY" =~ (yes|y|Y)$ ]]; then
  76. # INSTALL_HALT=1
  77. # echo -n "GPIO pin for halt button: "
  78. # read
  79. # HALT_PIN=$REPLY
  80. # fi
  81. #
  82. # echo -n "Enable kernel panic watchdog? [y/N] "
  83. # read
  84. # if [[ "$REPLY" =~ (yes|y|Y)$ ]]; then
  85. # INSTALL_WATCHDOG=1
  86. # echo "Target system type:"
  87. # selectN "${SYS_TYPES[0]}" \
  88. # "${SYS_TYPES[1]}"
  89. # WD_TARGET=$?
  90. # fi
  91. # VERIFY SELECTIONS BEFORE CONTINUING --------------------------------------
  92. # echo
  93. # if [ $INSTALL_RW_JUMPER -eq 1 ]; then
  94. # echo "Boot-time R/W jumper: YES (GPIO$RW_PIN)"
  95. # else
  96. # echo "Boot-time R/W jumper: NO"
  97. # fi
  98. # if [ $INSTALL_HALT -eq 1 ]; then
  99. # echo "Install GPIO-halt: YES (GPIO$HALT_PIN)"
  100. # else
  101. # echo "Install GPIO-halt: NO"
  102. # fi
  103. # if [ $INSTALL_WATCHDOG -eq 1 ]; then
  104. # echo "Enable watchdog: YES (${SYS_TYPES[WD_TARGET-1]})"
  105. # else
  106. # echo "Enable watchdog: NO"
  107. # fi
  108. # echo
  109. # echo -n "CONTINUE? [y/N] "
  110. # read
  111. # if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  112. # echo "Canceled."
  113. # exit 0
  114. # fi
  115. # START INSTALL ------------------------------------------------------------
  116. # All selections have been validated at this point...
  117. # Given a filename, a regex pattern to match and a replacement string:
  118. # Replace string if found, else no change.
  119. # (# $1 = filename, $2 = pattern to match, $3 = replacement)
  120. replace() {
  121. grep $2 $1 >/dev/null
  122. if [ $? -eq 0 ]; then
  123. # Pattern found; replace in file
  124. sed -i "s/$2/$3/g" $1 >/dev/null
  125. fi
  126. }
  127. # Given a filename, a regex pattern to match and a replacement string:
  128. # If found, perform replacement, else append file w/replacement on new line.
  129. replaceAppend() {
  130. grep $2 $1 >/dev/null
  131. if [ $? -eq 0 ]; then
  132. # Pattern found; replace in file
  133. sed -i "s/$2/$3/g" $1 >/dev/null
  134. else
  135. # Not found; append on new line (silently)
  136. echo $3 | sudo tee -a $1 >/dev/null
  137. fi
  138. }
  139. # Given a filename, a regex pattern to match and a string:
  140. # If found, no change, else append file with string on new line.
  141. append1() {
  142. grep $2 $1 >/dev/null
  143. if [ $? -ne 0 ]; then
  144. # Not found; append on new line (silently)
  145. echo $3 | sudo tee -a $1 >/dev/null
  146. fi
  147. }
  148. # Given a filename, a regex pattern to match and a string:
  149. # If found, no change, else append space + string to last line --
  150. # this is used for the single-line /boot/cmdline.txt file.
  151. append2() {
  152. grep $2 $1 >/dev/null
  153. if [ $? -ne 0 ]; then
  154. # Not found; insert in file before EOF
  155. sed -i "s/\'/ $3/g" $1 >/dev/null
  156. fi
  157. }
  158. echo
  159. echo "Starting installation..."
  160. echo "Removing unwanted packages..."
  161. #apt-get remove -y --force-yes --purge triggerhappy logrotate dbus \
  162. # dphys-swapfile xserver-common lightdm fake-hwclock
  163. # Let's keep dbus...that includes avahi-daemon, a la 'raspberrypi.local',
  164. # also keeping xserver & lightdm for GUI login (WIP, not working yet)
  165. apt-get remove -y --force-yes --purge triggerhappy logrotate \
  166. dphys-swapfile fake-hwclock
  167. apt-get -y --force-yes autoremove --purge
  168. # Replace log management with busybox (use logread if needed)
  169. echo "Installing ntp and busybox-syslogd..."
  170. apt-get -y --force-yes install ntp busybox-syslogd; dpkg --purge rsyslog
  171. echo "Configuring system..."
  172. # Add fastboot, noswap and/or ro to end of /boot/cmdline.txt
  173. append2 /boot/cmdline.txt fastboot fastboot
  174. append2 /boot/cmdline.txt noswap noswap
  175. append2 /boot/cmdline.txt ro^o^t ro
  176. # Move /var/spool to /tmp
  177. rm -rf /var/spool
  178. ln -s /tmp /var/spool
  179. # Change spool permissions in var.conf (rondie/Margaret fix)
  180. replace /usr/lib/tmpfiles.d/var.conf "spool\s*0755" "spool 1777"
  181. # Move dhcpd.resolv.conf to tmpfs
  182. touch /tmp/dhcpcd.resolv.conf
  183. rm /etc/resolv.conf
  184. ln -s /tmp/dhcpcd.resolv.conf /etc/resolv.conf
  185. # Make edits to fstab
  186. replace /etc/fstab "ext4\s*defaults,noatime\s" "ext4 defaults,noatime,ro "
  187. cat << EOF >> /etc/fstab
  188. tmpfs /var/log tmpfs nodev,nosuid 0 0
  189. tmpfs /var/tmp tmpfs nodev,nosuid 0 0
  190. tmpfs /var/lib/ntp tmpfs nodev,nosuid 0 0
  191. tmpfs /var/lib/pyheatpump tmpfs nodev,nosuid 0 0
  192. tmpfs /var/run tmpfs nodev,nosuid 0 0
  193. tmpfs /tmp tmpfs nodev,nosuid 0 0
  194. EOF
  195. # PROMPT FOR REBOOT --------------------------------------------------------
  196. echo "Done."
  197. echo
  198. echo "Settings take effect on next boot."
  199. echo
  200. echo -n "REBOOT NOW? [y/N] "
  201. read
  202. if [[ ! "$REPLY" =~ ^(yes|y|Y)$ ]]; then
  203. echo "Exiting without reboot."
  204. exit 0
  205. fi
  206. echo "Reboot started..."
  207. reboot
  208. exit 0