platform.sh 2.2 KB

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