xref: /freebsd/libexec/rc/rc.d/dumpon (revision a39a5a6905612447def27b66ffe73b9d11efd80c)
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		while read dev mp type more ; do
65			[ "${type}" = "swap" ] || continue
66			case ${dev} in
67			*.bde|*.eli)
68				dumpon_warn_unencrypted
69				dev=${dev%.*}
70				;;
71			esac
72			[ -c "${dev}" ] || continue
73			dumpon_try "${dev}" 2>/dev/null && return 0
74		done </etc/fstab
75		echo "No suitable dump device was found." 1>&2
76		return 1
77		;;
78	*)
79		root_hold_wait
80		dumpon_try "${dumpdev}"
81		;;
82	esac
83}
84
85dumpon_stop()
86{
87	case ${dumpdev} in
88	[Nn][Oo] | '')
89		;;
90	*)
91		rm -f /dev/dumpdev
92		/sbin/dumpon -v off
93		;;
94	esac
95}
96
97load_rc_config $name
98run_rc_command "$1"
99