This script allows you to mount and chroot into a Raspberry Pi OS image file. It automates the process of attaching the image to a loop device, mounting partitions, and entering a chroot environment for modifications.
- A Linux system with root access
- The following utilities installed:
losetup
fdisk
mount
chroot
awk
- A Raspberry Pi OS image file
Run the script with root privileges, providing the path to the image file as an argument:
sudo bash sandbox.sh /path/to/image.img [--arm64]
If you are running the script on x86 architecture remember to include --arm64
flag.
To write the .img
file to an SD card, use the following command:
sudo dd if=<.img file path> of=<sd card path> bs=4M status=progress conv=fsync
Example:
sudo dd if=/path/to/image.img of=/dev/sdX bs=4M status=progress conv=fsync
(Replace /dev/sdX
with your actual SD card device path. Be careful, as this operation is destructive.)
-
Checks for Root Privileges
- Ensures the script is run as root.
-
Accepts Image File as an Argument
- Requires the user to provide a valid
.img
file.
- Requires the user to provide a valid
-
Creates a Temporary Mount Directory
- A randomly generated directory is used for mounting.
-
Attaches Image to a Loop Device
- Uses
losetup
to map the image partitions.
- Uses
-
Extracts and Mounts Partitions
- The boot and root partitions are identified and mounted.
- A
tmpfs
is used for temporary files.
-
Enters the Chroot Environment
- Allows modifications to the mounted filesystem.
-
Automatic Cleanup
- Ensures unmounting and removal of the temporary directory upon script exit.
Loop Device: /dev/loop0
Boot Partition: /dev/loop0p1
Root Partition: /dev/loop0p2
Entering chroot...
Sandbox session complete.
- Mount point does not exist: Ensure the image is valid and contains expected partitions.
- Permission denied: Ensure the script is run with
sudo
.
This script has been tested on Raspberry Pi OS on arm64 and on Debian 12 bookworm on x86.
This script is licensed under the MIT License. Modify and distribute as needed.