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$
2897203Sgordon
2997203Sgordondelete="NO"
30230812Semastekenv=
3197203Sgordonforce="NO"
3297203Sgordonnextboot_file="/boot/nextboot.conf"
3397203Sgordon
34230812Semasteadd_kenv()
35230812Semaste{
36230812Semaste	local var value
37230812Semaste
38230812Semaste	var=$1
39230812Semaste	# strip literal quotes if passed in
40230812Semaste	value=${2%\"*}
41230812Semaste	value=${value#*\"}
42230812Semaste
43230812Semaste	if [ -n "${kenv}" ]; then
44230812Semaste		kenv="${kenv}
45230812Semaste"
46230812Semaste	fi
47230812Semaste	kenv="${kenv}${var}=\"${value}\""
48230812Semaste}
49230812Semaste
5097203Sgordondisplay_usage() {
51230812Semaste	echo "Usage: nextboot [-e variable=value] [-f] [-k kernel] [-o options]"
5297203Sgordon	echo "       nextboot -D"
5397203Sgordon}
5497203Sgordon
55230812Semastewhile getopts "De:fk:o:" argument ; do
56154498Swes	case "${argument}" in
57154498Swes	D)
5897203Sgordon		delete="YES"
5997203Sgordon		;;
60230812Semaste	e)
61230812Semaste		var=${OPTARG%%=*}
62230812Semaste		value=${OPTARG#*=}
63230812Semaste		if [ -z "$var" -o -z "$value" ]; then
64230812Semaste			display_usage
65230812Semaste			exit 1
66230812Semaste		fi
67230812Semaste		add_kenv "$var" "$value"
68230812Semaste		;;
69154498Swes	f)
7097203Sgordon		force="YES"
7197203Sgordon		;;
72154498Swes	k)
73154498Swes		kernel="${OPTARG}"
74230812Semaste		add_kenv kernel "$kernel"
7597203Sgordon		;;
76154498Swes	o)
77230812Semaste		add_kenv kernel_options "${OPTARG}"
7897203Sgordon		;;
7997203Sgordon	*)
8097203Sgordon		display_usage
8197203Sgordon		exit 1
8297203Sgordon		;;
8397203Sgordon	esac
8497203Sgordondone
8597203Sgordon
8697203Sgordonif [ ${delete} = "YES" ]; then
8797203Sgordon	rm -f ${nextboot_file}
8897203Sgordon	exit 0
8997203Sgordonfi
9097203Sgordon
91230812Semasteif [ -z "${kenv}" ]; then
9297203Sgordon	display_usage
9397203Sgordon	exit 1
9497203Sgordonfi
9597203Sgordon
96230812Semasteif [ -n "${kernel}" -a ${force} = "NO" -a ! -d /boot/${kernel} ]; then
9797203Sgordon	echo "Error: /boot/${kernel} doesn't exist. Use -f to override."
9897203Sgordon	exit 1
9997203Sgordonfi
10097203Sgordon
101212789Savgdf -Tn "/boot/" 2>/dev/null | while read _fs _type _other ; do
102212789Savg	[ "zfs" = "${_type}" ] || continue
103212789Savg	cat 1>&2 <<-EOF
104212789Savg		WARNING: loader(8) has only R/O support for ZFS
105212789Savg		nextboot.conf will NOT be reset in case of kernel boot failure
106212789Savg	EOF
107212789Savgdone
108212789Savg
10997203Sgordoncat > ${nextboot_file} << EOF
11097203Sgordonnextboot_enable="YES"
111230812Semaste$kenv
11297203SgordonEOF
113