autopart: work on MBR/DOS partitioned disks
authorDaniel Golle <daniel@makrotopia.org>
Fri, 9 Jul 2021 00:25:56 +0000 (01:25 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Sat, 10 Jul 2021 21:04:43 +0000 (22:04 +0100)
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 <daniel@makrotopia.org>
utils/uvol/files/autopart.defaults

index 0df2829c679328a78abd4f7fbf0765cd6fc77e1a..870cd441569e1dcc9f3b585dc30185dc4218fbc9 100644 (file)
@@ -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}"
 }