base-files: improve lib/upgrade/common.sh
authorKlaus Kudielka <klaus.kudielka@gmail.com>
Wed, 8 May 2019 18:40:50 +0000 (20:40 +0200)
committerChristian Lamparter <chunkeey@gmail.com>
Sat, 11 May 2019 14:37:11 +0000 (16:37 +0200)
Recently, upgrade device autodetection has been added to the mvebu target.
This exposes some shortcomings of the generic export_bootdevice function,
e.g. on the Turris Omnia: export_bootdevice silently reports the root
partition to be the boot device. This makes the sysupgrade process fail at
several places.

Fix this by clearly distinguishing between /proc/cmdline arguments which
specify the boot disk, and those which specify the root partition. Only in
the latter case, strip off the partition, and do it consistently.
root=PARTUUID=<pseudo PARTUUID for MBR> (any partition) and root=/dev/*
(any partition) are accepted.

The root of the problem is that the *existing* export_bootdevice in
/lib/upgrade/common.sh behaves differently, if the kernel is booted with
root=/dev/..., or if it is booted with root=PARTUUID=...

In the former case, it reports back major/minor of the root partition,
in the latter case it reports back major/minor of the complete boot disk.

Targets, which boot with root=/dev/... *and* use export_bootdevice /
export_partdevice, have added workarounds to this behaviour, by specifying
*negative* increments to the export_partdevice function.

Consequently, those targets have to be adapted to use positive increments,
otherwise they are broken by the change to export_bootdevice.

Fixes: 4e8345ff68 ("mvebu: base-files: autodetect upgrade device")
Signed-off-by: Klaus Kudielka <klaus.kudielka@gmail.com>
Tested-by: Tomasz Maciej Nowak <tomek_n@o2.pl>
package/base-files/files/lib/upgrade/common.sh
target/linux/apm821xx/base-files/lib/upgrade/wdbook.sh
target/linux/brcm2708/base-files/lib/upgrade/platform.sh
target/linux/omap/base-files/lib/preinit/79_move_config
target/linux/omap/base-files/lib/upgrade/platform.sh
target/linux/sunxi/base-files/lib/preinit/79_move_config
target/linux/sunxi/base-files/lib/upgrade/platform.sh

index b3a29fb32e31901c9d657d9751a8502553cf554a..bbedeefd262e8f2a25c7fe6e0570fffc64b33099 100644 (file)
@@ -101,35 +101,41 @@ get_magic_long() {
 }
 
 export_bootdevice() {
-       local cmdline uuid disk uevent line
+       local cmdline bootdisk rootpart uuid blockdev uevent line
        local MAJOR MINOR DEVNAME DEVTYPE
 
        if read cmdline < /proc/cmdline; then
                case "$cmdline" in
                        *block2mtd=*)
-                               disk="${cmdline##*block2mtd=}"
-                               disk="${disk%%,*}"
+                               bootdisk="${cmdline##*block2mtd=}"
+                               bootdisk="${bootdisk%%,*}"
                        ;;
                        *root=*)
-                               disk="${cmdline##*root=}"
-                               disk="${disk%% *}"
+                               rootpart="${cmdline##*root=}"
+                               rootpart="${rootpart%% *}"
                        ;;
                esac
 
-               case "$disk" in
-                       PARTUUID=[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]-02)
-                               uuid="${disk#PARTUUID=}"
-                               uuid="${uuid%-02}"
-                               for disk in $(find /dev -type b); do
-                                       set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
+               case "$bootdisk" in
+                       /dev/*)
+                               uevent="/sys/class/block/${bootdisk##*/}/uevent"
+                       ;;
+               esac
+
+               case "$rootpart" in
+                       PARTUUID=[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]-[a-f0-9][a-f0-9])
+                               uuid="${rootpart#PARTUUID=}"
+                               uuid="${uuid%-[a-f0-9][a-f0-9]}"
+                               for blockdev in $(find /dev -type b); do
+                                       set -- $(dd if=$blockdev bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
                                        if [ "$4$3$2$1" = "$uuid" ]; then
-                                               uevent="/sys/class/block/${disk##*/}/uevent"
+                                               uevent="/sys/class/block/${blockdev##*/}/uevent"
                                                break
                                        fi
                                done
                        ;;
                        /dev/*)
-                               uevent="/sys/class/block/${disk##*/}/uevent"
+                               uevent="/sys/class/block/${rootpart##*/}/../uevent"
                        ;;
                esac
 
index 2287e0619df38789963254e4ff965112ae936bf8..c1912711497d6df31bc1b05b016ec895bc12c335 100644 (file)
@@ -7,7 +7,7 @@ mbl_do_platform_check() {
 
        [ "$#" -gt 1 ] && return 1
 
-       export_bootdevice && export_partdevice diskdev -2 || {
+       export_bootdevice && export_partdevice diskdev 0 || {
                echo "Unable to determine upgrade device"
                return 1
        }
@@ -36,7 +36,7 @@ mbl_do_platform_check() {
 mbl_do_upgrade() {
        local diskdev partdev diff
 
-       export_bootdevice && export_partdevice diskdev -2 || {
+       export_bootdevice && export_partdevice diskdev 0 || {
                echo "Unable to determine upgrade device"
                return 1
        }
@@ -70,10 +70,6 @@ mbl_do_upgrade() {
 
        #iterate over each partition from the image and write it to the boot disk
        while read part start size; do
-               # root is /dev/sd[a|b]2 and not /dev/sd[a|b] this causes some problem
-               # one of which is this offset, I'm not sure what's the best fix, so
-               # here's a WA.
-               let part=$((part - 2))
                if export_partdevice partdev $part; then
                        echo "Writing image to /dev/$partdev..."
                        get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
@@ -90,8 +86,7 @@ mbl_do_upgrade() {
 mbl_copy_config() {
        local partdev
 
-       # Same as above /dev/sd[a|b]2 is root, so /boot is -1
-       if export_partdevice partdev -1; then
+       if export_partdevice partdev 1; then
                mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
                cp -af "$CONF_TAR" /mnt/
                umount /mnt
index 62eede53d32c8778cd5b5ff0a18bc7ac204dec71..37e479272bf8e10bb139ff2de87e96c015b22896 100644 (file)
@@ -9,7 +9,7 @@ platform_check_image() {
 
        [ "$#" -gt 1 ] && return 1
 
-       export_bootdevice && export_partdevice diskdev -2 || {
+       export_bootdevice && export_partdevice diskdev 0 || {
                echo "Unable to determine upgrade device"
                return 1
        }
@@ -38,7 +38,7 @@ platform_check_image() {
 platform_do_upgrade() {
        local diskdev partdev diff
 
-       export_bootdevice && export_partdevice diskdev -2 || {
+       export_bootdevice && export_partdevice diskdev 0 || {
                echo "Unable to determine upgrade device"
                return 1
        }
@@ -72,10 +72,6 @@ platform_do_upgrade() {
 
        #iterate over each partition from the image and write it to the boot disk
        while read part start size; do
-               # root is /dev/sd[a|b]2 and not /dev/sd[a|b] this causes some problem
-               # one of which is this offset, I'm not sure what's the best fix, so
-               # here's a WA.
-               let part=$((part - 2))
                if export_partdevice partdev $part; then
                        echo "Writing image to /dev/$partdev..."
                        get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
@@ -92,8 +88,7 @@ platform_do_upgrade() {
 platform_copy_config() {
        local partdev
 
-       # Same as above /dev/sd[a|b]2 is root, so /boot is -1
-       if export_partdevice partdev -1; then
+       if export_partdevice partdev 1; then
                mkdir -p /boot
                [ -f /boot/kernel.img ] || mount -t vfat -o rw,noatime "/dev/$partdev" /boot
                cp -af "$CONF_TAR" /boot/
index c112588689b15797e24dcbb741022489bf2de627..83171b3ba9dcc7edcf54139f9428b7ca880b3bd2 100644 (file)
@@ -6,7 +6,7 @@ move_config() {
 
        . /lib/upgrade/common.sh
 
-       if export_bootdevice && export_partdevice partdev -1; then
+       if export_bootdevice && export_partdevice partdev 1; then
                if mount -t vfat -o rw,noatime "/dev/$partdev" /mnt; then
                        if [ -f /mnt/sysupgrade.tgz ]; then
                                mv -f /mnt/sysupgrade.tgz /
index 88ef4790e9c1452f8ce57fe1c265ce47810830ee..abe910b154c7de928687abde7707ee58da8d59d5 100644 (file)
@@ -1,7 +1,7 @@
 platform_check_image() {
        local diskdev partdev diff
 
-       export_bootdevice && export_partdevice diskdev -2 || {
+       export_bootdevice && export_partdevice diskdev 0 || {
                echo "Unable to determine upgrade device"
                return 1
        }
@@ -28,7 +28,7 @@ platform_check_image() {
 platform_copy_config() {
        local partdev
 
-       if export_partdevice partdev -1; then
+       if export_partdevice partdev 1; then
                mount -t vfat -o rw,noatime "/dev/$partdev" /mnt
                cp -af "$CONF_TAR" /mnt/
                umount /mnt
@@ -38,7 +38,7 @@ platform_copy_config() {
 platform_do_upgrade() {
        local diskdev partdev diff
 
-       export_bootdevice && export_partdevice diskdev -2 || {
+       export_bootdevice && export_partdevice diskdev 0 || {
                echo "Unable to determine upgrade device"
                return 1
        }
@@ -74,7 +74,6 @@ platform_do_upgrade() {
        get_image "$@" | dd of="$diskdev" bs=1024 skip=8 seek=8 count=1016 conv=fsync
        #iterate over each partition from the image and write it to the boot disk
        while read part start size; do
-               part="$(($part - 2))"
                if export_partdevice partdev $part; then
                        echo "Writing image to /dev/$partdev..."
                        get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
index c112588689b15797e24dcbb741022489bf2de627..83171b3ba9dcc7edcf54139f9428b7ca880b3bd2 100644 (file)
@@ -6,7 +6,7 @@ move_config() {
 
        . /lib/upgrade/common.sh
 
-       if export_bootdevice && export_partdevice partdev -1; then
+       if export_bootdevice && export_partdevice partdev 1; then
                if mount -t vfat -o rw,noatime "/dev/$partdev" /mnt; then
                        if [ -f /mnt/sysupgrade.tgz ]; then
                                mv -f /mnt/sysupgrade.tgz /
index 88ef4790e9c1452f8ce57fe1c265ce47810830ee..abe910b154c7de928687abde7707ee58da8d59d5 100644 (file)
@@ -1,7 +1,7 @@
 platform_check_image() {
        local diskdev partdev diff
 
-       export_bootdevice && export_partdevice diskdev -2 || {
+       export_bootdevice && export_partdevice diskdev 0 || {
                echo "Unable to determine upgrade device"
                return 1
        }
@@ -28,7 +28,7 @@ platform_check_image() {
 platform_copy_config() {
        local partdev
 
-       if export_partdevice partdev -1; then
+       if export_partdevice partdev 1; then
                mount -t vfat -o rw,noatime "/dev/$partdev" /mnt
                cp -af "$CONF_TAR" /mnt/
                umount /mnt
@@ -38,7 +38,7 @@ platform_copy_config() {
 platform_do_upgrade() {
        local diskdev partdev diff
 
-       export_bootdevice && export_partdevice diskdev -2 || {
+       export_bootdevice && export_partdevice diskdev 0 || {
                echo "Unable to determine upgrade device"
                return 1
        }
@@ -74,7 +74,6 @@ platform_do_upgrade() {
        get_image "$@" | dd of="$diskdev" bs=1024 skip=8 seek=8 count=1016 conv=fsync
        #iterate over each partition from the image and write it to the boot disk
        while read part start size; do
-               part="$(($part - 2))"
                if export_partdevice partdev $part; then
                        echo "Writing image to /dev/$partdev..."
                        get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync