rc.firmware 5.57 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3 4 5 6 7 8
#!/bin/sh

# originally part of m0n0wall (http://neon1.net/m0n0wall)
# Copyright (C) 2005-2009 Scott Ullrich <sullrich@pfsense.org>.
# Copyright (C) 2003 Manuel Kasper <mk@neon1.net>.
# All rights reserved.

# mount /cf
9
/usr/local/etc/rc.conf_mount_rw
Ad Schellevis's avatar
Ad Schellevis committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

# Reset file(s)
echo "" >/conf/upgrade_log.txt
echo "" >/conf/firmware_update_misc_log.txt
echo "" >/conf/fdisk_upgrade_log.txt

exec 3>&2 2>>/conf/firmware_update_misc_log.txt

export ACTION=$1
export IMG=$2
if [ $# -eq 3 ]; then
	export CUSTOMIMG=$3
fi

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

file_notice() {
	/usr/local/bin/php -q -d auto_prepend_file=config.inc <<ENDOFF
	<?php
31
		require_once("globals.inc");
Ad Schellevis's avatar
Ad Schellevis committed
32 33 34 35 36 37 38 39 40
		require_once("functions.inc");
		file_notice("$1", "$2", "$1", "");
	?>
ENDOFF
}

output_env_to_log() {
	date >> /conf/upgrade_log.txt
	echo "" >> /conf/upgrade_log.txt
41

Ad Schellevis's avatar
Ad Schellevis committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	ls -lah /dev/ >> /conf/upgrade_log.txt
	echo "" >> /conf/upgrade_log.txt

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

	md5 $IMG >> /conf/upgrade_log.txt
	echo "" >> /conf/upgrade_log.txt

	mount >> /conf/upgrade_log.txt
	echo "" >> /conf/upgrade_log.txt

	top >> /conf/upgrade_log.txt
	echo "" >> /conf/upgrade_log.txt
}

backup_chflags() {
	TOPROCESS="bin lib libexec sbin usr"
	for files in $TOPROCESS; do
61
		/usr/sbin/mtree -Pcp /${files} | bzip2 -9 > /tmp/chflags.dist.${files}.bz2 2>> /conf/upgrade_log.txt
Ad Schellevis's avatar
Ad Schellevis committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	done
}

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

remove_chflags() {
	TOPROCESS="bin lib libexec sbin usr"
	for files in $TOPROCESS; do
		/bin/chflags -R noschg /${files}
		/bin/chmod -R u+rw /${files}
	done
}

case $ACTION in
pfSenseupgrade)

	# Sanity check - bail early if there's no firmware file!
	if [ ! -r $IMG ]; then
		echo "2nd parameter has not been passed or file does not exist. Exiting." >> /conf/upgrade_log.txt 2>&1
86
		/usr/local/etc/rc.conf_mount_ro
Ad Schellevis's avatar
Ad Schellevis committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		exit
	fi

	# wait 1 seconds before beginning
	sleep 1

	# Log that we are really doing a pfSense upgrade
	echo "" >> /conf/upgrade_log.txt
	echo "pfSenseupgrade upgrade starting" >> /conf/upgrade_log.txt
	echo "" >> /conf/upgrade_log.txt

	touch /var/run/firmwarelock.dirty

	if [ -f /tmp/perform_full_backup.txt ]; then
		echo "Performing full backup" >> /conf/upgrade_log.txt
102
		/usr/local/etc/rc.create_full_backup
Ad Schellevis's avatar
Ad Schellevis committed
103 104 105 106 107 108 109 110 111 112 113 114 115
		rm /tmp/perform_full_backup.txt
	fi

	touch /conf/upgrade_log.txt
	echo "" >> /conf/upgrade_log.txt

	# Output environment information to log file
	output_env_to_log

	backup_chflags
	remove_chflags

	# Do we have a pre-upgrade hook in the update file?
116
	if [ `tar tvzf $IMG | grep /tmp/pre_upgrade_command | wc -l` -gt 0 ]; then
Ad Schellevis's avatar
Ad Schellevis committed
117 118 119 120 121 122 123
		tar xzvf $IMG -C / ./tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
		chmod a+rx /tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
		sh /tmp/pre_upgrade_command >> /conf/upgrade_log.txt 2>&1
	fi

	echo "Firmware upgrade in progress..."  >> /conf/upgrade_log.txt 2>&1
	echo "Firmware upgrade in progress..."  | wall
Franco Fichtner's avatar
Franco Fichtner committed
124
	/usr/local/etc/rc.notify_message -e -g -m "Firmware upgrade in progress..."
125

Ad Schellevis's avatar
Ad Schellevis committed
126 127 128 129 130 131 132 133 134
	# backup config
	[ -d /tmp/configbak ] && rm -rf /tmp/configbak
	/bin/mkdir -p /tmp/configbak
	cp -Rp /conf/* /tmp/configbak 2>/dev/null

	# Remove logs from backup dir to avoid clobbering upon restore.
	rm /tmp/configbak/*_log.txt 2>/dev/null

	# tar explode image onto hd
135
	ps -a | grep "configd" | awk '{print $1;}' | kill -9
Ad Schellevis's avatar
Ad Schellevis committed
136 137
	echo "Installing $IMG." >> /conf/upgrade_log.txt 2>&1
	cd / && /usr/bin/tar --exclude=./dev -xzUPf $IMG >> /conf/upgrade_log.txt 2>&1
138
	/usr/local/opnsense/service/configd.py
Ad Schellevis's avatar
Ad Schellevis committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	echo "Image installed $IMG." >> /conf/upgrade_log.txt 2>&1

    # process custom image if its passed
    if [ $# -eq 3 ]; then
	    if [ -f $CUSTOMIMG ]; then
	        echo "Custom image $CUSTOMIMG found." >> /conf/upgrade_log.txt 2>&1
	        echo "Custom image ($CUSTOMIMG) found." >> /conf/upgrade_log.txt 2>&1
	        PWD_DIR=`pwd`
	        cd / && /usr/bin/tar xzPUf $CUSTOMIMG >> /conf/upgrade_log.txt 2>&1
	        cd $PWD_DIR
	        echo "Custom image $CUSTOMIMG installed." >> /conf/upgrade_log.txt 2>&1
	    fi
    fi

	# restore config
	cp -Rp /tmp/configbak/* /conf 2>/dev/null

	# restore /etc symlinks
	rm /etc/hosts
	ln -s /var/etc/hosts /etc/hosts

	restore_chflags

	# Remove upgrade file
	rm -f $IMG

	if [ -e /etc/init_bootloader.sh ]; then
		if [ ! -x /etc/init_bootloader.sh ]; then
			chmod ug+x /etc/init_bootloader.sh
		fi
		/etc/init_bootloader.sh >> /conf/upgrade_log.txt 2>&1
	fi

	# If /tmp/post_upgrade_command exists after update
	# then execute the command.
	if [ -f /tmp/post_upgrade_command ]; then
		if [ ! -x /tmp/post_upgrade_command ]; then
			chmod ug+x /tmp/post_upgrade_command
		fi
		/tmp/post_upgrade_command >> /conf/upgrade_log.txt 2>&1
	fi

	date >> /conf/upgrade_log.txt
	echo "" >> /conf/upgrade_log.txt

	# remount /cf ro
185
	/usr/local/etc/rc.conf_mount_ro
Ad Schellevis's avatar
Ad Schellevis committed
186 187 188 189 190 191 192 193

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

	echo "Firmware upgrade is complete. Rebooting in 10 seconds."  >> /conf/upgrade_log.txt 2>&1
	echo "Firmware upgrade is complete. Rebooting in 10 seconds."  | wall
Franco Fichtner's avatar
Franco Fichtner committed
194
	/usr/local/etc/rc.notify_message -e -g -m "Firmware upgrade is complete. Rebooting in 10 seconds."
Ad Schellevis's avatar
Ad Schellevis committed
195 196 197 198 199 200 201 202 203 204

	# Sleep and allow disks to catch up
	sleep 10

	# If the archive has unpacked a file called
	# /tmp/no_upgrade_reboot_required then do
	# not reboot after upgrade.
	if [ -f /tmp/no_upgrade_reboot_required ]; then
		rm /tmp/no_upgrade_reboot_required
	else
Franco Fichtner's avatar
Franco Fichtner committed
205
		. /usr/local/etc/rc.reboot
Ad Schellevis's avatar
Ad Schellevis committed
206 207 208 209
	fi

	;;
esac