Project

General

Profile

Download (18.3 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 "	-y          - Consider yes as the answer for any possible interaction" >&2
61
	echo "" >&2
62
	echo "Following parameters are mutually exclusive:" >&2
63
	echo "	-i PKG_NAME - Install package PKG_NAME" >&2
64
	echo "	-r PKG_NAME - Remove package PKG_NAME" >&2
65
	echo "	-u          - Update repository information" >&2
66
}
67

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

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

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

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

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

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

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

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

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

    
127
	pkg_lock ${kernel_pkg}
128

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

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

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

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

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

    
147
	exit ${_rc}
148
}
149

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

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

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

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

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

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

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

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

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

    
193
	fi
194

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

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

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

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

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

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

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

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

    
234
pkg_update() {
235
	local _run_update=1
236

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

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

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

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

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

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

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

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

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

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

    
289
		pkg_update
290

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

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

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

    
306
		pkg_unlock ${kernel_pkg}
307

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

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

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

    
343
		# Download all upgrade packages first
344
		fetch_upgrade_packages
345

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

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

    
353
		if [ -n "${need_reboot}" -a "${platform}" != "nanobsd" ]; then
354
			_echo "Rebooting..."
355
			/etc/rc.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
			_echo "Rebooting..."
385
			/etc/rc.reboot &
386
			_exit 0
387
		fi
388

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

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

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

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

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

    
423
	pkg_update
424

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

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

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

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

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

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

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

    
466
	pkg_update force
467

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

    
473
}
474

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
555
	pkg_update
556

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

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

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

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

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

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

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

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

    
608
pid_file="/var/run/$(basename $0).pid"
609
last_update_file="/var/run/$(basename $0)-last-update"
610
logfile="/cf/conf/upgrade_log.txt"
611
stdout='/dev/null'
612

    
613
# pkg should not ask for confirmations
614
export ASSUME_ALWAYS_YES=true
615

    
616
# Disable automatic update
617
export REPO_AUTOUPDATE=false
618

    
619
export product=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var product_name pfSense)
620
export pkg_prefix=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var pkg_prefix pfSense-pkg-)
621
export platform=$(cat /etc/platform)
622

    
623
USE_MFS_TMPVAR=$(/usr/local/sbin/read_xml_tag.sh boolean system/use_mfs_tmpvar)
624
if [ "${platform}" = "nanobsd" ] || [ "${USE_MFS_TMPVAR}" = "true" ]; then
625
	export PKG_DBDIR=/root/var/db/pkg
626
	export PKG_CACHEDIR=/root/var/cache/pkg
627
fi
628

    
629
# Upgrade process on nanobsd will happen in chroot
630
export pkg_chroot=""
631
export chroot_dir=""
632

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

    
723
if [ -n "${force_ipv4}" ]; then
724
	export IP_VERSION="4"
725
elif [ -n "${force_ipv6}" ]; then
726
	export IP_VERSION="6"
727
fi
728

    
729
# Set default action when no parameter is set
730
: ${action:="upgrade"}
731

    
732
if pgrep -qF ${pid_file} >/dev/null 2>&1; then
733
	echo "Another instance is already running... Aborting!"
734
	exit 1
735
fi
736

    
737
if [ -z "${booting}" -o "${boot_stage}" != "2" ]; then
738
	/etc/rc.conf_mount_rw
739
fi
740

    
741
echo $$ > ${pid_file}
742

    
743
trap _exit 1 2 15 EXIT
744

    
745
if [ "${action}" != "upgrade" -a -f "${logfile}" ]; then
746
	rm -f ${logfile}
747
fi
748

    
749
progress_file=${logfile%.*}.json
750

    
751
if [ -e "${progress_file}" ]; then
752
	rm -f ${progress_file}
753
fi
754

    
755
case "${action}" in
756
	check)
757
		check_upgrade
758
		;;
759
	upgrade)
760
		pkg_upgrade
761
		;;
762
	update)
763
		pkg_update force
764
		;;
765
	install)
766
		if [ ${action_pkg} == "ALL_PACKAGES" ] && [ -n ${force} ]; then
767
			pkg_reinstall_all
768
		else
769
			pkg_install ${action_pkg} ${force}
770
		fi
771
		;;
772
	delete)
773
		pkg_delete ${action_pkg}
774
		;;
775
	*)
776
		_echo "ERROR: Invalid action!"
777
		_exit 1
778
esac
779

    
780
_exit 0
(10-10/23)