xref: /freebsd/libexec/rc/rc.d/dumpon (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1#!/bin/sh
2#
3# $FreeBSD$
4#
5
6# PROVIDE: dumpon
7# BEFORE: disks
8# KEYWORD: nojail
9
10. /etc/rc.subr
11
12name="dumpon"
13desc="Dump kernel corefiles from swap to disk"
14start_cmd="dumpon_start"
15stop_cmd="dumpon_stop"
16
17dumpon_try()
18{
19	local flags
20
21	flags=${dumpon_flags}
22	if [ -n "${dumppubkey}" ]; then
23		warn "The dumppubkey variable is deprecated.  Use dumpon_flags."
24		flags="${flags} -k ${dumppubkey}"
25	fi
26	/sbin/dumpon ${flags} "${1}"
27	if [ $? -eq 0 ]; then
28		# Make a symlink in devfs for savecore
29		ln -fs "${1}" /dev/dumpdev
30		return 0
31	fi
32	warn "unable to specify $1 as a dump device"
33	return 1
34}
35
36dumpon_warn_unencrypted()
37{
38	if [ -n "${dumppubkey}" ]; then
39		return
40	fi
41	for flag in ${dumpon_flags}; do
42		if [ $flag = -k ]; then
43			return
44		fi
45	done
46	warn "Kernel dumps will be written to the swap partition without encryption."
47}
48
49dumpon_start()
50{
51	# Enable dumpdev so that savecore can see it. Enable it
52	# early so a crash early in the boot process can be caught.
53	#
54	case ${dumpdev} in
55	[Nn][Oo])
56		;;
57	[Aa][Uu][Tt][Oo] | '')
58		root_hold_wait
59		dev=$(/bin/kenv -q dumpdev)
60		if [ -n "${dev}" ] ; then
61			dumpon_try "${dev}"
62			return $?
63		fi
64		if [ -z ${dumpdev} ] ; then
65			return
66		fi
67		while read dev mp type more ; do
68			[ "${type}" = "swap" ] || continue
69			case ${dev} in
70			*.bde|*.eli)
71				dumpon_warn_unencrypted
72				dev=${dev%.*}
73				;;
74			esac
75			[ -c "${dev}" ] || continue
76			dumpon_try "${dev}" 2>/dev/null && return 0
77		done </etc/fstab
78		echo "No suitable dump device was found." 1>&2
79		return 1
80		;;
81	*)
82		root_hold_wait
83		dumpon_try "${dumpdev}"
84		;;
85	esac
86}
87
88dumpon_stop()
89{
90	case ${dumpdev} in
91	[Nn][Oo])
92		;;
93	*)
94		rm -f /dev/dumpdev
95		/sbin/dumpon -v off
96		;;
97	esac
98}
99
100load_rc_config $name
101run_rc_command "$1"
102