实现在树莓派的GPIO接口上用跳线(短路帽)轻松选择启用或者禁用overlayfs影子系统
前面我发过一篇blog说明如何使用overlayfs打造一个只读的不怕意外关机的树莓派Raspberry Pi。
https://blog.csdn.net/zhufu86/article/details/78906046
下面我来实现在树莓派的GPIO接口上用跳线(短路帽)选择启用或者禁用overlayfs影子系统。
【接线】
我是用了用了如下材料
--- 跳线帽(短路帽) * 1
--- 连接线 * 2 (可选)
--- LED * 1 (可选)
--- 电阻 1k欧姆 * 1 (可选)
如上图所示,在BCM GPIO 4 以及和右边的一位针脚(GND)上使用跳线帽,在BCM GPIO 18和其右边的一位针脚(GND)上连接LED,并串联1K电阻,LED的阴极接GND,阳极接电阻,电阻的另一端接GPIO 18。
接LED是为了在启动时指示脚本的运行状态,并不是必须要接的,如果不接也可以。
【原理】
需要在原overlayRoot.sh脚本的基础上,代码开头增加检测GPIO端口的代码。
首先,要mount sysfs,以便于后面访问GPIO
mount -t sysfs sysfs /sys
if [ $? -ne 0 ]; thenfail "ERROR: could not mount sysfs"
fi
设置GPIO引脚变量,可以按照你自己的实际接线更改设置。
# The jumper is on BCM GPIO 4
GPIO_PIN_JP=4
# The LED is connected on BCM GPIO 18
GPIO_PIN_LED=18
定义闪LED的函数,功能是点亮LED 0.4秒钟并熄灭。
# Function - Flash/light-up the LED one time
FlashLED()
{# Export LED pinecho $GPIO_PIN_LED > /sys/class/gpio/export# Set LED pin as output portecho out > /sys/class/gpio/gpio$GPIO_PIN_LED/direction# Set LED pin output HIGHecho 1 > /sys/class/gpio/gpio$GPIO_PIN_LED/valuesleep 0.4# Set LED pin output LOWecho 0 > /sys/class/gpio/gpio$GPIO_PIN_LED/value# Set LED pin as input portecho in > /sys/class/gpio/gpio$GPIO_PIN_LED/direction# Unexport LED pinecho $GPIO_PIN_LED > /sys/class/gpio/unexportsleep 0.4
}
如果不打算连接LED,而且GPIO 18也可能别有他用,可以把这段代码改为无用代码:
# Function - Flash/light-up the LED one time
FlashLED()
{echo I am a LED.
}
后面的代码开始干正事儿。
首先点亮LED 0.4秒后熄灭。
# Flash the LED one time
FlashLED
然后每隔50ms检测一次有没有跳线短接GPIO 4到GND,连续检测10次
# Export Jumper pin
echo "$GPIO_PIN_JP" > /sys/class/gpio/export
# Set Jumper pin as input port
echo in > /sys/class/gpio/gpio$GPIO_PIN_JP/directioni=1
count=0while [ "$i" -le 10 ]
do# Detect the jumper pin every 0.05secsleep 0.05 # If the Jumper is shorted to GNDif [ $(cat "/sys/class/gpio/gpio${GPIO_PIN_JP}/value") = 0 ]; thencount=$((count+1))fii=$((i+1))
done
# Uncomment to print the count value for test purpose
#echo $count# Unexport the Jumper pin
echo $GPIO_PIN_JP > /sys/class/gpio/unexport
如果发现10次检测有9次以上为低电平(即GPIO短路到GND),则点亮LED 0.4秒后熄灭,然后继续常规的init过程(/sbin/init)而不开启overlayfs模式,否则继续后面的开启overlayfs模式的代码。 继续之前不忘记unmount sysfs。
if [ $count -ge 9 ]; then# if the Jumper is shorted to GND, exit to the normal init.# Flash the LED one timeFlashLEDumount /sysecho [Info.] Overlayfs jumper setting DISABLE.# Continue with regular initexec /sbin/init#exit 0elseumount /sysecho [Info.] Overlayfs jumper setting ENABLE.# Continue with overlayfs activation process.
fi
【完整的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
#
# * Modified 2018-Jul-18 by Yang Chen
#
# 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
}# Add the function +++
# Use a jumper on the Head connector to activate(OPEN) or
# deactivate(SHORT) the overlayfs mode
# Added by C.Yang 20180718
# jumper function code start @@@@@mount -t sysfs sysfs /sys
if [ $? -ne 0 ]; thenfail "ERROR: could not mount sysfs"
fi# The jumper is on BCM GPIO 4
GPIO_PIN_JP=4
# The LED is connected on BCM GPIO 18
GPIO_PIN_LED=18# Function - Flash/light-up the LED one time
FlashLED()
{# Export LED pinecho $GPIO_PIN_LED > /sys/class/gpio/export# Set LED pin as output portecho out > /sys/class/gpio/gpio$GPIO_PIN_LED/direction# Set LED pin output HIGHecho 1 > /sys/class/gpio/gpio$GPIO_PIN_LED/valuesleep 0.4# Set LED pin output LOWecho 0 > /sys/class/gpio/gpio$GPIO_PIN_LED/value# Set LED pin as input portecho in > /sys/class/gpio/gpio$GPIO_PIN_LED/direction# Unexport LED pinecho $GPIO_PIN_LED > /sys/class/gpio/unexportsleep 0.4
}# Flash the LED one time
FlashLED# Export Jumper pin
echo "$GPIO_PIN_JP" > /sys/class/gpio/export
# Set Jumper pin as input port
echo in > /sys/class/gpio/gpio$GPIO_PIN_JP/directioni=1
count=0while [ "$i" -le 10 ]
do# Detect the jumper pin every 0.05secsleep 0.05 # If the Jumper is shorted to GNDif [ $(cat "/sys/class/gpio/gpio${GPIO_PIN_JP}/value") = 0 ]; thencount=$((count+1))fii=$((i+1))
done
# Uncomment to print the count value for test purpose
#echo $count# Unexport the Jumper pin
echo $GPIO_PIN_JP > /sys/class/gpio/unexportif [ $count -ge 9 ]; then# if the Jumper is shorted to GND, exit to the normal init.# Flash the LED one timeFlashLEDumount /sysecho [Info.] Overlayfs jumper setting DISABLE.# Continue with regular initexec /sbin/init#exit 0elseumount /sysecho [Info.] Overlayfs jumper setting ENABLE.# Continue with overlayfs activation process.
fi
# jumper function code END @@@@@# Load overlay module
modprobe overlay
if [ $? -ne 0 ]; thenfail "ERROR: missing overlay kernel module"
fi# Mount /proc
mount -t proc proc /proc
if [ $? -ne 0 ]; thenfail "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 ]; thenfail "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 ]; thenfail "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 ]; thenfail "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 ]; thenfail "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 rootmount --move /mnt/mnt/lower/ /roif [ $? -ne 0 ]; thenecho "ERROR: could not move ro-root into newroot"/bin/bashfimount --move /mnt/mnt/rw /rwif [ $? -ne 0 ]; thenecho "ERROR: could not move tempfs rw mount into newroot"/bin/bashfi# Unmount unneeded mounts so we can unmout the old readonly rootumount /mnt/mntumount /mnt/procumount /mnt/devumount /mnt# Continue with regular initexec /sbin/init
END
)"# End of file
【备注】
启动之前使用跳线帽短路GPIO 4和GND,则启动过程会看到LED闪烁两次,启动后可以看到overlayfs并未启用。
启动之前如果空开GPIO 4和GND,即不短接跳线帽,则启动过程只会看到LED闪烁一次,启动后可以看到overlayfs已启用。
不连接LED,脚本也是可以正常工作的。GPIO 18如果别有他用,则需要按文中所述方法修改闪烁LED的那段函数。
理论上来说应该在内核启动时设置跳线帽连接的那个GPIO(我的例子里为GPIO 4)为上拉状态,不过我没有做任何处理,似乎也没问题。
欢迎邮件交流
实现在树莓派的GPIO接口上用跳线(短路帽)轻松选择启用或者禁用overlayfs影子系统相关推荐
- 光纤、光模块、光接口、光跳线等弱电系统常用知识
以太网交换机常用的光模块有SFP,GBIC,XFP,XENPAK. 它们的英文全称: SFP:Small Form-factorPluggabletransceiver ,小封装可插拔收发器 GBIC ...
- 树莓派python gpio 模仿iic_Adafruit的树莓派教程:GPIO配置
概览 树莓派最令人兴奋的特点之一是它有一个GPIO连接器可以用来接其他的硬件设备. GPIO连接器实际上是由许多不同类型的接口组成的: 真正的GPIO(General Purpose Input Ou ...
- 树莓派的GPIO编程
树莓派除了提供常见的网口和USB接口 ,还提供了一组GPIO(General Purpose Input/Output)接口.这组GPIO接口大大拓展了树莓派的能力.GPIO不仅能实现通信,还能直接控 ...
- 主板电源开关接口图解_组装电脑时主板跳线如何接?DIY装机主板接线教程
如今装机不再像以前那么神秘,不用再去电脑城问东问西,只要上天猫或京东等网上商城即可放心买到各种电脑配件.那么,自己组装电脑最难的是什么,CPU.散热器.内存.显卡安装都很简单,很多小伙伴自己组装电脑的 ...
- 双路服务器主板跳线位置,主板上对应的跳线位置要认清
03主板上对应的跳线位置要认清 主板上的接口没那么难认: 说完机箱,我们再来看看主板上的标准,和机箱的线材对应,主板上也有对应的插槽区,即PANEL或F_PANEL,很多主板还提供了更多的功能和插槽, ...
- 微星h61m主板jsp1接线图_主板上面的音频跳线如何接 买的惠普台式机的拆机主板 MS 7184 微星的板子图片大家可以百度。...
匿名用户 1级 2010-08-23 回答 电源开关:POWER SW 英文全称:Power Swicth 可能用名:POWER.POWER SWITCH.ON/OFF.POWER SETUP.PWR ...
- 树莓派pythongpio编程_树莓派gpio接口及编程方法
树莓派现在越来越火,网上树莓派的资料也越来越多.树莓派源自英国,国外嵌入式开源领域具有良好的分享精神,树莓派各种集成库也层出不穷,下面详细介绍一下树莓派gpio接口及编程方法. GPIO基本介绍 GP ...
- 树莓派Pico直流电机接口技术及PWM电机调速控制MicroPython编程
内容目录: 一.树莓派Pico开发板直流电机接口技术 1.H桥驱动电路的基本工作原理 2.典型H桥驱动电路分析 3.DRV8833双H桥电机驱动模块介绍 4.Pico开发板与直流电机接口 二.Pico ...
- 树莓派python控制gpio_树莓派的GPIO控制
陈拓chentuo@ms.xab.ac.cn 2018.06.09/2018.06.10 从网上下载了几张精美的图片,感谢图片的制作者! 0. 概述 本文介绍树莓派 Zero W的GPIO控制,并用L ...
最新文章
- Windows中的tree命令不可用的解决办法
- 使用RBTool自动提交code review请求
- 用R语言 画条形图(基于ggplot2包)
- Django 的JsonResponse 与json
- 使用Docker-数据卷挂载案例1
- 链表题目--2 求链表的中间结点 和 求链表中倒数第k个结点
- windows搜索工具_加快搞定并替代 Windows 10 搜索框搜索文件速度的免费小工具
- (18):Silverlight 2 综合实例之RSS阅读器
- Better Explained 以通俗易懂的语言阐释数学
- native2ascii 命令
- 产品经理必看书籍推荐
- Radasm使用简明手册(中文版)
- Unity:骨骼动画
- 一文搞懂什么是单代号网络图!
- HTML制作导航条的方法
- CTF实验吧-简单的sql注入【SQL注入关键词绕过】
- vue3+element plus下面,自定义el-table表格标题
- Django源码cookie解读:关于中文cookie会被吞掉并截断的问题。
- 【Ubuntu22.04 安装星火商店报错解决办法】
- HTML+JS+CSS筋斗云导航栏效果
热门文章
- HTML:img图片找不到时 什么都不显示
- 3.(20分)HTML文件是(),全国2009年4月高等教育自学考试网页设计与制作试题
- 如何利用现货黄金走势?
- qt5 python gui programming_Python GUI教程(五):通过Qt设计师在GUI中添加窗口部件
- Pycharm专业版远程登录服务器的详细教程
- 【时间之外】为什么WPS占用内存大?
- Python定义字典,写联系人及电话号码,查找……
- 看图就懂:为什么L1正则化比L2正则化更容易得到稀疏解?为什么L2正则化可以用于防止过拟合?
- 8.1 切比雪夫近似值求正余弦
- 务必收藏,我珍藏多年的Python奇淫技巧,不看后悔啊~