Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | set -xe |
| 4 | |
| 5 | IMAGE="2020-02-13-raspbian-buster-lite.img" |
| 6 | PARTITION="2020-02-13-raspbian-buster-lite.img.partition" |
| 7 | HOSTNAME="pi-8971-1" |
| 8 | |
| 9 | function target() { |
| 10 | HOME=/root/ USER=root sudo proot -0 -q qemu-arm-static -w / -r "${PARTITION}" "$@" |
| 11 | } |
| 12 | |
| 13 | function user_pi_target() { |
| 14 | USER=root sudo proot -0 -q qemu-arm-static -w / -r "${PARTITION}" sudo -h 127.0.0.1 -u pi "$@" |
| 15 | } |
| 16 | |
Austin Schuh | 90c8278 | 2020-02-26 19:54:51 -0800 | [diff] [blame^] | 17 | |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 18 | mkdir -p "${PARTITION}" |
| 19 | |
| 20 | if mount | grep "${PARTITION}" >/dev/null ; |
| 21 | then |
| 22 | echo "Already mounted" |
| 23 | else |
Austin Schuh | 90c8278 | 2020-02-26 19:54:51 -0800 | [diff] [blame^] | 24 | OFFSET="$(fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print 512*$2}')" |
| 25 | |
| 26 | if [[ "$(stat -c %s "${IMAGE}")" == 1849688064 ]]; then |
| 27 | echo "Growing image" |
| 28 | dd if=/dev/zero bs=1M count=1024 >> "${IMAGE}" |
| 29 | START="$(fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print $2}')" |
| 30 | |
| 31 | sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk "${IMAGE}" |
| 32 | d # remove old partition |
| 33 | 2 |
| 34 | n # new partition |
| 35 | p # primary partition |
| 36 | 2 # partion number 2 |
| 37 | 532480 # start where the old one starts |
| 38 | # To the end |
| 39 | p # print the in-memory partition table |
| 40 | w # Flush |
| 41 | q # and we're done |
| 42 | EOF |
| 43 | |
| 44 | sudo losetup -o "${OFFSET}" -f "${IMAGE}" |
| 45 | LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')" |
| 46 | sudo e2fsck -f "${LOOPBACK}" |
| 47 | sudo resize2fs "${LOOPBACK}" |
| 48 | sudo losetup -d "${LOOPBACK}" |
| 49 | fi |
| 50 | |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 51 | echo "Mounting" |
| 52 | sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${PARTITION}" |
| 53 | fi |
| 54 | |
| 55 | sudo cp target_configure.sh "${PARTITION}/tmp/" |
| 56 | sudo cp dhcpcd.conf "${PARTITION}/tmp/dhcpcd.conf" |
| 57 | sudo cp change_hostname.sh "${PARTITION}/tmp/change_hostname.sh" |
| 58 | |
| 59 | target /bin/mkdir -p /home/pi/.ssh/ |
| 60 | cat ~/.ssh/id_rsa.pub | target tee /home/pi/.ssh/authorized_keys |
| 61 | |
| 62 | target /bin/bash /tmp/target_configure.sh |
| 63 | |
| 64 | # Run a prompt as root inside the target to poke around and check things. |
| 65 | target /bin/bash --rcfile /root/.bashrc |
| 66 | |
| 67 | sudo umount "${PARTITION}" |
| 68 | rmdir "${PARTITION}" |