x86: sysupgrade: move partition table change check to platform_check_image
[oweals/openwrt.git] / target / linux / x86 / base-files / lib / upgrade / platform.sh
1 platform_check_image() {
2         local diskdev partdev diff
3         [ "$#" -gt 1 ] && return 1
4
5         case "$(get_magic_word "$1")" in
6                 eb48|eb63) ;;
7                 *)
8                         echo "Invalid image type"
9                         return 1
10                 ;;
11         esac
12
13         export_bootdevice && export_partdevice diskdev 0 || {
14                 echo "Unable to determine upgrade device"
15                 return 1
16         }
17
18         get_partitions "/dev/$diskdev" bootdisk
19
20         #extract the boot sector from the image
21         get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b 2>/dev/null
22
23         get_partitions /tmp/image.bs image
24
25         #compare tables
26         diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
27
28         rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
29
30         if [ -n "$diff" ]; then
31                 echo "Partition layout has changed. Full image will be written."
32                 ask_bool 0 "Abort" && exit 1
33                 return 0
34         fi
35 }
36
37 platform_copy_config() {
38         local partdev
39
40         if export_partdevice partdev 1; then
41                 mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
42                 cp -af "$CONF_TAR" /mnt/
43                 umount /mnt
44         fi
45 }
46
47 platform_do_upgrade() {
48         local diskdev partdev diff
49
50         if export_bootdevice && export_partdevice diskdev 0; then
51                 sync
52                 if [ "$SAVE_PARTITIONS" = "1" ]; then
53                         get_partitions "/dev/$diskdev" bootdisk
54
55                         #extract the boot sector from the image
56                         get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
57
58                         get_partitions /tmp/image.bs image
59
60                         #compare tables
61                         diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
62                         if [ -n "$diff" ]; then
63                                 get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
64                                 return 0
65                         fi
66
67                         #iterate over each partition from the image and write it to the boot disk
68                         while read part start size; do
69                                 if export_partdevice partdev $part; then
70                                         echo "Writing image to /dev/$partdev..."
71                                         get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
72                                 else
73                                         echo "Unable to find partition $part device, skipped."
74                                 fi
75                         done < /tmp/partmap.image
76
77                         #copy partition uuid
78                         echo "Writing new UUID to /dev/$diskdev..."
79                         get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
80                 else
81                         get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
82                 fi
83
84                 sleep 1
85         fi
86 }