Skip to content

Instantly share code, notes, and snippets.

@speters
Last active February 4, 2016 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save speters/e945150b612574064fa9 to your computer and use it in GitHub Desktop.
Save speters/e945150b612574064fa9 to your computer and use it in GitHub Desktop.
mount ARM (OdroidC1, Raspberry Pi,...) filesystem image, and chroot into it using qemu-user-static
#!/usr/bin/sudo /bin/bash
MOUNTPOINT=/mnt/odroidc1
FSIMG=${HOME}/odroidc1-sdcard-jessie.img
CHROOTNAME=`basename ${MOUNTPOINT}`
#DEBUG=1
if [ -z "$DEBUG" ]; then
DEBUG=''
else
DEBUG='echo '
fi
OFFSET=`LANG=C fdisk -lu ${FSIMG} | awk '/\.img2/ && /Linux/{print $2 * 512}'`
${DEBUG} mount -o loop,offset=${OFFSET} ${FSIMG} ${MOUNTPOINT}
${DEBUG} cp -f /usr/bin/qemu-arm-static ${MOUNTPOINT}/usr/bin/
${DEBUG} mv ${MOUNTPOINT}/usr/sbin/policy-rc.d ${MOUNTPOINT}/usr/sbin/policy-rc.d.save
${DEBUG} echo -e '#!/bin/sh\nexit 101' > ${MOUNTPOINT}/usr/sbin/policy-rc.d
${DEBUG} chmod +x ${MOUNTPOINT}/usr/sbin/policy-rc.d
${DEBUG} echo -e "${CHROOTNAME}-chroot" > ${MOUNTPOINT}/etc/debian_chroot
${DEBUG} mount -o rbind /dev ${MOUNTPOINT}/dev
${DEBUG} mount -t proc none ${MOUNTPOINT}/proc
${DEBUG} mount -o bind /sys ${MOUNTPOINT}/sys
${DEBUG} mount -o bind /tmp ${MOUNTPOINT}/tmp
# if [ ! -z "$DEBUG" ]; then
# ${DEBUG} echo "':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:' > /proc/sys/fs/binfmt_misc/register"
# else
# echo -e ':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:' > /proc/sys/fs/binfmt_misc/register
# fi
${DEBUG} chroot ${MOUNTPOINT} su - ${SUDO_USER}
${DEBUG} umount -fRl ${MOUNTPOINT}/dev
${DEBUG} umount ${MOUNTPOINT}/proc
${DEBUG} umount ${MOUNTPOINT}/sys
${DEBUG} umount ${MOUNTPOINT}/tmp
${DEBUG} mv -f ${MOUNTPOINT}/usr/sbin/policy-rc.d.save ${MOUNTPOINT}/usr/sbin/policy-rc.d
rm -f ${MOUNTPOINT}/etc/debian_chroot
${DEBUG} umount ${MOUNTPOINT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment