Project

General

Profile

Download (16.9 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 78d7e759 jim-p
echo "" >/conf/firmware_update_misc_log.txt
15 c5eb3a17 Scott Ullrich
echo "" >/conf/fdisk_upgrade_log.txt
16 f22c94e0 Scott Ullrich
17 78d7e759 jim-p
exec 3>&2 2>>/conf/firmware_update_misc_log.txt
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 bf072179 jim-p
	# Save the old shutdown binary. If we switch from i386 to amd64 (or back) the reboot binary won't run at the end since it doesn't match up.
87 6fe72bc8 jim-p
	/bin/cp -p /sbin/shutdown /sbin/shutdown.old
88 84aa381e Scott Ullrich
	backup_chflags
89
	remove_chflags
90
	cd /tmp/patches
91
	for i in `/usr/bin/tar tvzf $TGZ | egrep -v "(^d|_md5)" | nawk '{print $9;}'`;
92
	 do
93
	   FILE=`basename ${i}`
94
	   echo "Working on ${i}"
95
	   # Untar patch file and md5 files
96
	   /usr/bin/tar xzf ${TGZ} ${i} ${i}.old_file_md5 ${i}.new_patch_md5 ${i}.new_file_md5 2>>${ERR_F}
97 d0b3b0b1 Scott Ullrich
98 84aa381e Scott Ullrich
	   # Apply patch - oldfile newfile patchfile
99
	   /usr/local/bin/bspatch /${i} /tmp/patched/${FILE} /tmp/patches/${i} 2>>${ERR_F}
100 d0b3b0b1 Scott Ullrich
101 84aa381e Scott Ullrich
	   OLD_FILE_MD5=`cat /tmp/patches/${i}.old_file_md5 2>/dev/null`
102
	   NEW_PATCH_MD5=`cat /tmp/patches/${i}.new_patch_md5 2>/dev/null`
103
	   NEW_FILE_MD5=`cat /tmp/patches/${i}.new_file_md5 2>/dev/null`
104
	   PATCHED_MD5=`/sbin/md5 -q /tmp/patched/${FILE} 2>/dev/null`
105 2b61eeb1 Scott Ullrich
106 84aa381e Scott Ullrich
	   if [ "$PATCHED_MD5" = "$NEW_PATCH_MD5" ]; then
107
		/usr/bin/install -S  /tmp/patched/${FILE} /${i}
108
	   else
109
		#echo "${i} file does not match intended final md5."
110
		echo "${i} file does not match intended final md5." >> ${ERR_F}
111
	   fi
112
113
	   /bin/rm /tmp/patched/${FILE} >> ${ERR_F}
114
	   /bin/rm /tmp/patches/${i} >> ${ERR_F}
115
	   /bin/rm /tmp/patches/${i}.* >> ${ERR_F}
116
	done
117
	/bin/rm -rf /tmp/patched /tmp/patches >> ${ERR_F}
118
	restore_chflags
119
}
120
121
case $ACTION in
122
enable)
123 6b3e4734 Scott Ullrich
	touch /conf/upgrade_log.txt
124 0e50dda6 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
125
	echo "Enable" >> /conf/upgrade_log.txt
126
	echo "" >> /conf/upgrade_log.txt		
127 b2f626b2 Ermal Lu?i
	/etc/rc.conf_mount_ro
128 84aa381e Scott Ullrich
	;;
129
auto)
130 a368a026 Ermal Lu?i
	touch /var/run/firmwarelock.dirty
131 84aa381e Scott Ullrich
	backup_chflags
132
	remove_chflags
133
	/etc/rc.firmware_auto
134
	restore_chflags
135 b2f626b2 Ermal Lu?i
	/etc/rc.conf_mount_ro
136 84aa381e Scott Ullrich
	;;
137 c2aff9da Scott Ullrich
pfSenseNanoBSDupgrade)
138
139
	# Sanity check - bail early if there's no firmware file!
140
	if [ ! -r $IMG ]; then
141 6b3e4734 Scott Ullrich
		echo "2nd parameter has not been passed or file does not exist. Exiting." >> /conf/upgrade_log.txt 2>&1
142 bc43f0ee Scott Ullrich
		/etc/rc.conf_mount_ro
143 ac8de020 Scott Ullrich
		exit 1
144 c2aff9da Scott Ullrich
	fi
145
146 9581e85b Scott Ullrich
	# Prevent full upgrade file from being used to upgrade
147
	if [ `echo $IMG | grep "full"` ]; then
148 a331bc2a sullrich
		echo "You cannot use a full file for upgrade.  Please use a file labeled nanobsd upgrade."
149 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."
150 9974dae5 Scott Ullrich
		rm -f $IMG
151 bc43f0ee Scott Ullrich
		/etc/rc.conf_mount_ro		
152 9581e85b Scott Ullrich
		exit 1
153
	fi
154
155 a368a026 Ermal Lu?i
	touch /var/run/firmwarelock.dirty
156 6a581b52 Scott Ullrich
157 6b3e4734 Scott Ullrich
	echo "NanoBSD Firmware upgrade in progress..."  >> /conf/upgrade_log.txt 2>&1
158 e3ddf385 Scott Ullrich
	echo "NanoBSD Firmware upgrade in progress..." | wall
159 62fc138e Warren Baker
	/etc/rc.notify_message -e -g -m "NanoBSD Firmware upgrade in progress..."
160 476aa14b Scott Ullrich
	
161 c2aff9da Scott Ullrich
	# backup config
162
	/bin/mkdir -p /tmp/configbak
163 ce09186c jim-p
	cp -Rp /conf/* /tmp/configbak 2>/dev/null
164 c2aff9da Scott Ullrich
165 2cd85631 jim-p
	# Remove logs from backup dir to avoid clobbering upon restore.
166
	rm /tmp/configbak/*_log.txt 2>/dev/null
167
168 0e50dda6 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
169 36fcc0ad Scott Ullrich
170 e588bd54 jim-p
	echo "Installing ${IMG}." >> /conf/upgrade_log.txt 2>&1
171
	echo "Installing ${IMG}." | wall
172 f93a2816 Scott Ullrich
173 15533c1e Scott Ullrich
	# resolve glabel label that we booted from
174 69025059 Scott Ullrich
	BOOT_DEVICE=`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`
175 15533c1e Scott Ullrich
	# resolve glabel to the real boot dev entry
176 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' '`
177 15533c1e Scott Ullrich
	# grab the boot device, example ad1, ad0
178 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`
179 15533c1e Scott Ullrich
	# test the slice.  if we are on slice 1 we need to flash 2 and vica versa
180 004675b6 Scott Ullrich
	if [ `echo $REAL_BOOT_DEVICE | /usr/bin/grep "s1"` ]; then 
181
		SLICE="2"
182 63f29ad5 Scott Ullrich
		OLDSLICE="1"
183 99760ae6 Scott Ullrich
		TOFLASH="${BOOT_DRIVE}s${SLICE}"
184 63f29ad5 Scott Ullrich
		COMPLETE_PATH="${BOOT_DRIVE}s${SLICE}a"
185 93585de9 Scott Ullrich
		GLABEL_SLICE="pfsense1"
186 feff69c9 Scott Ullrich
		UFS_ID="1"
187 36bb9bea Scott Ullrich
		OLD_UFS_ID="0"
188 e348af75 Scott Ullrich
	else
189 004675b6 Scott Ullrich
		SLICE="1"
190 63f29ad5 Scott Ullrich
		OLDSLICE="2"		
191 99760ae6 Scott Ullrich
		TOFLASH="${BOOT_DRIVE}s${SLICE}"
192 63f29ad5 Scott Ullrich
		COMPLETE_PATH="${BOOT_DRIVE}s${SLICE}a"
193 feff69c9 Scott Ullrich
		GLABEL_SLICE="pfsense0"
194
		UFS_ID="0"
195 36bb9bea Scott Ullrich
		OLD_UFS_ID="1"
196 e348af75 Scott Ullrich
	fi
197
198 79b9570c Scott Ullrich
	# Output specifc information that this script is using
199 e588bd54 jim-p
	echo "SLICE         ${SLICE}" >> /conf/upgrade_log.txt
200
	echo "OLDSLICE      ${OLDSLICE}" >> /conf/upgrade_log.txt
201
	echo "TOFLASH       ${TOFLASH}" >> /conf/upgrade_log.txt
202
	echo "COMPLETE_PATH ${COMPLETE_PATH}" >> /conf/upgrade_log.txt
203
	echo "GLABEL_SLICE  ${GLABEL_SLICE}" >> /conf/upgrade_log.txt
204 a3b8edea Scott Ullrich
205 b90885ff Scott Ullrich
	# First ensure the new file can fit inside the 
206
	# slice that we are going to be operating on.
207
	NEW_IMG_SIZE=`echo $((\`gzip -l ${IMG} | grep -v compressed | awk '{ print $2}'\` / 1024 / 1024))`
208
	SIZE=`/sbin/fdisk ${COMPLETE_PATH} | /usr/bin/grep Meg | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d"(" -f2`
209 dd40d664 jim-p
	# USB slices are under-reported even more than CF slices when viewed
210
	#   directly, instead of when looking at the entire disk. Compensate
211
	#   by adding a few MB.
212
	SIZE=`expr $SIZE + 4`
213 b90885ff Scott Ullrich
	if [ "$SIZE" -lt "$NEW_IMG_SIZE" ]; then
214 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"
215 b90885ff Scott Ullrich
		echo "Upgrade failed.  Please check the system log file for more information" | wall
216 9974dae5 Scott Ullrich
		rm -f $IMG
217 7335c4cf Scott Ullrich
		rm -f /var/run/firmwarelock.dirty
218
		rm -f /var/run/firmware.lock
219 3bcd5555 Renato Botelho
		rm -f ${IMG}
220 b90885ff Scott Ullrich
		/etc/rc.conf_mount_ro		
221
		exit 1
222
	fi
223
224 79b9570c Scott Ullrich
	# Output environment information to log file
225
	output_env_to_log
226 6b3e4734 Scott Ullrich
	
227
	# Grab a before upgrade look at fdisk
228 c5eb3a17 Scott Ullrich
	echo "" >> /conf/fdisk_upgrade_log.txt
229
	echo "Before upgrade fdisk/bsdlabel" >> /conf/fdisk_upgrade_log.txt
230
	fdisk $BOOT_DRIVE >> /conf/fdisk_upgrade_log.txt
231 e588bd54 jim-p
	bsdlabel -A ${BOOT_DRIVE}s1 >> /conf/fdisk_upgrade_log.txt
232
	bsdlabel -A ${BOOT_DRIVE}s2 >> /conf/fdisk_upgrade_log.txt
233
	bsdlabel -A ${BOOT_DRIVE}s3 >> /conf/fdisk_upgrade_log.txt
234 c5eb3a17 Scott Ullrich
	echo "---------------------------------------------------------------" >> /conf/fdisk_upgrade_log.txt
235
	echo "" >> /conf/fdisk_upgrade_log.txt
236 6b3e4734 Scott Ullrich
	
237 024445ee Scott Ullrich
	# Log that we are really doing a NanoBSD upgrade
238 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
239
	echo "NanoBSD upgrade starting" >> /conf/upgrade_log.txt
240
	echo "" >> /conf/upgrade_log.txt
241 a3b8edea Scott Ullrich
242 e348af75 Scott Ullrich
	# Remove TOFLASH and get ready for new flash image
243 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
244
	echo "dd if=/dev/zero of=/dev/${TOFLASH} bs=1m count=1" >> /conf/upgrade_log.txt	
245
	dd if=/dev/zero of=/dev/${TOFLASH} bs=1m count=1 >> /conf/upgrade_log.txt 2>&1
246 e348af75 Scott Ullrich
247
	# Stream gzipped image to dd and explode image to new area
248 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
249 ac2bed30 Scott Ullrich
	echo "/usr/bin/gzip -dc $IMG | /bin/dd of=/dev/${TOFLASH} obs=64k" >> /conf/upgrade_log.txt
250
	/usr/bin/gzip -dc $IMG | /bin/dd of=/dev/${TOFLASH} obs=64k >> /conf/upgrade_log.txt 2>&1
251 6b3e4734 Scott Ullrich
252
	# Grab a after upgrade look at fdisk
253 c5eb3a17 Scott Ullrich
	echo "" >> /conf/fdisk_upgrade_log.txt
254 0e50dda6 Scott Ullrich
	echo "After upgrade fdisk/bsdlabel" >> /conf/upgrade_log.txt
255 c5eb3a17 Scott Ullrich
	fdisk $BOOT_DRIVE >> /conf/fdisk_upgrade_log.txt
256 e588bd54 jim-p
	bsdlabel -A ${BOOT_DRIVE}s1 >> /conf/fdisk_upgrade_log.txt
257
	bsdlabel -A ${BOOT_DRIVE}s2 >> /conf/fdisk_upgrade_log.txt
258
	bsdlabel -A ${BOOT_DRIVE}s3 >> /conf/fdisk_upgrade_log.txt
259 c5eb3a17 Scott Ullrich
	echo "---------------------------------------------------------------" >> /conf/fdisk_upgrade_log.txt
260
	echo "" >> /conf/fdisk_upgrade_log.txt
261 e348af75 Scott Ullrich
	
262
	# Ensure that our new system is sound and bail if it is not and file a notice
263 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
264 e588bd54 jim-p
	echo "/sbin/fsck_ufs -y /dev/${COMPLETE_PATH}" >> /conf/upgrade_log.txt
265
	/sbin/fsck_ufs -y /dev/${COMPLETE_PATH} >> /conf/upgrade_log.txt 2>&1
266 e348af75 Scott Ullrich
	if [ $? != 0 ]; then
267 62520171 Scott Ullrich
		file_notice "UpgradeFailure" "{\$g['product_name']} upgrade has failed.   Your system has been left in a usable state."
268 9974dae5 Scott Ullrich
		rm -f $IMG
269 7335c4cf Scott Ullrich
		rm -f /var/run/firmwarelock.dirty
270
		rm -f /var/run/firmware.lock
271 bc43f0ee Scott Ullrich
		/etc/rc.conf_mount_ro		
272 9492ad9d Scott Ullrich
		exit 1
273 e348af75 Scott Ullrich
	fi
274
275 0bc8c0b0 Scott Ullrich
	# Enable foot shooting
276 32a6b30c Scott Ullrich
	sysctl kern.geom.debugflags=16
277
278 63f29ad5 Scott Ullrich
	# Add back the corresponding glabel
279 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
280 e588bd54 jim-p
	echo "/sbin/tunefs -L ${GLABEL_SLICE} /dev/${COMPLETE_PATH}" >> /conf/upgrade_log.txt
281
	/sbin/tunefs -L ${GLABEL_SLICE} /dev/${COMPLETE_PATH} >> /conf/upgrade_log.txt 2>&1
282 63f29ad5 Scott Ullrich
283 c2aff9da Scott Ullrich
	# restore config
284 ce09186c jim-p
	cp -Rp /tmp/configbak/* /conf 2>/dev/null
285 c2aff9da Scott Ullrich
286
	# Remove upgrade file
287
	rm -f $IMG
288
289 5924c7f8 Scott Ullrich
	# Mount newly prepared slice
290 db7c7513 Scott Ullrich
	mkdir /tmp/$GLABEL_SLICE
291
	mount /dev/ufs/$GLABEL_SLICE /tmp/$GLABEL_SLICE
292 5924c7f8 Scott Ullrich
293 ba8e0870 Erik Fonnesbeck
	# If /boot/loader.conf.local exists
294
	# copy to the other slice.
295
	if [ -f /boot/loader.conf.local ]; then
296
		cp /boot/loader.conf.local /tmp/$GLABEL_SLICE/boot/loader.conf.local
297
	fi
298
299 8eaa9957 Scott Ullrich
	# If /tmp/$GLABEL_SLICE/tmp/post_upgrade_command exists 
300 baabd21d Scott Ullrich
	# after update then execute the command.
301 baef6be8 jim-p
	echo "Checking for post_upgrade_command..." >> /conf/upgrade_log.txt
302 8eaa9957 Scott Ullrich
	if [ -f /tmp/$GLABEL_SLICE/tmp/post_upgrade_command ]; then
303 baef6be8 jim-p
		echo "Found post_upgrade_command, executing ($GLABEL_SLICE)..." >> /conf/upgrade_log.txt
304 196d0085 jim-p
		sh /tmp/$GLABEL_SLICE/tmp/post_upgrade_command $GLABEL_SLICE >> /conf/upgrade_log.txt 2>&1
305 c2aff9da Scott Ullrich
	fi
306 5924c7f8 Scott Ullrich
307 63f29ad5 Scott Ullrich
	# Update fstab
308 507960cb Scott Ullrich
	cp /etc/fstab /tmp/$GLABEL_SLICE/etc/fstab
309 36bb9bea Scott Ullrich
	sed -i "" "s/pfsense${OLD_UFS_ID}/pfsense${UFS_ID}/g" /tmp/$GLABEL_SLICE/etc/fstab
310 45c710da Scott Ullrich
	if [ $? != 0 ]; then
311
		echo "Something went wrong when trying to update the fstab entry.  Aborting upgrade."
312
		file_notice "UpgradeFailure" "Something went wrong when trying to update the fstab entry.  Aborting upgrade."
313 9974dae5 Scott Ullrich
		rm -f $IMG
314 7335c4cf Scott Ullrich
		rm -f /var/run/firmwarelock.dirty
315
		rm -f /var/run/firmware.lock
316 45c710da Scott Ullrich
		umount /tmp/$GLABEL_SLICE
317 bc43f0ee Scott Ullrich
		/etc/rc.conf_mount_ro
318 45c710da Scott Ullrich
		exit 1
319
	fi
320 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
321
	cat /tmp/$GLABEL_SLICE/etc/fstab >> /conf/upgrade_log.txt
322 5924c7f8 Scott Ullrich
323 53e85f50 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
324 af214f1d Scott Ullrich
	find /tmp/$GLABEL_SLICE >/conf/file_upgrade_log.txt
325 53e85f50 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
326
327 5924c7f8 Scott Ullrich
	# Unmount newly prepared slice
328 db7c7513 Scott Ullrich
	umount /tmp/$GLABEL_SLICE
329 7f38e934 Scott Ullrich
	
330
	sync
331 c2aff9da Scott Ullrich
332 45c710da Scott Ullrich
	# Set active mount slice in fdisk
333 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
334
	echo "gpart set -a active -i ${SLICE} ${BOOT_DRIVE}" >> /conf/upgrade_log.txt
335 0bc8c0b0 Scott Ullrich
	gpart set -a active -i ${SLICE} ${BOOT_DRIVE} >> /conf/upgrade_log.txt 2>&1
336 45c710da Scott Ullrich
337 7f38e934 Scott Ullrich
	sync
338
339 e213b6fc Scott Ullrich
	# Set active boot source - NanoBSD does not do this but otherwise we
340
	# end up with the wrong partition being active.
341 b4047760 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
342
	echo "/usr/sbin/boot0cfg -s ${SLICE} -v /dev/${BOOT_DRIVE}" >> /conf/upgrade_log.txt
343
	/usr/sbin/boot0cfg -s ${SLICE} -v /dev/${BOOT_DRIVE} >> /conf/upgrade_log.txt 2>&1
344 6b3e4734 Scott Ullrich
345 0bc8c0b0 Scott Ullrich
	# Disable foot shooting
346 32a6b30c Scott Ullrich
	sysctl kern.geom.debugflags=0
347
348 6b3e4734 Scott Ullrich
	# Grab a final look at fdisk
349 c5eb3a17 Scott Ullrich
	echo "" >> /conf/fdisk_upgrade_log.txt
350
	echo "Final upgrade fdisk/bsdlabel" >> /conf/fdisk_upgrade_log.txt
351
	fdisk $BOOT_DRIVE >> /conf/fdisk_upgrade_log.txt
352 e588bd54 jim-p
	bsdlabel -A ${BOOT_DRIVE}s1 >> /conf/fdisk_upgrade_log.txt
353
	bsdlabel -A ${BOOT_DRIVE}s2 >> /conf/fdisk_upgrade_log.txt
354
	bsdlabel -A ${BOOT_DRIVE}s3 >> /conf/fdisk_upgrade_log.txt
355 c5eb3a17 Scott Ullrich
	echo "---------------------------------------------------------------" >> /conf/fdisk_upgrade_log.txt
356
	echo "" >> /conf/fdisk_upgrade_log.txt
357 45c710da Scott Ullrich
358 5924c7f8 Scott Ullrich
	# Remove extra stuff
359 c2aff9da Scott Ullrich
	rm -rf /etc/rc.conf
360
	rm -rf /etc/motd
361
	rm -rf /usr/savecore/*
362
363 6b3e4734 Scott Ullrich
	date >> /conf/upgrade_log.txt
364
	echo "" >> /conf/upgrade_log.txt
365 db7c7513 Scott Ullrich
366 359d9ec9 Scott Ullrich
	# Trigger a package reinstallation on reobot
367 65c79f55 Scott Ullrich
	touch /conf/needs_package_sync
368 359d9ec9 Scott Ullrich
369 5924c7f8 Scott Ullrich
	# remount /cf ro
370
	/etc/rc.conf_mount_ro
371 c2aff9da Scott Ullrich
	/bin/sync
372 903f12d4 Scott Ullrich
373 d29d05ab Scott Ullrich
	echo "NanoBSD Firmware upgrade is complete.  Rebooting in 10 seconds."  >> /conf/upgrade_log.txt 2>&1
374
	echo "NanoBSD Firmware upgrade is complete.  Rebooting in 10 seconds." | wall
375 62fc138e Warren Baker
	/etc/rc.notify_message -e -g -m "NanoBSD Firmware upgrade is complete.  Rebooting in 10 seconds."
376 d29d05ab Scott Ullrich
377 d4b8f9a8 Scott Ullrich
	sleep 10
378
379 a368a026 Ermal Lu?i
	rm -f /var/run/firmwarelock.dirty
380 7335c4cf Scott Ullrich
	rm -f /var/run/firmware.lock
381 5423f1a0 Scott Ullrich
	sh /etc/rc.reboot
382 c2aff9da Scott Ullrich
383 84aa381e Scott Ullrich
	;;
384
pfSenseupgrade)
385 50ba07ea Scott Ullrich
386 84aa381e Scott Ullrich
	# Sanity check - bail early if there's no firmware file!
387
	if [ ! -r $IMG ]; then
388 6b3e4734 Scott Ullrich
		echo "2nd parameter has not been passed or file does not exist. Exiting." >> /conf/upgrade_log.txt 2>&1
389 50ba07ea Scott Ullrich
		/etc/rc.conf_mount_ro
390 84aa381e Scott Ullrich
		exit
391
	fi
392
393 6a581b52 Scott Ullrich
	# wait 1 seconds before beginning
394
	sleep 1
395
396 d2307ffb Scott Ullrich
	# Log that we are really doing a pfSense upgrade
397 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
398 efdf0b9c sullrich
	echo "pfSenseupgrade upgrade starting" >> /conf/upgrade_log.txt
399 6b3e4734 Scott Ullrich
	echo "" >> /conf/upgrade_log.txt
400 50ba07ea Scott Ullrich
401 a368a026 Ermal Lu?i
	touch /var/run/firmwarelock.dirty
402 6a581b52 Scott Ullrich
403 b2a67b55 Scott Ullrich
	if [ -f /tmp/perform_full_backup.txt ]; then
404
		echo "Performing full backup" >> /conf/upgrade_log.txt
405
		/etc/rc.create_full_backup
406
		rm /tmp/perform_full_backup.txt
407
	fi
408
409 6b3e4734 Scott Ullrich
	touch /conf/upgrade_log.txt
410
	echo "" >> /conf/upgrade_log.txt
411 79b9570c Scott Ullrich
412
	# Output environment information to log file
413
	output_env_to_log
414
415 84aa381e Scott Ullrich
	backup_chflags
416
	remove_chflags
417
418 4a8e802d Scott Ullrich
	# Do we have a pre-upgrade hook in the update file?
419
	if [ `tar tvzf $IMG | grep /tmp/pre_upgrade_command | wc -l` -gt 0 ]; then 
420 6b3e4734 Scott Ullrich
		tar xzvf $IMG -C / ./tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
421
		chmod a+rx /tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
422
		sh /tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
423 4a8e802d Scott Ullrich
	fi
424
425 6b3e4734 Scott Ullrich
	echo "Firmware upgrade in progress..."  >> /conf/upgrade_log.txt 2>&1
426 79b9570c Scott Ullrich
	echo "Firmware upgrade in progress..."  | wall
427 62fc138e Warren Baker
	/etc/rc.notify_message -e -g -m "Firmware upgrade in progress..."
428 476aa14b Scott Ullrich
	
429 84aa381e Scott Ullrich
	# backup config
430
	/bin/mkdir -p /tmp/configbak
431 ce09186c jim-p
	cp -Rp /conf/* /tmp/configbak 2>/dev/null
432 84aa381e Scott Ullrich
433 2cd85631 jim-p
	# Remove logs from backup dir to avoid clobbering upon restore.
434
	rm /tmp/configbak/*_log.txt 2>/dev/null
435
436 84aa381e Scott Ullrich
	# tar explode image onto hd
437 6b3e4734 Scott Ullrich
	echo "Installing $IMG." >> /conf/upgrade_log.txt 2>&1
438
	cd / && /usr/bin/tar xzUPf $IMG >> /conf/upgrade_log.txt 2>&1
439 84aa381e Scott Ullrich
	/usr/bin/find / -name CVS -exec rm -fr {} \;
440 6b3e4734 Scott Ullrich
	echo "Image installed $IMG." >> /conf/upgrade_log.txt 2>&1
441 84aa381e Scott Ullrich
442
    # process custom image if its passed
443
    if [ $# -eq 3 ]; then
444
	    if [ -f $CUSTOMIMG ]; then
445 6b3e4734 Scott Ullrich
	        echo "Custom image $CUSTOMIMG found." >> /conf/upgrade_log.txt 2>&1
446
	        echo "Custom image ($CUSTOMIMG) found." >> /conf/upgrade_log.txt 2>&1
447 84aa381e Scott Ullrich
	        PWD_DIR=`pwd`
448 6b3e4734 Scott Ullrich
	        cd / && /usr/bin/tar xzPUf $CUSTOMIMG >> /conf/upgrade_log.txt 2>&1
449 84aa381e Scott Ullrich
	        cd $PWD_DIR
450 6b3e4734 Scott Ullrich
	        echo "Custom image $CUSTOMIMG installed." >> /conf/upgrade_log.txt 2>&1
451 84aa381e Scott Ullrich
	    fi
452
    fi
453
454
	# restore config
455 ce09186c jim-p
	cp -Rp /tmp/configbak/* /conf 2>/dev/null
456 84aa381e Scott Ullrich
457
	# restore /etc symlinks
458
	rm /etc/hosts
459
	ln -s /var/etc/hosts /etc/hosts
460
461
	restore_chflags
462
463
	# Remove upgrade file
464
	rm -f $IMG
465
466 6e75ac00 Scott Ullrich
	if [ -e /etc/init_bootloader.sh ]; then
467 6b3e4734 Scott Ullrich
		sh /etc/init_bootloader.sh >> /conf/upgrade_log.txt 2>&1
468 6e75ac00 Scott Ullrich
	fi
469 84aa381e Scott Ullrich
470 147b2be1 Erik Fonnesbeck
	# Remove saved commit ID for gitsync
471
	rm -f /etc/version.gitsync
472
473 6e75ac00 Scott Ullrich
	# If /tmp/post_upgrade_command exists after update
474
	# then execute the command.
475
	if [ -f /tmp/post_upgrade_command ]; then
476 6b3e4734 Scott Ullrich
		sh /tmp/post_upgrade_command >> /conf/upgrade_log.txt 2>&1
477 6e75ac00 Scott Ullrich
	fi
478
479 79b9570c Scott Ullrich
	# remove unused files
480 84aa381e Scott Ullrich
	rm -rf /etc/rc.conf
481
	rm -rf /etc/motd
482
	rm -rf /usr/savecore/*
483 79b9570c Scott Ullrich
484 6b3e4734 Scott Ullrich
	date >> /conf/upgrade_log.txt
485
	echo "" >> /conf/upgrade_log.txt
486 db7c7513 Scott Ullrich
487 79b9570c Scott Ullrich
	# remount /cf ro
488 84aa381e Scott Ullrich
	/etc/rc.conf_mount_ro
489 6e75ac00 Scott Ullrich
490 79b9570c Scott Ullrich
	# release the firmware lock
491 a368a026 Ermal Lu?i
	rm -f /var/run/firmwarelock.dirty
492 7335c4cf Scott Ullrich
	rm -f /var/run/firmware.lock
493 84aa381e Scott Ullrich
	/bin/sync
494
495 d29d05ab Scott Ullrich
	echo "Firmware upgrade is complete. Rebooting in 10 seconds."  >> /conf/upgrade_log.txt 2>&1
496
	echo "Firmware upgrade is complete. Rebooting in 10 seconds."  | wall
497 62fc138e Warren Baker
	/etc/rc.notify_message -e -g -m "Firmware upgrade is complete. Rebooting in 10 seconds."
498 d29d05ab Scott Ullrich
499 d4b8f9a8 Scott Ullrich
	# Sleep and allow disks to catch up
500
	sleep 10
501
502 84aa381e Scott Ullrich
	# If the archive has unpacked a file called
503
	# /tmp/no_upgrade_reboot_required then do
504
	# not reboot after upgrade.
505
	if [ -f /tmp/no_upgrade_reboot_required ]; then
506
		rm /tmp/no_upgrade_reboot_required
507
	else
508
		sh /etc/rc.reboot
509
	fi
510 6e75ac00 Scott Ullrich
511 84aa381e Scott Ullrich
	;;
512
delta_update)
513 a368a026 Ermal Lu?i
	touch /var/run/firmwarelock.dirty
514 84aa381e Scott Ullrich
	backup_chflags
515
	remove_chflags
516
	binary_update $IMG
517
	restore_chflags
518
	rm -rf /etc/rc.conf
519
	rm -rf /etc/motd
520
	find / -name CVS -type d -exec rm {} \;
521
	rm -rf /usr/savecore/*
522
	/etc/rc.conf_mount_ro
523
	/sbin/umount -f /cf 2>/dev/null
524
	/sbin/mount -r /cf 2>/dev/null
525
	/sbin/umount -f / 2>/dev/null
526
	/sbin/mount -r / 2>/dev/null
527
	if [ -e /etc/init_bootloader.sh ]; then
528
		sh /etc/init_bootloader.sh
529
	fi
530
531
	;;
532
esac