#!/bin/bash host=https://us-east.manta.joyent.com latest_path=$(curl -sk "$host/Joyent_Dev/public/SmartOS/latest") version="${latest_path##*/}" platform_file="platform-$version.tgz" platform_dir="platform-$version" platform_url="$host$latest_path/$platform_file" md5sums_url="$host$latest_path/md5sums.txt" force="false" while getopts :f option; do case "$option" in f) force="true" ;; \?) echo "Invalid option: -$OPTARG" >&2 exit -1 ;; esac done shift $((OPTIND-1)) IFS=_ read brand kernel < <(uname -v) if [[ $kernel == $version ]]; then echo "Already on latest version ($kernel)." $force || exit -1 fi tmp=$(mktemp -d) cd "$tmp" || exit -1 echo -n "Downloading latest platform ($platform_file)..." if ! curl -sk -o "$platform_file" "$platform_url" ; then echo " failed" exit -1 else echo " OK" fi echo -n "Verifying checksum..." curl -sk "$md5sums_url" \ | grep "$platform_file" \ | awk '{print $1}' > expected.md5 openssl md5 "$platform_file" | awk '{print $2}' > actual.md5 if ! cmp -s actual.md5 expected.md5 ; then echo " failed" exit -1 else echo " OK" fi echo -n "Extracting latest platform..." if ! gtar zxf "$platform_file" ; then echo " failed" exit -1 else echo " OK" fi echo -n "Marking release version..." if ! echo $version > $platform_dir/VERSION ; then echo " failed" exit -1 else echo " OK" fi echo -n "Checking current boot device..." if [[ -z $1 ]] ; then removables=($(disklist -r)) echo -n " detected ${removables[@]}" if [[ ${#removables[@]} -gt 1 ]]; then echo echo "Error: more than one removable device detected." echo "Specify correct device on the command line." exit -1 fi usb="/dev/dsk/${removables[0]}p1" else usb="$1" echo -n " using $usb" fi umount "$usb" 2>/dev/null mkdir usb if ! mount -F pcfs -o foldcase "$usb" "$tmp/usb" ; then echo ", mount failed" exit -1 else echo -n ", mounted" fi if [[ ! -d usb/platform ]] ; then echo ", missing platform dir" exit -1 else echo ", OK" fi echo -n "Updating platform on boot device..." if ! rsync -a "$platform_dir/" usb/platform.new/ ; then echo " failed" exit -1 else echo " OK" fi echo -n "Remounting boot device..." umount "$usb" 2>/dev/null if ! mount -F pcfs -o foldcase "$usb" "$tmp/usb" ; then echo " failed" exit -1 else echo " OK" fi echo -n "Verifying kernel checksum on boot device..." openssl dgst -sha1 "$platform_dir"/i86pc/kernel/amd64/unix | cut -d ' ' -f 2 > kernel.expected openssl dgst -sha1 usb/platform.new/i86pc/kernel/amd64/unix | cut -d ' ' -f 2 > kernel.actual if ! cmp -s kernel.actual kernel.expected ; then echo " failed" exit -1 else echo " OK" fi echo -n "Verifying boot_archive checksum on boot device..." openssl dgst -sha1 usb/platform.new/i86pc/amd64/boot_archive | cut -d ' ' -f 2 > boot_archive.actual if ! cmp -s boot_archive.actual usb/platform.new/i86pc/amd64/boot_archive.hash ; then echo " failed" exit -1 else echo " OK" fi echo -n "Activating new platform on $usb..." rm -rf usb/old mkdir usb/old if ! ( mv usb/platform usb/old && mv usb/platform.new usb/platform ) ; then echo " failed" exit -1 else echo " OK" fi echo echo "Boot device upgraded. To do:" echo echo " 1) Sanity check the contents of $tmp/usb" echo " 2) umount $usb" echo " 3) reboot"