chroot-to-pi.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. # This script allows you to chroot ("work on")
  3. # the raspbian sd card as if it's the raspberry pi
  4. # on your Ubuntu desktop/laptop
  5. # just much faster and more convenient
  6. # credits: https://gist.github.com/jkullick/9b02c2061fbdf4a6c4e8a78f1312a689
  7. # make sure you have issued
  8. # (sudo) apt install qemu qemu-user-static binfmt-support
  9. # Write the raspbian image onto the sd card,
  10. # boot the pi with the card once
  11. # so it expands the fs automatically
  12. # then plug back to your laptop/desktop
  13. # and chroot to it with this script.
  14. # Invoke:
  15. # (sudo) ./chroot-to-pi.sh /dev/sdb
  16. # assuming /dev/sdb is your sd-card
  17. # if you don't know, when you plug the card in, type:
  18. # dmesg | tail -n30
  19. # Note: If you have an image file instead of the sd card,
  20. # you will need to issue
  21. # (sudo) apt install kpartx
  22. # (sudo) kpartx -v -a 2017-11-29-raspbian-stretch-lite.img
  23. # then
  24. # (sudo) ./chroot-to-pi.sh /dev/mapper/loop0p
  25. # With the vanilla image, you have very little space to work on
  26. # I have not figured out a reliable way to resize it
  27. # Something like this should work, but it didn't in my experience
  28. # https://gist.github.com/htruong/0271d84ae81ee1d301293d126a5ad716
  29. # so it's better just to let the pi resize the partitions
  30. # check if package is not installed
  31. #if [ $(dpkg-query -W -f='${Status}' qemu-user-static 2>/dev/null | grep -c "ok installed") -eq 0 ];
  32. mkdir -p /mnt/raspbian
  33. # mount partition
  34. mount -o rw ${1}2 /mnt/raspbian
  35. mount -o rw ${1}1 /mnt/raspbian/boot
  36. # mount binds
  37. mount --bind /dev /mnt/raspbian/dev/
  38. mount --bind /sys /mnt/raspbian/sys/
  39. mount --bind /proc /mnt/raspbian/proc/
  40. mount --bind /dev/pts /mnt/raspbian/dev/pts
  41. # ld.so.preload fix
  42. sed -i 's/^/#CHROOT /g' /mnt/raspbian/etc/ld.so.preload
  43. # copy qemu binary
  44. cp /usr/bin/qemu-arm-static /mnt/raspbian/usr/bin/
  45. echo "You will be transferred to the bash shell now."
  46. echo "Issue 'exit' when you are done."
  47. echo "Issue 'su pi' if you need to work as the user pi."
  48. # chroot to raspbian
  49. chroot /mnt/raspbian /bin/bash
  50. # ----------------------------
  51. # Clean up
  52. # revert ld.so.preload fix
  53. sed -i 's/^#CHROOT //g' /mnt/raspbian/etc/ld.so.preload
  54. # unmount everything
  55. umount /mnt/raspbian/{dev/pts,dev,sys,proc,boot,}