0088-x86-generalize-partition-discovery-for-sysupgrade.patch 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. From: Jo-Philipp Wich <jo@mein.io>
  2. Date: Tue, 24 May 2016 12:07:02 +0200
  3. Subject: x86: generalize partition discovery for sysupgrade
  4. Generalize the partition discovery in sysupgrade in order to fix sysupgrade
  5. and config backup/recovery on MMC block devices which use a different naming
  6. scheme compared to mtdblock or sd* devices.
  7. The change also adds the find applet to the ramdisk utilities so that upgrade
  8. code can rely on it.
  9. The commit is based on the initial submission by Russell Senior at
  10. http://patchwork.ozlabs.org/patch/625440/ .
  11. Signed-off-by: Russell Senior <russell@personaltelco.net>
  12. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
  13. Backport of LEDE 1012701014bd944197031a3c0023527861b521b4
  14. diff --git a/package/base-files/files/lib/upgrade/common.sh b/package/base-files/files/lib/upgrade/common.sh
  15. index dc865544f65c16399dcced55b8b09c671b31b5d1..14684959dd46d04b1c4b72213dbe06943ae64686 100644
  16. --- a/package/base-files/files/lib/upgrade/common.sh
  17. +++ b/package/base-files/files/lib/upgrade/common.sh
  18. @@ -53,7 +53,7 @@ run_ramfs() { # <command> [...]
  19. /bin/dd /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \
  20. /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc \
  21. /bin/cut /usr/bin/printf /bin/sync /bin/mkdir /bin/rmdir \
  22. - /bin/rm /usr/bin/basename /bin/kill /bin/chmod
  23. + /bin/rm /usr/bin/basename /bin/kill /bin/chmod /usr/bin/find
  24. install_bin /sbin/mtd
  25. install_bin /sbin/mount_root
  26. diff --git a/target/linux/x86/base-files/lib/preinit/79_move_config b/target/linux/x86/base-files/lib/preinit/79_move_config
  27. index 1d4873d78b480cb3f11e04e6246559417b431a8a..5ac81cb90d1c15782a9c4f271720cfa66d6d03a9 100644
  28. --- a/target/linux/x86/base-files/lib/preinit/79_move_config
  29. +++ b/target/linux/x86/base-files/lib/preinit/79_move_config
  30. @@ -2,10 +2,12 @@
  31. # Copyright (C) 2012-2015 OpenWrt.org
  32. move_config() {
  33. + local partdev
  34. +
  35. . /lib/upgrade/platform.sh
  36. - if platform_export_bootpart; then
  37. - mount -t ext4 -o rw,noatime "$BOOTPART" /mnt
  38. + if platform_export_bootdevice && platform_export_partdevice partdev 1; then
  39. + mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
  40. mv -f /mnt/sysupgrade.tgz /
  41. umount /mnt
  42. fi
  43. diff --git a/target/linux/x86/base-files/lib/upgrade/platform.sh b/target/linux/x86/base-files/lib/upgrade/platform.sh
  44. index 29eac77dfb0ae52f241696f3f62dce7d16106b20..c8bc3f7f608fc82ee3afc049b64af3a740fd2c37 100644
  45. --- a/target/linux/x86/base-files/lib/upgrade/platform.sh
  46. +++ b/target/linux/x86/base-files/lib/upgrade/platform.sh
  47. @@ -1,5 +1,21 @@
  48. -platform_export_bootpart() {
  49. - local cmdline uuid disk
  50. +platform_export_partdevice() {
  51. + local var="$1" offset="$2"
  52. + local uevent MAJOR MINOR DEVNAME DEVTYPE
  53. +
  54. + for uevent in /sys/class/block/*/uevent; do
  55. + . "$uevent"
  56. + if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
  57. + export "$var=$DEVNAME"
  58. + return 0
  59. + fi
  60. + done
  61. +
  62. + return 1
  63. +}
  64. +
  65. +platform_export_bootdevice() {
  66. + local cmdline uuid disk uevent
  67. + local MAJOR MINOR DEVNAME DEVTYPE
  68. if read cmdline < /proc/cmdline; then
  69. case "$cmdline" in
  70. @@ -17,20 +33,27 @@ platform_export_bootpart() {
  71. 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)
  72. uuid="${disk#PARTUUID=}"
  73. uuid="${uuid%-02}"
  74. - for disk in /dev/*; do
  75. - [ -b "$disk" ] || continue
  76. + for disk in $(find /dev -type b); do
  77. set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
  78. if [ "$4$3$2$1" = "$uuid" ]; then
  79. - export BOOTPART="${disk}1"
  80. - return 0
  81. + uevent="/sys/class/block/${disk##*/}/uevent"
  82. + break
  83. fi
  84. done
  85. ;;
  86. /dev/*)
  87. - export BOOTPART="${disk%[0-9]}1"
  88. - return 0
  89. + uevent="/sys/class/block/${disk##*/}/uevent"
  90. ;;
  91. esac
  92. +
  93. + if [ -e "$uevent" ]; then
  94. + . "$uevent"
  95. +
  96. + export BOOTDEV_MAJOR=$MAJOR
  97. + export BOOTDEV_MINOR=$MINOR
  98. +
  99. + return 0
  100. + fi
  101. fi
  102. return 1
  103. @@ -49,8 +72,10 @@ platform_check_image() {
  104. }
  105. platform_copy_config() {
  106. - if [ -b "$BOOTPART" ]; then
  107. - mount -t ext4 -o rw,noatime "$BOOTPART" /mnt
  108. + local partdev
  109. +
  110. + if platform_export_partdevice partdev 1; then
  111. + mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
  112. cp -af "$CONF_TAR" /mnt/
  113. umount /mnt
  114. fi
  115. @@ -87,18 +112,16 @@ get_partitions() { # <device> <filename>
  116. }
  117. platform_do_upgrade() {
  118. - platform_export_bootpart
  119. - disk="${BOOTPART%[0-9]}"
  120. + local diskdev partdev ibs diff
  121. - if [ -b "$disk" ]; then
  122. + if platform_export_bootdevice && platform_export_partdevice diskdev 0; then
  123. sync
  124. if [ "$SAVE_PARTITIONS" = "1" ]; then
  125. - get_partitions "$disk" bootdisk
  126. -
  127. + get_partitions "/dev/$diskdev" bootdisk
  128. #get block size
  129. - if [ -f "/sys/block/${disk##*/}/queue/physical_block_size" ]; then
  130. - ibs="$(cat "/sys/block/${disk##*/}/queue/physical_block_size")"
  131. + if [ -f "/sys/block/$diskdev/queue/physical_block_size" ]; then
  132. + ibs="$(cat "/sys/block/$diskdev/queue/physical_block_size")"
  133. else
  134. ibs=512
  135. fi
  136. @@ -114,21 +137,25 @@ platform_do_upgrade() {
  137. echo "Partition layout is changed. Full image will be written."
  138. ask_bool 0 "Abort" && exit
  139. - get_image "$@" | dd of="$disk" bs=4096 conv=fsync
  140. + get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
  141. return 0
  142. fi
  143. #iterate over each partition from the image and write it to the boot disk
  144. while read part start size; do
  145. - echo "Writing image to $disk$part..."
  146. - get_image "$@" | dd of="$disk$part" ibs="$ibs" obs=1M skip="$start" count="$size" conv=fsync
  147. + if platform_export_partdevice partdev $part; then
  148. + echo "Writing image to /dev/$partdev..."
  149. + get_image "$@" | dd of="/dev/$partdev" ibs="$ibs" obs=1M skip="$start" count="$size" conv=fsync
  150. + else
  151. + echo "Unable to find partition $part device, skipped."
  152. + fi
  153. done < /tmp/partmap.image
  154. #copy partition uuid
  155. - echo "Writing new UUID to $disk$part..."
  156. - get_image "$@" | dd of="$disk" bs=1 skip=440 count=4 seek=440 conv=fsync
  157. + echo "Writing new UUID to /dev/$diskdev..."
  158. + get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
  159. else
  160. - get_image "$@" | dd of="$disk" bs=4096 conv=fsync
  161. + get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
  162. fi
  163. sleep 1