aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-02-03 12:11:37 +0100
committerlonkaars <loek@pipeframe.xyz>2024-02-03 12:11:37 +0100
commitb4054ac324b5d9ddb10236db0563d424fa9c4017 (patch)
treee5d32c52c909d7071707af3ede2041917c7afff3
parent2b2cb626b527606436b6d7903407b7dbf66d9671 (diff)
fully automatic sd card partitioning and formatting
-rw-r--r--makefile25
-rwxr-xr-xstate/gen97
-rwxr-xr-xutil/part25
3 files changed, 99 insertions, 48 deletions
diff --git a/makefile b/makefile
index da94b65..a211676 100644
--- a/makefile
+++ b/makefile
@@ -18,7 +18,8 @@ CTRIZE += --env-file ./env
CTRIZE += $(CTR_IMG_TAG)
export
-# create files that represent otherwise PHONY targets
+# create dummy files to represent non-file targets (container image build
+# status, sd card partition status, git submodule initialization, etc.)
$(shell state/gen)
-include state/auto.mk
@@ -26,8 +27,12 @@ $(shell state/gen)
include ./env
export
+# shortcuts
.PHONY: all
all: $(BOOTLOADER_FILES)
+.PHONY: sd_format sd_partition
+sd_format: state/sdcard_fmt
+sd_partition: state/sdcard_part
state/container_img: Containerfile
$(CTR) build --tag $(CTR_IMG_TAG) .
@@ -38,6 +43,15 @@ state/submodules:
$(GIT) submodule update
touch $@
+state/sdcard_part:
+ $(AS_ROOT) util/part $(SDCARD_DISK)
+ touch $@
+
+state/sdcard_fmt: state/sdcard_part
+ $(AS_ROOT) mkfs.vfat -n BOOT -F 32 $(SDCARD_PART_BOOT)
+ $(AS_ROOT) mkfs.ext4 -L ROOTFS -F $(SDCARD_PART_ROOTFS)
+ touch $@
+
BOOTLOADER_FILES += bootloader/MLO
BOOTLOADER_FILES += bootloader/u-boot.img
BOOTLOADER_FILES += bootloader/u-boot.dtb
@@ -59,8 +73,9 @@ load_bootloader: $(BOOTLOADER_FILES)
$(AS_ROOT) $(CP) $(BOOTLOADER_FILES) mnt/boot
$(UMOUNT) mnt/boot
-.PHONY: sd_format
-sd_format:
- $(AS_ROOT) mkfs.vfat -n BOOT -F 32 $(SDCARD_PART_BOOT)
- $(AS_ROOT) mkfs.ext4 -L ROOTFS -F $(SDCARD_PART_ROOTFS)
+.PHONY: status
+status:
+ @echo '### STATUS REPORT'
+ @echo '# DETECTED SD CARD'
+ @cat state/auto.mk
diff --git a/state/gen b/state/gen
index 23a3382..6066685 100755
--- a/state/gen
+++ b/state/gen
@@ -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
diff --git a/util/part b/util/part
new file mode 100755
index 0000000..cf9abe5
--- /dev/null
+++ b/util/part
@@ -0,0 +1,25 @@
+#!/bin/sh
+SDCARD_DISK="$1"
+if ! [ -n "$SDCARD_DISK" ] ; then
+ cat << EOF >&2
+ERROR: could not automatically detect an SD card. Please insert an SD card of
+approx. 8GB, or manually set SDCARD_DISK in ./makefile
+EOF
+ exit 1
+fi
+if ! [ -w "$SDCARD_DISK" ] ; then
+ echo "ERROR: cannot not write to $SDCARD_DISK. Are you root?" >&2
+ exit 1
+fi
+if ! [ -b "$SDCARD_DISK" ] ; then
+ echo "ERROR: $SDCARD_DISK does not appear to be a block device" >&2
+ exit 1
+fi
+
+set -e
+
+sfdisk "$SDCARD_DISK" << 'EOF'
+label: dos
+,64M,0C,*
+,+ ,83,
+EOF