blob: 4fb9f4660e8377da00153044deea7b5cfb9d8b44 [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"
6PARTITION="2020-02-13-raspbian-buster-lite.img.partition"
7HOSTNAME="pi-8971-1"
8
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}"
19
20if mount | grep "${PARTITION}" >/dev/null ;
21then
22 echo "Already mounted"
23else
Austin Schuh90c82782020-02-26 19:54:51 -080024 OFFSET="$(fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print 512*$2}')"
25
26 if [[ "$(stat -c %s "${IMAGE}")" == 1849688064 ]]; then
27 echo "Growing image"
28 dd if=/dev/zero bs=1M count=1024 >> "${IMAGE}"
29 START="$(fdisk -lu "${IMAGE}" | grep "${IMAGE}2" | awk '{print $2}')"
30
31 sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk "${IMAGE}"
32 d # remove old partition
33 2
34 n # new partition
35 p # primary partition
36 2 # partion number 2
37 532480 # start where the old one starts
38 # To the end
39 p # print the in-memory partition table
40 w # Flush
41 q # and we're done
42EOF
43
44 sudo losetup -o "${OFFSET}" -f "${IMAGE}"
45 LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
46 sudo e2fsck -f "${LOOPBACK}"
47 sudo resize2fs "${LOOPBACK}"
48 sudo losetup -d "${LOOPBACK}"
49 fi
50
Austin Schuhab8c0c52020-02-22 13:42:37 -080051 echo "Mounting"
52 sudo mount -o loop,offset=${OFFSET} "${IMAGE}" "${PARTITION}"
53fi
54
55sudo cp target_configure.sh "${PARTITION}/tmp/"
56sudo cp dhcpcd.conf "${PARTITION}/tmp/dhcpcd.conf"
57sudo cp change_hostname.sh "${PARTITION}/tmp/change_hostname.sh"
58
59target /bin/mkdir -p /home/pi/.ssh/
60cat ~/.ssh/id_rsa.pub | target tee /home/pi/.ssh/authorized_keys
61
62target /bin/bash /tmp/target_configure.sh
63
64# Run a prompt as root inside the target to poke around and check things.
65target /bin/bash --rcfile /root/.bashrc
66
67sudo umount "${PARTITION}"
68rmdir "${PARTITION}"