blob: eb87113471daf3bce77b7ebecd4b865f9cad0c38 [file] [log] [blame]
Austin Schuhab8c0c52020-02-22 13:42:37 -08001#!/bin/bash
2
3set -xe
4
Jim Ostrowskid59ebe62021-03-07 00:11:51 -08005# Full path to Raspberry Pi Buster disk image
6IMAGE="2020-08-20-raspios-buster-armhf-lite.img"
Austin Schuhd4df9552020-09-13 18:59:50 -07007BOOT_PARTITION="${IMAGE}.boot_partition"
8PARTITION="${IMAGE}.partition"
Austin Schuhab8c0c52020-02-22 13:42:37 -08009
10function target() {
11 HOME=/root/ USER=root sudo proot -0 -q qemu-arm-static -w / -r "${PARTITION}" "$@"
12}
13
14function 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 Schuh90c82782020-02-26 19:54:51 -080018
Austin Schuhab8c0c52020-02-22 13:42:37 -080019mkdir -p "${PARTITION}"
Austin Schuhe6965982020-02-26 23:12:05 -080020mkdir -p "${BOOT_PARTITION}"
21
22if mount | grep "${BOOT_PARTITION}" >/dev/null ;
23then
24 echo "Already mounted"
25else
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080026 OFFSET="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}1" | awk '{print 512*$2}')"
Austin Schuhe6965982020-02-26 23:12:05 -080027 sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${BOOT_PARTITION}"
28fi
29
30# Enable the camera on boot.
31if ! grep "start_x=1" "${BOOT_PARTITION}/config.txt"; then
32 echo "start_x=1" | sudo tee -a "${BOOT_PARTITION}/config.txt"
33fi
34if ! grep "gpu_mem=128" "${BOOT_PARTITION}/config.txt"; then
35 echo "gpu_mem=128" | sudo tee -a "${BOOT_PARTITION}/config.txt"
36fi
37
38sudo umount "${BOOT_PARTITION}"
39rmdir "${BOOT_PARTITION}"
Austin Schuhab8c0c52020-02-22 13:42:37 -080040
41if mount | grep "${PARTITION}" >/dev/null ;
42then
43 echo "Already mounted"
44else
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080045 OFFSET="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print 512*$2}')"
Austin Schuh90c82782020-02-26 19:54:51 -080046
Austin Schuh7da6fe42021-06-20 15:37:55 -070047 if [[ "$(stat -c %s "${IMAGE}")" < 2000000000 ]]; then
Austin Schuh90c82782020-02-26 19:54:51 -080048 echo "Growing image"
Austin Schuhd4df9552020-09-13 18:59:50 -070049 dd if=/dev/zero bs=1G count=1 >> "${IMAGE}"
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080050 START="$(/sbin/fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print $2}')"
Austin Schuh90c82782020-02-26 19:54:51 -080051
Jim Ostrowski33ea41e2021-03-11 12:17:38 -080052 sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | /sbin/fdisk "${IMAGE}"
Austin Schuh90c82782020-02-26 19:54:51 -080053 d # remove old partition
54 2
55 n # new partition
56 p # primary partition
57 2 # partion number 2
58 532480 # start where the old one starts
59 # To the end
60 p # print the in-memory partition table
61 w # Flush
62 q # and we're done
63EOF
64
65 sudo losetup -o "${OFFSET}" -f "${IMAGE}"
66 LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
67 sudo e2fsck -f "${LOOPBACK}"
68 sudo resize2fs "${LOOPBACK}"
69 sudo losetup -d "${LOOPBACK}"
70 fi
71
Austin Schuhab8c0c52020-02-22 13:42:37 -080072 echo "Mounting"
73 sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${PARTITION}"
74fi
75
76sudo cp target_configure.sh "${PARTITION}/tmp/"
77sudo cp dhcpcd.conf "${PARTITION}/tmp/dhcpcd.conf"
Austin Schuh2fe4b712020-03-15 14:21:45 -070078sudo cp sctp.conf "${PARTITION}/etc/sysctl.d/sctp.conf"
Austin Schuhf3318a72021-11-01 22:05:32 -070079sudo cp logind.conf "${PARTITION}/etc/systemd/logind.conf"
Austin Schuhab8c0c52020-02-22 13:42:37 -080080sudo cp change_hostname.sh "${PARTITION}/tmp/change_hostname.sh"
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080081sudo cp frc971.service "${PARTITION}/etc/systemd/system/frc971.service"
Austin Schuh92aa2de2020-09-19 19:09:55 -070082sudo cp rt.conf "${PARTITION}/etc/security/limits.d/rt.conf"
Austin Schuhab8c0c52020-02-22 13:42:37 -080083
84target /bin/mkdir -p /home/pi/.ssh/
85cat ~/.ssh/id_rsa.pub | target tee /home/pi/.ssh/authorized_keys
86
87target /bin/bash /tmp/target_configure.sh
88
89# Run a prompt as root inside the target to poke around and check things.
90target /bin/bash --rcfile /root/.bashrc
91
92sudo umount "${PARTITION}"
93rmdir "${PARTITION}"