blob: 15bdd802650ddad2e83a3ff916479440c2730ea2 [file] [log] [blame]
Austin Schuhab8c0c52020-02-22 13:42:37 -08001#!/bin/bash
2
3set -xe
4
Jim Ostrowskic58989a2022-01-29 16:12:08 -08005# Full path to Raspberry Pi Bullseye disk image
Austin Schuhf7970ca2022-02-11 22:19:29 -08006IMAGE="2022-01-28-raspios-bullseye-arm64-lite.img"
7# Kernel built with build_kernel.sh
8KERNEL="kernel_5.10.tar.gz"
Austin Schuhd4df9552020-09-13 18:59:50 -07009BOOT_PARTITION="${IMAGE}.boot_partition"
10PARTITION="${IMAGE}.partition"
Austin Schuhab8c0c52020-02-22 13:42:37 -080011
12function target() {
Austin Schuhf7970ca2022-02-11 22:19:29 -080013 HOME=/root/ USER=root sudo proot -0 -q qemu-aarch64-static -w / -r "${PARTITION}" "$@"
Austin Schuhab8c0c52020-02-22 13:42:37 -080014}
15
16function user_pi_target() {
Austin Schuhf7970ca2022-02-11 22:19:29 -080017 USER=root sudo proot -0 -q qemu-aarch64-static -w / -r "${PARTITION}" sudo -h 127.0.0.1 -u pi "$@"
Austin Schuhab8c0c52020-02-22 13:42:37 -080018}
19
Austin Schuh90c82782020-02-26 19:54:51 -080020
Austin Schuhab8c0c52020-02-22 13:42:37 -080021mkdir -p "${PARTITION}"
Austin Schuhe6965982020-02-26 23:12:05 -080022mkdir -p "${BOOT_PARTITION}"
23
24if mount | grep "${BOOT_PARTITION}" >/dev/null ;
25then
26 echo "Already mounted"
27else
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080028 OFFSET="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}1" | awk '{print 512*$2}')"
Austin Schuhe6965982020-02-26 23:12:05 -080029 sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${BOOT_PARTITION}"
30fi
31
32# Enable the camera on boot.
33if ! grep "start_x=1" "${BOOT_PARTITION}/config.txt"; then
34 echo "start_x=1" | sudo tee -a "${BOOT_PARTITION}/config.txt"
35fi
36if ! grep "gpu_mem=128" "${BOOT_PARTITION}/config.txt"; then
37 echo "gpu_mem=128" | sudo tee -a "${BOOT_PARTITION}/config.txt"
38fi
Austin Schuhc0ec2a82022-02-24 17:26:29 -080039if ! grep "enable_uart=1" "${BOOT_PARTITION}/config.txt"; then
40 echo "enable_uart=1" | sudo tee -a "${BOOT_PARTITION}/config.txt"
41fi
Jim Ostrowskic58989a2022-01-29 16:12:08 -080042# For now, disable the new libcamera driver in favor of legacy ones
43sudo sed -i s/^camera_auto_detect=1/#camera_auto_detect=1/ "${BOOT_PARTITION}/config.txt"
Austin Schuhc0ec2a82022-02-24 17:26:29 -080044# Enable SPI.
45sudo sed -i s/^.*dtparam=spi=on/dtparam=spi=on/ "${BOOT_PARTITION}/config.txt"
Austin Schuhe6965982020-02-26 23:12:05 -080046
Austin Schuhf7970ca2022-02-11 22:19:29 -080047sudo tar -zxvf "${KERNEL}" --strip-components 2 -C ${BOOT_PARTITION}/ ./fat32
48
Jim Ostrowskic58989a2022-01-29 16:12:08 -080049# Seeing a race condition with umount, so doing lazy umount
50sudo umount -l "${BOOT_PARTITION}"
Austin Schuhe6965982020-02-26 23:12:05 -080051rmdir "${BOOT_PARTITION}"
Austin Schuhab8c0c52020-02-22 13:42:37 -080052
53if mount | grep "${PARTITION}" >/dev/null ;
54then
55 echo "Already mounted"
56else
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080057 OFFSET="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print 512*$2}')"
Austin Schuh90c82782020-02-26 19:54:51 -080058
Austin Schuh7da6fe42021-06-20 15:37:55 -070059 if [[ "$(stat -c %s "${IMAGE}")" < 2000000000 ]]; then
Austin Schuh90c82782020-02-26 19:54:51 -080060 echo "Growing image"
Austin Schuhd4df9552020-09-13 18:59:50 -070061 dd if=/dev/zero bs=1G count=1 >> "${IMAGE}"
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080062 START="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print $2}')"
Austin Schuh90c82782020-02-26 19:54:51 -080063
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080064 sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | /sbin/fdisk "${IMAGE}"
Austin Schuh90c82782020-02-26 19:54:51 -080065 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
75EOF
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 Schuhab8c0c52020-02-22 13:42:37 -080084 echo "Mounting"
85 sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${PARTITION}"
86fi
87
Austin Schuhc0ec2a82022-02-24 17:26:29 -080088if [[ ! -e wiringpi-2.70-1.deb ]]; then
89 wget --continue https://software.frc971.org/Build-Dependencies/wiringpi-2.70-1.deb
90fi
91
Austin Schuhab8c0c52020-02-22 13:42:37 -080092sudo cp target_configure.sh "${PARTITION}/tmp/"
Austin Schuhc0ec2a82022-02-24 17:26:29 -080093sudo cp wiringpi-2.70-1.deb "${PARTITION}/tmp/"
Austin Schuhab8c0c52020-02-22 13:42:37 -080094sudo cp dhcpcd.conf "${PARTITION}/tmp/dhcpcd.conf"
Austin Schuh2fe4b712020-03-15 14:21:45 -070095sudo cp sctp.conf "${PARTITION}/etc/sysctl.d/sctp.conf"
Austin Schuhf3318a72021-11-01 22:05:32 -070096sudo cp logind.conf "${PARTITION}/etc/systemd/logind.conf"
Austin Schuhab8c0c52020-02-22 13:42:37 -080097sudo cp change_hostname.sh "${PARTITION}/tmp/change_hostname.sh"
Austin Schuhc0ec2a82022-02-24 17:26:29 -080098sudo cp enable_imu.sh "${PARTITION}/tmp/"
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080099sudo cp frc971.service "${PARTITION}/etc/systemd/system/frc971.service"
Austin Schuhf7970ca2022-02-11 22:19:29 -0800100sudo cp frc971chrt.service "${PARTITION}/etc/systemd/system/frc971chrt.service"
Austin Schuh92aa2de2020-09-19 19:09:55 -0700101sudo cp rt.conf "${PARTITION}/etc/security/limits.d/rt.conf"
Austin Schuh867bd922021-11-07 16:59:52 -0800102sudo cp usb-mount@.service "${PARTITION}/etc/systemd/system/usb-mount@.service"
103sudo cp 99-usb-mount.rules "${PARTITION}/etc/udev/rules.d/99-usb-mount.rules"
Austin Schuhab8c0c52020-02-22 13:42:37 -0800104
105target /bin/mkdir -p /home/pi/.ssh/
106cat ~/.ssh/id_rsa.pub | target tee /home/pi/.ssh/authorized_keys
107
Austin Schuhf7970ca2022-02-11 22:19:29 -0800108sudo rm -rf "${PARTITION}/lib/modules/"*
109sudo tar -zxvf "${KERNEL}" --strip-components 4 -C "${PARTITION}/lib/modules/" ./ext4/lib/modules/
Austin Schuhc0ec2a82022-02-24 17:26:29 -0800110sudo cp adis16505.ko "${PARTITION}/lib/modules/5.10.78-rt55-v8+/kernel/"
111target /usr/sbin/depmod 5.10.78-rt55-v8+
Austin Schuhf7970ca2022-02-11 22:19:29 -0800112
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800113# Downloads and installs our target libraries
Austin Schuhab8c0c52020-02-22 13:42:37 -0800114target /bin/bash /tmp/target_configure.sh
115
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800116# Add a file to show when this image was last modified and by whom
117TIMESTAMP_FILE="${PARTITION}/home/pi/.ImageModifiedDate.txt"
Jim Ostrowski2a483b32022-02-15 18:19:14 -0800118echo "Date modified:"`date` > "${TIMESTAMP_FILE}"
119echo "Git tag: "`git rev-parse HEAD` >> "${TIMESTAMP_FILE}"
120echo "User: "`whoami` >> "${TIMESTAMP_FILE}"
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800121
Austin Schuhab8c0c52020-02-22 13:42:37 -0800122# Run a prompt as root inside the target to poke around and check things.
123target /bin/bash --rcfile /root/.bashrc
124
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800125sudo umount -l "${PARTITION}"
Austin Schuhab8c0c52020-02-22 13:42:37 -0800126rmdir "${PARTITION}"
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800127
128# Move the image to a different name, to indicated we've modified it
129MOD_IMAGE_NAME=`echo ${IMAGE} | sed s/.img/-frc-mods.img/`
130mv ${IMAGE} ${MOD_IMAGE_NAME}