Project

General

Profile

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

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

    
49
usage() {
50
	echo "Usage: $(basename ${0}) [-46bdyf] [-u|-i PKG_NAME|-r PKG_NAME]" >&2
51
	echo "	-4          - Force IPv4"
52
	echo "	-6          - Force IPv6"
53
	echo "	-b          - Platform is booting" >&2
54
	echo "	-c          - Check if upgrade is necessary" >&2
55
	echo "	-d          - Turn on debug" >&2
56
	echo "	-f          - Force package installation" >&2
57
	echo "	-h          - Show this usage help" >&2
58
	echo "	-l          - Logfile path (defaults to /cf/conf/upgrade_log.txt)" >&2
59
	echo "	-n          - Dry run" >&2
60
	echo "	-p socket   - Write pkg progress to socket"
61
	echo "	-R          - Do not reboot (this can be dangerous)"
62
	echo "	-y          - Assume yes as the answer to any possible interaction" >&2
63
	echo "" >&2
64
	echo "The following parameters are mutually exclusive:" >&2
65
	echo "	-i PKG_NAME - Install package PKG_NAME" >&2
66
	echo "	-r PKG_NAME - Remove package PKG_NAME" >&2
67
	echo "	-u          - Update repository information" >&2
68
}
69

    
70
_echo() {
71
	local _n=""
72
	if [ "${1}" = "-n" ]; then
73
		shift
74
		_n="-n"
75
	fi
76

    
77
	if [ -z "${logfile}" ]; then
78
		logfile=/dev/null
79
	fi
80

    
81
	echo ${_n} "${1}" | tee -a ${logfile}
82
}
83

    
84
_exec() {
85
	local _cmd="${1}"
86
	local _msg="${2}"
87
	local _mute="${3}"
88
	local _ignore_result="${4}"
89
	local _stdout="${stdout}"
90

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

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

    
99
	_echo -n ">>> ${_msg}... "
100
	if [ -z "${_stdout}" ]; then
101
		_echo ""
102
		# Ref. http://stackoverflow.com/questions/1221833/bash-pipe-output-and-capture-exit-status
103
		exec 4>&1
104
		local _result=$({ { ${_cmd} 2>&1 3>&-; printf $? 1>&3; } 4>&- | \
105
			tee -a ${logfile} 1>&4; } 3>&1)
106
		exec 4>&-
107
	else
108
		# Ref. http://stackoverflow.com/questions/1221833/bash-pipe-output-and-capture-exit-status
109
		exec 4>&1
110
		local _result=$({ { ${_cmd} >${_stdout} 2>&1 3>&-; printf $? 1>&3; } 4>&- | \
111
			tee -a ${logfile} 1>&4; } 3>&1)
112
		exec 4>&-
113
	fi
114

    
115
	if [ ${_result} -eq 0 -o -n "${_ignore_result}" ]; then
116
		[ -n "${_stdout}" ] \
117
			&& _echo "done."
118
		return 0
119
	else
120
		[ -n "${_stdout}" ] \
121
			&& _echo "failed."
122
		_exit 1
123
	fi
124
}
125

    
126
_exit() {
127
	trap "-" 1 2 15 EXIT
128

    
129
	pkg_lock ${kernel_pkg}
130

    
131
	if [ -f "${pid_file}" ]; then
132
		rm -f ${pid_file}
133
	fi
134

    
135
	if [ -n "${chroot_dir}" ]; then
136
		umount -f ${chroot_dir} >/dev/null 2>&1
137
	fi
138

    
139
	if [ -z "${booting}" -o "${boot_stage}" != "2" ]; then
140
		/usr/local/bin/php /etc/rc.conf_mount_ro
141
	fi
142

    
143
	if [ -n "${nc_pid}" ] && ps -p ${nc_pid} >/dev/null 2>&1; then
144
		kill ${nc_pid}
145
	fi
146

    
147
	if [ -n "${delete_annotation}" ]; then
148
		pkg ${pkg_chroot} annotate -q -D ${kernel_pkg} next_stage
149
	fi
150

    
151
	if [ -n "${unlock_additional_pkgs}" ]; then
152
		pkg_unlock "${pkg_prefix}*"
153
	fi
154

    
155
	local _rc=${1:-"0"}
156

    
157
	# If EVENT_PIPE is defined, GUI is calling
158
	if [ -n "${progress_socket}" ]; then
159
		local _need_reboot_str=""
160
		[ -n "${need_reboot}" ] \
161
			&& _need_reboot_str=" __REBOOT_AFTER=${reboot_after}"
162
		_echo "__RC=${_rc}${_need_reboot_str}"
163
	fi
164

    
165
	exit ${_rc}
166
}
167

    
168
pkg_with_pb() {
169
	local _event_pipe=""
170

    
171
	if [ -n "${progress_socket}" ]; then
172
		if [ -e "${chroot_dir}${progress_socket}" ]; then
173
			rm -f ${chroot_dir}${progress_socket}
174
		fi
175

    
176
		_event_pipe="-o EVENT_PIPE=${progress_socket}"
177

    
178
		nc -lU ${chroot_dir}${progress_socket} >> ${progress_file} &
179
		nc_pid=$!
180

    
181
		while [ ! -e "${chroot_dir}${progress_socket}" ]; do
182
			sleep 0.1
183
		done
184
	fi
185

    
186
	pkg ${_event_pipe} $@
187
	local _pkg_result=$?
188
	nc_pid=""
189
	return ${_pkg_result}
190
}
191

    
192
fetch_upgrade_packages() {
193
	local _pkgs_to_fetch=""
194
	if [ "${platform}" = "nanobsd" ]; then
195
		local _pkg=""
196

    
197
		# Check if all non-auto packages installed on 2nd partition are
198
		# installed on current one, if not, mark them to be deleted by
199
		# pkg autoremove
200
		for _pkg in $(pkg ${pkg_chroot} query -e '%a == 0' %n); do
201
			if ! pkg info -e ${_pkg}; then
202
				_exec "pkg ${pkg_chroot} set -A 1 ${_pkg}" "Scheduling package ${_pkg} for removal"
203
			fi
204
		done
205

    
206
		# Check if all non-auto packages installed on current partition are
207
		# installed on 2nd one, if not, we need to fetch them
208
		for _pkg in $(pkg query -e '%a == 0' %n); do
209
			if ! pkg ${pkg_chroot} info -e ${_pkg}; then
210
				_pkgs_to_fetch="${_pkgs_to_fetch}${_pkgs_to_fetch:+ }${_pkg}"
211
			fi
212
		done
213

    
214
	fi
215

    
216
	_exec "pkg_with_pb ${pkg_chroot} upgrade -F" "Downloading upgrade packages"
217

    
218
	if [ -n "${_pkgs_to_fetch}" ]; then
219
		_exec "pkg_with_pb ${pkg_chroot} fetch -d ${_pkgs_to_fetch}" \
220
			"Fetching packages not present on upgrade partition"
221
	fi
222
}
223

    
224
pkg_lock() {
225
	local _pkg="${1}"
226

    
227
	if [ -z "${_pkg}" ]; then
228
		return
229
	fi
230

    
231
	if [ "$(pkg ${pkg_chroot} query %k ${_pkg})" = "0" ]; then
232
		_exec "pkg ${pkg_chroot} lock ${_pkg}" "Locking package ${_pkg}" mute
233
	fi
234
}
235

    
236
pkg_unlock() {
237
	local _pkg="${1}"
238

    
239
	if [ -z "${_pkg}" ]; then
240
		return
241
	fi
242

    
243
	if [ "$(pkg ${pkg_chroot} query %k ${_pkg})" = "1" ]; then
244
		_exec "pkg ${pkg_chroot} unlock ${_pkg}" "Unlocking package ${_pkg}" mute
245
	fi
246
}
247

    
248
pkg_update() {
249
	local _run_update=1
250

    
251
	local _force=""
252
	if [ "${1}" = "force" ]; then
253
		_force=" -f"
254
	fi
255

    
256
	_exec "pkg ${pkg_chroot} update${_force}" "Updating repositories metadata"
257
}
258

    
259
pkg_upgrade() {
260
	# figure out which kernel variant is running
261
	export kernel_pkg=$(pkg query %n $(pkg info ${product}-kernel-\* | grep -v -- -debug-))
262

    
263
	if [ -z "${kernel_pkg}" ]; then
264
		_echo "ERROR: It was not possible to identify which ${product} kernel is installed"
265
		_exit 1
266
	fi
267

    
268
	export next_stage=$(pkg annotate -q -S ${kernel_pkg} next_stage)
269

    
270
	if [ -n "${next_stage}" -a -n "${booting}" -a -n "${boot_stage}" ]; then
271
		if [ ${boot_stage} != ${next_stage} ]; then
272
			_exit 0
273
		fi
274
	fi
275

    
276
	# If it's booting and first stage didn't run, just exit
277
	if [ -n "${booting}" -a -z "${next_stage}" ]; then
278
		_exit 0
279
	fi
280

    
281
	unset need_reboot
282
	# First upgrade stage
283
	if [ -z "${next_stage}" ]; then
284
		if [ -f "${logfile}" ]; then
285
			rm -f ${logfile}
286
		fi
287

    
288
		pkg_update
289

    
290
		if [ "$(compare_pkg_version pkg)" = "<" ]; then
291
			_exec "pkg upgrade pkg" "Upgrading pkg" mute
292
			pkg_update force
293
		fi
294

    
295
		local _repo_pkg="${product}-repo"
296

    
297
		# Deprecated pa
298
		if is_pkg_installed ${product}-repo-devel; then
299
			_exec "pkg ${pkg_chroot} set -A 1 ${product}-repo-devel" \
300
				"Scheduling package ${product}-repo-devel for removal"
301
			_exec "pkg install ${_repo_pkg}" "Installing ${_repo_pkg}" mute
302
			_exec "pkg delete ${product}-repo-devel" "Removing ${product}-repo-devel" \
303
				mute ignore_result
304
			validate_repo_conf
305
			pkg_update force
306
		fi
307

    
308
		if [ "$(compare_pkg_version ${_repo_pkg})" = "<" ]; then
309
			cp /usr/local/etc/pkg/repos/${product}.conf \
310
				/tmp/${product}.conf.copy
311
			_exec "pkg upgrade ${_repo_pkg}" "Upgrading ${_repo_pkg}" mute
312
			# If conf differs, for an update
313
			if ! cmp -s /usr/local/etc/pkg/repos/${product}.conf /tmp/${product}.conf.copy; then
314
				pkg_update force
315

    
316
				# New repo may contain newer pkg
317
				if [ "$(compare_pkg_version pkg)" = "<" ]; then
318
					_exec "pkg upgrade pkg" "Upgrading pkg" mute
319
					pkg_update force
320
				fi
321
			fi
322
			rm -f /tmp/${product}.conf.copy
323
		fi
324

    
325
		if [ $(pkg upgrade -nq | wc -l) -le 1 ]; then
326
			_echo "Your packages are up to date"
327
			_exit 0
328
		fi
329

    
330
		if [ -n "${dry_run}" ]; then
331
			pkg_unlock ${kernel_pkg}
332
			pkg ${pkg_chroot} upgrade -nq 2>&1 | tee -a ${logfile}
333
			pkg_lock ${kernel_pkg}
334
			_exit 0
335
		fi
336

    
337
		local _meta_pkg=$(get_meta_pkg_name)
338
		if [ $(pkg upgrade -r ${product}-core -nq | wc -l) -gt 1 ]; then
339
			if [ "${platform}" = "nanobsd" ]; then
340
				_echo "**** WARNING ****"
341
				_echo "Duplicate slice required!!"
342
				_echo ""
343
				_echo "Before starting the upgrade process, the currently mounted nanobsd partition"
344
				_echo "needs to be cloned to the secondary partition, where the update will happen"
345
				_echo ""
346
				_echo "After installation a reboot will be required to switch partition."
347
				_echo ""
348
				if [ -z "${yes}" ]; then
349
					_echo -n "Proceed with upgrade? (y/N) "
350
					read answer
351
					if [ "${answer}" != "y" ]; then
352
						_echo "Aborting..."
353
						_exit 0
354
					fi
355
					# Do not make the user have to answer again.
356
					yes=1
357
				fi
358
				setup_nanobsd_env
359
			fi
360
			need_reboot=1
361
		elif pkg upgrade -r ${product} -nq ${_meta_pkg} >/dev/null 2>&1; then
362
			need_reboot=1
363
		fi
364

    
365
		pkg_unlock ${kernel_pkg}
366

    
367
		if [ -z "${yes}" ]; then
368
			# Show user which packages are going to be upgraded
369
			pkg ${pkg_chroot} upgrade -nq 2>&1 | tee -a ${logfile}
370

    
371
			_echo ""
372
			if [ -n "${need_reboot}" ]; then
373
				_echo "**** WARNING ****"
374
				_echo "Reboot will be required!!"
375
			fi
376
			_echo -n "Proceed with upgrade? (y/N) "
377
			read answer
378
			if [ "${answer}" != "y" ]; then
379
				_echo "Aborting..."
380
				_exit 0
381
			fi
382
		fi
383

    
384
		# Download all upgrade packages first
385
		fetch_upgrade_packages
386

    
387
		if [ $(pkg ${pkg_chroot} upgrade -nq ${kernel_pkg} | wc -l) -gt 1 ]; then
388
			_exec "pkg ${pkg_chroot} upgrade ${kernel_pkg}" "Upgrading ${product} kernel"
389
		fi
390

    
391
		pkg ${pkg_chroot} annotate -q -M ${kernel_pkg} next_stage 2
392
		next_stage=2
393

    
394
		if [ -n "${need_reboot}" -a "${platform}" != "nanobsd" ]; then
395
			do_reboot
396
			_exit 0
397
		fi
398
	fi
399

    
400
	if [ "${next_stage}" = "2" ]; then
401
		pkg_lock "${pkg_prefix}*"
402
		unlock_additional_pkgs=1
403

    
404
		# XXX: Workaround to upgrade strongswan
405
		# If those symlinks are present, pkg exit because it expects them
406
		# to be a directory
407
		if [ $(pkg ${pkg_chroot} upgrade -nq strongswan | wc -l) -gt 1 ]; then
408
			if [ -L ${chroot_dir}/usr/local/etc/ipsec.d ]; then
409
				rm -f ${chroot_dir}/usr/local/etc/ipsec.d
410
			fi
411
			if [ -L ${chroot_dir}/usr/local/etc/ipsec.conf ]; then
412
				rm -f ${chroot_dir}/usr/local/etc/ipsec.conf
413
			fi
414
			if [ -L ${chroot_dir}/usr/local/etc/strongswan.d ]; then
415
				rm -f ${chroot_dir}/usr/local/etc/strongswan.d
416
			fi
417
			if [ -L ${chroot_dir}/usr/local/etc/strongswan.conf ]; then
418
				rm -f ${chroot_dir}/usr/local/etc/strongswan.conf
419
			fi
420
		fi
421

    
422
		if [ $(pkg ${pkg_chroot} upgrade -nq | wc -l) -gt 1 ]; then
423
			delete_annotation=1
424
			_exec "pkg ${pkg_chroot} upgrade" "Upgrading necessary packages"
425
			delete_annotation=""
426
		fi
427

    
428
		# XXX: workaround for #5300
429
		sort -u ${chroot_dir}/usr/local/etc/php/extensions.ini > /tmp/extensions.ini
430
		mv /tmp/extensions.ini ${chroot_dir}/usr/local/etc/php/extensions.ini
431

    
432
		pkg ${pkg_chroot} annotate -q -M ${kernel_pkg} next_stage 3
433
		next_stage=3
434

    
435
		pkg_unlock "${pkg_prefix}*"
436
		unlock_additional_pkgs=""
437

    
438
		if [ -n "${need_reboot}" -a "${platform}" = "nanobsd" ]; then
439
			switch_active_nanobsd_partition
440
			do_reboot
441
			_exit 0
442
		fi
443

    
444
		if [ -n "${booting}" ]; then
445
			_exit 0
446
		fi
447
	fi
448

    
449
	if [ "${next_stage}" = "3" ]; then
450
		if [ $(pkg upgrade -nq | wc -l) -gt 1 ]; then
451
			delete_annotation=1
452
			_exec "pkg ${pkg_chroot} upgrade" "Upgrading necessary packages"
453
			delete_annotation=""
454
		fi
455

    
456
		pkg ${pkg_chroot} annotate -q -D ${kernel_pkg} next_stage
457

    
458
		# cleanup caches
459
		_exec "pkg ${pkg_chroot} autoremove" "Removing unnecessary packages" mute ignore_result
460
		_exec "pkg ${pkg_chroot} clean" "Cleanup pkg cache" mute ignore_result
461
	fi
462

    
463
	gitsync=$(/usr/local/sbin/read_xml_tag.sh boolean system/gitsync/synconupgrade)
464
	if [ "${gitsync}" = "true" ]; then
465
		repository_url=$(/usr/local/sbin/read_xml_tag.sh string system/gitsync/repositoryurl)
466
		branch=$(/usr/local/sbin/read_xml_tag.sh string system/gitsync/branch)
467

    
468
		# Repository URL is not mandatory
469
		if [ -n "${branch}" ]; then
470
			_exec "/usr/local/sbin/pfSsh.php playback gitsync \
471
				${repositoryurl} ${branch} --upgrading" \
472
				"Running gitsync" mute ignore_result
473
		fi
474
	fi
475
}
476

    
477
get_meta_pkg_name() {
478
	# figure out main meta package name
479
	if is_pkg_installed ${product}-vmware; then
480
		echo "${product}-vmware"
481
	elif is_pkg_installed ${product}; then
482
		echo "${product}"
483
	else
484
		_echo "ERROR: It was not possible to identify which ${product} meta package is installed"
485
		_exit 1
486
	fi
487
}
488

    
489
check_upgrade() {
490
	local _meta_pkg=$(get_meta_pkg_name)
491

    
492
	pkg_update
493

    
494
	if [ "$(compare_pkg_version ${_meta_pkg})" = "<" ]; then
495
		local _new_version=$(pkg rquery %v ${_meta_pkg})
496
		_echo "${_new_version} version of ${product} is available"
497
		_exit 2
498
	else
499
		for _pkg in $(pkg query -e "%n ~ ${product}-*" %n); do
500
			# Ignore additional packages
501
			if echo "${_pkg}" | grep -q "^${pkg_prefix}"; then
502
				continue
503
			fi
504
			if [ "$(compare_pkg_version ${_pkg})" = "<" ]; then
505
				local _new_version=$(pkg rquery %v ${_pkg})
506
				_echo "${_new_version} version of ${_pkg} is available"
507
				_exit 2
508
			fi
509
		done
510
	fi
511

    
512
	_echo "Your system is up to date"
513
	_exit 0
514
}
515

    
516
setup_nanobsd_env() {
517
	if [ "${platform}" != "nanobsd" ]; then
518
		return;
519
	fi
520

    
521
	chroot_dir=/tmp/nanobsd_upgrade
522
	mkdir -p ${chroot_dir} 2>/dev/null
523
	local _cur_partition=$(mount -p / | cut -f1)
524
	local _update_partition=$(echo ${_cur_partition} | sed -e 's,0$,2,; s,1$,0,; s,2$,1,')
525

    
526
	if [ ! -e "${_update_partition}" ]; then
527
		_echo "Secondary partition (${_update_partition}), used for upgrade not found"
528
		_exit 1
529
	fi
530

    
531
	# Remove /dev
532
	_update_partition=$(echo ${_update_partition} | sed 's,^/dev/,,')
533
	local _update_slice=$(glabel status -s | awk "\$1 == \"${_update_partition}\" { print \$3 }")
534

    
535
	if [ -z "${_update_slice}" -o ! -e "/dev/${_update_slice}" ]; then
536
		_echo "Secondary slice (${_update_slice}), use_update_sliced for upgrade not found"
537
		_exit 1
538
	fi
539

    
540
	_update_slice="/dev/${_update_slice}"
541

    
542
	# Clone slice using same logic from nanobsd_clone_slice()
543
	sysctl kern.geom.debugflags=16 >/dev/null 2>&1
544
	_exec "dd if=/dev/zero of=${_update_slice} bs=1m count=1" "Cleaning secondary partition" mute
545
	_exec "dd if=${_cur_partition} of=${_update_slice} bs=64k" "Duplicating current slice" mute
546
	_exec "tunefs -L ${_update_partition##*/} ${_update_slice}" "Restoring slice label" mute
547
	sysctl kern.geom.debugflags=0 >/dev/null 2>&1
548

    
549
	_exec "/sbin/fsck -y -t ufs /dev/${_update_partition}" "Testing duplicated partition integrity" mute
550
	_exec "mount /dev/${_update_partition} ${chroot_dir}" "Mounting second partition to run upgrade" mute
551

    
552
	sed -i '' -e "s,^${_cur_partition},/dev/${_update_partition}," \
553
		${chroot_dir}/etc/fstab
554

    
555
	pkg_chroot="-c ${chroot_dir}"
556
}
557

    
558
switch_active_nanobsd_partition() {
559
	if [ "${platform}" != "nanobsd" ]; then
560
		return;
561
	fi
562

    
563
	local _cur_partition=$(mount -p / | cut -f1 | sed 's,^/dev/,,')
564
	local _disk=$(glabel status -s | \
565
		awk "\$1 == \"${_cur_partition}\" { print substr(\$3, 0, length(\$3)-3)}")
566
	local _i=$(echo ${_cur_partition} | cut -c ${#_cur_partition})
567

    
568
	if ! echo "${_i}" | egrep -q '^[0-9]$'; then
569
		_echo "Invalid partition label ${_cur_partition}"
570
		_exit 1
571
	fi
572

    
573
	# pfsense0 == part 1 / pfsense1 == part 2
574
	if [ ${_i} -eq 0 ]; then
575
		_i=2
576
	else
577
		_i=1
578
	fi
579

    
580
	_exec "gpart set -a active -i ${_i} ${_disk}" "Setting secondary partition as active" mute
581
}
582

    
583
is_pkg_installed() {
584
	local _pkg_name="${1}"
585
	shift
586
	local _pkg_chroot="$@"
587

    
588
	pkg ${_pkg_chroot} info -e ${_pkg_name}
589
	return $?
590
}
591

    
592
compare_pkg_version() {
593
	local _pkg_name="${1}"
594

    
595
	if ! is_pkg_installed ${_pkg_name} ${pkg_chroot}; then
596
		echo '!'
597
		return 1
598
	fi
599

    
600
	local _lver=$(pkg ${pkg_chroot} query %v ${_pkg_name})
601

    
602
	if [ -z "${_lver}" ]; then
603
		_echo "ERROR: It was not possible to determine ${_pkg_name} local version"
604
		_exit 1
605
	fi
606

    
607
	local _rver=$(pkg ${pkg_chroot} rquery %v ${_pkg_name})
608

    
609
	if [ -z "${_rver}" ]; then
610
		_echo "ERROR: It was not possible to determine ${_pkg_name} remote version"
611
		_exit 1
612
	fi
613

    
614
	local _version=$(pkg version -t ${_lver} ${_rver})
615

    
616
	if [ $? -ne 0 ]; then
617
		_echo "ERROR: Error comparing ${_pkg_name} local and remote versions"
618
		_exit 1
619
	fi
620

    
621
	echo ${_version}
622
	return 0
623
}
624

    
625
pkg_install() {
626
	local _pkg_name="${1}"
627

    
628
	local _force=""
629
	if [ -n "${2}" ]; then
630
		_force="-f"
631
	fi
632

    
633
	if [ -z "${_pkg_name}" ]; then
634
		_echo "ERROR: Blank package name"
635
		_exit 1
636
	fi
637

    
638
	if is_pkg_installed ${_pkg_name}; then
639
		local _cversion=$(compare_pkg_version ${_pkg_name})
640

    
641
		if [ -z "${_force}" ]; then
642
			if [ "${_cversion}" = "=" ]; then
643
				_echo "Package ${_pkg_name} is up to date"
644
				_exit 0
645
			elif [ "${_cversion}" = ">" ]; then
646
				_echo "Installed ${_pkg_name} version is newer than remote"
647
				_exit 0
648
			fi
649
		fi
650
		local _cmd="upgrade ${_force}"
651
		local _msg="Upgrading"
652
	else
653
		local _cmd="install"
654
		local _msg="Installing"
655
	fi
656

    
657
	_exec "pkg_with_pb ${_cmd}${dry_run:+ }${dry_run} ${_pkg_name}" "${_msg} ${_pkg_name}"
658
	_exec "pkg clean" "Cleaning up cache" mute ignore_result
659
}
660

    
661
# Reinstall every pfSense-pkg-* package
662
pkg_reinstall_all() {
663
	for _pkg in $(pkg query -e '%a == 0' %n); do
664
		case ${_pkg} in "${pkg_prefix}"* )
665
			_echo "Reinstalling ${_pkg}"
666
			pkg_install ${_pkg} 1
667
			;;
668
		esac
669
	done
670
}
671

    
672
pkg_delete() {
673
	local _pkg_name="${1}"
674

    
675
	if [ -z "${_pkg_name}" ]; then
676
		_echo "ERROR: Blank package name"
677
		_exit 1
678
	fi
679

    
680
	if ! is_pkg_installed ${_pkg_name}; then
681
		_echo "ERROR: Package ${_pkg_name} is not installed"
682
		_exit 1
683
	fi
684

    
685
	_exec "pkg_with_pb delete${dry_run:+ }${dry_run} ${_pkg_name}" "Removing ${_pkg_name}"
686
	_exec "pkg autoremove" "Removing stale packages" mute ignore_result
687
}
688

    
689
# Delete every pfSense-pkg-* package
690
pkg_delete_all() {
691
	for _pkg in $(pkg query -e '%a == 0' %n); do
692
		case ${_pkg} in "${pkg_prefix}"* )
693
			_echo "Removing ${_pkg}"
694
			pkg_delete ${_pkg}
695
			;;
696
		esac
697
	done
698
}
699

    
700
do_reboot() {
701
	if [ -z "${dont_reboot}" ]; then
702
		_echo "Upgrade is complete.  Rebooting in ${reboot_after} seconds."
703
		echo "Upgrade is complete.  Rebooting in ${reboot_after} seconds." | wall
704
		/etc/rc.notify_message -e -g -m "Upgrade is complete.  Rebooting in ${reboot_after} seconds." \
705
			>/dev/null 2>&1
706
		(sleep ${reboot_after} && /etc/rc.reboot) &
707
	else
708
		_echo "Upgrade is complete."
709
		echo "Upgrade is complete." | wall
710
		/etc/rc.notify_message -e -g -m "Upgrade is complete." >/dev/null 2>&1
711
	fi
712
}
713

    
714
validate_repo_conf() {
715
	# Make sure to use default repo conf when it doesn't exist
716
	pkg_repo_conf="/usr/local/etc/pkg/repos/${product}.conf"
717
	default_pkg_repo_conf_path="/usr/local/share/${product}/pkg/repos/${product}-repo.conf"
718

    
719
	pkg_repo_conf_path=$(/usr/local/sbin/read_xml_tag.sh string system/pkg_repo_conf_path)
720

    
721
	if [ -z "${pkg_repo_conf_path}" -o ! -f "${pkg_repo_conf_path}" ]; then
722
		pkg_repo_conf_path=${default_pkg_repo_conf_path}
723
	fi
724

    
725
	if [ -f "${pkg_repo_conf_path}" ]; then
726
		if [ -e "${pkg_repo_conf}" -a ! -L "${pkg_repo_conf}" ]; then
727
			rm -f ${pkg_repo_conf}
728
			ln -sf ${pkg_repo_conf_path} ${pkg_repo_conf}
729
		fi
730

    
731
		if [ "$(readlink ${pkg_repo_conf})" != "${pkg_repo_conf_path}" ]; then
732
			mkdir -p /usr/local/etc/pkg/repos
733
			ln -sf ${pkg_repo_conf_path} ${pkg_repo_conf}
734
		fi
735
	fi
736
}
737

    
738
export LANG=C
739

    
740
pid_file="/var/run/$(basename $0).pid"
741
logfile="/cf/conf/upgrade_log.txt"
742
stdout='/dev/null'
743

    
744
# Setup proxy settings
745
HTTP_PROXY=$(/usr/local/sbin/read_xml_tag.sh string system/proxyurl)
746
if [ "${HTTP_PROXY}" != "" ]; then
747
	HTTP_PROXY_PORT=$(/usr/local/sbin/read_xml_tag.sh string system/proxyport)
748
	if [ "${HTTP_PROXY_PORT}" != "" ]; then
749
		HTTP_PROXY="${HTTP_PROXY}:${HTTP_PROXY_PORT}"
750
	fi
751
	export HTTP_PROXY
752
fi
753

    
754
# pkg should not ask for confirmations
755
export ASSUME_ALWAYS_YES=true
756
export FETCH_TIMEOUT=5
757
export FETCH_RETRY=2
758

    
759
export product=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var product_name pfSense)
760
export pkg_prefix=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var pkg_prefix pfSense-pkg-)
761
export platform=$(cat /etc/platform)
762

    
763
USE_MFS_TMPVAR=$(/usr/local/sbin/read_xml_tag.sh boolean system/use_mfs_tmpvar)
764
if [ "${platform}" = "nanobsd" ] || [ "${USE_MFS_TMPVAR}" = "true" ]; then
765
	export PKG_DBDIR=/root/var/db/pkg
766
	export PKG_CACHEDIR=/root/var/cache/pkg
767
fi
768

    
769
product_version=$(cat /etc/version)
770
do_not_send_host_uuid=$(/usr/local/sbin/read_xml_tag.sh boolean system/do_not_send_host_uuid)
771
if [ "${do_not_send_host_uuid}" != "true" ]; then
772
	hostuuid=$(sysctl kern.hostuuid)
773
	export HTTP_USER_AGENT="${product}/${product_version}:${hostuuid}"
774
else
775
	export HTTP_USER_AGENT="${product}/${product_version}"
776
fi
777

    
778
validate_repo_conf
779

    
780
# Flags used in _exit
781
export delete_annotation=""
782
export unlock_additional_pkgs=""
783

    
784
# Upgrade process on nanobsd will happen in chroot
785
export pkg_chroot=""
786
export chroot_dir=""
787

    
788
# Save nc_pid to be able to kill it
789
export nc_pid=""
790

    
791
# Reboot after 10 seconds
792
export reboot_after=10
793

    
794
unset dry_run
795
unset dont_reboot
796
unset booting
797
unset boot_stage
798
unset force
799
unset yes
800
unset progress_file
801
unset progress_socket
802
unset action
803
unset action_pkg
804
unset force_ipv4
805
unset force_ipv6
806
while getopts 46b:cdfi:hp:l:nr:Ruy opt; do
807
	case ${opt} in
808
		4)
809
			if [ -n "${force_ipv6}" ]; then
810
				usage
811
				_exit 1
812
			fi
813
			force_ipv4=1
814
			;;
815
		6)
816
			if [ -n "${force_ipv4}" ]; then
817
				usage
818
				_exit 1
819
			fi
820
			force_ipv6=1
821
			;;
822
		b)
823
			booting=1
824
			boot_stage="${OPTARG}"
825
			;;
826
		c)
827
			action="check"
828
			;;
829
		d)
830
			stdout=''
831
			;;
832
		f)
833
			force=1
834
			;;
835
		i)
836
			if [ -n "${action}" ]; then
837
				usage
838
				_exit 1
839
			fi
840
			action="install"
841
			action_pkg="${OPTARG}"
842
			;;
843
		h)
844
			usage
845
			_exit 0
846
			;;
847
		l)
848
			logfile="${OPTARG}"
849
			if [ -z "${logfile}" ]; then
850
				usage
851
				_exit 1
852
			fi
853
			;;
854
		n)
855
			dry_run="-n"
856
			;;
857
		p)
858
			progress_socket="${OPTARG}"
859
			if [ -z "${progress_socket}" ]; then
860
				usage
861
				_exit 1
862
			fi
863
			;;
864
		r)
865
			if [ -n "${action}" ]; then
866
				usage
867
				_exit 1
868
			fi
869
			action="delete"
870
			action_pkg="${OPTARG}"
871
			;;
872
		R)
873
			dont_reboot=1
874
			;;
875
		u)
876
			if [ -n "${action}" ]; then
877
				usage
878
				_exit 1
879
			fi
880
			action="update"
881
			;;
882
		y)
883
			yes=1
884
			;;
885
		*)
886
			usage
887
			_exit 1
888
			;;
889
	esac
890
done
891

    
892
if [ -n "${force_ipv4}" ]; then
893
	export IP_VERSION="4"
894
elif [ -n "${force_ipv6}" ]; then
895
	export IP_VERSION="6"
896
fi
897

    
898
# Set default action when no parameter is set
899
: ${action:="upgrade"}
900

    
901
if pgrep -qF ${pid_file} >/dev/null 2>&1; then
902
	echo "Another instance is already running... Aborting!"
903
	exit 1
904
fi
905

    
906
if [ -z "${booting}" -o "${boot_stage}" != "2" ]; then
907
	/usr/local/bin/php /etc/rc.conf_mount_rw
908
fi
909

    
910
if [ -n "${booting}" ]; then
911
	export REPO_AUTOUPDATE=false
912
fi
913

    
914
echo $$ > ${pid_file}
915

    
916
trap _exit 1 2 15 EXIT
917

    
918
if [ "${action}" != "upgrade" -a -f "${logfile}" ]; then
919
	rm -f ${logfile}
920
fi
921

    
922
progress_file=${logfile%.*}.json
923

    
924
if [ -e "${progress_file}" ]; then
925
	rm -f ${progress_file}
926
fi
927

    
928
case "${action}" in
929
	check)
930
		check_upgrade
931
		;;
932
	upgrade)
933
		pkg_upgrade
934
		;;
935
	update)
936
		pkg_update force
937
		;;
938
	install)
939
		if [ ${action_pkg} == "ALL_PACKAGES" ] && [ -n ${force} ]; then
940
			pkg_reinstall_all
941
		else
942
			pkg_install ${action_pkg} ${force}
943
		fi
944
		;;
945
	delete)
946
		if [ ${action_pkg} == "ALL_PACKAGES" ] && [ -n ${force} ]; then
947
			pkg_delete_all
948
		else
949
			pkg_delete ${action_pkg}
950
		fi
951
		;;
952
	*)
953
		_echo "ERROR: Invalid action!"
954
		_exit 1
955
esac
956

    
957
_exit 0
(11-11/24)