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