1 platform_export_bootpart() {
2 local cmdline uuid disk
4 if read cmdline < /proc/cmdline; then
7 disk="${cmdline##*block2mtd=}"
11 disk="${cmdline##*root=}"
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=}"
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"
29 export BOOTPART="${disk%[0-9]}1"
38 platform_check_image() {
39 [ "$#" -gt 1 ] && return 1
41 case "$(get_magic_word "$1")" in
44 echo "Invalid image type"
50 platform_copy_config() {
51 if [ -b "$BOOTPART" ]; then
52 mount -t ext4 -o rw,noatime "$BOOTPART" /mnt
53 cp -af "$CONF_TAR" /mnt/
58 get_partitions() { # <device> <filename>
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"
68 platform_do_upgrade() {
69 platform_export_bootpart
70 disk="${BOOTPART%[0-9]}"
72 if [ -b "$disk" ]; then
74 if [ "$SAVE_PARTITIONS" = "1" ]; then
75 get_partitions "$disk" bootdisk
79 sectors="$(partx -r $disk -gbo SECTORS --nr 1:1)"
80 size="$(partx -r $disk -gbo SIZE --nr 1:1)"
81 ibs="$(($size / $sectors))"
83 #extract the boot sector from the image
84 get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
86 get_partitions /tmp/image.bs image
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
94 get_image "$@" | dd of="$disk" bs=4096 conv=fsync
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
105 echo "Writing new UUID to $disk$part..."
106 get_image "$@" | dd of="$disk" bs=1 skip=440 count=4 seek=440 conv=fsync
108 get_image "$@" | dd of="$disk" bs=4096 conv=fsync