Project

General

Profile

Download (15.8 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
#!/bin/sh
2
3 84aa381e Scott Ullrich
# /etc/rc.firmware
4 79b9570c Scott Ullrich
# originally part of m0n0wall (http://neon1.net/m0n0wall)
5 498be2fc Scott Ullrich
# Copyright (C) 2005-2009 Scott Ullrich <sullrich@pfsense.org>.
6 84aa381e Scott Ullrich
# Copyright (C) 2003 Manuel Kasper <mk@neon1.net>.
7
# All rights reserved.
8 5b237745 Scott Ullrich
9 3e0621ca Scott Ullrich
# mount /cf
10
/etc/rc.conf_mount_rw
11
12 f22c94e0 Scott Ullrich
# Reset file(s)
13
echo "" >/conf/upgrade_log.txt
14
echo "" >/conf/firmware_update_misc.log
15 c5eb3a17 Scott Ullrich
echo "" >/conf/fdisk_upgrade_log.txt
16 f22c94e0 Scott Ullrich
17 1a4075a9 Scott Ullrich
exec 3>&2 2>>/conf/firmware_update_misc.log
18 51c9db03 Scott Ullrich
19 84aa381e Scott Ullrich
export ACTION=$1
20
export IMG=$2
21
if [ $# -eq 3 ]; then
22
	export CUSTOMIMG=$3
23
fi
24 2b61eeb1 Scott Ullrich
25 84aa381e Scott Ullrich
if [ $ACTION != "upgrade" ]; then
26
	/sbin/umount -f /ftmp > /dev/null 2>&1
27 fee835af Scott Ullrich
fi
28 5b237745 Scott Ullrich
29 62520171 Scott Ullrich
file_notice() {
30
	/usr/local/bin/php -q -d auto_prepend_file=config.inc <<ENDOFF
31
	<?php
32
		require_once("globals.inc");		
33
		require_once("functions.inc");
34
		file_notice("$1", "$2", "$1", "");
35
	?>
36
ENDOFF
37
}
38
39 79b9570c Scott Ullrich
output_env_to_log() {
40 6b3e4734 Scott Ullrich
	date >> /conf/upgrade_log.txt
41
	echo "" >> /conf/upgrade_log.txt
42 db7c7513 Scott Ullrich
	
43 6b3e4734 Scott Ullrich
	ls -lah /dev/ >> /conf/upgrade_log.txt
44
	echo "" >> /conf/upgrade_log.txt
45 79b9570c Scott Ullrich
46 6b3e4734 Scott Ullrich
	ls -lah $IMG >> /conf/upgrade_log.txt
47
	echo "" >> /conf/upgrade_log.txt
48 79b9570c Scott Ullrich
49 6b3e4734 Scott Ullrich
	md5 $IMG >> /conf/upgrade_log.txt
50
	echo "" >> /conf/upgrade_log.txt
51 79b9570c Scott Ullrich
52 6b3e4734 Scott Ullrich
	mount >> /conf/upgrade_log.txt
53
	echo "" >> /conf/upgrade_log.txt
54 79b9570c Scott Ullrich
55 6b3e4734 Scott Ullrich
	top >> /conf/upgrade_log.txt
56
	echo "" >> /conf/upgrade_log.txt
57 79b9570c Scott Ullrich
}
58
59 84aa381e Scott Ullrich
backup_chflags() {
60
	TOPROCESS="bin lib libexec sbin usr"
61
	for files in $TOPROCESS; do
62 d2307ffb Scott Ullrich
		/usr/sbin/mtree -Pcp /${files} | bzip2 -9 > /tmp/chflags.dist.${files}.bz2 2>> /conf/upgrade_log.txt 
63 84aa381e Scott Ullrich
	done
64
}
65 390c8e72 Scott Ullrich
66 84aa381e Scott Ullrich
restore_chflags() {
67
	TOPROCESS="bin lib libexec sbin usr"
68
	for files in $TOPROCESS; do
69 6b3e4734 Scott Ullrich
		cd / && /usr/bin/bzcat /tmp/chflags.dist.${files}.bz2 | /usr/sbin/mtree -PU -p /${files} >> /conf/upgrade_log.txt 2>&1
70 84aa381e Scott Ullrich
	done
71
}
72 8652a410 Scott Ullrich
73 84aa381e Scott Ullrich
remove_chflags() {
74
	TOPROCESS="bin lib libexec sbin usr"
75
	for files in $TOPROCESS; do
76
		/bin/chflags -R noschg /${files}
77
		/bin/chmod -R u+rw /${files}
78
	done
79
}
80 21fbee83 Scott Ullrich
81 84aa381e Scott Ullrich
binary_update() {
82
	TGZ=$1
83
	ERR_F="/tmp/bdiff.log"
84
	rm ${ERR_F} 2>/dev/null
85
	/bin/mkdir /tmp/patched /tmp/patches 2>>${ERR_F}
86
	backup_chflags
87
	remove_chflags
88
	cd /tmp/patches
89
	for i in `/usr/bin/tar tvzf $TGZ | egrep -v "(^d|_md5)" | nawk '{print $9;}'`;
90
	 do
91
	   FILE=`basename ${i}`
92
	   echo "Working on ${i}"
93
	   # Untar patch file and md5 files
94
	   /usr/bin/tar xzf ${TGZ} ${i} ${i}.old_file_md5 ${i}.new_patch_md5 ${i}.new_file_md5 2>>${ERR_F}
95 d0b3b0b1 Scott Ullrich
96 84aa381e Scott Ullrich
	   # Apply patch - oldfile newfile patchfile
97
	   /usr/local/bin/bspatch /${i} /tmp/patched/${FILE} /tmp/patches/${i} 2>>${ERR_F}
98 d0b3b0b1 Scott Ullrich
99 84aa381e Scott Ullrich
	   OLD_FILE_MD5=`cat /tmp/patches/${i}.old_file_md5 2>/dev/null`
100
	   NEW_PATCH_MD5=`cat /tmp/patches/${i}.new_patch_md5 2>/dev/null`
101
	   NEW_FILE_MD5=`cat /tmp/patches/${i}.new_file_md5 2>/dev/null`
102
	   PATCHED_MD5=`/sbin/md5 -q /tmp/patched/${FILE} 2>/dev/null`
103 2b61eeb1 Scott Ullrich
104 84aa381e Scott Ullrich
	   if [ "$PATCHED_MD5" = "$NEW_PATCH_MD5" ]; then
105
		/usr/bin/install -S  /tmp/patched/${FILE} /${i}
106
	   else
107
		#echo "${i} file does not match intended final md5."
108
		echo "${i} file does not match intended final md5." >> ${ERR_F}
109
	   fi
110
111
	   /bin/rm /tmp/patched/${FILE} >> ${ERR_F}
112
	   /bin/rm /tmp/patches/${i} >> ${ERR_F}
113
	   /bin/rm /tmp/patches/${i}.* >> ${ERR_F}
114
	done
115
	/bin/rm -rf /tmp/patched /tmp/patches >> ${ERR_F}
116
	restore_chflags
117
}
118
119
case $ACTION in
120
enable)
121 6b3e4734 Scott Ullrich
	touch /conf/upgrade_log.txt
122 0e50dda6 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
123
	echo "Enable" >> /conf/upgrade_log.txt
124
	echo "" >> /conf/upgrade_log.txt		
125 b2f626b2 Ermal Lu?i
	/etc/rc.conf_mount_ro
126 84aa381e Scott Ullrich
	;;
127
auto)
128 a368a026 Ermal Lu?i
	touch /var/run/firmwarelock.dirty
129 84aa381e Scott Ullrich
	backup_chflags
130
	remove_chflags
131
	/etc/rc.firmware_auto
132
	restore_chflags
133 b2f626b2 Ermal Lu?i
	/etc/rc.conf_mount_ro
134 84aa381e Scott Ullrich
	;;
135 c2aff9da Scott Ullrich
pfSenseNanoBSDupgrade)
136
137
	# Sanity check - bail early if there's no firmware file!
138
	if [ ! -r $IMG ]; then
139 6b3e4734 Scott Ullrich
		echo "2nd parameter has not been passed or file does not exist. Exiting." >> /conf/upgrade_log.txt 2>&1
140 bc43f0ee Scott Ullrich
		/etc/rc.conf_mount_ro
141 ac8de020 Scott Ullrich
		exit 1
142 c2aff9da Scott Ullrich
	fi
143
144 9581e85b Scott Ullrich
	# Prevent full upgrade file from being used to upgrade
145
	if [ `echo $IMG | grep "full"` ]; then
146 a331bc2a sullrich
		echo "You cannot use a full file for upgrade.  Please use a file labeled nanobsd upgrade."
147 62520171 Scott Ullrich
		file_notice "NanoBSDUpgradeFailure" "You have attemped to use a full NanoBSD installation file as an upgrade.  Please use a NanoBSD file labeled 'upgrade' instead."
148 9974dae5 Scott Ullrich
		rm -f $IMG
149 bc43f0ee Scott Ullrich
		/etc/rc.conf_mount_ro		
150 9581e85b Scott Ullrich
		exit 1
151
	fi
152
153 a368a026 Ermal Lu?i
	touch /var/run/firmwarelock.dirty
154 6a581b52 Scott Ullrich
155 6b3e4734 Scott Ullrich
	echo "NanoBSD Firmware upgrade in progress..."  >> /conf/upgrade_log.txt 2>&1
156 e3ddf385 Scott Ullrich
	echo "NanoBSD Firmware upgrade in progress..." | wall
157 c2aff9da Scott Ullrich
158
	# backup config
159
	/bin/mkdir -p /tmp/configbak
160
	cp -p /conf/* /tmp/configbak 2>/dev/null
161
162 0e50dda6 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
163 36fcc0ad Scott Ullrich
164 6b3e4734 Scott Ullrich
	echo "Installing $IMG." >> /conf/upgrade_log.txt 2>&1
165 d2307ffb Scott Ullrich
	echo "Installing $IMG." | wall
166 f93a2816 Scott Ullrich
167 15533c1e Scott Ullrich
	# resolve glabel label that we booted from
168 69025059 Scott Ullrich
	BOOT_DEVICE=`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`
169 15533c1e Scott Ullrich
	# resolve glabel to the real boot dev entry
170 004675b6 Scott Ullrich
	REAL_BOOT_DEVICE=`/sbin/glabel list | /usr/bin/grep -B2 ufs/${BOOT_DEVICE} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`
171 15533c1e Scott Ullrich
	# grab the boot device, example ad1, ad0
172 bbae22d6 Scott Ullrich
	BOOT_DRIVE=`/sbin/glabel list | /usr/bin/grep -B2 ufs/pfsense | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' ' | /usr/bin/cut -d's' -f1`
173 15533c1e Scott Ullrich
	# test the slice.  if we are on slice 1 we need to flash 2 and vica versa
174 004675b6 Scott Ullrich
	if [ `echo $REAL_BOOT_DEVICE | /usr/bin/grep "s1"` ]; then 
175
		SLICE="2"
176 63f29ad5 Scott Ullrich
		OLDSLICE="1"
177 99760ae6 Scott Ullrich
		TOFLASH="${BOOT_DRIVE}s${SLICE}"
178 63f29ad5 Scott Ullrich
		COMPLETE_PATH="${BOOT_DRIVE}s${SLICE}a"
179 93585de9 Scott Ullrich
		GLABEL_SLICE="pfsense1"
180 feff69c9 Scott Ullrich
		UFS_ID="1"
181 36bb9bea Scott Ullrich
		OLD_UFS_ID="0"
182 e348af75 Scott Ullrich
	else
183 004675b6 Scott Ullrich
		SLICE="1"
184 63f29ad5 Scott Ullrich
		OLDSLICE="2"		
185 99760ae6 Scott Ullrich
		TOFLASH="${BOOT_DRIVE}s${SLICE}"
186 63f29ad5 Scott Ullrich
		COMPLETE_PATH="${BOOT_DRIVE}s${SLICE}a"
187 feff69c9 Scott Ullrich
		GLABEL_SLICE="pfsense0"
188
		UFS_ID="0"
189 36bb9bea Scott Ullrich
		OLD_UFS_ID="1"
190 e348af75 Scott Ullrich
	fi
191
192 79b9570c Scott Ullrich
	# Output specifc information that this script is using
193 6b3e4734 Scott Ullrich
	echo "SLICE $SLICE" >> /conf/upgrade_log.txt
194
	echo "OLDSLICE $OLDSLICE" >> /conf/upgrade_log.txt
195
	echo "TOFLASH $TOFLASH" >> /conf/upgrade_log.txt
196
	echo "COMPLETE_PATH $COMPLETE_PATH" >> /conf/upgrade_log.txt
197
	echo "GLABEL_SLICE $GLABEL_SLICE" >> /conf/upgrade_log.txt
198 a3b8edea Scott Ullrich
199 b90885ff Scott Ullrich
	# First ensure the new file can fit inside the 
200
	# slice that we are going to be operating on.
201
	NEW_IMG_SIZE=`echo $((\`gzip -l ${IMG} | grep -v compressed | awk '{ print $2}'\` / 1024 / 1024))`
202
	SIZE=`/sbin/fdisk ${COMPLETE_PATH} | /usr/bin/grep Meg | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d"(" -f2`
203 80682f55 Scott Ullrich
	SIZE=`expr $SIZE + 1`
204 b90885ff Scott Ullrich
	if [ "$SIZE" -lt "$NEW_IMG_SIZE" ]; then
205 5d15e968 Scott Ullrich
		file_notice "UpgradeFailure" "Upgrade failed due to the upgrade image being larger than the partition that is configured on disk.  Halting. Size on disk: $SIZE < Size of new image: $NEW_IMG_SIZE"
206 b90885ff Scott Ullrich
		echo "Upgrade failed.  Please check the system log file for more information" | wall
207 9974dae5 Scott Ullrich
		rm -f $IMG
208 7335c4cf Scott Ullrich
		rm -f /var/run/firmwarelock.dirty
209
		rm -f /var/run/firmware.lock
210 3bcd5555 Renato Botelho
		rm -f ${IMG}
211 b90885ff Scott Ullrich
		/etc/rc.conf_mount_ro		
212
		exit 1
213
	fi
214
215 79b9570c Scott Ullrich
	# Output environment information to log file
216
	output_env_to_log
217 6b3e4734 Scott Ullrich
	
218
	# Grab a before upgrade look at fdisk
219 c5eb3a17 Scott Ullrich
	echo "" >> /conf/fdisk_upgrade_log.txt
220
	echo "Before upgrade fdisk/bsdlabel" >> /conf/fdisk_upgrade_log.txt
221
	fdisk $BOOT_DRIVE >> /conf/fdisk_upgrade_log.txt
222
	fdisk $BOOT_DRIVEs1 >> /conf/fdisk_upgrade_log.txt
223
	fdisk $BOOT_DRIVEs1a >> /conf/fdisk_upgrade_log.txt
224
	fdisk $BOOT_DRIVEs2 >> /conf/fdisk_upgrade_log.txt
225
	fdisk $BOOT_DRIVEs2a >> /conf/fdisk_upgrade_log.txt		
226
	fdisk $BOOT_DRIVEs3 >> /conf/fdisk_upgrade_log.txt
227
	bsdlabel -A $BOOT_DRIVEs1 >> /conf/fdisk_upgrade_log.txt
228
	bsdlabel -A $BOOT_DRIVEs2 >> /conf/fdisk_upgrade_log.txt
229
	bsdlabel -A $BOOT_DRIVEs3 >> /conf/fdisk_upgrade_log.txt
230
	echo "---------------------------------------------------------------" >> /conf/fdisk_upgrade_log.txt
231
	echo "" >> /conf/fdisk_upgrade_log.txt
232 6b3e4734 Scott Ullrich
	
233 024445ee Scott Ullrich
	# Log that we are really doing a NanoBSD upgrade
234 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
235
	echo "NanoBSD upgrade starting" >> /conf/upgrade_log.txt
236
	echo "" >> /conf/upgrade_log.txt
237 a3b8edea Scott Ullrich
238 e348af75 Scott Ullrich
	# Remove TOFLASH and get ready for new flash image
239 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
240
	echo "dd if=/dev/zero of=/dev/${TOFLASH} bs=1m count=1" >> /conf/upgrade_log.txt	
241
	dd if=/dev/zero of=/dev/${TOFLASH} bs=1m count=1 >> /conf/upgrade_log.txt 2>&1
242 e348af75 Scott Ullrich
243
	# Stream gzipped image to dd and explode image to new area
244 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
245 ac2bed30 Scott Ullrich
	echo "/usr/bin/gzip -dc $IMG | /bin/dd of=/dev/${TOFLASH} obs=64k" >> /conf/upgrade_log.txt
246
	/usr/bin/gzip -dc $IMG | /bin/dd of=/dev/${TOFLASH} obs=64k >> /conf/upgrade_log.txt 2>&1
247 6b3e4734 Scott Ullrich
248
	# Grab a after upgrade look at fdisk
249 c5eb3a17 Scott Ullrich
	echo "" >> /conf/fdisk_upgrade_log.txt
250 0e50dda6 Scott Ullrich
	echo "After upgrade fdisk/bsdlabel" >> /conf/upgrade_log.txt
251 c5eb3a17 Scott Ullrich
	fdisk $BOOT_DRIVE >> /conf/fdisk_upgrade_log.txt
252
	fdisk $BOOT_DRIVEs1 >> /conf/fdisk_upgrade_log.txt
253
	fdisk $BOOT_DRIVEs1a >> /conf/fdisk_upgrade_log.txt
254
	fdisk $BOOT_DRIVEs2 >> /conf/fdisk_upgrade_log.txt
255
	fdisk $BOOT_DRIVEs2a >> /conf/fdisk_upgrade_log.txt		
256
	fdisk $BOOT_DRIVEs3 >> /conf/fdisk_upgrade_log.txt
257
	bsdlabel -A $BOOT_DRIVEs1 >> /conf/fdisk_upgrade_log.txt
258
	bsdlabel -A $BOOT_DRIVEs2 >> /conf/fdisk_upgrade_log.txt
259
	bsdlabel -A $BOOT_DRIVEs3 >> /conf/fdisk_upgrade_log.txt
260
	echo "---------------------------------------------------------------" >> /conf/fdisk_upgrade_log.txt
261
	echo "" >> /conf/fdisk_upgrade_log.txt
262 e348af75 Scott Ullrich
	
263
	# Ensure that our new system is sound and bail if it is not and file a notice
264 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
265 e7b0b0f7 Scott Ullrich
	echo "/sbin/fsck_ufs -y /dev/$COMPLETE_PATH" >> /conf/upgrade_log.txt
266
	/sbin/fsck_ufs -y /dev/$COMPLETE_PATH >> /conf/upgrade_log.txt 2>&1
267 e348af75 Scott Ullrich
	if [ $? != 0 ]; then
268 62520171 Scott Ullrich
		file_notice "UpgradeFailure" "{\$g['product_name']} upgrade has failed.   Your system has been left in a usable state."
269 9974dae5 Scott Ullrich
		rm -f $IMG
270 7335c4cf Scott Ullrich
		rm -f /var/run/firmwarelock.dirty
271
		rm -f /var/run/firmware.lock
272 bc43f0ee Scott Ullrich
		/etc/rc.conf_mount_ro		
273 9492ad9d Scott Ullrich
		exit 1
274 e348af75 Scott Ullrich
	fi
275
276 0bc8c0b0 Scott Ullrich
	# Enable foot shooting
277 32a6b30c Scott Ullrich
	sysctl kern.geom.debugflags=16
278
279 63f29ad5 Scott Ullrich
	# Add back the corresponding glabel
280 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
281
	echo "/sbin/tunefs -L ${GLABEL_SLICE} /dev/$COMPLETE_PATH" >> /conf/upgrade_log.txt
282
	/sbin/tunefs -L ${GLABEL_SLICE} /dev/$COMPLETE_PATH >> /conf/upgrade_log.txt 2>&1
283 63f29ad5 Scott Ullrich
284 c2aff9da Scott Ullrich
	# restore config
285
	cp -p /tmp/configbak/* /conf 2>/dev/null
286
287
	# Remove upgrade file
288
	rm -f $IMG
289
290 5924c7f8 Scott Ullrich
	# Mount newly prepared slice
291 db7c7513 Scott Ullrich
	mkdir /tmp/$GLABEL_SLICE
292
	mount /dev/ufs/$GLABEL_SLICE /tmp/$GLABEL_SLICE
293 5924c7f8 Scott Ullrich
294 8eaa9957 Scott Ullrich
	# If /tmp/$GLABEL_SLICE/tmp/post_upgrade_command exists 
295 baabd21d Scott Ullrich
	# after update then execute the command.
296 8eaa9957 Scott Ullrich
	if [ -f /tmp/$GLABEL_SLICE/tmp/post_upgrade_command ]; then
297 6b3e4734 Scott Ullrich
		sh /tmp/$GLABEL_SLICE/tmp/post_upgrade_command >> /conf/upgrade_log.txt 2>&1
298 c2aff9da Scott Ullrich
	fi
299 5924c7f8 Scott Ullrich
300 63f29ad5 Scott Ullrich
	# Update fstab
301 507960cb Scott Ullrich
	cp /etc/fstab /tmp/$GLABEL_SLICE/etc/fstab
302 36bb9bea Scott Ullrich
	sed -i "" "s/pfsense${OLD_UFS_ID}/pfsense${UFS_ID}/g" /tmp/$GLABEL_SLICE/etc/fstab
303 45c710da Scott Ullrich
	if [ $? != 0 ]; then
304
		echo "Something went wrong when trying to update the fstab entry.  Aborting upgrade."
305
		file_notice "UpgradeFailure" "Something went wrong when trying to update the fstab entry.  Aborting upgrade."
306 9974dae5 Scott Ullrich
		rm -f $IMG
307 7335c4cf Scott Ullrich
		rm -f /var/run/firmwarelock.dirty
308
		rm -f /var/run/firmware.lock
309 45c710da Scott Ullrich
		umount /tmp/$GLABEL_SLICE
310 bc43f0ee Scott Ullrich
		/etc/rc.conf_mount_ro
311 45c710da Scott Ullrich
		exit 1
312
	fi
313 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
314
	cat /tmp/$GLABEL_SLICE/etc/fstab >> /conf/upgrade_log.txt
315 5924c7f8 Scott Ullrich
316 53e85f50 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
317 af214f1d Scott Ullrich
	find /tmp/$GLABEL_SLICE >/conf/file_upgrade_log.txt
318 53e85f50 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
319
320 5924c7f8 Scott Ullrich
	# Unmount newly prepared slice
321 db7c7513 Scott Ullrich
	umount /tmp/$GLABEL_SLICE
322 7f38e934 Scott Ullrich
	
323
	sync
324 c2aff9da Scott Ullrich
325 45c710da Scott Ullrich
	# Set active mount slice in fdisk
326 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
327
	echo "gpart set -a active -i ${SLICE} ${BOOT_DRIVE}" >> /conf/upgrade_log.txt
328 0bc8c0b0 Scott Ullrich
	gpart set -a active -i ${SLICE} ${BOOT_DRIVE} >> /conf/upgrade_log.txt 2>&1
329 45c710da Scott Ullrich
330 7f38e934 Scott Ullrich
	sync
331
332 e213b6fc Scott Ullrich
	# Set active boot source - NanoBSD does not do this but otherwise we
333
	# end up with the wrong partition being active.
334 b4047760 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
335
	echo "/usr/sbin/boot0cfg -s ${SLICE} -v /dev/${BOOT_DRIVE}" >> /conf/upgrade_log.txt
336
	/usr/sbin/boot0cfg -s ${SLICE} -v /dev/${BOOT_DRIVE} >> /conf/upgrade_log.txt 2>&1
337 6b3e4734 Scott Ullrich
338 0bc8c0b0 Scott Ullrich
	# Disable foot shooting
339 32a6b30c Scott Ullrich
	sysctl kern.geom.debugflags=0
340
341 6b3e4734 Scott Ullrich
	# Grab a final look at fdisk
342 c5eb3a17 Scott Ullrich
	echo "" >> /conf/fdisk_upgrade_log.txt
343
	echo "Final upgrade fdisk/bsdlabel" >> /conf/fdisk_upgrade_log.txt
344
	fdisk $BOOT_DRIVE >> /conf/fdisk_upgrade_log.txt
345
	fdisk $BOOT_DRIVEs1 >> /conf/fdisk_upgrade_log.txt
346
	fdisk $BOOT_DRIVEs1a >> /conf/fdisk_upgrade_log.txt
347
	fdisk $BOOT_DRIVEs2 >> /conf/fdisk_upgrade_log.txt
348
	fdisk $BOOT_DRIVEs2a >> /conf/fdisk_upgrade_log.txt		
349
	fdisk $BOOT_DRIVEs3 >> /conf/fdisk_upgrade_log.txt
350
	bsdlabel -A $BOOT_DRIVEs1 >> /conf/fdisk_upgrade_log.txt
351
	bsdlabel -A $BOOT_DRIVEs2 >> /conf/fdisk_upgrade_log.txt
352
	bsdlabel -A $BOOT_DRIVEs3 >> /conf/fdisk_upgrade_log.txt
353
	echo "---------------------------------------------------------------" >> /conf/fdisk_upgrade_log.txt
354
	echo "" >> /conf/fdisk_upgrade_log.txt
355 45c710da Scott Ullrich
356 5924c7f8 Scott Ullrich
	# Remove extra stuff
357 c2aff9da Scott Ullrich
	rm -rf /etc/rc.conf
358
	rm -rf /etc/motd
359
	rm -rf /usr/savecore/*
360
361 6b3e4734 Scott Ullrich
	date >> /conf/upgrade_log.txt
362
	echo "" >> /conf/upgrade_log.txt
363 db7c7513 Scott Ullrich
364 359d9ec9 Scott Ullrich
	# Trigger a package reinstallation on reobot
365 65c79f55 Scott Ullrich
	touch /conf/needs_package_sync
366 359d9ec9 Scott Ullrich
367 5924c7f8 Scott Ullrich
	# remount /cf ro
368
	/etc/rc.conf_mount_ro
369 c2aff9da Scott Ullrich
	/bin/sync
370 903f12d4 Scott Ullrich
371 d4b8f9a8 Scott Ullrich
	sleep 10
372
373 a368a026 Ermal Lu?i
	rm -f /var/run/firmwarelock.dirty
374 7335c4cf Scott Ullrich
	rm -f /var/run/firmware.lock
375 5423f1a0 Scott Ullrich
	sh /etc/rc.reboot
376 c2aff9da Scott Ullrich
377 84aa381e Scott Ullrich
	;;
378
pfSenseupgrade)
379 50ba07ea Scott Ullrich
380 84aa381e Scott Ullrich
	# Sanity check - bail early if there's no firmware file!
381
	if [ ! -r $IMG ]; then
382 6b3e4734 Scott Ullrich
		echo "2nd parameter has not been passed or file does not exist. Exiting." >> /conf/upgrade_log.txt 2>&1
383 50ba07ea Scott Ullrich
		/etc/rc.conf_mount_ro
384 84aa381e Scott Ullrich
		exit
385
	fi
386
387 6a581b52 Scott Ullrich
	# wait 1 seconds before beginning
388
	sleep 1
389
390 d2307ffb Scott Ullrich
	# Log that we are really doing a pfSense upgrade
391 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
392 efdf0b9c sullrich
	echo "pfSenseupgrade upgrade starting" >> /conf/upgrade_log.txt
393 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
394 50ba07ea Scott Ullrich
395 a368a026 Ermal Lu?i
	touch /var/run/firmwarelock.dirty
396 6a581b52 Scott Ullrich
397 6b3e4734 Scott Ullrich
	touch /conf/upgrade_log.txt
398
	echo "" >> /conf/upgrade_log.txt
399 79b9570c Scott Ullrich
400
	# Output environment information to log file
401
	output_env_to_log
402
403 84aa381e Scott Ullrich
	backup_chflags
404
	remove_chflags
405
406 4a8e802d Scott Ullrich
	# Do we have a pre-upgrade hook in the update file?
407
	if [ `tar tvzf $IMG | grep /tmp/pre_upgrade_command | wc -l` -gt 0 ]; then 
408 6b3e4734 Scott Ullrich
		tar xzvf $IMG -C / ./tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
409
		chmod a+rx /tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
410
		sh /tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
411 4a8e802d Scott Ullrich
	fi
412
413 6b3e4734 Scott Ullrich
	echo "Firmware upgrade in progress..."  >> /conf/upgrade_log.txt 2>&1
414 79b9570c Scott Ullrich
	echo "Firmware upgrade in progress..."  | wall
415 50ba07ea Scott Ullrich
416 84aa381e Scott Ullrich
	# backup config
417
	/bin/mkdir -p /tmp/configbak
418
	cp -p /conf/* /tmp/configbak 2>/dev/null
419
420
	# tar explode image onto hd
421 6b3e4734 Scott Ullrich
	echo "Installing $IMG." >> /conf/upgrade_log.txt 2>&1
422
	cd / && /usr/bin/tar xzUPf $IMG >> /conf/upgrade_log.txt 2>&1
423 84aa381e Scott Ullrich
	/usr/bin/find / -name CVS -exec rm -fr {} \;
424 6b3e4734 Scott Ullrich
	echo "Image installed $IMG." >> /conf/upgrade_log.txt 2>&1
425 84aa381e Scott Ullrich
426
    # process custom image if its passed
427
    if [ $# -eq 3 ]; then
428
	    if [ -f $CUSTOMIMG ]; then
429 6b3e4734 Scott Ullrich
	        echo "Custom image $CUSTOMIMG found." >> /conf/upgrade_log.txt 2>&1
430
	        echo "Custom image ($CUSTOMIMG) found." >> /conf/upgrade_log.txt 2>&1
431 84aa381e Scott Ullrich
	        PWD_DIR=`pwd`
432 6b3e4734 Scott Ullrich
	        cd / && /usr/bin/tar xzPUf $CUSTOMIMG >> /conf/upgrade_log.txt 2>&1
433 84aa381e Scott Ullrich
	        cd $PWD_DIR
434 6b3e4734 Scott Ullrich
	        echo "Custom image $CUSTOMIMG installed." >> /conf/upgrade_log.txt 2>&1
435 84aa381e Scott Ullrich
	    fi
436
    fi
437
438
	# restore config
439
	cp -p /tmp/configbak/* /conf 2>/dev/null
440
441
	# restore /etc symlinks
442
	rm /etc/hosts
443
	ln -s /var/etc/hosts /etc/hosts
444
445
	restore_chflags
446
447
	# Remove upgrade file
448
	rm -f $IMG
449
450 6e75ac00 Scott Ullrich
	if [ -e /etc/init_bootloader.sh ]; then
451 6b3e4734 Scott Ullrich
		sh /etc/init_bootloader.sh >> /conf/upgrade_log.txt 2>&1
452 6e75ac00 Scott Ullrich
	fi
453 84aa381e Scott Ullrich
454 6e75ac00 Scott Ullrich
	# If /tmp/post_upgrade_command exists after update
455
	# then execute the command.
456
	if [ -f /tmp/post_upgrade_command ]; then
457 6b3e4734 Scott Ullrich
		sh /tmp/post_upgrade_command >> /conf/upgrade_log.txt 2>&1
458 6e75ac00 Scott Ullrich
	fi
459
460 79b9570c Scott Ullrich
	# remove unused files
461 84aa381e Scott Ullrich
	rm -rf /etc/rc.conf
462
	rm -rf /etc/motd
463
	rm -rf /usr/savecore/*
464 79b9570c Scott Ullrich
465 6b3e4734 Scott Ullrich
	date >> /conf/upgrade_log.txt
466
	echo "" >> /conf/upgrade_log.txt
467 db7c7513 Scott Ullrich
468 79b9570c Scott Ullrich
	# remount /cf ro
469 84aa381e Scott Ullrich
	/etc/rc.conf_mount_ro
470 6e75ac00 Scott Ullrich
471 79b9570c Scott Ullrich
	# release the firmware lock
472 a368a026 Ermal Lu?i
	rm -f /var/run/firmwarelock.dirty
473 7335c4cf Scott Ullrich
	rm -f /var/run/firmware.lock
474 84aa381e Scott Ullrich
	/bin/sync
475
476 d4b8f9a8 Scott Ullrich
	# Sleep and allow disks to catch up
477
	sleep 10
478
479 84aa381e Scott Ullrich
	# If the archive has unpacked a file called
480
	# /tmp/no_upgrade_reboot_required then do
481
	# not reboot after upgrade.
482
	if [ -f /tmp/no_upgrade_reboot_required ]; then
483
		rm /tmp/no_upgrade_reboot_required
484
	else
485
		rm -f /var/run/config.lock
486
		sh /etc/rc.reboot
487
	fi
488 6e75ac00 Scott Ullrich
489 84aa381e Scott Ullrich
	;;
490
delta_update)
491 a368a026 Ermal Lu?i
	touch /var/run/firmwarelock.dirty
492 84aa381e Scott Ullrich
	backup_chflags
493
	remove_chflags
494
	binary_update $IMG
495
	restore_chflags
496
	rm -rf /etc/rc.conf
497
	rm -rf /etc/motd
498
	find / -name CVS -type d -exec rm {} \;
499
	rm -rf /usr/savecore/*
500
	/etc/rc.conf_mount_ro
501
	/sbin/umount -f /cf 2>/dev/null
502
	/sbin/mount -r /cf 2>/dev/null
503
	/sbin/umount -f / 2>/dev/null
504
	/sbin/mount -r / 2>/dev/null
505
	if [ -e /etc/init_bootloader.sh ]; then
506
		sh /etc/init_bootloader.sh
507
	fi
508
509
	;;
510
esac