aboutsummaryrefslogtreecommitdiff
path: root/state/gen
blob: 60666859e1fce79af6ad5c37d4c8254d07e88130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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() {
	PREV_EC="$?"
	if [ "$PREV_EC" -eq 0 ] ; then
		! [ -e "$1" ] && touch "$1"
	else
		rm -f "$1"
	fi
	return "$PREV_EC"
}

# 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 sdcard_part sdcard_fmt
# if no explicit SDCARD_DISK is set, guess which device is the sd card by
# checking if it is removable AND has a size of approx. 8GB
if [ -z "$SDCARD_DISK" ] ; then
	SDCARD_DISK="$(lsblk --noheadings --bytes --output PATH,SIZE,TYPE,RM | awk '
		$3 != "disk" { next }
		$2 < 7900000000 { next }
		$2 > 8000000000 { next }
		$4 != 1 { next }
		{ print $1 }' | head -n1)"
fi
if [ -n "$SDCARD_DISK" ] ; then
	# check if the sd card is partitioned correctly if no partition path
	# variables are set
	if [ -z "$SDCARD_PART_BOOT" ] || [ -z "$SDCARD_PART_ROOTFS" ] ; then
		PARTS="$(lsblk "$SDCARD_DISK" --bytes --noheadings \
			--output PATH,SIZE,TYPE,PTTYPE,FSTYPE,LABEL,PARTFLAGS)"

		# check if the disk is partitioned correctly
		echo "$PARTS" | awk '
			$3 == "part" { parts += 1 }
			NR == 1 { if ($4 != "dos") exit(1) }
			NR == 2 { if ($2 != 64 * 2^20) exit(1) }
			END { if (parts != 2) exit(1) }' && touch sdcard_part

		if [ $? -eq 0 ] ; then
			SDCARD_PART_BOOT="$(echo "$PARTS" | awk 'NR == 2 { print $1 }')"
			SDCARD_PART_ROOTFS="$(echo "$PARTS" | awk 'NR == 3 { print $1 }')"

			# check if the disk is formatted correctly
			echo "$PARTS" | awk '
				NR == 2 {
					if ($5 != "vfat") exit(1)
					if ($6 != "BOOT") exit(1)
					if ($7 != "0x80") exit(1)
				}
				NR == 3 {
					if ($5 != "ext4") exit(1)
					if ($6 != "ROOTFS") exit(1)
				}' && touch sdcard_fmt
		fi
	fi
fi

cat << EOF > auto.mk
SDCARD_DISK ?= $SDCARD_DISK
SDCARD_PART_BOOT ?= $SDCARD_PART_BOOT
SDCARD_PART_ROOTFS ?= $SDCARD_PART_ROOTFS
EOF