summaryrefslogtreecommitdiffstats
path: root/src/usr/local/sbin/pfsense-upgrade.sh
blob: 60814428829559c3e8d667c4d38b8bec84c9e183 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/sh

# Copyright (c) 2004-2015 Electric Sheep Fencing, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgment:
#    "This product includes software developed by the pfSense Project
#    for use in the pfSense® software distribution. (http://www.pfsense.org/).
#
# 4. The names "pfSense" and "pfSense Project" must not be used to
#    endorse or promote products derived from this software without
#    prior written permission. For written permission, please contact
#    coreteam@pfsense.org.
#
# 5. Products derived from this software may not be called "pfSense"
#    nor may "pfSense" appear in their names without prior written
#    permission of the Electric Sheep Fencing, LLC.
#
# 6. Redistributions of any form whatsoever must retain the following
#    acknowledgment:
#
# "This product includes software developed by the pfSense Project
# for use in the pfSense software distribution (http://www.pfsense.org/).
#
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.

# pkg should not ask for confirmations
export ASSUME_ALWAYS_YES=true

# Firmware lock subsystem
firmwarelock=/var/run/firmwarelock.dirty

# File used to detect second call, after kernel update and reboot
upgrade_in_progress="/cf/conf/upgrade_in_progress"

if [ -f "${firmwarelock}" ]; then
	echo "ERROR: Another upgrade is running... aborting."
	exit 0
fi

stdout='/dev/null'
unset yes
while getopts dys opt; do
	case ${opt} in
		d)
			stdout=''
			;;
		y)
			yes=1
			;;
		*)
			usage
			exit 1
			;;
	esac
done

usage() {
	echo "Usage: $(basename ${0}) [-d] [-y] [-c]"
}

_exec() {
	local _cmd="${1}"
	local _msg="${2}"
	local _mute="${3}"
	local _ignore_result="${4}"
	local _stdout="${stdout}"

	if [ -z "${_cmd}" -o -z "${_msg}" ]; then
		return 1
	fi

	if [ "${_mute}" != "mute" ]; then
		_stdout=''
	fi

	echo -n ">>> ${_msg}... "
	if [ -z "${_stdout}" ]; then
		echo ""
		${_cmd} 2>&1
	else
		${_cmd} >${_stdout} 2>&1
	fi
	local _result=$?

	if [ ${_result} -eq 0 -o -n "${_ignore_result}" ]; then
		[ -n "${_stdout}" ] \
			&& echo "done."
		return 0
	else
		[ -n "${_stdout}" ] \
			&& echo "failed."
		return 1
	fi
}

_exit() {
	if [ -n "${kernel_pkg}" ]; then
		_exec "pkg lock ${kernel_pkg}" "Locking kernel package" mute ignore_result
	fi
	if [ -f "${firmwarelock}" ]; then
		rm -f ${firmwarelock}
	fi
}

first_step() {
	_exec "pkg update" "Updating repositories" mute ignore_result

	# figure out which kernel variant is running
	kernel_pkg=$(pkg query %n $(pkg info pfSense-kernel-\*))

	if [ -z "${kernel_pkg}" ]; then
		echo "ERROR: It was not possible to identify which pfSense kernel is installed"
		exit 1
	fi

	kernel_local=$(pkg query %v ${kernel_pkg})

	if [ -z "${kernel_local}" ]; then
		echo "ERROR: It was not possible to determine pfSense kernel local version"
		exit 1
	fi

	kernel_remote=$(pkg rquery %v ${kernel_pkg})

	if [ -z "${kernel_remote}" ]; then
		echo "ERROR: It was not possible to determine pfSense kernel remote version"
		exit 1
	fi

	kernel_version_compare=$(pkg version -t ${kernel_local} ${kernel_remote})

	if [ "${kernel_version_compare}" = "<" ]; then
		kernel_update=1
		# Make sure we lock kernel package again
		trap _exit 1 2 15 EXIT
		_exec "pkg unlock ${kernel_pkg}" "Unlocking kernel package" mute ignore_result
	elif [ "${kernel_version_compare}" = "=" ]; then
		kernel_update=0
	elif [ "${kernel_version_compare}" = ">" ]; then
		echo "ERROR: You are using a newer kernel version than remote repository"
		exit 1
	else
		echo "ERROR: Error comparing pfSense kernel local and remote versions"
		exit 1
	fi

	# XXX find a samrter way to do it
	l=$(pkg upgrade -Unq | wc -l)
	if [ ${l} -eq 1 ]; then
		echo "Your packages are up to date"
		exit 0
	fi

	if [ -z "${yes}" ]; then
		# Show user which packages are going to be upgraded
		pkg upgrade -Unq

		echo ""
		if [ ${kernel_update} -eq 1 ]; then
			echo "**** WARNING ****"
			echo "Reboot will be required!!"
		fi
		echo -n "Proceed with upgrade? (y/N) "
		read answer
		if [ "${answer}" != "y" ]; then
			echo "Aborting..."
			exit 0
		fi
	fi

	echo ">>> Downloading packages..."
	if ! pkg upgrade -UF; then
		echo "ERROR: It was not possible to download packages"
		exit 1
	fi

	# Mark firmware subsystem dirty
	trap _exit 1 2 15 EXIT
	touch ${firmwarelock}

	# First upgrade kernel and reboot
	if [ ${kernel_update} -eq 1 ]; then
		_exec "pkg upgrade -U ${kernel_pkg}" "Upgrading pfSense krenel"
		touch ${upgrade_in_progress}
		echo "Rebooting..."
		reboot
	fi
}

second_step() {
	echo "Upgrading necessary packages..."
	if ! pkg upgrade -U; then
		echo "ERROR: An error occurred when upgrade was running..."
		exit 1
	fi

	_exec "pkg autoremove" "Removing unnecessary packages" mute ignore_result
	_exec "pkg clean" "Cleanup pkg cache" mute ignore_result

	# cleanup caches

	rm -f ${upgrade_in_progress}
	rm -f ${firmwarelock}
}

unset need_reboot
if [ ! -f "${upgrade_in_progress}" ]; then
	first_step
else
	need_reboot=1
fi

second_step

if [ -n "${need_reboot}" ]; then
	echo "Rebooting..."
	reboot
fi

exit 0
OpenPOWER on IntegriCloud