platform.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. v "Invalid image type"
  9. return 1
  10. ;;
  11. esac
  12. export_bootdevice && export_partdevice diskdev 0 || {
  13. v "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 "$1" of=/tmp/image.bs count=63 bs=512b
  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. v "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 parttype=ext4
  31. if export_partdevice partdev 1; then
  32. part_magic_fat "/dev/$partdev" && parttype=vfat
  33. mount -t $parttype -o rw,noatime "/dev/$partdev" /mnt
  34. cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
  35. umount /mnt
  36. fi
  37. }
  38. platform_do_bootloader_upgrade() {
  39. local bootpart parttable=msdos
  40. local diskdev="$1"
  41. if export_partdevice bootpart 1; then
  42. mkdir -p /tmp/boot
  43. mount -o rw,noatime "/dev/$bootpart" /tmp/boot
  44. echo "(hd0) /dev/$diskdev" > /tmp/device.map
  45. part_magic_efi "/dev/$diskdev" && parttable=gpt
  46. v "Upgrading bootloader on /dev/$diskdev..."
  47. grub-bios-setup \
  48. -m "/tmp/device.map" \
  49. -d "/tmp/boot/boot/grub" \
  50. -r "hd0,${parttable}1" \
  51. "/dev/$diskdev" \
  52. && touch /tmp/boot/boot/grub/upgraded
  53. umount /tmp/boot
  54. fi
  55. }
  56. platform_do_upgrade() {
  57. local diskdev partdev diff
  58. export_bootdevice && export_partdevice diskdev 0 || {
  59. v "Unable to determine upgrade device"
  60. return 1
  61. }
  62. sync
  63. if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
  64. get_partitions "/dev/$diskdev" bootdisk
  65. #extract the boot sector from the image
  66. get_image_dd "$1" of=/tmp/image.bs count=63 bs=512b
  67. get_partitions /tmp/image.bs image
  68. #compare tables
  69. diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
  70. else
  71. diff=1
  72. fi
  73. if [ -n "$diff" ]; then
  74. get_image_dd "$1" of="/dev/$diskdev" bs=4096 conv=fsync
  75. # Separate removal and addtion is necessary; otherwise, partition 1
  76. # will be missing if it overlaps with the old partition 2
  77. partx -d - "/dev/$diskdev"
  78. partx -a - "/dev/$diskdev"
  79. return 0
  80. fi
  81. #iterate over each partition from the image and write it to the boot disk
  82. while read part start size; do
  83. if export_partdevice partdev $part; then
  84. v "Writing image to /dev/$partdev..."
  85. get_image_dd "$1" of="/dev/$partdev" ibs=512 obs=1M skip="$start" count="$size" conv=fsync
  86. else
  87. v "Unable to find partition $part device, skipped."
  88. fi
  89. done < /tmp/partmap.image
  90. #copy partition uuid
  91. v "Writing new UUID to /dev/$diskdev..."
  92. get_image_dd "$1" of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
  93. platform_do_bootloader_upgrade "$diskdev"
  94. local parttype=ext4
  95. part_magic_efi "/dev/$diskdev" || return 0
  96. if export_partdevice partdev 1; then
  97. part_magic_fat "/dev/$partdev" && parttype=vfat
  98. mount -t $parttype -o rw,noatime "/dev/$partdev" /mnt
  99. set -- $(dd if="/dev/$diskdev" bs=1 skip=1168 count=16 2>/dev/null | hexdump -v -e '8/1 "%02x "" "2/1 "%02x""-"6/1 "%02x"')
  100. sed -i "s/\(PARTUUID=\)[a-f0-9-]\+/\1$4$3$2$1-$6$5-$8$7-$9/ig" /mnt/boot/grub/grub.cfg
  101. umount /mnt
  102. fi
  103. }