platform.sh 2.2 KB

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