-
Star
(115)
You must be signed in to star a gist -
Fork
(46)
You must be signed in to fork a gist
-
-
Save htruong/7df502fb60268eeee5bca21ef3e436eb to your computer and use it in GitHub Desktop.
| #!/bin/bash | |
| # 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: https://gist.github.com/jkullick/9b02c2061fbdf4a6c4e8a78f1312a689 | |
| # 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) ./chroot-to-pi.sh /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) ./chroot-to-pi.sh /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 | |
| # https://gist.github.com/htruong/0271d84ae81ee1d301293d126a5ad716 | |
| # 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 | |
| # ld.so.preload fix | |
| sed -i 's/^/#CHROOT /g' /mnt/raspbian/etc/ld.so.preload | |
| # 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 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,} |
Alternative to dmesg | tail -n30
lsblk -o NAME,MOUNTPOINT- check the mount in
/media/<user_name>/
You rock! Just tested the script in a Rasp Pi 4 and it worked like magic!
Alternative to
dmesg | tail -n30
lsblk -o NAME,MOUNTPOINT- check the mount in
/media/<user_name>/
I learned this neat trick, if you do dmesg -w and then plug the drive in, you'll be able to watch the dmesg in real-time!
Or, just use systemd-nspawn (systemd-container package in apt repos)
If an img file, loop and mount root and boot, then:
sudo systemd-nspawn -D /mnt bin/bash
Pretty straight forward description here: https://wiki.debian.org/RaspberryPi/qemu-user-static (Option 2.2)
Edit:
If you need internet access you might want to include the option --resolv-conf=replace-stub or --resolv-conf=copy-host depending on how your host is setup. If you have systemd-resolved on host, try replace-stub first.
In script for example (assuming you run it as root, this will drop you into the container):
#!/usr/bin/env bash
# do loop stuff and then mount root and boot
if systemctl is-active -q systemd-resolved.service; then
systemd-nspawn --resolv-conf=replace-stub -D /mnt bin/bash
else
systemd-nspawn --resolv-conf=copy-host -D /mnt bin/bash
fi
exit 0
Edit
If you only want to execute stuff from a script, you can either change bin/bash to the command you want to execute, or use --pipe and then exit the container (change --resolv-conf to the method you need):
systemd-nspawn --pipe --resolv-conf=copy-host -D /mnt bin/bash << EOF
apt update
apt upgrade
exit
EOF
shamless self promotion incoming
This will be implemented in next version of shrink-backup btw...
For those using Gentoo, make sure you've built
qemupackage withstatic-userandQEMU_SOFTMMU_TARGETS: arm QEMU_USER_TARGETS: arm. Further info in the official handbook: https://wiki.gentoo.org/wiki/Embedded_Handbook/General/Compiling_with_QEMU_user_chroot