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