platform-upgrade 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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=($(diskinfo | awk '/^USB/ { print $2 }'))
  67. echo -n " detected ${removables[@]}"
  68. if [[ ${#removables[@]} -gt 1 ]]; then
  69. echo
  70. echo "Error: more than one removable device detected."
  71. diskinfo | awk 'NR == 1 || /^USB/ { print }'
  72. echo "Specify correct device on the command line."
  73. exit -1
  74. fi
  75. usb="/dev/dsk/${removables[0]}p1"
  76. else
  77. usb="$1"
  78. echo -n " using $usb"
  79. fi
  80. umount "$usb" 2>/dev/null
  81. mkdir usb
  82. if ! mount -F pcfs -o foldcase "$usb" "$tmp/usb" ; then
  83. echo ", mount failed"
  84. exit -1
  85. else
  86. echo -n ", mounted"
  87. fi
  88. if [[ ! -d usb/platform ]] ; then
  89. echo ", missing platform dir"
  90. exit -1
  91. else
  92. echo ", OK"
  93. fi
  94. echo -n "Updating platform on boot device..."
  95. if ! rsync -a "$platform_dir/" usb/platform.new/ ; then
  96. echo " failed"
  97. exit -1
  98. else
  99. echo " OK"
  100. fi
  101. echo -n "Remounting boot device..."
  102. umount "$usb" 2>/dev/null
  103. if ! mount -F pcfs -o foldcase "$usb" "$tmp/usb" ; then
  104. echo " failed"
  105. exit -1
  106. else
  107. echo " OK"
  108. fi
  109. echo -n "Verifying kernel checksum on boot device..."
  110. openssl dgst -sha1 "$platform_dir"/i86pc/kernel/amd64/unix | cut -d ' ' -f 2 > kernel.expected
  111. openssl dgst -sha1 usb/platform.new/i86pc/kernel/amd64/unix | cut -d ' ' -f 2 > kernel.actual
  112. if ! cmp -s kernel.actual kernel.expected ; then
  113. echo " failed"
  114. exit -1
  115. else
  116. echo " OK"
  117. fi
  118. echo -n "Verifying boot_archive checksum on boot device..."
  119. openssl dgst -sha1 usb/platform.new/i86pc/amd64/boot_archive | cut -d ' ' -f 2 > boot_archive.actual
  120. if ! cmp -s boot_archive.actual usb/platform.new/i86pc/amd64/boot_archive.hash ; then
  121. echo " failed"
  122. exit -1
  123. else
  124. echo " OK"
  125. fi
  126. echo -n "Activating new platform on $usb..."
  127. rm -rf usb/old
  128. mkdir usb/old
  129. if ! ( mv usb/platform usb/old && mv usb/platform.new usb/platform ) ; then
  130. echo " failed"
  131. exit -1
  132. else
  133. echo " OK"
  134. fi
  135. echo
  136. echo "Boot device upgraded. To do:"
  137. echo
  138. echo " 1) Sanity check the contents of $tmp/usb"
  139. echo " 2) umount $usb"
  140. echo " 3) reboot"