x86: fix platform_export_bootpart() for Xen virtual disks
[openwrt/openwrt.git] / target / linux / x86 / base-files / lib / upgrade / platform.sh
1 platform_export_bootpart() {
2 local cmdline uuid disk
3
4 if read cmdline < /proc/cmdline; then
5 case "$cmdline" in
6 *block2mtd=*)
7 disk="${cmdline##*block2mtd=}"
8 disk="${disk%%,*}"
9 ;;
10 *root=*)
11 disk="${cmdline##*root=}"
12 disk="${disk%% *}"
13 ;;
14 esac
15
16 case "$disk" in
17 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)
18 uuid="${disk#PARTUUID=}"
19 uuid="${uuid%-02}"
20 for disk in /dev/[hsv]d[a-z] /dev/xvd[a-z]; do
21 set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
22 if [ "$4$3$2$1" = "$uuid" ]; then
23 export BOOTPART="${disk}1"
24 return 0
25 fi
26 done
27 ;;
28 /dev/*)
29 export BOOTPART="${disk%[0-9]}1"
30 return 0
31 ;;
32 esac
33 fi
34
35 return 1
36 }
37
38 platform_check_image() {
39 [ "$#" -gt 1 ] && return 1
40
41 case "$(get_magic_word "$1")" in
42 eb48|eb63) return 0;;
43 *)
44 echo "Invalid image type"
45 return 1
46 ;;
47 esac
48 }
49
50 platform_copy_config() {
51 if [ -b "$BOOTPART" ]; then
52 mount -t ext4 -o rw,noatime "$BOOTPART" /mnt
53 cp -af "$CONF_TAR" /mnt/
54 umount /mnt
55 fi
56 }
57
58 get_partitions() { # <device> <filename>
59 local disk="$1"
60 local filename="$2"
61
62 if [ -b "$disk" -o -f "$disk" ]; then
63 echo "Reading partition table from $filename..."
64 partx -r "$disk" -gbo NR,START,SECTORS > "/tmp/partx.$filename"
65 fi
66 }
67
68 platform_do_upgrade() {
69 platform_export_bootpart
70 disk="${BOOTPART%[0-9]}"
71
72 if [ -b "$disk" ]; then
73 sync
74 if [ "$SAVE_PARTITIONS" = "1" ]; then
75 get_partitions "$disk" bootdisk
76
77
78 #get block size
79 sectors="$(partx -r $disk -gbo SECTORS --nr 1:1)"
80 size="$(partx -r $disk -gbo SIZE --nr 1:1)"
81 ibs="$(($size / $sectors))"
82
83 #extract the boot sector from the image
84 get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
85
86 get_partitions /tmp/image.bs image
87
88 #compare tables
89 diff="$(grep -F -x -v -f /tmp/partx.bootdisk /tmp/partx.image)"
90 if [ -n "$diff" ]; then
91 echo "Partition layout is changed. Full image will be written."
92 ask_bool 0 "Abort" && exit
93
94 get_image "$@" | dd of="$disk" bs=4096 conv=fsync
95 return 0
96 fi
97
98 #iterate over each partition from the image and write it to the boot disk
99 while read part start size; do
100 echo "Writing image to $disk$part..."
101 get_image "$@" | dd of="$disk$part" ibs="$ibs" obs=1M skip="$start" count="$size" conv=fsync
102 done < /tmp/partx.image
103
104 #copy partition uuid
105 echo "Writing new UUID to $disk$part..."
106 get_image "$@" | dd of="$disk" bs=1 skip=440 count=4 seek=440 conv=fsync
107 else
108 get_image "$@" | dd of="$disk" bs=4096 conv=fsync
109 fi
110
111 sleep 1
112 fi
113 }