blob: 6ebeeca561e29c0ca9ad436f73673df5ec64ec4c [file] [log] [blame]
Austin Schuhab8c0c52020-02-22 13:42:37 -08001#!/bin/bash
2
3set -xe
4
5IMAGE="2020-02-13-raspbian-buster-lite.img"
Austin Schuhd4df9552020-09-13 18:59:50 -07006BOOT_PARTITION="${IMAGE}.boot_partition"
7PARTITION="${IMAGE}.partition"
Austin Schuhab8c0c52020-02-22 13:42:37 -08008
9function target() {
10 HOME=/root/ USER=root sudo proot -0 -q qemu-arm-static -w / -r "${PARTITION}" "$@"
11}
12
13function user_pi_target() {
14 USER=root sudo proot -0 -q qemu-arm-static -w / -r "${PARTITION}" sudo -h 127.0.0.1 -u pi "$@"
15}
16
Austin Schuh90c82782020-02-26 19:54:51 -080017
Austin Schuhab8c0c52020-02-22 13:42:37 -080018mkdir -p "${PARTITION}"
Austin Schuhe6965982020-02-26 23:12:05 -080019mkdir -p "${BOOT_PARTITION}"
20
21if mount | grep "${BOOT_PARTITION}" >/dev/null ;
22then
23 echo "Already mounted"
24else
25 OFFSET="$(fdisk -lu "${IMAGE}" | grep "${IMAGE}1" | awk '{print 512*$2}')"
26 sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${BOOT_PARTITION}"
27fi
28
29# Enable the camera on boot.
30if ! grep "start_x=1" "${BOOT_PARTITION}/config.txt"; then
31 echo "start_x=1" | sudo tee -a "${BOOT_PARTITION}/config.txt"
32fi
33if ! grep "gpu_mem=128" "${BOOT_PARTITION}/config.txt"; then
34 echo "gpu_mem=128" | sudo tee -a "${BOOT_PARTITION}/config.txt"
35fi
36
37sudo umount "${BOOT_PARTITION}"
38rmdir "${BOOT_PARTITION}"
Austin Schuhab8c0c52020-02-22 13:42:37 -080039
40if mount | grep "${PARTITION}" >/dev/null ;
41then
42 echo "Already mounted"
43else
Austin Schuh90c82782020-02-26 19:54:51 -080044 OFFSET="$(fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print 512*$2}')"
45
Austin Schuhd4df9552020-09-13 18:59:50 -070046 if [[ "$(stat -c %s "${IMAGE}")" < 2000000000 ]]; then
Austin Schuh90c82782020-02-26 19:54:51 -080047 echo "Growing image"
Austin Schuhd4df9552020-09-13 18:59:50 -070048 dd if=/dev/zero bs=1G count=1 >> "${IMAGE}"
Austin Schuh90c82782020-02-26 19:54:51 -080049 START="$(fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print $2}')"
50
51 sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk "${IMAGE}"
52 d # remove old partition
53 2
54 n # new partition
55 p # primary partition
56 2 # partion number 2
57 532480 # start where the old one starts
58 # To the end
59 p # print the in-memory partition table
60 w # Flush
61 q # and we're done
62EOF
63
64 sudo losetup -o "${OFFSET}" -f "${IMAGE}"
65 LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
66 sudo e2fsck -f "${LOOPBACK}"
67 sudo resize2fs "${LOOPBACK}"
68 sudo losetup -d "${LOOPBACK}"
69 fi
70
Austin Schuhab8c0c52020-02-22 13:42:37 -080071 echo "Mounting"
72 sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${PARTITION}"
73fi
74
75sudo cp target_configure.sh "${PARTITION}/tmp/"
76sudo cp dhcpcd.conf "${PARTITION}/tmp/dhcpcd.conf"
Austin Schuh2fe4b712020-03-15 14:21:45 -070077sudo cp sctp.conf "${PARTITION}/etc/sysctl.d/sctp.conf"
Austin Schuhab8c0c52020-02-22 13:42:37 -080078sudo cp change_hostname.sh "${PARTITION}/tmp/change_hostname.sh"
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080079sudo cp frc971.service "${PARTITION}/etc/systemd/system/frc971.service"
Austin Schuh92aa2de2020-09-19 19:09:55 -070080sudo cp rt.conf "${PARTITION}/etc/security/limits.d/rt.conf"
Austin Schuhab8c0c52020-02-22 13:42:37 -080081
82target /bin/mkdir -p /home/pi/.ssh/
83cat ~/.ssh/id_rsa.pub | target tee /home/pi/.ssh/authorized_keys
84
85target /bin/bash /tmp/target_configure.sh
86
87# Run a prompt as root inside the target to poke around and check things.
88target /bin/bash --rcfile /root/.bashrc
89
90sudo umount "${PARTITION}"
91rmdir "${PARTITION}"