Last active
August 11, 2025 09:25
-
-
Save mixaz/6e9f8d948fe34452da9700ff07371390 to your computer and use it in GitHub Desktop.
Bash script to extract rootfs from Raspbian images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| PATH=/sbin:${PATH} | |
| set -e | |
| usage() | |
| { | |
| cat <<EOF | |
| `basename $0`: | |
| Make a Raspberry Pi SD card image | |
| -h : This help message | |
| -d : Destination directory | |
| -i : The name of the image file ( `basename ${IMGFILE}` ) | |
| -v : Turn on verbose output | |
| EOF | |
| } | |
| while getopts “hi:d:v” OPTION | |
| do | |
| case $OPTION in | |
| h) | |
| HELP_OPT=1 | |
| ;; | |
| i) | |
| IMAGEFILE_OPT=$OPTARG | |
| ;; | |
| d) | |
| DESTDIR_OPT=$OPTARG | |
| ;; | |
| v) | |
| VERBOSE=1 | |
| ;; | |
| ?) | |
| usage | |
| exit | |
| ;; | |
| esac | |
| done | |
| IMGFILE=${IMAGEFILE_OPT:-2012-09-18-wheezy-raspbian.img} | |
| DESTDIR=${DESTDIR_OPT:-rootfs} | |
| if [ ! -z "${HELP_OPT:-}" ] ; then | |
| usage | |
| exit | |
| fi | |
| if [[ ${EUID} != 0 && ${UID} != 0 ]] ; then | |
| echo "$0 must be run as root" | |
| usage | |
| exit -1 | |
| fi | |
| BYTES_PER_SECTOR=`fdisk -lu ${IMGFILE} | grep ^Units | awk '{print $8}'` | |
| LINUX_START_SECTOR=`fdisk -lu ${IMGFILE} | grep ^${IMGFILE}2 | awk '{print $2}'` | |
| LINUX_OFFSET=`expr ${LINUX_START_SECTOR} \* ${BYTES_PER_SECTOR}` | |
| if [ ! -z "${DESTDIR}" ] ; then | |
| if [ ! -d ${DESTDIR} ] ; then | |
| mkdir -p ${DESTDIR} | |
| fi | |
| LINUXMOUNT="__linuxmnt.$$" | |
| mkdir -p ${LINUXMOUNT} | |
| mount -o loop,offset=${LINUX_OFFSET} ${IMGFILE} ${LINUXMOUNT} | |
| cd ${LINUXMOUNT}; | |
| tar cf - * | ( cd ../${DESTDIR}; tar xvf - ) | |
| cd - | |
| umount ${LINUXMOUNT} | |
| rm -rf ${LINUXMOUNT} | |
| fi |
Author
If you are on a non English system just prefix with LANG=C to make it work since the script parses english output of fdisk.
LANG=C sudo ./rpi_copyrootfs.sh -i 2020-08-20-raspios-buster-armhf.img -d rootfs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stolen from https://midnightyell.wordpress.com/2012/10/13/compiling-mame-for-the-raspberry-pi-with-qemu/ and updated
Usage:
sudo ./rpi_copyrootfs.sh -i 2020-08-20-raspios-buster-armhf.img -d rootfs