1 | #!/bin/bash |
---|
2 | # |
---|
3 | # This is designed to be called from cron |
---|
4 | |
---|
5 | debug() { |
---|
6 | [ $DEBUG -eq 1 ] && echo "DEBUG: $*" |
---|
7 | } |
---|
8 | |
---|
9 | complain() { |
---|
10 | if [ $DEBUG -eq 1 ]; then |
---|
11 | echo "ERROR: $*" |
---|
12 | else |
---|
13 | logger -t "athena-auto-upgrade" -p user.notice "$*" |
---|
14 | fi |
---|
15 | } |
---|
16 | |
---|
17 | maybe_quit() { |
---|
18 | if [ $FORCE -eq 1 ]; then |
---|
19 | echo "Would normally quit here, but you passed -f, so we keep going..." |
---|
20 | else |
---|
21 | exit 0 |
---|
22 | fi |
---|
23 | } |
---|
24 | |
---|
25 | DEBUG=0 |
---|
26 | MAYBE="" |
---|
27 | FORCE=0 |
---|
28 | |
---|
29 | UPGRADE_TO_TESTING=no |
---|
30 | UPGRADE_ANYWAY=no |
---|
31 | [ -f /etc/default/debathena-auto-update ] && . /etc/default/debathena-auto-update |
---|
32 | |
---|
33 | while getopts "fdt" opt; do |
---|
34 | case "$opt" in |
---|
35 | d) |
---|
36 | DEBUG=1 |
---|
37 | MAYBE=echo |
---|
38 | UPGRADE_ANYWAY=yes |
---|
39 | ;; |
---|
40 | f) |
---|
41 | FORCE=1 |
---|
42 | ;; |
---|
43 | t) |
---|
44 | UPGRADE_TO_TESTING=yes |
---|
45 | ;; |
---|
46 | \?) |
---|
47 | echo "Usage: $0 [-d] [-t] [-f]" |
---|
48 | ;; |
---|
49 | esac |
---|
50 | done |
---|
51 | |
---|
52 | if [ $DEBUG -eq 1 ] && [ $FORCE -eq 1 ]; then |
---|
53 | echo "ERROR: -d and -f are mutually exclusive." |
---|
54 | exit 2 |
---|
55 | fi |
---|
56 | if [ $FORCE -eq 1 ]; then |
---|
57 | echo "Using -f is a terrible idea. Press Ctrl-C to reconsider and" |
---|
58 | echo "save your machine, or Enter to continue." |
---|
59 | read r |
---|
60 | fi |
---|
61 | |
---|
62 | if [ $DEBUG -ne 1 ]; then |
---|
63 | # Redirect output to a log file |
---|
64 | # Unless we're in debug mode |
---|
65 | exec >>/var/log/athena-upgrade 2>&1 |
---|
66 | fi |
---|
67 | |
---|
68 | echo "Starting upgrade at $(date)" |
---|
69 | |
---|
70 | # Not supported on Debian |
---|
71 | if [ "$(lsb_release -si)" != "Ubuntu" ]; then |
---|
72 | debug "Only supported on Ubuntu.". |
---|
73 | exit 0 |
---|
74 | fi |
---|
75 | |
---|
76 | # Skip other sanity checks in debug mode |
---|
77 | if [ $DEBUG -ne 1 ]; then |
---|
78 | # Only run this on cluster |
---|
79 | if [ "$(machtype -L)" != "debathena-cluster" ] && \ |
---|
80 | [ "$UPGRADE_ANYWAY" != "yes" ]; then |
---|
81 | debug "Not a cluster machine and UPGRADE_ANYWAY != yes.". |
---|
82 | exit 0 |
---|
83 | fi |
---|
84 | |
---|
85 | # Bail if someone is logged in (stolen from auto-update) |
---|
86 | ttys=$(w -h -s | awk '{print $2}') |
---|
87 | for tty in $ttys; do |
---|
88 | pids=$(ps --no-heading -j -t "$tty" 2>/dev/null \ |
---|
89 | | awk '($1 == $3) {print $1}') |
---|
90 | if [ -n "$pids" ]; then |
---|
91 | debug "Users logged in, won't continue." |
---|
92 | debug "PIDs: $pids" |
---|
93 | maybe_quit |
---|
94 | fi |
---|
95 | done |
---|
96 | # screen processes count as logins. |
---|
97 | if pgrep '^screen' > /dev/null; then |
---|
98 | debug "Screen processes found, won't continue." |
---|
99 | maybe_quit |
---|
100 | fi |
---|
101 | fi |
---|
102 | |
---|
103 | CLUSTERINFO=`getcluster -b $(lsb_release -sr)` |
---|
104 | [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1 |
---|
105 | eval $CLUSTERINFO |
---|
106 | if [ ! -z "$NEW_PRODUCTION_RELEASE" ]; then |
---|
107 | debug "Taking new production release: $NEW_PRODUCTION_RELEASE" |
---|
108 | NEWCLUSTER=`getcluster -b $NEW_PRODUCTION_RELEASE` |
---|
109 | [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1 |
---|
110 | eval $NEWCLUSTER |
---|
111 | elif [ ! -z "$NEW_TESTING_RELEASE" ] && \ |
---|
112 | [ "$UPGRADE_TO_TESTING" = "yes" ]; then |
---|
113 | debug "Taking new testing release: $NEW_TESTING_RELEASE" |
---|
114 | NEWCLUSTER=`getcluster -b $NEW_TESTING_RELEASE` |
---|
115 | [ $? != 0 ] && complain "Failed to get clusterinfo" && exit 1 |
---|
116 | eval $NEWCLUSTER |
---|
117 | else |
---|
118 | debug "No new releases." |
---|
119 | exit 0 |
---|
120 | fi |
---|
121 | if [ "$(lsb_release -sc)" = "$UBUNTU_RELEASE" ]; then |
---|
122 | complain "Tried to upgrade to already running release; shouldn't happen" |
---|
123 | exit 1 |
---|
124 | fi |
---|
125 | debug "OK, trying to upgrade to $UBUNTU_RELEASE" |
---|
126 | |
---|
127 | # That's a space and then a tab inside the brackets |
---|
128 | if egrep -q '^flags[ ].* lm( |$)' /proc/cpuinfo; then |
---|
129 | arch=amd64 |
---|
130 | else |
---|
131 | arch=i386 |
---|
132 | fi |
---|
133 | debug "Arch: $arch" |
---|
134 | IPADDR= |
---|
135 | NETMASK= |
---|
136 | eval `ifconfig eth0 |perl -ne'/^\s+inet addr:([\d\.]+).*? Mask:([\d\.]+)/ && print "IPADDR=$1 NETMASK=$2"'` |
---|
137 | if [ -z "$IPADDR" ]; then |
---|
138 | complain "Couldn't get IPADDR from ifconfig!" |
---|
139 | exit 0 |
---|
140 | fi |
---|
141 | GATEWAY=`route -n | awk '/^0\.0\.0\.0/ { print $2 }'` |
---|
142 | debug "Using IPADDR=$IPADDR" |
---|
143 | debug "Using NETMASK=$NETMASK" |
---|
144 | debug "Using GATEWAY=$GATEWAY" |
---|
145 | if [ -n "$NETMASK" ] && [ -n "$GATEWAY" ]; then |
---|
146 | knetinfo="netcfg/get_hostname=$(hostname) \ |
---|
147 | netcfg/disable_dhcp=true \ |
---|
148 | netcfg/get_domain=mit.edu \ |
---|
149 | netcfg/get_nameservers=\"18.72.0.3 18.70.0.160\"\ |
---|
150 | netcfg/get_ipaddress=$IPADDR \ |
---|
151 | netcfg/get_netmask=$NETMASK \ |
---|
152 | netcfg/get_gateway=$GATEWAY \ |
---|
153 | netcfg/confirm_static=true" |
---|
154 | else |
---|
155 | knetinfo="netcfg/dhcp_timeout=60 netcfg/get_hostname=$(hostname)" |
---|
156 | fi |
---|
157 | |
---|
158 | $MAYBE mkdir /h |
---|
159 | $MAYBE cd /h |
---|
160 | $MAYBE wget -N http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/initrd.gz |
---|
161 | $MAYBE wget -N http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/linux |
---|
162 | case "$UBUNTU_RELEASE" in |
---|
163 | natty) |
---|
164 | # Sigh |
---|
165 | kbdcode="keyboard-configuration/layoutcode=us" |
---|
166 | ;; |
---|
167 | *) |
---|
168 | kbdcode="console-setup/layoutcode=us" |
---|
169 | ;; |
---|
170 | esac |
---|
171 | # This is just the guts of the hackboot script: |
---|
172 | dkargs="DEBCONF_DEBUG=5" |
---|
173 | kargs="$knetinfo locale=en_US $kbdcode \ |
---|
174 | interface=auto \ |
---|
175 | url=http://18.9.60.73/installer/${UBUNTU_RELEASE}/debathena.preseed \ |
---|
176 | debathena/pxetype=cluster --" |
---|
177 | dead_chicken() { |
---|
178 | debug "Waving dead chicken..." |
---|
179 | debug "Scribbling over Plymouth" |
---|
180 | ln -nsf /bin/true /sbin/plymouthd |
---|
181 | $MAYBE chvt 1 |
---|
182 | sleep 2 |
---|
183 | # Run the video card's POST to reset it |
---|
184 | $MAYBE vbetool post |
---|
185 | sleep 2 |
---|
186 | # Disable the framebuffer, falling back to VGA |
---|
187 | if [ $DEBUG -eq 1 ]; then |
---|
188 | echo "Would echo 0 > /sys/class/vtconsole/vtcon1/bind" |
---|
189 | else |
---|
190 | echo 0 > /sys/class/vtconsole/vtcon1/bind |
---|
191 | fi |
---|
192 | } |
---|
193 | product_name="$(cat /sys/class/dmi/id/product_name | sed -e 's/[ ]*$//')" |
---|
194 | # Yes, really. The actual value of product_name on the Vostro is: |
---|
195 | # "Vostro 320 " |
---|
196 | case "$product_name" in |
---|
197 | # This is not so much Vostro 320-specific as it is i915 specific |
---|
198 | # Known to be necessary on PCI ID 8086:2e32, rev 03 |
---|
199 | # Actually, on all hardware, because Dell sucks. Or possibly Ubuntu. |
---|
200 | OptiPlex*) |
---|
201 | model="$(echo "$product_name" | cut -d\ -f2)" |
---|
202 | case "$model" in |
---|
203 | 790) |
---|
204 | debug "Adding 'reboot=pci' for Dell 790" |
---|
205 | kargs="$kargs reboot=pci" |
---|
206 | dead_chicken |
---|
207 | ;; |
---|
208 | GX620|745|755|760|780) |
---|
209 | dead_chicken |
---|
210 | ;; |
---|
211 | esac |
---|
212 | ;; |
---|
213 | Vostro\ 320) |
---|
214 | dead_chicken |
---|
215 | ;; |
---|
216 | *) |
---|
217 | echo "You appear to have functional hardware. Huzzah." |
---|
218 | ;; |
---|
219 | esac |
---|
220 | if [ $(echo $kargs | wc -c) -ge 512 ]; then |
---|
221 | complain "kargs exceeds 512 bytes. That's not good." |
---|
222 | fi |
---|
223 | # Don't kexec -e here, because modern Ubuntu is unable to kexec while |
---|
224 | # X is running. Instead, kexec -l and let the init script take care of. |
---|
225 | # Until Oneiric, when this will probably stop working if kexec-tools hasn't |
---|
226 | # been Upstartified. |
---|
227 | $MAYBE /sbin/kexec -l linux --append="$dkargs $kargs" --initrd=initrd.gz |
---|
228 | [ $DEBUG -eq 1 ] && exit 0 |
---|
229 | if [ "`cat /sys/kernel/kexec_loaded`" != "1" ]; then |
---|
230 | echo "Hrm, looks like kexec -l didn't work. This upgrade will fail." |
---|
231 | exit 1 |
---|
232 | fi |
---|
233 | echo "Kernel loaded. Rebooting in order to exec it." |
---|
234 | reboot |
---|
235 | exit 0 |
---|