Skip to content

Instantly share code, notes, and snippets.

@retro486
Forked from htruong/chroot-to-pi.sh
Last active June 4, 2020 15:27
Show Gist options
  • Select an option

  • Save retro486/4e17ffd211c8fc2d4052e9d84bba36b7 to your computer and use it in GitHub Desktop.

Select an option

Save retro486/4e17ffd211c8fc2d4052e9d84bba36b7 to your computer and use it in GitHub Desktop.
Chroot to pi sd card
#!/bin/bash
#
##############################
# Forked from: https://gist.github.com/htruong/7df502fb60268eeee5bca21ef3e436eb
#
# Changes:
# * Make compatible with Archlinux/Manjaro (but only tested on Manjaro)
#
# Prerequisites:
# * https://aur.archlinux.org/packages/binfmt-qemu-static-all-arch/
# * https://aur.archlinux.org/packages/qemu-user-static-bin/
#
# Note: I tried to use the available qemu static packages in the
# Manjaro repos but the binfmt wasn't working. The above two AUR
# packages are working for me as of 4 June 2020.
##############################
# 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
#
# 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 ./chroot-to-pi.sh /dev/sdb
# assuming /dev/sdb is your sd-card
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
# ld.so.preload fix
sed -i 's/^/#CHROOT /g' /mnt/raspbian/etc/ld.so.preload
# copy qemu static 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 ld.so.preload fix
sed -i 's/^#CHROOT //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