platform.sh 2.8 KB

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