diff options
author | lonkaars <loek@pipeframe.xyz> | 2024-02-03 12:11:37 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2024-02-03 12:11:37 +0100 |
commit | b4054ac324b5d9ddb10236db0563d424fa9c4017 (patch) | |
tree | e5d32c52c909d7071707af3ede2041917c7afff3 /state/gen | |
parent | 2b2cb626b527606436b6d7903407b7dbf66d9671 (diff) |
fully automatic sd card partitioning and formatting
Diffstat (limited to 'state/gen')
-rwxr-xr-x | state/gen | 97 |
1 files changed, 54 insertions, 43 deletions
@@ -4,9 +4,17 @@ 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 +[ "${MAKEFLAGS#*n}" != "$MAKEFLAGS" ] && exit -touch_rm_exit_code() { [ $? -eq 0 ] && touch "$1" || rm -f "$1" ; } +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 @@ -16,50 +24,53 @@ touch_rm_exit_code container_img test -z "$(git submodule status | grep '^-')" touch_rm_exit_code submodules -rm -f auto.mk +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)" -# 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 + # 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 [ -n "$SDCARD_DISK" ]; then - echo "SDCARD_DISK ?= $SDCARD_DISK" >> auto.mk -fi + if [ $? -eq 0 ] ; then + SDCARD_PART_BOOT="$(echo "$PARTS" | awk 'NR == 2 { print $1 }')" + SDCARD_PART_ROOTFS="$(echo "$PARTS" | awk 'NR == 3 { print $1 }')" -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 + # 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 -# export all variables in auto.mk -echo "export" >> auto.mk +cat << EOF > auto.mk +SDCARD_DISK ?= $SDCARD_DISK +SDCARD_PART_BOOT ?= $SDCARD_PART_BOOT +SDCARD_PART_ROOTFS ?= $SDCARD_PART_ROOTFS +EOF |