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