Project

General

Profile

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

    
3
# Copyright (c) 2004-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 "	-p socket   - Write pkg progress to socket"
60
	echo "	-R          - Do not reboot (it can be dangerous)"
61
	echo "	-y          - Consider yes as the answer for any possible interaction" >&2
62
	echo "" >&2
63
	echo "Following parameters are mutually exclusive:" >&2
64
	echo "	-i PKG_NAME - Install package PKG_NAME" >&2
65
	echo "	-r PKG_NAME - Remove package PKG_NAME" >&2
66
	echo "	-u          - Update repository information" >&2
67
}
68

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

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

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

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

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

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

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

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

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

    
128
	pkg_lock ${kernel_pkg}
129

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

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

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

    
142
	local _rc=${1:-"0"}
143

    
144
	# If EVENT_PIPE is defined, GUI is calling
145
	[ -n "${progress_socket}" ] \
146
		&& _echo "__RC=${_rc}"
147

    
148
	exit ${_rc}
149
}
150

    
151
pkg_with_pb() {
152
	local _event_pipe=""
153

    
154
	if [ -n "${progress_socket}" ]; then
155
		if [ -e "${progress_socket}" ]; then
156
			rm -f ${progress_socket}
157
		fi
158

    
159
		_event_pipe="-o EVENT_PIPE=${progress_socket}"
160

    
161
		nc -lU ${progress_socket} >> ${progress_file} &
162

    
163
		while [ ! -e "${progress_socket}" ]; do
164
			sleep 0.1
165
		done
166
	fi
167

    
168
	pkg ${_event_pipe} $@
169
	return $?
170
}
171

    
172
fetch_upgrade_packages() {
173
	local _pkgs_to_fetch=""
174
	if [ "${platform}" = "nanobsd" ]; then
175
		local _pkg=""
176

    
177
		# Check if all non-auto packages installed on 2nd partition are
178
		# installed on current one, if not, mark them to be deleted by
179
		# pkg autoremove
180
		for _pkg in $(pkg ${pkg_chroot} query -e '%a == 0' %n); do
181
			if ! pkg info -e ${_pkg}; then
182
				_exec "pkg ${pkg_chroot} set -A 1 ${_pkg}" "Scheduling package ${_pkg} for removal"
183
			fi
184
		done
185

    
186
		# Check if all non-auto packages installed on current partition are
187
		# installed on 2nd one, if not, we need to fetch them
188
		for _pkg in $(pkg query -e '%a == 0' %n); do
189
			if ! pkg ${pkg_chroot} info -e ${_pkg}; then
190
				_pkgs_to_fetch="${_pkgs_to_fetch}${_pkgs_to_fetch:+ }${_pkg}"
191
			fi
192
		done
193

    
194
	fi
195

    
196
	_echo ">>> Downloading upgrade packages..."
197
	if ! pkg_with_pb ${pkg_chroot} upgrade -F 2>&1 | tee -a ${logfile}; then
198
		_echo "ERROR: It was not possible to download packages"
199
		_exit 1
200
	fi
201

    
202
	if [ -n "${_pkgs_to_fetch}" ]; then
203
		_echo ">>> Fetching packages not present on upgrade partition..."
204
		if ! pkg_with_pb ${pkg_chroot} fetch -d ${_pkgs_to_fetch} 2>&1 | tee -a ${logfile}; then
205
			_echo "ERROR: It was not possible to fetch packages"
206
			_exit 1
207
		fi
208
	fi
209
}
210

    
211
pkg_lock() {
212
	local _pkg="${1}"
213

    
214
	if [ -z "${_pkg}" ]; then
215
		return
216
	fi
217

    
218
	if [ "$(pkg ${pkg_chroot} query %k ${_pkg})" = "0" ]; then
219
		_exec "pkg ${pkg_chroot} lock ${_pkg}" "Locking package ${_pkg}" mute
220
	fi
221
}
222

    
223
pkg_unlock() {
224
	local _pkg="${1}"
225

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

    
230
	if [ "$(pkg ${pkg_chroot} query %k ${_pkg})" = "1" ]; then
231
		_exec "pkg ${pkg_chroot} unlock ${_pkg}" "Unlocking package ${_pkg}" mute
232
	fi
233
}
234

    
235
pkg_update() {
236
	local _run_update=1
237

    
238
	local _force=""
239
	if [ "${1}" = "force" ]; then
240
		local _force=1
241
	fi
242

    
243
	if [ -z "${_force}" -a -f ${last_update_file} ]; then
244
		local _last_update=$(head -n 1 ${last_update_file})
245
		# Verify if content contain only numbers
246
		if echo "${_last_update}" | grep -E -q '^[0-9]+$'; then
247
			local _now=$(date +%s)
248
			# Only run update hourly, and if last update is in the future
249
			[ ${_now} -gt ${_last_update} -a $((${_now} - ${_last_update})) -le $((60 * 60)) ] \
250
				&& unset _run_update
251
		fi
252
	fi
253

    
254
	[ -z "${_run_update}" ] \
255
		&& return 0
256

    
257
	_exec "pkg ${pkg_chroot} update" "Updating repositories" mute
258
	date +%s > ${last_update_file}
259
}
260

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

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

    
270
	export next_stage=$(pkg annotate -q -S ${kernel_pkg} next_stage)
271

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

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

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

    
290
		pkg_update
291

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

    
297
		if [ $(pkg upgrade -nq | wc -l) -le 1 ]; then
298
			_echo "Your packages are up to date"
299
			_exit 0
300
		fi
301

    
302
		if [ $(pkg upgrade -r ${product}-core -nq | wc -l) -gt 1 ]; then
303
			setup_nanobsd_env
304
			need_reboot=1
305
		fi
306

    
307
		pkg_unlock ${kernel_pkg}
308

    
309
		if [ "${platform}" = "nanobsd" ] && \
310
		   [ $(pkg ${pkg_chroot} upgrade -nq | wc -l) -le 1 ]; then
311
			_echo "**** WARNING ****"
312
			_echo "Reboot will be required!!"
313
			_echo "Secondary partition is up to date"
314
			if [ -z "${yes}" ]; then
315
				_echo -n "Proceed with upgrade? (y/N) "
316
				read answer
317
				if [ "${answer}" != "y" ]; then
318
					_echo "Aborting..."
319
					_exit 0
320
				fi
321
			fi
322
			switch_active_nanobsd_partition
323
			do_reboot
324
			_exit 0
325
		fi
326

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

    
331
			_echo ""
332
			if [ -n "${need_reboot}" ]; then
333
				_echo "**** WARNING ****"
334
				_echo "Reboot will be required!!"
335
			fi
336
			_echo -n "Proceed with upgrade? (y/N) "
337
			read answer
338
			if [ "${answer}" != "y" ]; then
339
				_echo "Aborting..."
340
				_exit 0
341
			fi
342
		fi
343

    
344
		# Download all upgrade packages first
345
		fetch_upgrade_packages
346

    
347
		if [ $(pkg ${pkg_chroot} upgrade -nq ${kernel_pkg} | wc -l) -gt 1 ]; then
348
			_exec "pkg ${pkg_chroot} upgrade ${kernel_pkg}" "Upgrading ${product} kernel"
349
		fi
350

    
351
		pkg ${pkg_chroot} annotate -q -M ${kernel_pkg} next_stage 2
352
		next_stage=2
353

    
354
		if [ -n "${need_reboot}" -a "${platform}" != "nanobsd" ]; then
355
			do_reboot
356
			_exit 0
357
		fi
358
	fi
359

    
360
	if [ "${next_stage}" = "2" ]; then
361
		pkg_lock "${pkg_prefix}*"
362

    
363
		if [ $(pkg ${pkg_chroot} upgrade -nq | wc -l) -gt 1 ]; then
364
			_echo "Upgrading necessary packages..."
365
			if ! pkg ${pkg_chroot} upgrade 2>&1 | tee -a ${logfile}; then
366
				pkg ${pkg_chroot} annotate -q -D ${kernel_pkg} next_stage
367
				pkg_unlock "${pkg_prefix}*"
368
				_echo "ERROR: An error occurred when upgrade was running..."
369
				_exit 1
370
			fi
371
		fi
372

    
373
		# XXX: workaround for #5300
374
		sort -u ${chroot_dir}/usr/local/etc/php/extensions.ini > /tmp/extensions.ini
375
		mv /tmp/extensions.ini ${chroot_dir}/usr/local/etc/php/extensions.ini
376

    
377
		pkg ${pkg_chroot} annotate -q -M ${kernel_pkg} next_stage 3
378
		next_stage=3
379

    
380
		pkg_unlock "${pkg_prefix}*"
381

    
382
		if [ -n "${need_reboot}" -a "${platform}" = "nanobsd" ]; then
383
			switch_active_nanobsd_partition
384
			do_reboot
385
			_exit 0
386
		fi
387

    
388
		if [ -n "${booting}" ]; then
389
			_exit 0
390
		fi
391
	fi
392

    
393
	if [ "${next_stage}" = "3" ]; then
394
		if [ $(pkg upgrade -nq | wc -l) -gt 1 ]; then
395
			_echo "Upgrading necessary packages..."
396
			if ! pkg ${pkg_chroot} upgrade 2>&1 | tee -a ${logfile}; then
397
				pkg ${pkg_chroot} annotate -q -D ${kernel_pkg} next_stage
398
				_echo "ERROR: An error occurred when upgrade was running..."
399
				_exit 1
400
			fi
401
		fi
402

    
403
		pkg ${pkg_chroot} annotate -q -D ${kernel_pkg} next_stage
404

    
405
		# cleanup caches
406
		_exec "pkg ${pkg_chroot} autoremove" "Removing unnecessary packages" mute ignore_result
407
		_exec "pkg ${pkg_chroot} clean" "Cleanup pkg cache" mute ignore_result
408
	fi
409
}
410

    
411
check_upgrade() {
412
	# figure out main meta package name
413
	if is_pkg_installed ${product}-vmware; then
414
		local _meta_pkg="${product}-vmware"
415
	elif is_pkg_installed ${product}; then
416
		local _meta_pkg="${product}"
417
	else
418
		_echo "ERROR: It was not possible to identify which ${product} meta package is installed"
419
		_exit 1
420
	fi
421

    
422
	pkg_update
423

    
424
	if [ "$(compare_pkg_version ${_meta_pkg})" = "<" ]; then
425
		local _new_version=$(pkg rquery %v ${_meta_pkg})
426
		_echo "${_new_version} version of ${product} is available"
427
		_exit 2
428
	else
429
		for _pkg in $(pkg query -e "%n ~ ${product}-*" %n); do
430
			# Ignore additional packages
431
			if echo "${_pkg}" | grep -q "^${pkg_prefix}"; then
432
				continue
433
			fi
434
			if [ "$(compare_pkg_version ${_pkg})" = "<" ]; then
435
				local _new_version=$(pkg rquery %v ${_pkg})
436
				_echo "${_new_version} version of ${_pkg} is available"
437
				_exit 2
438
			fi
439
		done
440
	fi
441

    
442
	_echo "Your system is up to date"
443
	_exit 0
444
}
445

    
446
setup_nanobsd_env() {
447
	if [ "${platform}" != "nanobsd" ]; then
448
		return;
449
	fi
450

    
451
	chroot_dir=/tmp/nanobsd_upgrade
452
	mkdir -p ${chroot_dir} 2>/dev/null
453
	local _cur_partition=$(mount -p / | cut -f1)
454
	local _update_partition=$(echo ${_cur_partition} | sed -e 's,0$,2,; s,1$,0,; s,2$,1,')
455

    
456
	if [ ! -e "${_update_partition}" ]; then
457
		_echo "Secondary partition (${_update_partition}), used for upgrade not found"
458
		_exit 1
459
	fi
460

    
461
	_exec "mount ${_update_partition} ${chroot_dir}" "Mounting second partition to run upgrade" mute
462

    
463
	pkg_chroot="-c ${chroot_dir}"
464

    
465
	pkg_update force
466

    
467
	if [ "$(compare_pkg_version pkg)" = "<" ]; then
468
		_exec "pkg ${pkg_chroot} upgrade pkg" "Upgrading pkg" mute
469
		pkg_update force
470
	fi
471

    
472
}
473

    
474
switch_active_nanobsd_partition() {
475
	if [ "${platform}" != "nanobsd" ]; then
476
		return;
477
	fi
478

    
479
	local _cur_partition=$(mount -p / | cut -f1 | sed 's,^/dev/,,')
480
	local _disk=$(glabel status -s | \
481
		awk "\$1 == \"${_cur_partition}\" { print substr(\$3, 0, length(\$3)-3)}")
482
	local _i=$(echo ${_cur_partition} | cut -c ${#_cur_partition})
483

    
484
	if ! echo "${_i}" | egrep -q '^[0-9]$'; then
485
		_echo "Invalid partition label ${_cur_partition}"
486
		_exit 1
487
	fi
488

    
489
	# pfsense0 == part 1 / pfsense1 == part 2
490
	if [ ${_i} -eq 0 ]; then
491
		_i=2
492
	else
493
		_i=1
494
	fi
495

    
496
	_exec "gpart set -a active -i ${_i} ${_disk}" "Setting secondary partition as active" mute
497
}
498

    
499
is_pkg_installed() {
500
	local _pkg_name="${1}"
501
	shift
502
	local _pkg_chroot="$@"
503

    
504
	pkg ${_pkg_chroot} info -e ${_pkg_name}
505
	return $?
506
}
507

    
508
compare_pkg_version() {
509
	local _pkg_name="${1}"
510

    
511
	if ! is_pkg_installed ${_pkg_name} ${pkg_chroot}; then
512
		echo '!'
513
		return -1
514
	fi
515

    
516
	local _lver=$(pkg ${pkg_chroot} query %v ${_pkg_name})
517

    
518
	if [ -z "${_lver}" ]; then
519
		_echo "ERROR: It was not possible to determine ${_pkg_name} local version"
520
		_exit 1
521
	fi
522

    
523
	local _rver=$(pkg ${pkg_chroot} rquery %v ${_pkg_name})
524

    
525
	if [ -z "${_rver}" ]; then
526
		_echo "ERROR: It was not possible to determine ${_pkg_name} remote version"
527
		_exit 1
528
	fi
529

    
530
	local _version=$(pkg version -t ${_lver} ${_rver})
531

    
532
	if [ $? -ne 0 ]; then
533
		_echo "ERROR: Error comparing ${_pkg_name} local and remote versions"
534
		_exit 1
535
	fi
536

    
537
	echo ${_version}
538
	return 0
539
}
540

    
541
pkg_install() {
542
	local _pkg_name="${1}"
543

    
544
	local _force=""
545
	if [ -n "${2}" ]; then
546
		_force="-f"
547
	fi
548

    
549
	if [ -z "${_pkg_name}" ]; then
550
		_echo "ERROR: Blank package name"
551
		_exit 1
552
	fi
553

    
554
	pkg_update
555

    
556
	if is_pkg_installed ${_pkg_name}; then
557
		local _cversion=$(compare_pkg_version ${_pkg_name})
558

    
559
		if [ -z "${_force}" ]; then
560
			if [ "${_cversion}" = "=" ]; then
561
				_echo "Package ${_pkg_name} is up to date"
562
				_exit 0
563
			elif [ "${_cversion}" = ">" ]; then
564
				_echo "Installed ${_pkg_name} version is newer than remote"
565
				_exit 0
566
			fi
567
		fi
568
		local _cmd="upgrade ${_force}"
569
		local _msg="Upgrading"
570
	else
571
		local _cmd="install"
572
		local _msg="Installing"
573
	fi
574

    
575
	_exec "pkg_with_pb ${_cmd} ${_pkg_name}" "${_msg} ${_pkg_name}"
576
	_exec "pkg clean" "Cleaning up cache" mute ignore_result
577
}
578

    
579
pkg_delete() {
580
	local _pkg_name="${1}"
581

    
582
	if [ -z "${_pkg_name}" ]; then
583
		_echo "ERROR: Blank package name"
584
		_exit 1
585
	fi
586

    
587
	if ! is_pkg_installed ${_pkg_name}; then
588
		_echo "ERROR: Package ${_pkg_name} is not installed"
589
		_exit 1
590
	fi
591

    
592
	_exec "pkg_with_pb delete ${_pkg_name}" "Removing ${_pkg_name}"
593
	_exec "pkg autoremove" "Removing stale packages" mute ignore_result
594
}
595

    
596
# Reinstall every pfSense-pkg-* package
597
pkg_reinstall_all() {
598
	for _pkg in $(pkg query -e '%a == 0' %n); do
599
		case ${_pkg} in "${pkg_prefix}"* )
600
			_echo "Reinstalling ${_pkg}"
601
			pkg_install ${_pkg} 1
602
			;;
603
		esac
604
	done
605
}
606

    
607
do_reboot() {
608
	_echo "Upgrade is complete.  Rebooting in 10 seconds."
609
	echo "Upgrade is complete.  Rebooting in 10 seconds." | wall
610
	/etc/rc.notify_message -e -g -m "Upgrade is complete.  Rebooting in 10 seconds."
611
	if [ -z "${dont_reboot}" ]; then
612
		(sleep 10 && /etc/rc.reboot) &
613
	fi
614
}
615

    
616
pid_file="/var/run/$(basename $0).pid"
617
last_update_file="/var/run/$(basename $0)-last-update"
618
logfile="/cf/conf/upgrade_log.txt"
619
stdout='/dev/null'
620

    
621
# pkg should not ask for confirmations
622
export ASSUME_ALWAYS_YES=true
623

    
624
# Disable automatic update
625
export REPO_AUTOUPDATE=false
626

    
627
export product=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var product_name pfSense)
628
export pkg_prefix=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var pkg_prefix pfSense-pkg-)
629
export platform=$(cat /etc/platform)
630

    
631
USE_MFS_TMPVAR=$(/usr/local/sbin/read_xml_tag.sh boolean system/use_mfs_tmpvar)
632
if [ "${platform}" = "nanobsd" ] || [ "${USE_MFS_TMPVAR}" = "true" ]; then
633
	export PKG_DBDIR=/root/var/db/pkg
634
	export PKG_CACHEDIR=/root/var/cache/pkg
635
fi
636

    
637
# Upgrade process on nanobsd will happen in chroot
638
export pkg_chroot=""
639
export chroot_dir=""
640

    
641
unset dont_reboot
642
unset booting
643
unset boot_stage
644
unset force
645
unset yes
646
unset progress_file
647
unset progress_socket
648
unset action
649
unset action_pkg
650
unset force_ipv4
651
unset force_ipv6
652
while getopts 46b:cdfi:hp:l:r:Ruy opt; do
653
	case ${opt} in
654
		4)
655
			if [ -n "${force_ipv6}" ]; then
656
				usage
657
				_exit 1
658
			fi
659
			force_ipv4=1
660
			;;
661
		6)
662
			if [ -n "${force_ipv4}" ]; then
663
				usage
664
				_exit 1
665
			fi
666
			force_ipv6=1
667
			;;
668
		b)
669
			booting=1
670
			boot_stage="${OPTARG}"
671
			;;
672
		c)
673
			action="check"
674
			;;
675
		d)
676
			stdout=''
677
			;;
678
		f)
679
			force=1
680
			;;
681
		i)
682
			if [ -n "${action}" ]; then
683
				usage
684
				_exit 1
685
			fi
686
			action="install"
687
			action_pkg="${OPTARG}"
688
			;;
689
		h)
690
			usage
691
			_exit 0
692
			;;
693
		l)
694
			logfile="${OPTARG}"
695
			if [ -z "${logfile}" ]; then
696
				usage
697
				_exit 1
698
			fi
699
			;;
700
		p)
701
			progress_socket="${OPTARG}"
702
			if [ -z "${progress_socket}" ]; then
703
				usage
704
				_exit 1
705
			fi
706
			;;
707
		r)
708
			if [ -n "${action}" ]; then
709
				usage
710
				_exit 1
711
			fi
712
			action="delete"
713
			action_pkg="${OPTARG}"
714
			;;
715
		R)
716
			dont_reboot=1
717
			;;
718
		u)
719
			if [ -n "${action}" ]; then
720
				usage
721
				_exit 1
722
			fi
723
			action="update"
724
			;;
725
		y)
726
			yes=1
727
			;;
728
		*)
729
			usage
730
			_exit 1
731
			;;
732
	esac
733
done
734

    
735
if [ -n "${force_ipv4}" ]; then
736
	export IP_VERSION="4"
737
elif [ -n "${force_ipv6}" ]; then
738
	export IP_VERSION="6"
739
fi
740

    
741
# Set default action when no parameter is set
742
: ${action:="upgrade"}
743

    
744
if pgrep -qF ${pid_file} >/dev/null 2>&1; then
745
	echo "Another instance is already running... Aborting!"
746
	exit 1
747
fi
748

    
749
if [ -z "${booting}" -o "${boot_stage}" != "2" ]; then
750
	/etc/rc.conf_mount_rw
751
fi
752

    
753
echo $$ > ${pid_file}
754

    
755
trap _exit 1 2 15 EXIT
756

    
757
if [ "${action}" != "upgrade" -a -f "${logfile}" ]; then
758
	rm -f ${logfile}
759
fi
760

    
761
progress_file=${logfile%.*}.json
762

    
763
if [ -e "${progress_file}" ]; then
764
	rm -f ${progress_file}
765
fi
766

    
767
case "${action}" in
768
	check)
769
		check_upgrade
770
		;;
771
	upgrade)
772
		pkg_upgrade
773
		;;
774
	update)
775
		pkg_update force
776
		;;
777
	install)
778
		if [ ${action_pkg} == "ALL_PACKAGES" ] && [ -n ${force} ]; then
779
			pkg_reinstall_all
780
		else
781
			pkg_install ${action_pkg} ${force}
782
		fi
783
		;;
784
	delete)
785
		pkg_delete ${action_pkg}
786
		;;
787
	*)
788
		_echo "ERROR: Invalid action!"
789
		_exit 1
790
esac
791

    
792
_exit 0
(10-10/23)