Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | set -xe |
| 4 | |
Jim Ostrowski | c58989a | 2022-01-29 16:12:08 -0800 | [diff] [blame^] | 5 | # Full path to Raspberry Pi Bullseye disk image |
| 6 | IMAGE="2021-10-30-raspios-bullseye-armhf-lite.img" |
Austin Schuh | d4df955 | 2020-09-13 18:59:50 -0700 | [diff] [blame] | 7 | BOOT_PARTITION="${IMAGE}.boot_partition" |
| 8 | PARTITION="${IMAGE}.partition" |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 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 |
Jim Ostrowski | 33ea41e | 2021-03-11 12:17:38 -0800 | [diff] [blame] | 26 | OFFSET="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}1" | awk '{print 512*$2}')" |
Austin Schuh | e696598 | 2020-02-26 23:12:05 -0800 | [diff] [blame] | 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 |
Jim Ostrowski | c58989a | 2022-01-29 16:12:08 -0800 | [diff] [blame^] | 37 | # For now, disable the new libcamera driver in favor of legacy ones |
| 38 | sudo sed -i s/^camera_auto_detect=1/#camera_auto_detect=1/ "${BOOT_PARTITION}/config.txt" |
Austin Schuh | e696598 | 2020-02-26 23:12:05 -0800 | [diff] [blame] | 39 | |
Jim Ostrowski | c58989a | 2022-01-29 16:12:08 -0800 | [diff] [blame^] | 40 | # Seeing a race condition with umount, so doing lazy umount |
| 41 | sudo umount -l "${BOOT_PARTITION}" |
Austin Schuh | e696598 | 2020-02-26 23:12:05 -0800 | [diff] [blame] | 42 | rmdir "${BOOT_PARTITION}" |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 43 | |
| 44 | if mount | grep "${PARTITION}" >/dev/null ; |
| 45 | then |
| 46 | echo "Already mounted" |
| 47 | else |
Jim Ostrowski | 33ea41e | 2021-03-11 12:17:38 -0800 | [diff] [blame] | 48 | OFFSET="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print 512*$2}')" |
Austin Schuh | 90c8278 | 2020-02-26 19:54:51 -0800 | [diff] [blame] | 49 | |
Austin Schuh | 7da6fe4 | 2021-06-20 15:37:55 -0700 | [diff] [blame] | 50 | if [[ "$(stat -c %s "${IMAGE}")" < 2000000000 ]]; then |
Austin Schuh | 90c8278 | 2020-02-26 19:54:51 -0800 | [diff] [blame] | 51 | echo "Growing image" |
Austin Schuh | d4df955 | 2020-09-13 18:59:50 -0700 | [diff] [blame] | 52 | dd if=/dev/zero bs=1G count=1 >> "${IMAGE}" |
Jim Ostrowski | 33ea41e | 2021-03-11 12:17:38 -0800 | [diff] [blame] | 53 | START="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print $2}')" |
Austin Schuh | 90c8278 | 2020-02-26 19:54:51 -0800 | [diff] [blame] | 54 | |
Jim Ostrowski | 33ea41e | 2021-03-11 12:17:38 -0800 | [diff] [blame] | 55 | sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | /sbin/fdisk "${IMAGE}" |
Austin Schuh | 90c8278 | 2020-02-26 19:54:51 -0800 | [diff] [blame] | 56 | d # remove old partition |
| 57 | 2 |
| 58 | n # new partition |
| 59 | p # primary partition |
| 60 | 2 # partion number 2 |
| 61 | 532480 # start where the old one starts |
| 62 | # To the end |
| 63 | p # print the in-memory partition table |
| 64 | w # Flush |
| 65 | q # and we're done |
| 66 | EOF |
| 67 | |
| 68 | sudo losetup -o "${OFFSET}" -f "${IMAGE}" |
| 69 | LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')" |
| 70 | sudo e2fsck -f "${LOOPBACK}" |
| 71 | sudo resize2fs "${LOOPBACK}" |
| 72 | sudo losetup -d "${LOOPBACK}" |
| 73 | fi |
| 74 | |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 75 | echo "Mounting" |
| 76 | sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${PARTITION}" |
| 77 | fi |
| 78 | |
| 79 | sudo cp target_configure.sh "${PARTITION}/tmp/" |
| 80 | sudo cp dhcpcd.conf "${PARTITION}/tmp/dhcpcd.conf" |
Austin Schuh | 2fe4b71 | 2020-03-15 14:21:45 -0700 | [diff] [blame] | 81 | sudo cp sctp.conf "${PARTITION}/etc/sysctl.d/sctp.conf" |
Austin Schuh | f3318a7 | 2021-11-01 22:05:32 -0700 | [diff] [blame] | 82 | sudo cp logind.conf "${PARTITION}/etc/systemd/logind.conf" |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 83 | sudo cp change_hostname.sh "${PARTITION}/tmp/change_hostname.sh" |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 84 | sudo cp frc971.service "${PARTITION}/etc/systemd/system/frc971.service" |
Austin Schuh | 92aa2de | 2020-09-19 19:09:55 -0700 | [diff] [blame] | 85 | sudo cp rt.conf "${PARTITION}/etc/security/limits.d/rt.conf" |
Austin Schuh | 867bd92 | 2021-11-07 16:59:52 -0800 | [diff] [blame] | 86 | sudo cp usb-mount@.service "${PARTITION}/etc/systemd/system/usb-mount@.service" |
| 87 | sudo cp 99-usb-mount.rules "${PARTITION}/etc/udev/rules.d/99-usb-mount.rules" |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 88 | |
| 89 | target /bin/mkdir -p /home/pi/.ssh/ |
| 90 | cat ~/.ssh/id_rsa.pub | target tee /home/pi/.ssh/authorized_keys |
| 91 | |
Jim Ostrowski | c58989a | 2022-01-29 16:12:08 -0800 | [diff] [blame^] | 92 | # Downloads and installs our target libraries |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 93 | target /bin/bash /tmp/target_configure.sh |
| 94 | |
Jim Ostrowski | c58989a | 2022-01-29 16:12:08 -0800 | [diff] [blame^] | 95 | # Add a file to show when this image was last modified and by whom |
| 96 | TIMESTAMP_FILE="${PARTITION}/home/pi/.ImageModifiedDate.txt" |
| 97 | date > "${TIMESTAMP_FILE}" |
| 98 | git rev-parse HEAD >> "${TIMESTAMP_FILE}" |
| 99 | whoami >> "${TIMESTAMP_FILE}" |
| 100 | |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 101 | # Run a prompt as root inside the target to poke around and check things. |
| 102 | target /bin/bash --rcfile /root/.bashrc |
| 103 | |
Jim Ostrowski | c58989a | 2022-01-29 16:12:08 -0800 | [diff] [blame^] | 104 | sudo umount -l "${PARTITION}" |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 105 | rmdir "${PARTITION}" |
Jim Ostrowski | c58989a | 2022-01-29 16:12:08 -0800 | [diff] [blame^] | 106 | |
| 107 | # Move the image to a different name, to indicated we've modified it |
| 108 | MOD_IMAGE_NAME=`echo ${IMAGE} | sed s/.img/-frc-mods.img/` |
| 109 | mv ${IMAGE} ${MOD_IMAGE_NAME} |