gbde revision 140671
1235783Skib#!/bin/sh
2235783Skib#
3235783Skib# This file, originally written by Garrett A. Wollman, is in the public
4235783Skib# domain.
5235783Skib#
6235783Skib# $FreeBSD: head/etc/rc.d/gbde 140671 2005-01-23 16:43:55Z pjd $
7235783Skib#
8235783Skib
9235783Skib# PROVIDE: disks
10235783Skib# KEYWORD: nojail
11235783Skib
12235783Skib. /etc/rc.subr
13235783Skib
14235783Skibname="gbde"
15235783Skibstart_precmd="find_gbde_devices start"
16235783Skibstop_precmd="find_gbde_devices stop"
17235783Skibstart_cmd="gbde_start"
18235783Skibstop_cmd="gbde_stop"
19235783Skib
20235783Skib# Change every ${_src} in ${_str} to ${_dst}.
21235783Skiblocal_tr()
22235783Skib{
23235783Skib	_str=$1
24235783Skib	_src=$2
25235783Skib	_dst=$3
26235783Skib	_out=""
27235783Skib
28254817Sdumbbell	IFS=${_src}
29254817Sdumbbell	for _com in ${_str}; do
30254817Sdumbbell		if [ -z "${_out}" ]; then
31254817Sdumbbell			_out="${_com}"
32254817Sdumbbell		else
33254817Sdumbbell			_out="${_out}${_dst}${_com}"
34254817Sdumbbell		fi
35254817Sdumbbell	done
36254817Sdumbbell	echo "${_out}"
37254817Sdumbbell}
38254817Sdumbbell
39254817Sdumbbellfind_gbde_devices()
40254817Sdumbbell{
41235783Skib	case "${gbde_devices-auto}" in
42235783Skib	[Aa][Uu][Tt][Oo])
43235783Skib		gbde_devices=""
44235783Skib		;;
45235783Skib	*)
46235783Skib		return 0
47235783Skib		;;
48235783Skib	esac
49235783Skib
50235783Skib	case "$1" in
51235783Skib	start)
52235783Skib		fstab="/etc/fstab"
53235783Skib		;;
54235783Skib	stop)
55235783Skib		fstab=$(mktemp /tmp/mtab.XXXXXX)
56235783Skib		mount -p >${fstab}
57235783Skib		;;
58235783Skib	esac
59235783Skib
60235783Skib	#
61235783Skib	# We can't use "mount -p | while ..." because when a shell loop
62235783Skib	# is the target of a pipe it executes in a subshell, and so can't
63235783Skib	# modify variables in the script.
64235783Skib	#
65235783Skib	while read device mountpt type options dump pass; do
66235783Skib		case "$device" in
67254817Sdumbbell		*.bde)
68235783Skib			# Ignore swap devices
69235783Skib			case "$type" in
70235783Skib			swap)
71235783Skib				continue
72235783Skib				;;
73235783Skib			esac
74235783Skib
75235783Skib			case "$options" in
76235783Skib			*noauto*)
77235783Skib				if checkyesno gbde_autoattach_all; then
78235783Skib					gbde_devices="${gbde_devices} ${device}"
79235783Skib				fi
80235783Skib				;;
81235783Skib			*)
82235783Skib				gbde_devices="${gbde_devices} ${device}"
83254817Sdumbbell				;;
84235783Skib			esac
85235783Skib			;;
86235783Skib		esac
87254817Sdumbbell	done <${fstab}
88254817Sdumbbell
89254817Sdumbbell	case "$1" in
90254817Sdumbbell	stop)
91235783Skib		rm -f ${fstab}
92254817Sdumbbell		;;
93254817Sdumbbell	esac
94254817Sdumbbell
95254817Sdumbbell	return 0
96254817Sdumbbell}
97254817Sdumbbell
98254817Sdumbbellgbde_start()
99254817Sdumbbell{
100254817Sdumbbell	for device in $gbde_devices; do
101254817Sdumbbell		parent=${device%.bde}
102254817Sdumbbell		parent=${parent#/dev/}
103254817Sdumbbell		parent_=`local_tr ${parent} '/' '_'`
104254817Sdumbbell		eval "lock=\${gbde_lock_${parent_}-\"${gbde_lockdir}/${parent_}.lock\"}"
105254817Sdumbbell		if [ -e "/dev/${parent}" -a ! -e "/dev/${parent}.bde" ]; then
106254817Sdumbbell			echo "Configuring Disk Encryption for ${parent}."
107254817Sdumbbell
108235783Skib			count=1
109254817Sdumbbell			while [ ${count} -le ${gbde_attach_attempts} ]; do
110235783Skib				if [ -e "${lock}" ]; then
111235783Skib					gbde attach ${parent} -l ${lock}
112235783Skib				else
113235783Skib					gbde attach ${parent}
114235783Skib				fi
115235783Skib				if [ -e "/dev/${parent}.bde" ]; then
116235783Skib					break
117235783Skib				fi
118235783Skib				echo "Attach failed; attempt ${count} of ${gbde_attach_attempts}."
119235783Skib				count=$((${count} + 1))
120235783Skib			done
121254817Sdumbbell		fi
122254817Sdumbbell	done
123254817Sdumbbell}
124254817Sdumbbell
125254817Sdumbbellgbde_stop()
126254817Sdumbbell{
127254817Sdumbbell	for device in $gbde_devices; do
128254817Sdumbbell		parent=${device%.bde}
129254817Sdumbbell		parent=${parent#/dev/}
130254817Sdumbbell		if [ -e "/dev/${parent}.bde" ]; then
131254817Sdumbbell			umount "/dev/${parent}.bde" 2>/dev/null
132254817Sdumbbell			gbde detach "${parent}"
133254817Sdumbbell		fi
134254817Sdumbbell	done
135254817Sdumbbell}
136254817Sdumbbell
137254817Sdumbbellload_rc_config $name
138254817Sdumbbellrun_rc_command "$1"
139254817Sdumbbell