Instantly share code, notes, and snippets.

What would you like to do?
Chroot to pi sd card
# This script allows you to chroot ("work on")
# the raspbian sd card as if it's the raspberry pi
# on your Ubuntu desktop/laptop
# just much faster and more convenient
# credits:
# make sure you have issued
# (sudo) apt install qemu qemu-user-static binfmt-support
# Write the raspbian image onto the sd card,
# boot the pi with the card once
# so it expands the fs automatically
# then plug back to your laptop/desktop
# and chroot to it with this script.
# Invoke:
# (sudo) ./ /dev/sdb
# assuming /dev/sdb is your sd-card
# if you don't know, when you plug the card in, type:
# dmesg | tail -n30
# Note: If you have an image file instead of the sd card,
# you will need to issue
# (sudo) apt install kpartx
# (sudo) kpartx -v -a 2017-11-29-raspbian-stretch-lite.img
# then
# (sudo) ./ /dev/mapper/loop0p
# With the vanilla image, you have very little space to work on
# I have not figured out a reliable way to resize it
# Something like this should work, but it didn't in my experience
# so it's better just to let the pi resize the partitions
mkdir -p /mnt/raspbian
# mount partition
mount -o rw ${1}2 /mnt/raspbian
mount -o rw ${1}1 /mnt/raspbian/boot
# mount binds
mount --bind /dev /mnt/raspbian/dev/
mount --bind /sys /mnt/raspbian/sys/
mount --bind /proc /mnt/raspbian/proc/
mount --bind /dev/pts /mnt/raspbian/dev/pts
# fix
sed -i 's/^/#CHROOT /g' /mnt/raspbian/etc/
# copy qemu binary
cp /usr/bin/qemu-arm-static /mnt/raspbian/usr/bin/
echo "You will be transferred to the bash shell now."
echo "Issue 'exit' when you are done."
echo "Issue 'su pi' if you need to work as the user pi."
# chroot to raspbian
chroot /mnt/raspbian /bin/bash
# ----------------------------
# Clean up
# revert fix
sed -i 's/^#CHROOT //g' /mnt/raspbian/etc/
# unmount everything
umount /mnt/raspbian/{dev/pts,dev,sys,proc,boot,}

This comment has been minimized.

akhepcat commented Jan 24, 2018

Just in case somebody has previously commented out a line in /etc/, you should change your prefixing:

sed -i 's/^/#CHROOT /g' /mnt/raspbian/etc/
sed -i 's/^#CHROOT //g' /mnt/raspbian/etc/

so that only your specific changes get unwrapped - and if there's an issue, it's obvious what was touched.


This comment has been minimized.


htruong commented Jan 25, 2018

@akhepcat: Great idea, added to the script. Thanks!


This comment has been minimized.

simspace-dev commented Jul 25, 2018

Thanks for the script. I've been looking into emulation. This seems so much easier and it worked first time.

I need to install some packages, but I can't get WIFI to work.
When I ping, I get this message ...

Unsupported setsockopt level=41 optname=25
WARNING: your kernel is veeery old. No problems.
Unsupported setsockopt level=255 optname=7
setsockopt(RAW_CHECKSUM) failed - try to continue.Unsupported setsockopt level=58 optname=1
setsockopt(ICMP6_FILTER): Protocol not available

I did install the latest Stretch-Lite image

I did add cp -L /etc/resolv.conf /mnt/raspbian/etc/resolv.conf to the script, based on some posts I found regarding chroot and network connections, but still no luck.

Do you have any ideas how I can get the WIFI connection working, so I can install some packages and build a couple of apps?



This comment has been minimized.

simspace-dev commented Jul 25, 2018

I found another post and it looks like mounting the resolv.conf file (not copying) is what was needed.

I changed cp -L /etc/resolv.conf /mnt/raspbian/etc/resolv.conf to mount --bind /etc/resolv.conf /mnt/raspbian/etc/resolv.conf and I can now ping sites from my chrooted Raspbian SD card.

Now, can I get an app to build? We'll find out. On to the next challenge.

Thanks again for the script!

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