blob: 21b254f0fdfe66b9629b6e54fece0c73adaa36e5 [file] [log] [blame]
Austin Schuh791ac752022-11-26 19:14:39 -08001#!/bin/bash
2
3set -eux
4set -o pipefail
5
6UBOOT_VERSION=v2022.10
7
8IMAGE="arm64_bullseye_debian.img"
9KERNEL_VERSION=6.0.8-rt14-rockpi4b
10PARTITION="${IMAGE}.partition"
11
12# TODO(austin): Make sure flex, bison, gcc-aarch64-linux-gnu,
13# gcc-arm-none-eabi, device-tree-compiler, swig are installed
14# rather than let the user figure this out on their own the hard way.
15export CC=aarch64-linux-gnu-
16
17# Reset any existing mounts.
18if mount | grep "${PARTITION}/boot" >/dev/null; then
19 sudo umount "${PARTITION}/boot"
20fi
21
22if mount | grep "${PARTITION}" >/dev/null; then
23 sudo umount "${PARTITION}"
24fi
25
26LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
27if [[ -n "${LOOPBACK}" ]]; then
28 echo "Loop still exists..."
29 sudo losetup -d "${LOOPBACK}"
30fi
31
32# Build bl31.elf.
33if [[ ! -e arm-trusted-firmware ]]; then
34 git clone https://github.com/ARM-software/arm-trusted-firmware --depth=1
35fi
36
37pushd arm-trusted-firmware/
38git pull --ff-only
39#make CROSS_COMPILE="${CC}" realclean -j "$(nproc)"
40make CROSS_COMPILE="${CC}" PLAT=rk3399 -j "$(nproc)"
41popd
42
43export BL31="$(pwd)/arm-trusted-firmware/build/rk3399/release/bl31/bl31.elf"
44
45ls -lah "${BL31}"
46
47# Now, build uboot.
48if [[ ! -e u-boot ]]; then
49 git clone -b "${UBOOT_VERSION} https://github.com/u-boot/u-boot" --depth=1
50fi
51
52pushd u-boot/
53git fetch origin "${UBOOT_VERSION}"
54git reset --hard "${UBOOT_VERSION}"
55
56#make ARCH=arm CROSS_COMPILE="${CC}" distclean -j $(nproc)
57make ARCH=arm CROSS_COMPILE="${CC}" rock-pi-4-rk3399_defconfig -j "$(nproc)"
58make ARCH=arm CROSS_COMPILE="${CC}" -j "$(nproc)"
59echo "Made uboot"
60popd
61
62
63# Now build the base set of partitions.
64NEW_IMAGE=0
65if [[ ! -e "${IMAGE}" ]]; then
66 NEW_IMAGE=1
67 dd if=/dev/zero of="${IMAGE}" bs=1 count=0 seek=3G
68 dd if=./u-boot/idbloader.img of="${IMAGE}" seek=64 conv=notrunc
69 dd if=./u-boot/u-boot.itb of="${IMAGE}" seek=16384 conv=notrunc
70
71 sfdisk "${IMAGE}" <<-__EOF__
7216M,64M,L,*
7380M,,L,*
74__EOF__
75
76 sudo losetup -P -f "${IMAGE}"
77 LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
78
79 sudo mkfs.fat "${LOOPBACK}p1"
80 sudo mkfs.ext4 -L rootfs "${LOOPBACK}p2"
81
82 sudo losetup -d "${LOOPBACK}"
83fi
84
85
86# Mount them
87mkdir -p "${PARTITION}"
88
89sudo losetup -P -f "${IMAGE}"
90LOOPBACK="$(sudo losetup --list | grep "${IMAGE}" | awk '{print $1}')"
91sudo mount "${LOOPBACK}p2" "${PARTITION}"
92if [[ ! -e "${PARTITION}/boot" ]]; then
93 sudo mkdir "${PARTITION}/boot"
94fi
95sudo mount "${LOOPBACK}p1" "${PARTITION}/boot"
96
97# Run the string command as root inside the target.
98function target() {
99 sudo chroot --userspec=root.root "${PARTITION}" qemu-aarch64-static \
100 /bin/bash -c "$1"
101}
102
103# Run the string command as the pi user inside the target.
104function pi_target() {
105 sudo chroot --userspec=pi.pi "${PARTITION}" qemu-aarch64-static \
106 /bin/bash -c "$1"
107}
108
109# And, if we made a new image, debootstrap to get things going.
110if (( NEW_IMAGE == 1 )); then
111 sudo debootstrap --arch=arm64 --no-check-gpg --foreign bullseye \
112 "${PARTITION}" http://deb.debian.org/debian/
113 sudo cp /usr/bin/qemu-aarch64-static "${PARTITION}/usr/bin/"
114
115 target "/debootstrap/debootstrap --second-stage"
116
117 target "useradd -m -p '\$y\$j9T\$85lzhdky63CTj.two7Zj20\$pVY53UR0VebErMlm8peyrEjmxeiRw/rfXfx..9.xet1' -s /bin/bash pi"
118
119else
120 sudo cp /usr/bin/qemu-aarch64-static "${PARTITION}/usr/bin/"
121fi
122
123# Install the kernel.
124sudo rm -rf "${PARTITION}/boot/dtbs/${KERNEL_VERSION}/"
125sudo rm -rf "${PARTITION}/lib/modules/${KERNEL_VERSION}/"
126sudo mkdir -p "${PARTITION}/lib/modules/"
127sudo tar --owner=0 --group=0 --no-same-owner --no-same-permissions -xvf \
128 "linux-kernel-${KERNEL_VERSION}.tar.xz" -C "${PARTITION}/boot/" \
129 --strip-components=2 ./boot/
130sudo tar --strip-components=3 -xvf "linux-kernel-${KERNEL_VERSION}.tar.xz" \
131 -C "${PARTITION}/lib/modules/" ./lib/modules
132
133# Now, configure it to start automatically.
134sudo mkdir -p ${PARTITION}/boot/extlinux/
135cat << __EOF__ | sudo tee "${PARTITION}/boot/extlinux/extlinux.conf"
136label Linux ${KERNEL_VERSION}
137 kernel /vmlinuz-${KERNEL_VERSION}
138 append earlycon=uart8250,mmio32,0xff1a0000 earlyprintk console=ttyS2,1500000n8 root=/dev/mmcblk0p2 ro rootfstype=ext4 rootwait
139 fdtdir /dtbs/${KERNEL_VERSION}/
140__EOF__
141
142copyfile root.root 644 etc/apt/sources.list.d/bullseye-backports.list
143copyfile root.root 644 etc/apt/sources.list.d/frc971.list
144
145target "apt-get update"
146target "apt-get -y install -t bullseye-backports systemd"
147
148target "apt-get install -y sudo openssh-server python3 bash-completion git v4l-utils cpufrequtils pmount rsync vim-nox chrony libopencv-calib3d4.5 libopencv-contrib4.5 libopencv-core4.5 libopencv-features2d4.5 libopencv-flann4.5 libopencv-highgui4.5 libopencv-imgcodecs4.5 libopencv-imgproc4.5 libopencv-ml4.5 libopencv-objdetect4.5 libopencv-photo4.5 libopencv-shape4.5 libopencv-stitching4.5 libopencv-superres4.5 libopencv-video4.5 libopencv-videoio4.5 libopencv-videostab4.5 libopencv-viz4.5 libnice10 pmount libnice-dev feh libgstreamer1.0-0 libgstreamer-plugins-base1.0-0 libgstreamer-plugins-bad1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-nice usbutils"
149
150target "usermod -a -G sudo pi"
151target "usermod -a -G video pi"
152
153function target_mkdir() {
154 target "install -d -m $2 -o $(echo $1 | sed 's/\.[^.]*//') -g $(echo $1 | sed 's/[^.]*\.//') $3"
155}
156
157function copyfile() {
158 sudo cp contents/$3 ${PARTITION}/$3
159 sudo chmod $2 ${PARTITION}/$3
160 target "chown $1 /$3"
161}
162
163copyfile root.root 644 etc/fstab
164copyfile root.root 440 etc/sudoers
165copyfile root.root 444 etc/default/cpufrequtils
166target_mkdir root.root 755 etc/systemd/network
167copyfile root.root 644 etc/systemd/network/eth0.network
168target_mkdir pi.pi 755 home/pi/.ssh
169copyfile pi.pi 600 home/pi/.ssh/authorized_keys
170target_mkdir root.root 700 root/bin
171copyfile root.root 500 root/bin/grow.sh
172copyfile root.root 500 root/bin/change_hostname.sh
173copyfile root.root 500 root/bin/deploy_kernel.sh
174copyfile root.root 644 etc/systemd/system/grow-rootfs.service
175copyfile root.root 644 etc/sysctl.d/sctp.conf
176copyfile root.root 644 etc/systemd/logind.conf
177copyfile root.root 644 etc/security/limits.d/rt.conf
178copyfile root.root 644 etc/systemd/system/frc971.service
179copyfile root.root 644 etc/systemd/system/frc971chrt.service
180copyfile root.root 644 etc/systemd/system/usb-mount@.service
181copyfile root.root 644 etc/udev/rules.d/99-usb-mount.rules
182
183target "apt-get update"
184target "apt-get -y install -t bullseye-backports systemd"
185
186target "systemctl enable systemd-networkd"
187target "systemctl enable systemd-resolved"
188target "systemctl enable grow-rootfs"
189target "systemctl enable frc971"
190target "systemctl enable frc971chrt"
191target "/root/bin/change_hostname.sh pi-971-1"
192
193
194if [[ ! -e "${PARTITION}/home/pi/.dotfiles" ]]; then
195 pi_target "cd /home/pi/ && \
196 git clone --separate-git-dir=/home/pi/.dotfiles https://github.com/AustinSchuh/.dotfiles.git tmpdotfiles && \
197 rsync --recursive --verbose --exclude .git tmpdotfiles/ /home/pi/ && \
198 rm -r tmpdotfiles && \
199 git --git-dir=/home/pi/.dotfiles/ --work-tree=/home/pi/ config --local status.showUntrackedFiles no && \
200 vim -c \":qa!\""
201
202 target "cd /root/ && \
203 git clone --separate-git-dir=/root/.dotfiles https://github.com/AustinSchuh/.dotfiles.git tmpdotfiles && \
204 rsync --recursive --verbose --exclude .git tmpdotfiles/ /root/ && rm -r tmpdotfiles && \
205 git --git-dir=/root/.dotfiles/ --work-tree=/root/ config --local status.showUntrackedFiles no && \
206 vim -c \":qa!\""
207fi
208
209target "apt-get clean"
210
211sudo chroot ${PARTITION} qemu-aarch64-static /bin/bash
212
213# TODO(austin): This appears to not be working... pi_target doesn't apper to be happy
214sudo chroot --userspec=pi.pi ${PARTITION} qemu-aarch64-static /bin/bash
215
216# TODO(austin): everything else we were doing to the pi's.
217sudo rm ${PARTITION}/usr/bin/qemu-aarch64-static
218sudo umount ${PARTITION}/boot
219sudo umount ${PARTITION}
220rmdir ${PARTITION}