aboutsummaryrefslogtreecommitdiff
path: root/state/gen
blob: ddaf9d169a22a20587f872c44d194f67857f3b44 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/sh
# create files in the same directory as this script
cd "$(dirname "$0")"

set_state () {
	filename="$1"
	condition="$2"
	if [ "$condition" -eq 0 ] ; then
		# echo "$filename = TRUE" >&2
		if ! [ -e "$filename" ] ; then
			touch "$filename"
		fi
	else
		# echo "$filename = FALSE" >&2
		rm -f "$filename"
	fi
}

# container image (initialized here, updated in makefile)
$CTR image exists "$CTR_IMG_TAG" 1> /dev/null 2> /dev/null
set_state container_img $?

# check if all the submodules are initialized
SUBMODULES_OK=0
MODULES="$(git config \
	--file "$(git rev-parse --show-toplevel)/.gitmodules" \
	--name-only --get-regexp path |\
	sed -e 's/^submodule\./modules\//' -e 's/\.path$/\/HEAD/')"
for git_module in $MODULES ; do
	git rev-parse "$git_module" 1> /dev/null 2> /dev/null && continue
	echo "$git_module is the culprit" >&2
	SUBMODULES_OK=1
done
set_state submodules $SUBMODULES_OK

SDCARD_PART_OK=1
SDCARD_FMT_OK=1
# 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 --list \
		--output PATH,SIZE,TYPE,RM,NAME | awk '
		$3 != "disk" { next }
		$2 < 7900000000 { next }
		$2 > 8000000000 { next }
		($4 != 1) && (match($5, /^mmcblk/) == 0) { 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 \
			2> /dev/null)"

		# 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) }' && SDCARD_PART_OK=0

		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)
				}' && SDCARD_FMT_OK=0
		fi
	fi
fi
set_state sdcard_part $SDCARD_PART_OK
set_state sdcard_fmt $SDCARD_FMT_OK

cat << EOF
\$(eval SDCARD_DISK := $SDCARD_DISK)
\$(eval SDCARD_PART_BOOT := $SDCARD_PART_BOOT)
\$(eval SDCARD_PART_ROOTFS := $SDCARD_PART_ROOTFS)
\$(eval export)
EOF