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