platform.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. REQUIRE_IMAGE_METADATA=1
  2. platform_check_image() {
  3. local diskdev partdev diff
  4. [ "$#" -gt 1 ] && return 1
  5. export_bootdevice && export_partdevice diskdev 0 || {
  6. echo "Unable to determine upgrade device"
  7. return 1
  8. }
  9. get_partitions "/dev/$diskdev" bootdisk
  10. #extract the boot sector from the image
  11. get_image_dd "$1" of=/tmp/image.bs count=1 bs=512b
  12. get_partitions /tmp/image.bs image
  13. #compare tables
  14. diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
  15. rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
  16. if [ -n "$diff" ]; then
  17. echo "Partition layout has changed. Full image will be written."
  18. ask_bool 0 "Abort" && exit 1
  19. return 0
  20. fi
  21. }
  22. platform_copy_config() {
  23. local partdev
  24. if export_partdevice partdev 1; then
  25. mount -o rw,noatime "/dev/$partdev" /mnt
  26. cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
  27. umount /mnt
  28. fi
  29. }
  30. platform_do_upgrade() {
  31. local diskdev partdev diff
  32. export_bootdevice && export_partdevice diskdev 0 || {
  33. echo "Unable to determine upgrade device"
  34. return 1
  35. }
  36. sync
  37. if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
  38. get_partitions "/dev/$diskdev" bootdisk
  39. #extract the boot sector from the image
  40. get_image_dd "$1" of=/tmp/image.bs count=1 bs=512b
  41. get_partitions /tmp/image.bs image
  42. #compare tables
  43. diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
  44. else
  45. diff=1
  46. fi
  47. if [ -n "$diff" ]; then
  48. get_image_dd "$1" of="/dev/$diskdev" bs=4096 conv=fsync
  49. # Separate removal and addtion is necessary; otherwise, partition 1
  50. # will be missing if it overlaps with the old partition 2
  51. partx -d - "/dev/$diskdev"
  52. partx -a - "/dev/$diskdev"
  53. return 0
  54. fi
  55. #write uboot image
  56. get_image_dd "$1" of="$diskdev" bs=512 skip=1 seek=1 count=4097 conv=fsync,notrunc
  57. #iterate over each partition from the image and write it to the boot disk
  58. while read part start size; do
  59. if export_partdevice partdev $part; then
  60. echo "Writing image to /dev/$partdev..."
  61. get_image_dd "$1" of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
  62. else
  63. echo "Unable to find partition $part device, skipped."
  64. fi
  65. done < /tmp/partmap.image
  66. #copy partition uuid
  67. echo "Writing new UUID to /dev/$diskdev..."
  68. get_image_dd "$1" of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
  69. }