aboutsummaryrefslogtreecommitdiff
path: root/state/gen
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-02-02 21:52:11 +0100
committerlonkaars <loek@pipeframe.xyz>2024-02-02 21:52:11 +0100
commit2b2cb626b527606436b6d7903407b7dbf66d9671 (patch)
treeac46c4d259d06b0928343984faf3d42d4d5d470e /state/gen
parent1836f009172a248e087fcb15bbd2c549094073a5 (diff)
WIP more unneeded automation of setup
Diffstat (limited to 'state/gen')
-rwxr-xr-xstate/gen65
1 files changed, 65 insertions, 0 deletions
diff --git a/state/gen b/state/gen
new file mode 100755
index 0000000..23a3382
--- /dev/null
+++ b/state/gen
@@ -0,0 +1,65 @@
+#!/bin/sh
+# redirect stdout to stderr (stdout gets interpreted by make)
+exec 1>&2
+# create files in the same directory as this script
+cd "$(dirname "$0")"
+# do not run anything if make was run with -n (dry run)
+# [ "${MAKEFLAGS#*n}" != "$MAKEFLAGS" ] && exit
+
+touch_rm_exit_code() { [ $? -eq 0 ] && touch "$1" || rm -f "$1" ; }
+
+# container image (initialized here, updated in makefile)
+$CTR image exists "$CTR_IMG_TAG" 1> /dev/null 2> /dev/null
+touch_rm_exit_code container_img
+
+# check if all the submodules are initialized
+test -z "$(git submodule status | grep '^-')"
+touch_rm_exit_code submodules
+
+rm -f auto.mk
+
+# guess which device is the sd card by size (around 8GB)
+for SDCARD_DISK in "$(lsblk --noheadings --bytes --output PATH,SIZE,TYPE | awk '
+ $3 != "disk" { next }
+ ($2 < 7900000000) { next }
+ ($2 > 8000000000) { next }
+ { print $1 }')"
+do
+ [ -z "$SDCARD_DISK" ] && continue
+ # check if the sd card is already correctly partitioned & formatted
+ PARTS="$(lsblk "$SDCARD_DISK" --noheadings --output PATH,SIZE,TYPE,PTTYPE,FSTYPE,LABEL | awk '
+ NR == 1 {
+ if ($4 != "dos") exit(1)
+ }
+ NR == 2 {
+ if ($6 != "BOOT") exit(1)
+ if ($2 != "64M") exit(1)
+ if ($5 != "vfat") exit(1)
+ print $1
+ }
+ NR == 3 {
+ if ($6 != "ROOTFS") exit(1)
+ if ($5 != "ext4") exit(1)
+ print $1
+ }
+ END { if (NR != 3) exit(1) }')" || continue
+ SDCARD_PART_BOOT="$(echo "$PARTS" | sed '1!d')"
+ SDCARD_PART_ROOTFS="$(echo "$PARTS" | sed '2!d')"
+ break
+done
+
+if [ -n "$SDCARD_DISK" ]; then
+ echo "SDCARD_DISK ?= $SDCARD_DISK" >> auto.mk
+fi
+
+if [ -n "$SDCARD_PART_BOOT" ] && [ -n "$SDCARD_PART_ROOTFS" ]; then
+ echo "SDCARD_PART_BOOT ?= $SDCARD_PART_BOOT" >> auto.mk
+ echo "SDCARD_PART_ROOTFS ?= $SDCARD_PART_ROOTFS" >> auto.mk
+ touch sdcard_part_fmt
+else
+ rm -f sdcard_part_fmt
+fi
+
+# export all variables in auto.mk
+echo "export" >> auto.mk
+