Skip to content

Instantly share code, notes, and snippets.

@mixaz
Last active September 21, 2020 16:22
Show Gist options
  • Save mixaz/6e9f8d948fe34452da9700ff07371390 to your computer and use it in GitHub Desktop.
Save mixaz/6e9f8d948fe34452da9700ff07371390 to your computer and use it in GitHub Desktop.
Bash script to extract rootfs from Raspbian images
#!/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
@mixaz
Copy link
Author

mixaz commented Sep 21, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment