-
-
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,} |
Is there any way we can get kernel version of img file i just have to copy .ko compiled files for linux kernel into system. but rather than hard coding kernel version like /lib/modules/5.10.11-v7l+/ i would like to get kernel version dynamically so in future it would be easy. but the problem is uname -r provides kernel version of host os any help please
https://gist.github.com/Robokishan/8ffb8acec88fbbaf6c6c857d9c4ffe6a made a simple script for operation
How to do
- Download chroot-to-pi.sh
- in the same directory download above script
- Download img file of raspberrypi
- bash setup-chroot.sh
- Once work finished type exit and all the loop device will be deleted and mounted dir will be unmounted
Expand img file
https://gist.github.com/Robokishan/4dc13947b980bdcdfc20543b75c1f0fb using this script
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
@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!
https://github.com/cinderblock/RaspberryPi-Image-Generator
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.
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
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).
For those using Gentoo, make sure you've built qemu
package with static-user
and QEMU_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
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!
I've been chrooting into my raspberry pi's sdcard for a while now however, my method isn't so complicated. First after I boot the sdcard once, I make a directory called chroot-debian, then I use a sdcard-usb adapter so the sdcard appears as a usb device with two partitions, namely /dev/sdb1 which has the /boot/ and then /dev/sdb2 which is the /root partition. I issue these commands:
sudo mount -o bind /dev chroot-debian/dev
sudo mount -o bind /proc chroot-debian/proc
sudo chroot chroot-debian /bin/bash. Wonderful things happen after that I can issue apt update -y && apt upgrade -y and have it done. I have Debian Sid on that sdcard so if I need to build a package using debuild -uc -us to upgrade some of my aging apps in buster I can do it all on the same machine, all in my home directory. As I said my way is less complicated but I will study this method and see what wisdom I can gleam.