[24797] | 1 | #!/bin/bash |
---|
| 2 | # |
---|
| 3 | # This is designed to be called from cron |
---|
| 4 | |
---|
| 5 | DEBUG=0 |
---|
[25326] | 6 | FORCE=0 |
---|
[25516] | 7 | # You can set DEBUG_RELEASE in the environment for testing |
---|
[25440] | 8 | DEBUG_RELEASE="" |
---|
| 9 | VERBOSE=0 |
---|
| 10 | FAKE=0 |
---|
[25326] | 11 | UPGRADE_TO_TESTING=no |
---|
| 12 | UPGRADE_ANYWAY=no |
---|
| 13 | |
---|
[25487] | 14 | # Read defaults here so as not to clobber command-line args later |
---|
| 15 | [ -f /etc/default/debathena-auto-update ] && . /etc/default/debathena-auto-update |
---|
[25440] | 16 | |
---|
| 17 | while getopts "vfdt" opt; do |
---|
[25326] | 18 | case "$opt" in |
---|
| 19 | d) |
---|
| 20 | DEBUG=1 |
---|
[25440] | 21 | FAKE=1 |
---|
[25488] | 22 | UPGRADE_ANYWAY=yes |
---|
[25326] | 23 | ;; |
---|
| 24 | f) |
---|
| 25 | FORCE=1 |
---|
[25516] | 26 | UPGRADE_ANYWAY=yes |
---|
[25488] | 27 | ;; |
---|
[25326] | 28 | t) |
---|
[25488] | 29 | UPGRADE_TO_TESTING=yes |
---|
| 30 | ;; |
---|
[25440] | 31 | v) # Spam level |
---|
| 32 | VERBOSE=1 |
---|
| 33 | ;; |
---|
[25326] | 34 | \?) |
---|
[25440] | 35 | cat<<END |
---|
| 36 | Usage: $0 [OPTION] |
---|
| 37 | |
---|
| 38 | -d debug mode, bypasses all checks, only prints |
---|
| 39 | -t upgrade to testing |
---|
| 40 | -f bypasses login checks |
---|
| 41 | -v echo the cmds that are run |
---|
| 42 | |
---|
| 43 | END |
---|
| 44 | |
---|
[25488] | 45 | ;; |
---|
[25326] | 46 | esac |
---|
| 47 | done |
---|
| 48 | |
---|
[25440] | 49 | |
---|
[25326] | 50 | if [ $DEBUG -eq 1 ] && [ $FORCE -eq 1 ]; then |
---|
| 51 | echo "ERROR: -d and -f are mutually exclusive." |
---|
| 52 | exit 2 |
---|
| 53 | fi |
---|
| 54 | if [ $FORCE -eq 1 ]; then |
---|
[25488] | 55 | echo "Using -f is a terrible idea. Press Ctrl-C to reconsider and" |
---|
[25326] | 56 | echo "save your machine, or Enter to continue." |
---|
| 57 | read r |
---|
| 58 | fi |
---|
| 59 | |
---|
[25440] | 60 | function pie_grub() { |
---|
| 61 | # change DEFAULT in /etc/grub to SAVED |
---|
| 62 | if [[ $FAKE == 1 ]]; then |
---|
| 63 | echo perl -i.bak -pe 's/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/' /etc/default/grub |
---|
| 64 | elif [[ $VERBOSE == 0 ]]; then |
---|
| 65 | perl -i.bak -pe 's/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/' /etc/default/grub >/dev/null 2>&1 |
---|
| 66 | else |
---|
| 67 | echo perl -i.bak -pe 's/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/' /etc/default/grub |
---|
| 68 | perl -i.bak -pe 's/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/' /etc/default/grub |
---|
| 69 | fi |
---|
| 70 | if [[ $? -ne 0 ]]; then |
---|
| 71 | complain "Setting DEFAULT in /etc/default/grub returned a non-zero exit status" |
---|
| 72 | exit 1 |
---|
| 73 | fi |
---|
| 74 | |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | function complain() { |
---|
[25663] | 79 | echo "ERROR: $*" |
---|
| 80 | if [ $DEBUG -ne 1 ]; then |
---|
[25488] | 81 | logger -t "athena-auto-upgrade" -p user.notice "$*" |
---|
[25440] | 82 | fi |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | function runcmd() |
---|
| 88 | { |
---|
| 89 | CMD=$1 |
---|
| 90 | if [[ $FAKE == 1 ]]; then |
---|
| 91 | echo $CMD |
---|
| 92 | elif [[ $VERBOSE == 0 ]]; then |
---|
| 93 | $CMD > /dev/null 2>&1 |
---|
| 94 | else |
---|
| 95 | echo $CMD |
---|
| 96 | $CMD |
---|
| 97 | fi |
---|
| 98 | if [[ $? -ne 0 ]]; then |
---|
| 99 | complain "$CMD returned a non-zero exit status" |
---|
| 100 | exit 1 |
---|
| 101 | fi |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | function debug() { |
---|
| 105 | [ $DEBUG -eq 1 ] && echo "DEBUG: $*" |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | function maybe_quit() { |
---|
| 109 | if [ $FORCE -eq 1 ]; then |
---|
| 110 | echo "Would normally quit here, but you passed -f, so we keep going..." |
---|
| 111 | else |
---|
[25663] | 112 | echo "Ending upgrade at $(date)" |
---|
[25440] | 113 | exit 0 |
---|
| 114 | fi |
---|
| 115 | } |
---|
| 116 | |
---|
[25326] | 117 | if [ $DEBUG -ne 1 ]; then |
---|
[25231] | 118 | # Redirect output to a log file |
---|
| 119 | # Unless we're in debug mode |
---|
| 120 | exec >>/var/log/athena-upgrade 2>&1 |
---|
[24797] | 121 | fi |
---|
| 122 | |
---|
[25663] | 123 | echo |
---|
[25231] | 124 | echo "Starting upgrade at $(date)" |
---|
| 125 | |
---|
[24797] | 126 | # Not supported on Debian |
---|
[24806] | 127 | if [ "$(lsb_release -si)" != "Ubuntu" ]; then |
---|
[24811] | 128 | debug "Only supported on Ubuntu.". |
---|
[24797] | 129 | exit 0 |
---|
| 130 | fi |
---|
| 131 | # Skip other sanity checks in debug mode |
---|
| 132 | if [ $DEBUG -ne 1 ]; then |
---|
| 133 | # Only run this on cluster |
---|
[24806] | 134 | if [ "$(machtype -L)" != "debathena-cluster" ] && \ |
---|
[24805] | 135 | [ "$UPGRADE_ANYWAY" != "yes" ]; then |
---|
[25488] | 136 | debug "Not a cluster machine and UPGRADE_ANYWAY != yes.". |
---|
| 137 | exit 0 |
---|
[24797] | 138 | fi |
---|
| 139 | |
---|
| 140 | # Bail if someone is logged in (stolen from auto-update) |
---|
| 141 | ttys=$(w -h -s | awk '{print $2}') |
---|
| 142 | for tty in $ttys; do |
---|
[25488] | 143 | pids=$(ps --no-heading -j -t "$tty" 2>/dev/null \ |
---|
[24797] | 144 | | awk '($1 == $3) {print $1}') |
---|
[25488] | 145 | if [ -n "$pids" ]; then |
---|
| 146 | debug "Users logged in, won't continue." |
---|
| 147 | debug "PIDs: $pids" |
---|
| 148 | maybe_quit |
---|
| 149 | fi |
---|
[24797] | 150 | done |
---|
[25440] | 151 | |
---|
[24797] | 152 | # screen processes count as logins. |
---|
| 153 | if pgrep '^screen' > /dev/null; then |
---|
[25488] | 154 | debug "Screen processes found, won't continue." |
---|
| 155 | maybe_quit |
---|
[24797] | 156 | fi |
---|
| 157 | fi |
---|
[25440] | 158 | export CLUSTERINFO=$(getcluster -b $(lsb_release -sr)) |
---|
[24797] | 159 | [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1 |
---|
| 160 | eval $CLUSTERINFO |
---|
[25955] | 161 | REINSTALL=no |
---|
[25959] | 162 | UBUNTU_RELEASE= |
---|
[24797] | 163 | if [ ! -z "$NEW_PRODUCTION_RELEASE" ]; then |
---|
| 164 | debug "Taking new production release: $NEW_PRODUCTION_RELEASE" |
---|
| 165 | NEWCLUSTER=`getcluster -b $NEW_PRODUCTION_RELEASE` |
---|
| 166 | [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1 |
---|
[25959] | 167 | # This will set UBUNTU_RELEASE |
---|
[24797] | 168 | eval $NEWCLUSTER |
---|
[24812] | 169 | elif [ ! -z "$NEW_TESTING_RELEASE" ] && \ |
---|
| 170 | [ "$UPGRADE_TO_TESTING" = "yes" ]; then |
---|
[24797] | 171 | debug "Taking new testing release: $NEW_TESTING_RELEASE" |
---|
| 172 | NEWCLUSTER=`getcluster -b $NEW_TESTING_RELEASE` |
---|
| 173 | [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1 |
---|
[25959] | 174 | # This will set UBUNTU_RELEASE |
---|
[24797] | 175 | eval $NEWCLUSTER |
---|
[25955] | 176 | elif [ ! -z "$REINSTALL_AT" ] && echo $REINSTALL_AT | grep -qx '[0-9]*'; then |
---|
| 177 | debug "Found REINSTALL_AT = $REINSTALL_AT ..." |
---|
| 178 | installtime=$(stat -c "%Y" /var/log/athena-install.log 2>/dev/null) |
---|
| 179 | debug "Machine was last installed at $installtime" |
---|
| 180 | if [ -z "$installtime" ]; then |
---|
| 181 | echo "W: Could not stat /var/log/athena-install.log, forcing reinstall." |
---|
| 182 | installtime=0 |
---|
| 183 | fi |
---|
| 184 | if [ $installtime -lt $REINSTALL_AT ]; then |
---|
| 185 | debug "$installtime less than $REINSTALL_AT, so reinstalling" |
---|
| 186 | REINSTALL=yes |
---|
| 187 | UBUNTU_RELEASE="$(lsb_release -sc)" |
---|
| 188 | fi |
---|
[25959] | 189 | fi |
---|
| 190 | if [ "$(lsb_release -sc)" = "$UBUNTU_RELEASE" ] && \ |
---|
| 191 | [ "$REINSTALL" = "no" ]; then |
---|
| 192 | complain "Tried to upgrade to already running release; shouldn't happen" |
---|
| 193 | exit 1 |
---|
| 194 | fi |
---|
| 195 | if [ -z "$UBUNTU_RELEASE" ]; then |
---|
| 196 | if [ -n "$DEBUG_RELEASE" ]; then |
---|
[25440] | 197 | UBUNTU_RELEASE=$DEBUG_RELEASE |
---|
| 198 | else |
---|
[25663] | 199 | echo "No new releases found." |
---|
[25440] | 200 | exit 0 |
---|
| 201 | fi |
---|
[24797] | 202 | fi |
---|
[25663] | 203 | echo "Found $UBUNTU_RELEASE, starting upgrade" |
---|
[24797] | 204 | |
---|
| 205 | # That's a space and then a tab inside the brackets |
---|
| 206 | if egrep -q '^flags[ ].* lm( |$)' /proc/cpuinfo; then |
---|
| 207 | arch=amd64 |
---|
| 208 | else |
---|
| 209 | arch=i386 |
---|
| 210 | fi |
---|
| 211 | debug "Arch: $arch" |
---|
[25231] | 212 | IPADDR= |
---|
| 213 | NETMASK= |
---|
| 214 | eval `ifconfig eth0 |perl -ne'/^\s+inet addr:([\d\.]+).*? Mask:([\d\.]+)/ && print "IPADDR=$1 NETMASK=$2"'` |
---|
[24797] | 215 | if [ -z "$IPADDR" ]; then |
---|
[25231] | 216 | complain "Couldn't get IPADDR from ifconfig!" |
---|
[24797] | 217 | exit 0 |
---|
| 218 | fi |
---|
[25231] | 219 | GATEWAY=`route -n | awk '/^0\.0\.0\.0/ { print $2 }'` |
---|
[24797] | 220 | debug "Using IPADDR=$IPADDR" |
---|
[25231] | 221 | debug "Using NETMASK=$NETMASK" |
---|
| 222 | debug "Using GATEWAY=$GATEWAY" |
---|
[25516] | 223 | |
---|
[25231] | 224 | if [ -n "$NETMASK" ] && [ -n "$GATEWAY" ]; then |
---|
| 225 | knetinfo="netcfg/get_hostname=$(hostname) \ |
---|
[25881] | 226 | netcfg/disable_autoconfig=true \ |
---|
[25231] | 227 | netcfg/get_domain=mit.edu \ |
---|
[25721] | 228 | netcfg/get_nameservers=\"18.72.0.3 18.71.0.151 18.70.0.160\"\ |
---|
[25231] | 229 | netcfg/get_ipaddress=$IPADDR \ |
---|
| 230 | netcfg/get_netmask=$NETMASK \ |
---|
| 231 | netcfg/get_gateway=$GATEWAY \ |
---|
| 232 | netcfg/confirm_static=true" |
---|
| 233 | else |
---|
| 234 | knetinfo="netcfg/dhcp_timeout=60 netcfg/get_hostname=$(hostname)" |
---|
| 235 | fi |
---|
| 236 | |
---|
[25524] | 237 | runcmd "rm -rf /auto-upgrade" |
---|
[25440] | 238 | runcmd "mkdir -p /auto-upgrade" |
---|
| 239 | runcmd "wget http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/initrd.gz -O /auto-upgrade/initrd.gz" |
---|
| 240 | runcmd "wget http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/linux -O /auto-upgrade/linux" |
---|
| 241 | |
---|
[24797] | 242 | # This is just the guts of the hackboot script: |
---|
| 243 | dkargs="DEBCONF_DEBUG=5" |
---|
[25881] | 244 | kargs="$knetinfo locale=en_US \ |
---|
| 245 | keyboard-configuration/layoutcode=us \ |
---|
[25440] | 246 | quiet \ |
---|
| 247 | panic=5 \ |
---|
[25347] | 248 | interface=auto \ |
---|
[24797] | 249 | url=http://18.9.60.73/installer/${UBUNTU_RELEASE}/debathena.preseed \ |
---|
[25489] | 250 | da/pxe=cluster --" |
---|
[25440] | 251 | |
---|
| 252 | debug "USING kargs=$kargs" |
---|
| 253 | |
---|
[25231] | 254 | if [ $(echo $kargs | wc -c) -ge 512 ]; then |
---|
[25440] | 255 | complain "kargs exceeds 512 bytes. That's not good." |
---|
| 256 | exit 1 |
---|
[25231] | 257 | fi |
---|
[25440] | 258 | |
---|
| 259 | tmpfile="/tmp/$(basename $0).$RANDOM.tmp" |
---|
| 260 | |
---|
| 261 | cat >"$tmpfile"<<END |
---|
| 262 | #!/bin/sh |
---|
| 263 | |
---|
| 264 | echo "Adding auto-upgrade /auto-upgrade/linux" >&2 |
---|
| 265 | cat<<EOF |
---|
| 266 | menuentry "auto-upgrade" { |
---|
[25516] | 267 | insmod ext2 |
---|
| 268 | insmod part_msdos |
---|
| 269 | insmod lvm |
---|
| 270 | search --no-floppy --fs-uuid --set=root \${GRUB_DEVICE_UUID} |
---|
[25440] | 271 | echo "Loading auto-upgrade kernel for ${UBUNTU_RELEASE}" |
---|
| 272 | linux /auto-upgrade/linux $kargs |
---|
| 273 | echo "Loading auto-upgrade ramdisk for ${UBUNTU_RELEASE}" |
---|
| 274 | initrd /auto-upgrade/initrd.gz |
---|
| 275 | } |
---|
| 276 | EOF |
---|
| 277 | |
---|
| 278 | END |
---|
| 279 | |
---|
| 280 | |
---|
[25516] | 281 | debug "$(cat $tmpfile)" |
---|
[25440] | 282 | runcmd "cp $tmpfile /etc/grub.d/49_auto-upgrade" |
---|
| 283 | rm -f $tmpfile |
---|
| 284 | runcmd "chmod 755 /etc/grub.d/49_auto-upgrade" |
---|
| 285 | |
---|
| 286 | pie_grub |
---|
| 287 | |
---|
| 288 | runcmd "/usr/sbin/grub-set-default 0" |
---|
| 289 | runcmd "/usr/sbin/grub-reboot auto-upgrade" |
---|
| 290 | runcmd "/usr/sbin/update-grub" |
---|
| 291 | |
---|
| 292 | # rebooting into the installer kernel |
---|
| 293 | runcmd "/sbin/reboot" |
---|