Project

General

Profile

Download (14 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2

    
3
# /etc/rc.firmware
4
# originally part of m0n0wall (http://neon1.net/m0n0wall)
5
# Copyright (C) 2005-2009 Scott Ullrich <sullrich@pfsense.org>.
6
# Copyright (C) 2003 Manuel Kasper <mk@neon1.net>.
7
# All rights reserved.
8

    
9
# mount /cf
10
/etc/rc.conf_mount_rw
11

    
12
exec 3>&2 2>>/conf/firmware_update_misc.log
13

    
14
export ACTION=$1
15
export IMG=$2
16
if [ $# -eq 3 ]; then
17
	export CUSTOMIMG=$3
18
fi
19

    
20
if [ $ACTION != "upgrade" ]; then
21
	/sbin/umount -f /ftmp > /dev/null 2>&1
22
fi
23

    
24
file_notice() {
25
	/usr/local/bin/php -q -d auto_prepend_file=config.inc <<ENDOFF
26
	<?php
27
		require_once("globals.inc");		
28
		require_once("functions.inc");
29
		file_notice("$1", "$2", "$1", "");
30
	?>
31
ENDOFF
32
}
33

    
34
output_env_to_log() {
35
	date >> /conf/upgrade_log.txt
36
	echo "" >> /conf/upgrade_log.txt
37
	
38
	ls -lah /dev/ >> /conf/upgrade_log.txt
39
	echo "" >> /conf/upgrade_log.txt
40

    
41
	ls -lah $IMG >> /conf/upgrade_log.txt
42
	echo "" >> /conf/upgrade_log.txt
43

    
44
	md5 $IMG >> /conf/upgrade_log.txt
45
	echo "" >> /conf/upgrade_log.txt
46

    
47
	mount >> /conf/upgrade_log.txt
48
	echo "" >> /conf/upgrade_log.txt
49

    
50
	top >> /conf/upgrade_log.txt
51
	echo "" >> /conf/upgrade_log.txt
52
}
53

    
54
backup_chflags() {
55
	TOPROCESS="bin lib libexec sbin usr"
56
	for files in $TOPROCESS; do
57
		/usr/sbin/mtree -Pcp /${files} | bzip2 -9 > /tmp/chflags.dist.${files}.bz2 >> /conf/upgrade_log.txt 2>&1
58
	done
59
}
60

    
61
restore_chflags() {
62
	TOPROCESS="bin lib libexec sbin usr"
63
	for files in $TOPROCESS; do
64
		cd / && /usr/bin/bzcat /tmp/chflags.dist.${files}.bz2 | /usr/sbin/mtree -PU -p /${files} >> /conf/upgrade_log.txt 2>&1
65
	done
66
}
67

    
68
remove_chflags() {
69
	TOPROCESS="bin lib libexec sbin usr"
70
	for files in $TOPROCESS; do
71
		/bin/chflags -R noschg /${files}
72
		/bin/chmod -R u+rw /${files}
73
	done
74
}
75

    
76
binary_update() {
77
	TGZ=$1
78
	ERR_F="/tmp/bdiff.log"
79
	rm ${ERR_F} 2>/dev/null
80
	/bin/mkdir /tmp/patched /tmp/patches 2>>${ERR_F}
81
	backup_chflags
82
	remove_chflags
83
	cd /tmp/patches
84
	for i in `/usr/bin/tar tvzf $TGZ | egrep -v "(^d|_md5)" | nawk '{print $9;}'`;
85
	 do
86
	   FILE=`basename ${i}`
87
	   echo "Working on ${i}"
88
	   # Untar patch file and md5 files
89
	   /usr/bin/tar xzf ${TGZ} ${i} ${i}.old_file_md5 ${i}.new_patch_md5 ${i}.new_file_md5 2>>${ERR_F}
90

    
91
	   # Apply patch - oldfile newfile patchfile
92
	   /usr/local/bin/bspatch /${i} /tmp/patched/${FILE} /tmp/patches/${i} 2>>${ERR_F}
93

    
94
	   OLD_FILE_MD5=`cat /tmp/patches/${i}.old_file_md5 2>/dev/null`
95
	   NEW_PATCH_MD5=`cat /tmp/patches/${i}.new_patch_md5 2>/dev/null`
96
	   NEW_FILE_MD5=`cat /tmp/patches/${i}.new_file_md5 2>/dev/null`
97
	   PATCHED_MD5=`/sbin/md5 -q /tmp/patched/${FILE} 2>/dev/null`
98

    
99
	   if [ "$PATCHED_MD5" = "$NEW_PATCH_MD5" ]; then
100
		/usr/bin/install -S  /tmp/patched/${FILE} /${i}
101
	   else
102
		#echo "${i} file does not match intended final md5."
103
		echo "${i} file does not match intended final md5." >> ${ERR_F}
104
	   fi
105

    
106
	   /bin/rm /tmp/patched/${FILE} >> ${ERR_F}
107
	   /bin/rm /tmp/patches/${i} >> ${ERR_F}
108
	   /bin/rm /tmp/patches/${i}.* >> ${ERR_F}
109
	done
110
	/bin/rm -rf /tmp/patched /tmp/patches >> ${ERR_F}
111
	restore_chflags
112
}
113

    
114
case $ACTION in
115
enable)
116
	touch /conf/upgrade_log.txt
117
	echo "" > /conf/upgrade_log.txt
118
	;;
119
auto)
120
	touch /var/run/firmwarelock.dirty
121
	backup_chflags
122
	remove_chflags
123
	/etc/rc.firmware_auto
124
	restore_chflags
125
	;;
126
pfSenseNanoBSDupgrade)
127

    
128
	# Sanity check - bail early if there's no firmware file!
129
	if [ ! -r $IMG ]; then
130
		echo "2nd parameter has not been passed or file does not exist. Exiting." >> /conf/upgrade_log.txt 2>&1
131
		/etc/rc.conf_mount_ro
132
		exit 1
133
	fi
134

    
135
	# Prevent full upgrade file from being used to upgrade
136
	if [ `echo $IMG | grep "full"` ]; then
137
		echo "You cannot use a full file for upgrade.  Please use a file labeled upgrade."
138
		file_notice "NanoBSDUpgradeFailure" "You have attemped to use a full NanoBSD installation file as an upgrade.  Please use a NanoBSD file labeled 'upgrade' instead."
139
		/etc/rc.conf_mount_ro		
140
		exit 1
141
	fi
142

    
143
	touch /var/run/firmwarelock.dirty
144

    
145
	echo "NanoBSD Firmware upgrade in progress..."  >> /conf/upgrade_log.txt 2>&1
146
	echo "NanoBSD Firmware upgrade in progress..." | wall
147

    
148
	# backup config
149
	/bin/mkdir -p /tmp/configbak
150
	cp -p /conf/* /tmp/configbak 2>/dev/null
151

    
152
	touch /conf/upgrade_log.txt
153
	echo "" > /conf/upgrade_log.txt
154

    
155
	echo "Installing $IMG." >> /conf/upgrade_log.txt 2>&1
156
	echo "Installing $IMG." >> /conf/upgrade_log.txt
157

    
158
	# resolve glabel label that we booted from
159
	BOOT_DEVICE=`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`
160
	# resolve glabel to the real boot dev entry
161
	REAL_BOOT_DEVICE=`/sbin/glabel list | /usr/bin/grep -B2 ufs/${BOOT_DEVICE} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`
162
	# grab the boot device, example ad1, ad0
163
	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`
164
	# test the slice.  if we are on slice 1 we need to flash 2 and vica versa
165
	if [ `echo $REAL_BOOT_DEVICE | /usr/bin/grep "s1"` ]; then 
166
		SLICE="2"
167
		OLDSLICE="1"
168
		TOFLASH="${BOOT_DRIVE}s${SLICE}"
169
		COMPLETE_PATH="${BOOT_DRIVE}s${SLICE}a"
170
		GLABEL_SLICE="pfsense1"
171
		UFS_ID="1"
172
		OLD_UFS_ID="0"
173
	else
174
		SLICE="1"
175
		OLDSLICE="2"		
176
		TOFLASH="${BOOT_DRIVE}s${SLICE}"
177
		COMPLETE_PATH="${BOOT_DRIVE}s${SLICE}a"
178
		GLABEL_SLICE="pfsense0"
179
		UFS_ID="0"
180
		OLD_UFS_ID="1"
181
	fi
182

    
183
	# Output specifc information that this script is using
184
	echo "SLICE $SLICE" >> /conf/upgrade_log.txt
185
	echo "OLDSLICE $OLDSLICE" >> /conf/upgrade_log.txt
186
	echo "TOFLASH $TOFLASH" >> /conf/upgrade_log.txt
187
	echo "COMPLETE_PATH $COMPLETE_PATH" >> /conf/upgrade_log.txt
188
	echo "GLABEL_SLICE $GLABEL_SLICE" >> /conf/upgrade_log.txt
189

    
190
	# Output environment information to log file
191
	output_env_to_log
192
	
193
	# Grab a before upgrade look at fdisk
194
	echo "" >> /conf/upgrade_log.txt
195
	fdisk $BOOT_DRIVE >> /conf/upgrade_log.txt
196
	fdisk $BOOT_DRIVEs1 >> /conf/upgrade_log.txt
197
	fdisk $BOOT_DRIVEs1a >> /conf/upgrade_log.txt
198
	fdisk $BOOT_DRIVEs2 >> /conf/upgrade_log.txt
199
	fdisk $BOOT_DRIVEs2a >> /conf/upgrade_log.txt		
200
	fdisk $BOOT_DRIVEs3 >> /conf/upgrade_log.txt
201
	bsdlabel -A $BOOT_DRIVEs1 >> /conf/upgrade_log.txt
202
	bsdlabel -A $BOOT_DRIVEs2 >> /conf/upgrade_log.txt
203
	bsdlabel -A $BOOT_DRIVEs3 >> /conf/upgrade_log.txt
204
	echo "" >> /conf/upgrade_log.txt
205
	
206
	# Log that we are really doing a NanoBSD upgrade
207
	echo "" >> /conf/upgrade_log.txt
208
	echo "NanoBSD upgrade starting" >> /conf/upgrade_log.txt
209
	echo "" >> /conf/upgrade_log.txt
210
	
211
	# Foot shooting is fun!
212
	echo "" >> /conf/upgrade_log.txt
213
	echo "/sbin/sysctl kern.geom.debugflags=16" >> /conf/upgrade_log.txt
214
	/sbin/sysctl kern.geom.debugflags=16 >> /conf/upgrade_log.txt 2>&1
215

    
216
	# Remove TOFLASH and get ready for new flash image
217
	echo "" >> /conf/upgrade_log.txt
218
	echo "dd if=/dev/zero of=/dev/${TOFLASH} bs=1m count=1" >> /conf/upgrade_log.txt	
219
	dd if=/dev/zero of=/dev/${TOFLASH} bs=1m count=1 >> /conf/upgrade_log.txt 2>&1
220

    
221
	# Stream gzipped image to dd and explode image to new area
222
	echo "" >> /conf/upgrade_log.txt
223
	echo "/usr/bin/gzcat $IMG | /bin/dd of=/dev/${TOFLASH} bs=16k" >> /conf/upgrade_log.txt
224
	/usr/bin/gzcat $IMG | /bin/dd of=/dev/${TOFLASH} bs=16k >> /conf/upgrade_log.txt 2>&1
225

    
226
	# Grab a after upgrade look at fdisk
227
	echo "" >> /conf/upgrade_log.txt
228
	fdisk $BOOT_DRIVE >> /conf/upgrade_log.txt
229
	fdisk $BOOT_DRIVEs1 >> /conf/upgrade_log.txt
230
	fdisk $BOOT_DRIVEs1a >> /conf/upgrade_log.txt
231
	fdisk $BOOT_DRIVEs2 >> /conf/upgrade_log.txt
232
	fdisk $BOOT_DRIVEs2a >> /conf/upgrade_log.txt		
233
	fdisk $BOOT_DRIVEs3 >> /conf/upgrade_log.txt
234
	bsdlabel -A $BOOT_DRIVEs1 >> /conf/upgrade_log.txt
235
	bsdlabel -A $BOOT_DRIVEs2 >> /conf/upgrade_log.txt
236
	bsdlabel -A $BOOT_DRIVEs3 >> /conf/upgrade_log.txt
237
	echo "" >> /conf/upgrade_log.txt
238
	
239
	# Ensure that our new system is sound and bail if it is not and file a notice
240
	echo "" >> /conf/upgrade_log.txt
241
	echo "/sbin/fsck_ffs -y /dev/$COMPLETE_PATH" >> /conf/upgrade_log.txt
242
	/sbin/fsck_ffs -y /dev/$COMPLETE_PATH >> /conf/upgrade_log.txt 2>&1
243
	if [ $? != 0 ]; then
244
		file_notice "UpgradeFailure" "{\$g['product_name']} upgrade has failed.   Your system has been left in a usable state."
245
		rm /var/run/firmwarelock.dirty
246
		/etc/rc.conf_mount_ro		
247
		exit 1
248
	fi
249

    
250
	# Add back the corresponding glabel
251
	echo "" >> /conf/upgrade_log.txt
252
	echo "/sbin/tunefs -L ${GLABEL_SLICE} /dev/$COMPLETE_PATH" >> /conf/upgrade_log.txt
253
	/sbin/tunefs -L ${GLABEL_SLICE} /dev/$COMPLETE_PATH >> /conf/upgrade_log.txt 2>&1
254

    
255
	# restore config
256
	cp -p /tmp/configbak/* /conf 2>/dev/null
257

    
258
	# Remove upgrade file
259
	rm -f $IMG
260

    
261
	# Mount newly prepared slice
262
	mkdir /tmp/$GLABEL_SLICE
263
	mount /dev/ufs/$GLABEL_SLICE /tmp/$GLABEL_SLICE
264

    
265
	# If /tmp/$GLABEL_SLICE/tmp/post_upgrade_command exists 
266
	# after update then execute the command.
267
	if [ -f /tmp/$GLABEL_SLICE/tmp/post_upgrade_command ]; then
268
		sh /tmp/$GLABEL_SLICE/tmp/post_upgrade_command >> /conf/upgrade_log.txt 2>&1
269
	fi
270

    
271
	# Update fstab
272
	cp /etc/fstab /tmp/$GLABEL_SLICE/etc/fstab
273
	sed -i "" "s/pfsense${OLD_UFS_ID}/pfsense${UFS_ID}/g" /tmp/$GLABEL_SLICE/etc/fstab
274
	if [ $? != 0 ]; then
275
		echo "Something went wrong when trying to update the fstab entry.  Aborting upgrade."
276
		file_notice "UpgradeFailure" "Something went wrong when trying to update the fstab entry.  Aborting upgrade."
277
		rm /var/run/firmwarelock.dirty
278
		umount /tmp/$GLABEL_SLICE
279
		/etc/rc.conf_mount_ro
280
		exit 1
281
	fi
282
	echo "" >> /conf/upgrade_log.txt
283
	cat /tmp/$GLABEL_SLICE/etc/fstab >> /conf/upgrade_log.txt
284

    
285
	# Unmount newly prepared slice
286
	umount /tmp/$GLABEL_SLICE
287

    
288
	# Set active mount slice in fdisk
289
	echo "" >> /conf/upgrade_log.txt
290
	echo "gpart set -a active -i ${SLICE} ${BOOT_DRIVE}" >> /conf/upgrade_log.txt
291
	gpart set -a active -i ${SLICE} ${BOOT_DRIVE}
292

    
293
	# Set active boot source
294
	echo "" >> /conf/upgrade_log.txt
295
	echo "/usr/sbin/boot0cfg -s ${SLICE} -v /dev/${BOOT_DRIVE}" >> /conf/upgrade_log.txt
296
	/usr/sbin/boot0cfg -s ${SLICE} -v /dev/${BOOT_DRIVE} >> /conf/upgrade_log.txt 2>&1
297

    
298
	# Grab a final look at fdisk
299
	echo "" >> /conf/upgrade_log.txt
300
	fdisk $BOOT_DRIVE >> /conf/upgrade_log.txt
301
	fdisk $BOOT_DRIVEs1 >> /conf/upgrade_log.txt
302
	fdisk $BOOT_DRIVEs1a >> /conf/upgrade_log.txt
303
	fdisk $BOOT_DRIVEs2 >> /conf/upgrade_log.txt
304
	fdisk $BOOT_DRIVEs2a >> /conf/upgrade_log.txt		
305
	fdisk $BOOT_DRIVEs3 >> /conf/upgrade_log.txt
306
	bsdlabel -A $BOOT_DRIVEs1 >> /conf/upgrade_log.txt
307
	bsdlabel -A $BOOT_DRIVEs2 >> /conf/upgrade_log.txt
308
	bsdlabel -A $BOOT_DRIVEs3 >> /conf/upgrade_log.txt
309
	echo "" >> /conf/upgrade_log.txt
310

    
311
	# Remove extra stuff
312
	rm -rf /etc/rc.conf
313
	rm -rf /etc/motd
314
	rm -rf /usr/savecore/*
315

    
316
	date >> /conf/upgrade_log.txt
317
	echo "" >> /conf/upgrade_log.txt
318

    
319
	# Trigger a package reinstallation on reobot
320
	touch /conf/needs_package_sync
321

    
322
	# Enough fun for now.
323
	echo "" >> /conf/upgrade_log.txt
324
	echo "/sbin/sysctl kern.geom.debugflags=0" >> /conf/upgrade_log.txt
325
	/sbin/sysctl kern.geom.debugflags=0 >> /conf/upgrade_log.txt 2>&1
326

    
327
	# remount /cf ro
328
	/etc/rc.conf_mount_ro
329
	/bin/sync
330

    
331
	sleep 10
332

    
333
	rm -f /var/run/firmwarelock.dirty
334
	sh /etc/rc.reboot
335

    
336
	;;
337
pfSenseupgrade)
338

    
339
	# Sanity check - bail early if there's no firmware file!
340
	if [ ! -r $IMG ]; then
341
		echo "2nd parameter has not been passed or file does not exist. Exiting." >> /conf/upgrade_log.txt 2>&1
342
		/etc/rc.conf_mount_ro
343
		exit
344
	fi
345

    
346
	# wait 1 seconds before beginning
347
	sleep 1
348

    
349
	# Log that we are really doing a NanoBSD upgrade
350
	echo "" >> /conf/upgrade_log.txt
351
	echo "NanoBSD upgrade starting" >> /conf/upgrade_log.txt
352
	echo "" >> /conf/upgrade_log.txt
353

    
354
	touch /var/run/firmwarelock.dirty
355

    
356
	touch /conf/upgrade_log.txt
357
	echo "" >> /conf/upgrade_log.txt
358

    
359
	# Output environment information to log file
360
	output_env_to_log
361

    
362
	backup_chflags
363
	remove_chflags
364

    
365
	# Do we have a pre-upgrade hook in the update file?
366
	if [ `tar tvzf $IMG | grep /tmp/pre_upgrade_command | wc -l` -gt 0 ]; then 
367
		tar xzvf $IMG -C / ./tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
368
		chmod a+rx /tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
369
		sh /tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
370
	fi
371

    
372
	echo "Firmware upgrade in progress..."  >> /conf/upgrade_log.txt 2>&1
373
	echo "Firmware upgrade in progress..."  | wall
374

    
375
	# backup config
376
	/bin/mkdir -p /tmp/configbak
377
	cp -p /conf/* /tmp/configbak 2>/dev/null
378

    
379
	# tar explode image onto hd
380
	echo "Installing $IMG." >> /conf/upgrade_log.txt 2>&1
381
	cd / && /usr/bin/tar xzUPf $IMG >> /conf/upgrade_log.txt 2>&1
382
	/usr/bin/find / -name CVS -exec rm -fr {} \;
383
	echo "Image installed $IMG." >> /conf/upgrade_log.txt 2>&1
384

    
385
    # process custom image if its passed
386
    if [ $# -eq 3 ]; then
387
	    if [ -f $CUSTOMIMG ]; then
388
	        echo "Custom image $CUSTOMIMG found." >> /conf/upgrade_log.txt 2>&1
389
	        echo "Custom image ($CUSTOMIMG) found." >> /conf/upgrade_log.txt 2>&1
390
	        PWD_DIR=`pwd`
391
	        cd / && /usr/bin/tar xzPUf $CUSTOMIMG >> /conf/upgrade_log.txt 2>&1
392
	        cd $PWD_DIR
393
	        echo "Custom image $CUSTOMIMG installed." >> /conf/upgrade_log.txt 2>&1
394
	    fi
395
    fi
396

    
397
	# restore config
398
	cp -p /tmp/configbak/* /conf 2>/dev/null
399

    
400
	# restore /etc symlinks
401
	rm /etc/hosts
402
	ln -s /var/etc/hosts /etc/hosts
403

    
404
	restore_chflags
405

    
406
	# Remove upgrade file
407
	rm -f $IMG
408

    
409
	if [ -e /etc/init_bootloader.sh ]; then
410
		sh /etc/init_bootloader.sh >> /conf/upgrade_log.txt 2>&1
411
	fi
412

    
413
	# If /tmp/post_upgrade_command exists after update
414
	# then execute the command.
415
	if [ -f /tmp/post_upgrade_command ]; then
416
		sh /tmp/post_upgrade_command >> /conf/upgrade_log.txt 2>&1
417
	fi
418

    
419
	# remove unused files
420
	rm -rf /etc/rc.conf
421
	rm -rf /etc/motd
422
	rm -rf /usr/savecore/*
423

    
424
	date >> /conf/upgrade_log.txt
425
	echo "" >> /conf/upgrade_log.txt
426

    
427
	# remount /cf ro
428
	/etc/rc.conf_mount_ro
429

    
430
	# release the firmware lock
431
	rm -f /var/run/firmwarelock.dirty
432
	/bin/sync
433

    
434
	# Sleep and allow disks to catch up
435
	sleep 10
436

    
437
	# If the archive has unpacked a file called
438
	# /tmp/no_upgrade_reboot_required then do
439
	# not reboot after upgrade.
440
	if [ -f /tmp/no_upgrade_reboot_required ]; then
441
		rm /tmp/no_upgrade_reboot_required
442
	else
443
		rm -f /var/run/config.lock
444
		sh /etc/rc.reboot
445
	fi
446

    
447
	;;
448
delta_update)
449
	touch /var/run/firmwarelock.dirty
450
	backup_chflags
451
	remove_chflags
452
	binary_update $IMG
453
	restore_chflags
454
	rm -rf /etc/rc.conf
455
	rm -rf /etc/motd
456
	find / -name CVS -type d -exec rm {} \;
457
	rm -rf /usr/savecore/*
458
	/etc/rc.conf_mount_ro
459
	/sbin/umount -f /cf 2>/dev/null
460
	/sbin/mount -r /cf 2>/dev/null
461
	/sbin/umount -f / 2>/dev/null
462
	/sbin/mount -r / 2>/dev/null
463
	if [ -e /etc/init_bootloader.sh ]; then
464
		sh /etc/init_bootloader.sh
465
	fi
466

    
467
	;;
468
esac
469

    
470

    
(44-44/87)