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