#!/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