platform-upgrade 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/bash
  2. # https://github.com/calmh/smartos-platform-upgrade
  3. # Copyright (c) 2012-2016 Jakob Borg & Contributors
  4. # Distributed under the MIT License
  5. host=https://us-east.manta.joyent.com
  6. latest_path=$(curl -sk "$host/Joyent_Dev/public/SmartOS/latest")
  7. version="${latest_path##*/}"
  8. platform_file="platform-$version.tgz"
  9. platform_dir="platform-$version"
  10. platform_url="$host$latest_path/$platform_file"
  11. md5sums_url="$host$latest_path/md5sums.txt"
  12. force="false"
  13. while getopts :f option; do
  14. case "$option" in
  15. f)
  16. force="true"
  17. ;;
  18. \?)
  19. echo "Invalid option: -$OPTARG" >&2
  20. exit -1
  21. ;;
  22. esac
  23. done
  24. shift $((OPTIND-1))
  25. IFS=_ read brand kernel < <(uname -v)
  26. if [[ $kernel == $version ]]; then
  27. echo "Already on latest version ($kernel)."
  28. $force || exit -1
  29. fi
  30. tmp=$(mktemp -d)
  31. cd "$tmp" || exit -1
  32. echo -n "Downloading latest platform ($platform_file)..."
  33. if ! curl -sk -o "$platform_file" "$platform_url" ; then
  34. echo " failed"
  35. exit -1
  36. else
  37. echo " OK"
  38. fi
  39. echo -n "Verifying checksum..."
  40. curl -sk "$md5sums_url" \
  41. | grep "$platform_file" \
  42. | awk '{print $1}' > expected.md5
  43. openssl md5 "$platform_file" | awk '{print $2}' > actual.md5
  44. if ! cmp -s actual.md5 expected.md5 ; then
  45. echo " failed"
  46. exit -1
  47. else
  48. echo " OK"
  49. fi
  50. echo -n "Extracting latest platform..."
  51. if ! gtar zxf "$platform_file" ; then
  52. echo " failed"
  53. exit -1
  54. else
  55. echo " OK"
  56. fi
  57. echo -n "Marking release version..."
  58. if ! echo $version > $platform_dir/VERSION ; then
  59. echo " failed"
  60. exit -1
  61. else
  62. echo " OK"
  63. fi
  64. echo -n "Checking current boot device..."
  65. if [[ -z $1 ]] ; then
  66. removables=($(disklist -r))
  67. echo -n " detected ${removables[@]}"
  68. if [[ ${#removables[@]} -gt 1 ]]; then
  69. echo
  70. echo "Error: more than one removable device detected."
  71. echo "Specify correct device on the command line."
  72. exit -1
  73. fi
  74. usb="/dev/dsk/${removables[0]}p1"
  75. else
  76. usb="$1"
  77. echo -n " using $usb"
  78. fi
  79. umount "$usb" 2>/dev/null
  80. mkdir usb
  81. if ! mount -F pcfs -o foldcase "$usb" "$tmp/usb" ; then
  82. echo ", mount failed"
  83. exit -1
  84. else
  85. echo -n ", mounted"
  86. fi
  87. if [[ ! -d usb/platform ]] ; then
  88. echo ", missing platform dir"
  89. exit -1
  90. else
  91. echo ", OK"
  92. fi
  93. echo -n "Updating platform on boot device..."
  94. if ! rsync -a "$platform_dir/" usb/platform.new/ ; then
  95. echo " failed"
  96. exit -1
  97. else
  98. echo " OK"
  99. fi
  100. echo -n "Remounting boot device..."
  101. umount "$usb" 2>/dev/null
  102. if ! mount -F pcfs -o foldcase "$usb" "$tmp/usb" ; then
  103. echo " failed"
  104. exit -1
  105. else
  106. echo " OK"
  107. fi
  108. echo -n "Verifying kernel checksum on boot device..."
  109. openssl dgst -sha1 "$platform_dir"/i86pc/kernel/amd64/unix | cut -d ' ' -f 2 > kernel.expected
  110. openssl dgst -sha1 usb/platform.new/i86pc/kernel/amd64/unix | cut -d ' ' -f 2 > kernel.actual
  111. if ! cmp -s kernel.actual kernel.expected ; then
  112. echo " failed"
  113. exit -1
  114. else
  115. echo " OK"
  116. fi
  117. echo -n "Verifying boot_archive checksum on boot device..."
  118. openssl dgst -sha1 usb/platform.new/i86pc/amd64/boot_archive | cut -d ' ' -f 2 > boot_archive.actual
  119. if ! cmp -s boot_archive.actual usb/platform.new/i86pc/amd64/boot_archive.hash ; then
  120. echo " failed"
  121. exit -1
  122. else
  123. echo " OK"
  124. fi
  125. echo -n "Activating new platform on $usb..."
  126. rm -rf usb/old
  127. mkdir usb/old
  128. if ! ( mv usb/platform usb/old && mv usb/platform.new usb/platform ) ; then
  129. echo " failed"
  130. exit -1
  131. else
  132. echo " OK"
  133. fi
  134. echo
  135. echo "Boot device upgraded. To do:"
  136. echo
  137. echo " 1) Sanity check the contents of $tmp/usb"
  138. echo " 2) umount $usb"
  139. echo " 3) reboot"