platform.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. v "Extract 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. umount /tmp/boot
  53. fi
  54. }
  55. platform_do_upgrade() {
  56. local diskdev partdev diff
  57. export_bootdevice && export_partdevice diskdev 0 || {
  58. v "Unable to determine upgrade device"
  59. return 1
  60. }
  61. sync
  62. if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
  63. get_partitions "/dev/$diskdev" bootdisk
  64. v "Extract boot sector from the image"
  65. get_image_dd "$1" of=/tmp/image.bs count=63 bs=512b
  66. get_partitions /tmp/image.bs image
  67. #compare tables
  68. diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
  69. else
  70. diff=1
  71. fi
  72. if [ -n "$diff" ]; then
  73. get_image_dd "$1" of="/dev/$diskdev" bs=4096 conv=fsync
  74. # Separate removal and addtion is necessary; otherwise, partition 1
  75. # will be missing if it overlaps with the old partition 2
  76. partx -d - "/dev/$diskdev"
  77. partx -a - "/dev/$diskdev"
  78. return 0
  79. fi
  80. #iterate over each partition from the image and write it to the boot disk
  81. while read part start size; do
  82. if export_partdevice partdev $part; then
  83. v "Writing image to /dev/$partdev..."
  84. get_image_dd "$1" of="/dev/$partdev" ibs=512 obs=1M skip="$start" count="$size" conv=fsync
  85. else
  86. v "Unable to find partition $part device, skipped."
  87. fi
  88. done < /tmp/partmap.image
  89. v "Writing new UUID to /dev/$diskdev..."
  90. get_image_dd "$1" of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
  91. platform_do_bootloader_upgrade "$diskdev"
  92. local parttype=ext4
  93. part_magic_efi "/dev/$diskdev" || return 0
  94. if export_partdevice partdev 1; then
  95. part_magic_fat "/dev/$partdev" && parttype=vfat
  96. mount -t $parttype -o rw,noatime "/dev/$partdev" /mnt
  97. 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"')
  98. sed -i "s/\(PARTUUID=\)[a-f0-9-]\+/\1$4$3$2$1-$6$5-$8$7-$9/ig" /mnt/boot/grub/grub.cfg
  99. umount /mnt
  100. fi
  101. }