platform-upgrade 3.6 KB

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