2019-10-23 22:05:48 +02:00
2026-01-02 12:44:47 +01:00
2026-01-02 12:45:12 +01:00
2019-10-23 22:05:48 +02:00
2019-10-23 22:05:48 +02:00
2019-10-23 22:05:48 +02:00
2019-10-23 22:05:48 +02:00
2026-01-02 12:45:12 +01:00

Raspberry Pi Scripts

This repository contains my collection of scripts and other snippets for the Raspberry Pi. I am publishing these in case they are of any benefit to other enthusiasts. Use them freely and please let me know in case you encounter any issues or require changes.

The latest versions, documentation and bugtracker available on my Gitea instance

Copyright (c) 2019 - 2025 Frederik Lindenaar. free for distribution under the GNU General Public License, see below

Contents

This repository contains the following scripts:

gpio_trigger.py

This script was initially written to add a power-off button to a Raspberry Pi, see this blog post for the rationale behind it and how to construct and connect a physical button.

The script itself is a generic solution to monitor a GPIO pin and executes a command when the input signal on a pin changes. It can run as interactively as well as in the background and executes a command once or continuously upon any change or specific transition (e.g. HIGH to LOW).

The script is written in Python 2 and uses the RPi.GPIO library as both are installed by default on most distributions so should just work. Please note that by default the script should be started as root to gain access to the GPIO port.

To implement a simple power-off button, install the script in /usr/local/sbin, connect an NC switch (i.e. one that connects when pressed) between pin 39 (GND) and pin 40 of the Raspberry Pi and add:

if [ -x /usr/local/sbin/gpio_trigger.py ]; then
  /usr/local/sbin/gpio_trigger.py -D -H 5000 poweroff
fi

to the file /etc/rc.local. This will start the script in the background (-D) to wait for pin 40 (default pin) to be connected to ground for 5000ms (-H) and then run the command poweroff to shutdown the Raspberry Pi.

Please refer to the output of gpio_trigger.py -h for the options supported and defaults used when no option is specified.

Systemd services

The repository contains a number of .service files, which are systemd service descriptions to control specific on-board features of the Raspberry Pi (e.g. to disable unused ports). Their purpose should be pretty clear from their name (and comments they contain). The rationale of the initial scripts is covered in this blog post.

In general, to install these copy them to the directory /etc/systemd/system/

To manually disable the port, 'start' the 'service' with:

service <<filename without .service>> start

To manually enable the port again, 'stop' the 'service' with:

service <<filename without .service>> stop

To enable starting during system boot (to disable the port) run:

systemctl enable service <<filename without .service>>

To disable starting during system boot (to no longer disable the port) run:

systemctl disable service <<filename without .service>>

rpi-lvm-imager.sh

This script will wipe and partition a (large) storage device with a small boot partition, setup LVM for the rest of the disk, and then writes the latest version of Raspbian Pi OS (Lite) or another OS image availabile via the Raspberry Pi Imager to LVM managed partition(s) and makes the device bootable.

The reason I worte this script was that the Raspberry Pi Imager can only write an image to a full disk and does not allow one to partition it nor perform the installation on an LVM managed disk. This was needed for a Raspberry Pi 5 with NVME SSD as the device had way more diskspace than would ever be needed/useful for the system partition.

This script was inspired by the write-up found here. That was apparently superseded by this approach, hoever, I like the initial approach more. This script will write the OS to a device after setting up LVM but before booting it so it is just quicker than writing the OS and then moving it to an LVM managed partition.

To write the latest version of Raspberry Pi OS (64bit Lite) to an NVME SSD device simply run:

sudo rpi-lvm-imager.sh --lvm-group nvme /dev/nvme0n1

Please note that this will unmound and wipe the specified device so be careful!

to see all aailable options, run: ./rpi-lvm-imager.sh -h

License

These scripts, documentation & configration examples are free software: you can redistribute 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 script, documenatation and configuration examples are 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, download it from http://www.gnu.org/licenses/.

Description
My scripts for the Raspberry Pi
Readme GPL-3.0 56 KiB
Languages
Shell 75%
Python 25%