拒绝树莓派断电无法启动

创建影子系统

  • 背景:raspberry 意外断电后会导致 SD 卡文件内容丢失,造成无法启动。影子系统可以解决每次重启后恢复至初始状态,类似网吧的重启还原功能。也类似于Windows PE的内存挂载,将相关的内容挂载在内存使用
  • 基于raspberry 版本:2020-02-13-raspbian-buster
  • 参考信息来源:https://www.raspberrypi.org/forums/viewtopic.php?t=173063#p1151405
  • 建议使用 root 用户登录,避免一些不必要的授权(如:不需要使用sudo来执行相关命令)

修改root用户

# 修改为 root 用户,输入2次密码即可,之后重新登录
sudo passwd root
# 如果要使用 ssh 远程登录 root 还得修改 ssh 配置文件,追加一条:PermitRootLogin yes。默认是禁用的
vi /etc/ssh/sshd_config
1.更新系统及安装自己需要的软件
apt update
apt upgrade -y
2.禁用交换空间
dphys-swapfile swapoff
dphys-swapfile uninstall
update-rc.d dphys-swapfile remove
3.创建并拷贝overlayRoot.sh脚本

临时找个目录生成空的 overlayRoot.sh 文件

# 生成 overlayRoot.sh 文件
touch overlayRoot.sh
# 编辑 overlayRoot.sh 文件
vi overlayRoot.sh 

以上生成文件成功之后,拷贝以下内容至overlayRoot.sh文件中

#!/bin/sh
# Read-only Root-FS for Raspian using overlayfs
# Version 1.0
#
# Created 2017 by Pascal Suter @ DALCO AG, Switzerland
# to work on Raspian as custom init script
# (raspbian does not use an initramfs on boot)
#
# Modified 2017-Apr-21 by Tony McBeardsley 
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
#
#
# Tested with Raspbian mini, 2017-01-11
#
# This script will mount the root filesystem read-only and overlay it with a temporary tempfs 
# which is read-write mounted. This is done using the overlayFS which is part of the linux kernel 
# since version 3.18. 
# when this script is in use, all changes made to anywhere in the root filesystem mount will be lost 
# upon reboot of the system. The SD card will only be accessed as read-only drive, which significantly
# helps to prolong its life and prevent filesystem coruption in environments where the system is usually
# not shut down properly 
#
# Install: 
# copy this script to /sbin/overlayRoot.sh and add "init=/sbin/overlayRoot.sh" to the 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 
# not make sens as the swap file will be stored in the tempfs which again resides in the ram.
# run these commands on the booted raspberry pi BEFORE you set the init=/sbin/overlayRoot.sh boot option:
# sudo dphys-swapfile swapoff
# sudo dphys-swapfile uninstall
# sudo update-rc.d dphys-swapfile remove
#
# To install software, run upgrades and do other changes to the raspberry setup, simply remove the init= 
# entry from the cmdline.txt file and reboot, make the changes, add the init= entry and reboot once more. 

fail(){
 echo -e "$1"
 /bin/bash
}


# Load overlay module
modprobe overlay
if [ $? -ne 0 ]; then
    fail "ERROR: missing overlay kernel module"
fi


# Mount /proc
mount -t proc proc /proc
if [ $? -ne 0 ]; then
    fail "ERROR: could not mount proc"
fi


# Create a writable fs on /mnt to then create our mountpoints 
mount -t tmpfs inittemp /mnt
if [ $? -ne 0 ]; then
    fail "ERROR: could not create a temporary filesystem to mount the base filesystems for overlayfs"
fi


# Mount a tmpfs under /mnt/rw
mkdir /mnt/rw
mount -t tmpfs root-rw /mnt/rw
if [ $? -ne 0 ]; then
    fail "ERROR: could not create tempfs for upper filesystem"
fi



# Identify root fs device, PARTUUID, mount options and fs type

#rootDev=`blkid -o list | awk '$3 == "/" {print $1}'`
# Changed here(point to / ) in case the cmd above doesn't work # By ChenYang 20171122
rootDev=/dev/mmcblk0p2
rootPARTUUID=`awk '$2 == "/" {print $1}' /etc/fstab`
rootMountOpt=`awk '$2 == "/" {print $4}' /etc/fstab`
rootFsType=`awk '$2 == "/" {print $3}' /etc/fstab`


# Mount original root filesystem readonly under /mnt/lower
mkdir /mnt/lower
mount -t ${rootFsType} -o ${rootMountOpt},ro ${rootDev} /mnt/lower
if [ $? -ne 0 ]; then
    fail "ERROR: could not ro-mount original root partition"
fi


# Mount the overlay filesystem
mkdir /mnt/rw/upper
mkdir /mnt/rw/work
mkdir /mnt/newroot
mount -t overlay -o lowerdir=/mnt/lower,upperdir=/mnt/rw/upper,workdir=/mnt/rw/work overlayfs-root /mnt/newroot
if [ $? -ne 0 ]; then
    fail "ERROR: could not mount overlayFS"
fi


# Create mountpoints inside the new root filesystem-overlay
mkdir /mnt/newroot/ro
mkdir /mnt/newroot/rw

# Remove root mount from fstab (this is already a non-permanent modification)
grep -v "$rootPARTUUID" /mnt/lower/etc/fstab > /mnt/newroot/etc/fstab
echo "#the original root mount has been removed by overlayRoot.sh" >> /mnt/newroot/etc/fstab
echo "#this is only a temporary modification, the original fstab" >> /mnt/newroot/etc/fstab
echo "#stored on the disk can be found in /ro/etc/fstab" >> /mnt/newroot/etc/fstab


# Change to the new overlay root
cd /mnt/newroot
pivot_root . mnt
exec chroot . sh -c "$(cat <<END

 # Move ro and rw mounts to the new root
 mount --move /mnt/mnt/lower/ /ro
 if [ $? -ne 0 ]; then
     echo "ERROR: could not move ro-root into newroot"
     /bin/bash
 fi
 mount --move /mnt/mnt/rw /rw
 if [ $? -ne 0 ]; then
     echo "ERROR: could not move tempfs rw mount into newroot"
     /bin/bash
 fi

 # Unmount unneeded mounts so we can unmout the old readonly root
 umount /mnt/mnt
 umount /mnt/proc
 umount /mnt/dev
 umount /mnt

 # Continue with regular init
 exec /sbin/init
END
)"

拷贝生成的脚本至 /sbin 目录,并授权

# 拷贝脚本至 /sbin
cp overlayRoot.sh /sbin/overlayRoot.sh
# 授权脚本
chmod a+x /sbin/overlayRoot.sh
4.修改 cmdline.txt 文件

注意,修改该文件后,不要重启。重启后,即为影子系统了。任何新生文件或安装应用重启后均消失

# 打开 cmdline.txt
vi /boot/cmdline.txt

文件末尾追加以下内容: init=/sbin/overlayRoot.sh

追加前

➜ ~ cat /boot/cmdline.txt
console=serial0,115200 console=tty1 root=PARTUUID=ea7d04d6-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
➜ ~ 

追加后

➜ ~ cat /boot/cmdline.txt
console=serial0,115200 console=tty1 root=PARTUUID=ea7d04d6-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles init=/sbin/overlayRoot.sh
➜ ~ 
5.修改boot分区为只读

为了保证整个 SD 卡都为只读,必须要单独修改 boot 分区为只读。否则默认 boot 分区仍然不是只读,依然可以写文件,重启后不会丢失,如此一来有风险造成意外断电 boot 分区文件丢失,导致无法启动。

# 打开 fstab,修改 boot 为只读
vi /etc/fstab

修改前

➜ ~ cat /etc/fstab       
proc /proc proc defaults 0 0
PARTUUID=ea7d04d6-01 /boot vfat defaults 0 2
PARTUUID=ea7d04d6-02 / ext4 defaults,noatime 0 1
# a swapfile is not a swap partition, no line here
# use dphys-swapfile swap[on|off] for that
➜ ~

修改后

➜ ~ cat /etc/fstab
proc /proc proc defaults 0 0
PARTUUID=ea7d04d6-01 /boot vfat defaults,ro 0 2
PARTUUID=ea7d04d6-02 / ext4 defaults,noatime 0 1
# a swapfile is not a swap partition, no line here
# use dphys-swapfile swap[on|off] for that
➜ ~

以上操作后,可以通过 mount 命令来查看各分区的读写状态,以便观察相关分区都为只读。

撤消影子系统(更新系统或重新安装软件时需要)

1.remount boot 分区为可写
mount -o remount,rw /boot
2.删除 cmdline.txt init 属性
vi /boot/cmdline.txt

删除前

➜ ~ cat /boot/cmdline.txt
console=serial0,115200 console=tty1 root=PARTUUID=ea7d04d6-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles init=/sbin/overlayRoot.sh
➜ ~ 

删除后

➜ ~ cat /boot/cmdline.txt
console=serial0,115200 console=tty1 root=PARTUUID=ea7d04d6-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
➜ ~ 

以上修改后,重启即可生效

3.如果想修改 boot 分区内容,请修改 /etc/fstab 文件
# 打开 fstab
vi /etc/fstab

修改前

➜ ~ cat /etc/fstab
proc /proc proc defaults 0 0
PARTUUID=ea7d04d6-01 /boot vfat defaults,ro 0 2
PARTUUID=ea7d04d6-02 / ext4 defaults,noatime 0 1
# a swapfile is not a swap partition, no line here
# use dphys-swapfile swap[on|off] for that
➜ ~

修改后

➜ ~ cat /etc/fstab       
proc /proc proc defaults 0 0
PARTUUID=ea7d04d6-01 /boot vfat defaults 0 2
PARTUUID=ea7d04d6-02 / ext4 defaults,noatime 0 1
# a swapfile is not a swap partition, no line here
# use dphys-swapfile swap[on|off] for that
➜ ~
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容