Skip to content

Instantly share code, notes, and snippets.

@thisisjoyjacob
Forked from Robokishan/chroot-to-pi.sh
Created December 27, 2024 09:41
Show Gist options
  • Select an option

  • Save thisisjoyjacob/bd6ec5a734411ca9d49d5a2d64af76c1 to your computer and use it in GitHub Desktop.

Select an option

Save thisisjoyjacob/bd6ec5a734411ca9d49d5a2d64af76c1 to your computer and use it in GitHub Desktop.
Chroot to pi sd card
#!/bin/bash
# make sure you had
# apt install qemu qemu-user-static binfmt-support
# before you do this
# Write the raspbian image onto the sd card,
# boot the pi so it expands the fs, then plug back to your laptop/desktop
# and chroot to it with this script
# Invoke:
# sudo ./chroot-to-pi.sh /dev/sdb
# 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
# ld.so.preload fix
sed -i 's/^/#/g' /mnt/raspbian/etc/ld.so.preload
# copy qemu binary
cp /usr/bin/qemu-arm-static /mnt/raspbian/usr/bin/
# chroot to raspbian
chroot /mnt/raspbian /bin/bash
# You will be taken to the bash command line now
# So do whatever you need to do
# and issue
# exit
# when you're done
# revert ld.so.preload fix
sed -i 's/^#//g' /mnt/raspbian/etc/ld.so.preload
# unmount everything
umount /mnt/raspbian/{dev/pts,dev,sys,proc,boot,}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment