#!/bin/bash # https://github.com/calmh/smartos-platform-upgrade # Copyright (c) 2012-2016 Jakob Borg & Contributors # Distributed under the MIT License # us-east.manta.joyent.com currently uses a wildcard certificate based on the # Thawte Primary Root CA. # SHA-1=91:C6:D6:EE:3E:8A:C8:63:84:E5:48:C2:99:29:5C:75:6C:81:7B:81 # SHA-256=8D:72:2F:81:A9:C1:13:C0:79:1D:F1:36:A2:96:6D:B2:6C:95:0A:97:1D:B4:6B:41:99:F4:EA:54:B7:8B:FB:9F cert_file=$(mktemp) function cleanup { rm "$cert_file" } trap cleanup EXIT cat >"$cert_file" < 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=($(diskinfo -cH | \ awk 'BEGIN { FS="\t" } $7~/\?\?R./ { print $2 }')) echo -n " detected ${removables[@]}" if [[ ${#removables[@]} -eq 0 ]]; then echo echo "Error: Unable to detect removable device." diskinfo echo "Specify correct device on the command line." exit -1 elif [[ ${#removables[@]} -gt 1 ]]; then echo echo "Error: more than one removable device detected." diskinfo -cH | awk 'BEGIN { FS="\t" } $7~/\?\?R./ { print }' echo "Specify correct device on the command line." exit -1 fi # Look for a GPT/EFI VTOC; if there isn't one, then this is almost # certainly an MBR-partitioned device. If it's a GPT label, then we # want the slice that's of type 2 (ROOT). if [[ -e "/dev/dsk/${removables[0]}" ]]; then partition=$(/usr/sbin/prtvtoc -h "/dev/dsk/${removables[0]}" | \ awk ' $2 == 2 { print $1 }') if [[ $? -eq 0 && -n "$partition" ]]; then echo -n ", GPT label" usb="/dev/dsk/${removables[0]}s${partition}" fi fi if [[ -z "$usb" ]]; then echo -n ", MBR label" usb="/dev/dsk/${removables[0]}p1" fi 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 -rltD "$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"