Skip to content

Instantly share code, notes, and snippets.

Last active September 19, 2023 06:44
Star You must be signed in to star a gist
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,}
Copy link

It might seem obvious to some, but this method work wonderfull for very simple task, but due to the very nature of systemd and the way that chroot is working, you cannot get anything related to systemd done, you will get something like this:

Running in chroot, ignoring request: start
Running in chroot, ignoring request: restart
Running in chroot, ignoring request: reload
Running in chroot, ignoring request: daemon-reload
Running in chroot, ignoring request: is-active

Copy link

@gnthibault Those methods indead do not work because they do not make sense to run when the Pi is offline.

However systemctl enable ... and related configuration commands work just fine.

PS, I've combined some of the ideas here with some others into a full repo. I'd love for others to check it out!

Copy link

dmuiX commented Dec 4, 2022

I would add something like to establish internet connection:

# establish internet connection
mv /mnt/raspbian/etc/resolv.conf /mnt/raspbian/etc/resolv.conf_bak
cp /etc/resolv.conf /mnt/raspbian/etc/

And in the cleanup section:

# revert /etc/resolv.conf_bak
rm /mnt/raspbian/etc/resolv.conf 
mv /mnt/raspbian/etc/resolv.conf_bak /mnt/raspbian/etc/resolv.conf

Just saw that somebody above already suggested adding resolv.conf as a mount bind. I will test if this is the better way.

Copy link

SX-9 commented Apr 16, 2023

sudo ./pi-chroot /dev/sdc

You will be transferred to the bash shell now.
Issue 'exit' when you are done.
Issue 'su pi' if you need to work as the user pi.
chroot: failed to run command ‘/bin/bash’: Exec format error

i have a feeling i got this error since im running it on arch linux and binfmt-support is not avaible in arch

Copy link

@SX-9 Are you sure you got qemu working? There are some checks you can use here to see some more about what isn't setup properly.

Copy link

kelvie commented Jul 17, 2023

If anyone on arch is trying this, you can basically just install qemu-user-static-binfmt and just run sudo arch-chroot . on the rootfs (optionally mounting boot first).

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