Skip to content

Instantly share code, notes, and snippets.

@bilson
Forked from htruong/chroot-to-pi.sh
Created November 15, 2022 06:16
Show Gist options
  • Save bilson/5dcd925949627ebf7b6d253bea41196d to your computer and use it in GitHub Desktop.
Save bilson/5dcd925949627ebf7b6d253bea41196d to your computer and use it in GitHub Desktop.

Revisions

  1. @htruong htruong revised this gist Jan 25, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions chroot-to-pi.sh
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ 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
    sed -i 's/^/#CHROOT /g' /mnt/raspbian/etc/ld.so.preload

    # copy qemu binary
    cp /usr/bin/qemu-arm-static /mnt/raspbian/usr/bin/
    @@ -63,7 +63,7 @@ chroot /mnt/raspbian /bin/bash
    # ----------------------------
    # Clean up
    # revert ld.so.preload fix
    sed -i 's/^#//g' /mnt/raspbian/etc/ld.so.preload
    sed -i 's/^#CHROOT //g' /mnt/raspbian/etc/ld.so.preload

    # unmount everything
    umount /mnt/raspbian/{dev/pts,dev,sys,proc,boot,}
  2. @htruong htruong revised this gist Jan 24, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions chroot-to-pi.sh
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,8 @@
    # 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

  3. @htruong htruong revised this gist Jan 24, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions chroot-to-pi.sh
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,7 @@
    # 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
  4. @htruong htruong revised this gist Jan 24, 2018. 1 changed file with 12 additions and 16 deletions.
    28 changes: 12 additions & 16 deletions chroot-to-pi.sh
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,24 @@
    #!/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
    # 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

    # 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
    # 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, to find out, issue
    # assuming /dev/sdb is your sd-card
    # if you don't know, when you plug the card in, type:
    # dmesg | tail -n30
    # when you plug the card in, it will tell you.)


    # Note: If you have an image file instead of the sd card,
    @@ -49,19 +50,14 @@ sed -i 's/^/#/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

    # ------------------------------------------------
    # You will be taken to the bash command line now
    # So do whatever you need to do
    # and issue
    # exit
    # when you're done
    # if you need to work as the pi username, do
    # su pi
    # ------------------------------------------------

    # ----------------------------
    # Clean up
    # revert ld.so.preload fix
    sed -i 's/^#//g' /mnt/raspbian/etc/ld.so.preload
  5. @htruong htruong revised this gist Jan 24, 2018. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions chroot-to-pi.sh
    Original file line number Diff line number Diff line change
    @@ -8,24 +8,30 @@
    # (sudo) apt install qemu qemu-user-static binfmt-support

    # Write the raspbian image onto the sd card,
    # boot the pi with the card so it expands the fs
    # 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
    # and chroot to it with this script.

    # Invoke:
    # (sudo) ./chroot-to-pi.sh /dev/sdb

    # (assuming /dev/sdb is your sd-card, to find out, issue
    # dmesg | tail -n30
    # when you plug the card in, it will tell you.)

    # If you have an image file instead of the sd card, you will need to issue

    # 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
    # Note that with the vanilla image, you have very little space to work on
    # 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


    # mount partition
    mount -o rw ${1}2 /mnt/raspbian
    @@ -46,14 +52,17 @@ 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
    # if you need to work on the pi username, do
    # if you need to work as the pi username, do
    # su pi
    # ------------------------------------------------

    # Clean up
    # revert ld.so.preload fix
    sed -i 's/^#//g' /mnt/raspbian/etc/ld.so.preload

  6. @htruong htruong revised this gist Jan 24, 2018. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions chroot-to-pi.sh
    Original file line number Diff line number Diff line change
    @@ -8,10 +8,14 @@
    # (sudo) apt install qemu qemu-user-static binfmt-support

    # Write the raspbian image onto the sd card,
    # boot the pi so it expands the fs, then plug back to your laptop/desktop
    # boot the pi with the card 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
    # (sudo) ./chroot-to-pi.sh /dev/sdb
    # (assuming /dev/sdb is your sd-card, to find out, issue
    # dmesg | tail -n30
    # when you plug the card in, it will tell you.)

    # If you have an image file instead of the sd card, you will need to issue
    # (sudo) apt install kpartx
    @@ -47,6 +51,8 @@ chroot /mnt/raspbian /bin/bash
    # and issue
    # exit
    # when you're done
    # if you need to work on the pi username, do
    # su pi

    # revert ld.so.preload fix
    sed -i 's/^#//g' /mnt/raspbian/etc/ld.so.preload
  7. @htruong htruong revised this gist Jan 24, 2018. 1 changed file with 17 additions and 4 deletions.
    21 changes: 17 additions & 4 deletions chroot-to-pi.sh
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,27 @@
    #!/bin/bash

    # make sure you had
    # apt install qemu qemu-user-static binfmt-support
    # before you do this
    # 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

    # 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 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
    # (sudo) ./chroot-to-pi.sh /dev/sdb

    # 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
    # Note that 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

    # mount partition
    mount -o rw ${1}2 /mnt/raspbian
  8. @htruong htruong revised this gist Jan 24, 2018. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions chroot-to-pi.sh
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    # 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
    # https://gist.github.com/htruong/7df502fb60268eeee5bca21ef3e436eb
    # Invoke:
    # sudo ./chroot-to-pi.sh /dev/sdb

    # mount partition
    @@ -29,7 +29,11 @@ cp /usr/bin/qemu-arm-static /mnt/raspbian/usr/bin/
    # chroot to raspbian
    chroot /mnt/raspbian /bin/bash

    ########### You will be able to
    # 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
  9. @htruong htruong revised this gist Jan 24, 2018. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions chroot-to-pi.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,15 @@
    #!/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
    # https://gist.github.com/htruong/7df502fb60268eeee5bca21ef3e436eb
    # sudo ./chroot-to-pi.sh /dev/sdb

    # mount partition
    mount -o rw ${1}2 /mnt/raspbian
    mount -o rw ${1}1 /mnt/raspbian/boot
    @@ -19,6 +29,7 @@ cp /usr/bin/qemu-arm-static /mnt/raspbian/usr/bin/
    # chroot to raspbian
    chroot /mnt/raspbian /bin/bash

    ########### You will be able to

    # revert ld.so.preload fix
    sed -i 's/^#//g' /mnt/raspbian/etc/ld.so.preload
  10. @htruong htruong created this gist Jan 8, 2018.
    27 changes: 27 additions & 0 deletions chroot-to-pi.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/bin/bash

    # 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


    # 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,}