From e8c735f74a153f7dd570bb34357b68e0b8608b05 Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Fri, 9 Jul 2021 01:25:56 +0100 Subject: [PATCH] autopart: work on MBR/DOS partitioned disks Using GPT/UUID parition table is not always a possible choice. Add support for MBR/DOS partitioned disks to make autopart work on legacy targets like mt7623. Signed-off-by: Daniel Golle --- utils/uvol/files/autopart.defaults | 46 ++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/utils/uvol/files/autopart.defaults b/utils/uvol/files/autopart.defaults index 0df2829c6..870cd4415 100644 --- a/utils/uvol/files/autopart.defaults +++ b/utils/uvol/files/autopart.defaults @@ -6,27 +6,52 @@ OWRT_VOLUMES=owrt-volumes +load_partitions() { + local dev="$1" + json_init + json_load "$(sfdisk -J "$dev" 2>/dev/null)" + json_select "partitiontable" || return 1 + return 0 +} get_partition_by_name_gpt() { - local dev="$1" - local part parts node name - json_load "$(sfdisk -J "/dev/$dev" 2>/dev/null)" - json_select "partitiontable" || return + local label part parts node name + json_get_vars label + [ "$label" = "gpt" ] || return json_select "partitions" || return json_get_keys parts for part in $parts; do json_select "$part" json_get_vars node name - if [ "$2" = "$name" ]; then + if [ "$1" = "$name" ]; then echo "$node" break fi json_select .. done + json_select .. +} + +get_partition_by_type_mbr() { + local label part parts node type + json_get_vars label + [ "$label" = "dos" ] || return + json_select "partitions" || return + json_get_keys parts + for part in $parts; do + json_select "$part" + json_get_vars node type + if [ "$1" = "$type" ]; then + echo "$node" + break + fi + json_select .. + done + json_select .. } part_fixup() { - echo "write" | sfdisk --force -q -w never "$1" + echo "write" | sfdisk --force -q -w never "$1" 1>/dev/null 2>/dev/null } get_free_area() { @@ -60,7 +85,7 @@ create_lvm_part() { freepart="$(get_free_area "$disk")" if [ "$freepart" ]; then - echo "$freepart, type=lvm, name=$OWRT_VOLUMES" | sfdisk --force -w never -a "$disk" + echo "$freepart, type=lvm, name=$OWRT_VOLUMES" | sfdisk --force -w never -a "$disk" || return 1 partx -a "$disk" 1>/dev/null 2>/dev/null || true return 0 else @@ -87,11 +112,14 @@ autopart_init() { [ -e "/sys/class/block/$diskdev/device/cid" ] && diskserial="$diskserial$(cat "/sys/class/block/$diskdev/device/cid")" [ "$diskserial" ] || diskserial="$(cat /proc/sys/kernel/random/uuid)" diskhash="$(echo "$diskserial" | sha256sum | cut -d' ' -f1)" + part_fixup "/dev/$diskdev" create_lvm_part "/dev/$diskdev" || return - lvmpart="$(get_partition_by_name_gpt "$diskdev" "$OWRT_VOLUMES")" - + load_partitions "/dev/$diskdev" || return + lvmpart="$(get_partition_by_name_gpt "$OWRT_VOLUMES")" + [ "$lvmpart" ] || lvmpart="$(get_partition_by_type_mbr "8e")" [ "$lvmpart" ] || return + lvm_init "$lvmpart" "${OWRT_VOLUMES}-${diskhash:0:16}" }