xref: /freebsd-14.2/libexec/rc/rc.d/gptboot (revision d0b2dbfa)
1*0696600cSBjoern A. Zeeb#!/bin/sh
2*0696600cSBjoern A. Zeeb#
3*0696600cSBjoern A. Zeeb# Copyright (c) 2010 Pawel Jakub Dawidek <[email protected]>
4*0696600cSBjoern A. Zeeb# All rights reserved.
5*0696600cSBjoern A. Zeeb#
6*0696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
7*0696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
8*0696600cSBjoern A. Zeeb# are met:
9*0696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
10*0696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
11*0696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
12*0696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
13*0696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
14*0696600cSBjoern A. Zeeb#
15*0696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16*0696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*0696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*0696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19*0696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*0696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*0696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*0696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*0696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*0696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*0696600cSBjoern A. Zeeb# SUCH DAMAGE.
26*0696600cSBjoern A. Zeeb#
27*0696600cSBjoern A. Zeeb#
28*0696600cSBjoern A. Zeeb
29*0696600cSBjoern A. Zeeb# PROVIDE: gptboot
30*0696600cSBjoern A. Zeeb# REQUIRE: mountcritremote
31*0696600cSBjoern A. Zeeb# KEYWORD: nojail
32*0696600cSBjoern A. Zeeb
33*0696600cSBjoern A. Zeeb. /etc/rc.subr
34*0696600cSBjoern A. Zeeb
35*0696600cSBjoern A. Zeebname="gptboot"
36*0696600cSBjoern A. Zeebrcvar="gptboot_enable"
37*0696600cSBjoern A. Zeebstart_cmd="gptboot_report"
38*0696600cSBjoern A. Zeeb
39*0696600cSBjoern A. Zeebgptboot_report()
40*0696600cSBjoern A. Zeeb{
41*0696600cSBjoern A. Zeeb	gpart show | \
42*0696600cSBjoern A. Zeeb		egrep '(^=>| freebsd-ufs .*(\[|,)(bootfailed|bootonce)(,|\]))' | \
43*0696600cSBjoern A. Zeeb		sed 's/^=>//' | \
44*0696600cSBjoern A. Zeeb		egrep -v '(\[|,)bootme(,|\])' | \
45*0696600cSBjoern A. Zeeb	while read start size pos type attrs rest; do
46*0696600cSBjoern A. Zeeb		case "${pos}" in
47*0696600cSBjoern A. Zeeb		[0-9]*)
48*0696600cSBjoern A. Zeeb			if [ -n "${disk}" ]; then
49*0696600cSBjoern A. Zeeb				part="${disk}p${pos}"
50*0696600cSBjoern A. Zeeb				echo "${attrs}" | egrep -q '(\[|,)bootfailed(,|\])'
51*0696600cSBjoern A. Zeeb				bootfailed=$?
52*0696600cSBjoern A. Zeeb				echo "${attrs}" | egrep -q '(\[|,)bootonce(,|\])'
53*0696600cSBjoern A. Zeeb				bootonce=$?
54*0696600cSBjoern A. Zeeb				if [ ${bootfailed} -eq 0 ]; then
55*0696600cSBjoern A. Zeeb					logger -t gptboot -p local0.notice "Boot from ${part} failed."
56*0696600cSBjoern A. Zeeb					gpart unset -a bootfailed -i ${pos} ${disk} >/dev/null
57*0696600cSBjoern A. Zeeb				elif [ ${bootonce} -eq 0 ]; then
58*0696600cSBjoern A. Zeeb					# We want to log success after all failures.
59*0696600cSBjoern A. Zeeb					echo -n "Boot from ${part} succeeded."
60*0696600cSBjoern A. Zeeb					gpart unset -a bootonce -i ${pos} ${disk} >/dev/null
61*0696600cSBjoern A. Zeeb				fi
62*0696600cSBjoern A. Zeeb			fi
63*0696600cSBjoern A. Zeeb			;;
64*0696600cSBjoern A. Zeeb		*)
65*0696600cSBjoern A. Zeeb			if [ "${type}" = "GPT" ]; then
66*0696600cSBjoern A. Zeeb				disk="${pos}"
67*0696600cSBjoern A. Zeeb			else
68*0696600cSBjoern A. Zeeb				disk=""
69*0696600cSBjoern A. Zeeb			fi
70*0696600cSBjoern A. Zeeb			;;
71*0696600cSBjoern A. Zeeb		esac
72*0696600cSBjoern A. Zeeb	done | logger -t gptboot -p local0.notice
73*0696600cSBjoern A. Zeeb}
74*0696600cSBjoern A. Zeeb
75*0696600cSBjoern A. Zeebload_rc_config $name
76*0696600cSBjoern A. Zeebrun_rc_command "$1"
77