197203Sgordon#! /bin/sh
297203Sgordon#
3231129Semaste# Copyright (c) 2002 Gordon Tetlow. All rights reserved.
4230812Semaste# Copyright (c) 2012 Sandvine Incorporated. All rights reserved.
597203Sgordon#
6231129Semaste# Redistribution and use in source and binary forms, with or without
7231129Semaste# modification, are permitted provided that the following conditions
8231129Semaste# are met:
9231129Semaste# 1. Redistributions of source code must retain the above copyright
10231129Semaste#    notice, this list of conditions and the following disclaimer.
11231129Semaste# 2. Redistributions in binary form must reproduce the above copyright
12231129Semaste#    notice, this list of conditions and the following disclaimer in the
13231129Semaste#    documentation and/or other materials provided with the distribution.
14231129Semaste#
15231129Semaste# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16231129Semaste# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17231129Semaste# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18231129Semaste# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19231129Semaste# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20231129Semaste# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21231129Semaste# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22231129Semaste# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23231129Semaste# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24231129Semaste# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25231129Semaste# SUCH DAMAGE.
26231129Semaste#
2797203Sgordon# $FreeBSD: releng/11.0/sbin/reboot/nextboot.sh 297980 2016-04-14 18:03:55Z markj $
2897203Sgordon
29297772Smarkjappend="NO"
3097203Sgordondelete="NO"
31230812Semastekenv=
3297203Sgordonforce="NO"
3397203Sgordonnextboot_file="/boot/nextboot.conf"
3497203Sgordon
35230812Semasteadd_kenv()
36230812Semaste{
37230812Semaste	local var value
38230812Semaste
39230812Semaste	var=$1
40230812Semaste	# strip literal quotes if passed in
41230812Semaste	value=${2%\"*}
42230812Semaste	value=${value#*\"}
43230812Semaste
44230812Semaste	if [ -n "${kenv}" ]; then
45230812Semaste		kenv="${kenv}
46230812Semaste"
47230812Semaste	fi
48230812Semaste	kenv="${kenv}${var}=\"${value}\""
49230812Semaste}
50230812Semaste
5197203Sgordondisplay_usage() {
52297772Smarkj	cat <<-EOF
53297980Smarkj	Usage: nextboot [-af] [-e variable=value] [-k kernel] [-o options]
54297772Smarkj	       nextboot -D
55297772Smarkj	EOF
5697203Sgordon}
5797203Sgordon
58297772Smarkjwhile getopts "aDe:fk:o:" argument ; do
59154498Swes	case "${argument}" in
60297772Smarkj	a)
61297772Smarkj		append="YES"
62297772Smarkj		;;
63154498Swes	D)
6497203Sgordon		delete="YES"
6597203Sgordon		;;
66230812Semaste	e)
67230812Semaste		var=${OPTARG%%=*}
68230812Semaste		value=${OPTARG#*=}
69230812Semaste		if [ -z "$var" -o -z "$value" ]; then
70230812Semaste			display_usage
71230812Semaste			exit 1
72230812Semaste		fi
73230812Semaste		add_kenv "$var" "$value"
74230812Semaste		;;
75154498Swes	f)
7697203Sgordon		force="YES"
7797203Sgordon		;;
78154498Swes	k)
79154498Swes		kernel="${OPTARG}"
80230812Semaste		add_kenv kernel "$kernel"
8197203Sgordon		;;
82154498Swes	o)
83230812Semaste		add_kenv kernel_options "${OPTARG}"
8497203Sgordon		;;
8597203Sgordon	*)
8697203Sgordon		display_usage
8797203Sgordon		exit 1
8897203Sgordon		;;
8997203Sgordon	esac
9097203Sgordondone
9197203Sgordon
9297203Sgordonif [ ${delete} = "YES" ]; then
9397203Sgordon	rm -f ${nextboot_file}
9497203Sgordon	exit 0
9597203Sgordonfi
9697203Sgordon
97230812Semasteif [ -z "${kenv}" ]; then
9897203Sgordon	display_usage
9997203Sgordon	exit 1
10097203Sgordonfi
10197203Sgordon
102230812Semasteif [ -n "${kernel}" -a ${force} = "NO" -a ! -d /boot/${kernel} ]; then
10397203Sgordon	echo "Error: /boot/${kernel} doesn't exist. Use -f to override."
10497203Sgordon	exit 1
10597203Sgordonfi
10697203Sgordon
107212789Savgdf -Tn "/boot/" 2>/dev/null | while read _fs _type _other ; do
108212789Savg	[ "zfs" = "${_type}" ] || continue
109212789Savg	cat 1>&2 <<-EOF
110212789Savg		WARNING: loader(8) has only R/O support for ZFS
111212789Savg		nextboot.conf will NOT be reset in case of kernel boot failure
112212789Savg	EOF
113212789Savgdone
114212789Savg
115297772Smarkjset -e
116297772Smarkj
117297772Smarkjnextboot_tmp=$(mktemp $(dirname ${nextboot_file})/nextboot.XXXXXX)
118297772Smarkj
119297772Smarkjif [ ${append} = "YES" -a -f ${nextboot_file} ]; then
120297772Smarkj	cp -f ${nextboot_file} ${nextboot_tmp}
121297772Smarkjfi
122297772Smarkj
123297772Smarkjcat >> ${nextboot_tmp} << EOF
12497203Sgordonnextboot_enable="YES"
125230812Semaste$kenv
12697203SgordonEOF
127297772Smarkj
128297772Smarkjfsync ${nextboot_tmp}
129297772Smarkj
130297772Smarkjmv ${nextboot_tmp} ${nextboot_file}
131