blob: fd9bec9b1c16dcdcd341dc9282dbcf1a59ea1f1d [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
Jim Ostrowskic58989a2022-01-29 16:12:08 -080039# For now, disable the new libcamera driver in favor of legacy ones
40sudo sed -i s/^camera_auto_detect=1/#camera_auto_detect=1/ "${BOOT_PARTITION}/config.txt"
Austin Schuhe6965982020-02-26 23:12:05 -080041
Austin Schuhf7970ca2022-02-11 22:19:29 -080042sudo tar -zxvf "${KERNEL}" --strip-components 2 -C ${BOOT_PARTITION}/ ./fat32
43
Jim Ostrowskic58989a2022-01-29 16:12:08 -080044# Seeing a race condition with umount, so doing lazy umount
45sudo umount -l "${BOOT_PARTITION}"
Austin Schuhe6965982020-02-26 23:12:05 -080046rmdir "${BOOT_PARTITION}"
Austin Schuhab8c0c52020-02-22 13:42:37 -080047
48if mount | grep "${PARTITION}" >/dev/null ;
49then
50 echo "Already mounted"
51else
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080052 OFFSET="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print 512*$2}')"
Austin Schuh90c82782020-02-26 19:54:51 -080053
Austin Schuh7da6fe42021-06-20 15:37:55 -070054 if [[ "$(stat -c %s "${IMAGE}")" < 2000000000 ]]; then
Austin Schuh90c82782020-02-26 19:54:51 -080055 echo "Growing image"
Austin Schuhd4df9552020-09-13 18:59:50 -070056 dd if=/dev/zero bs=1G count=1 >> "${IMAGE}"
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080057 START="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print $2}')"
Austin Schuh90c82782020-02-26 19:54:51 -080058
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080059 sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | /sbin/fdisk "${IMAGE}"
Austin Schuh90c82782020-02-26 19:54:51 -080060 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
70EOF
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 Schuhab8c0c52020-02-22 13:42:37 -080079 echo "Mounting"
80 sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${PARTITION}"
81fi
82
83sudo cp target_configure.sh "${PARTITION}/tmp/"
84sudo cp dhcpcd.conf "${PARTITION}/tmp/dhcpcd.conf"
Austin Schuh2fe4b712020-03-15 14:21:45 -070085sudo cp sctp.conf "${PARTITION}/etc/sysctl.d/sctp.conf"
Austin Schuhf3318a72021-11-01 22:05:32 -070086sudo cp logind.conf "${PARTITION}/etc/systemd/logind.conf"
Austin Schuhab8c0c52020-02-22 13:42:37 -080087sudo cp change_hostname.sh "${PARTITION}/tmp/change_hostname.sh"
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080088sudo cp frc971.service "${PARTITION}/etc/systemd/system/frc971.service"
Austin Schuhf7970ca2022-02-11 22:19:29 -080089sudo cp frc971chrt.service "${PARTITION}/etc/systemd/system/frc971chrt.service"
Austin Schuh92aa2de2020-09-19 19:09:55 -070090sudo cp rt.conf "${PARTITION}/etc/security/limits.d/rt.conf"
Austin Schuh867bd922021-11-07 16:59:52 -080091sudo cp usb-mount@.service "${PARTITION}/etc/systemd/system/usb-mount@.service"
92sudo cp 99-usb-mount.rules "${PARTITION}/etc/udev/rules.d/99-usb-mount.rules"
Austin Schuhab8c0c52020-02-22 13:42:37 -080093
94target /bin/mkdir -p /home/pi/.ssh/
95cat ~/.ssh/id_rsa.pub | target tee /home/pi/.ssh/authorized_keys
96
Austin Schuhf7970ca2022-02-11 22:19:29 -080097sudo rm -rf "${PARTITION}/lib/modules/"*
98sudo tar -zxvf "${KERNEL}" --strip-components 4 -C "${PARTITION}/lib/modules/" ./ext4/lib/modules/
99
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800100# Downloads and installs our target libraries
Austin Schuhab8c0c52020-02-22 13:42:37 -0800101target /bin/bash /tmp/target_configure.sh
102
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800103# Add a file to show when this image was last modified and by whom
104TIMESTAMP_FILE="${PARTITION}/home/pi/.ImageModifiedDate.txt"
Jim Ostrowski2a483b32022-02-15 18:19:14 -0800105echo "Date modified:"`date` > "${TIMESTAMP_FILE}"
106echo "Git tag: "`git rev-parse HEAD` >> "${TIMESTAMP_FILE}"
107echo "User: "`whoami` >> "${TIMESTAMP_FILE}"
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800108
Austin Schuhab8c0c52020-02-22 13:42:37 -0800109# Run a prompt as root inside the target to poke around and check things.
110target /bin/bash --rcfile /root/.bashrc
111
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800112sudo umount -l "${PARTITION}"
Austin Schuhab8c0c52020-02-22 13:42:37 -0800113rmdir "${PARTITION}"
Jim Ostrowskic58989a2022-01-29 16:12:08 -0800114
115# Move the image to a different name, to indicated we've modified it
116MOD_IMAGE_NAME=`echo ${IMAGE} | sed s/.img/-frc-mods.img/`
117mv ${IMAGE} ${MOD_IMAGE_NAME}