1116456Swollman#!/bin/sh
2116456Swollman#
3116456Swollman# This file, originally written by Garrett A. Wollman, is in the public
4116456Swollman# domain.
5116456Swollman#
6116456Swollman# $FreeBSD$
7116456Swollman#
8116456Swollman
9116456Swollman# PROVIDE: disks
10136224Smtm# KEYWORD: nojail
11116456Swollman
12116456Swollman. /etc/rc.subr
13116456Swollman
14116456Swollmanname="gbde"
15116456Swollmanstart_precmd="find_gbde_devices start"
16116456Swollmanstop_precmd="find_gbde_devices stop"
17116456Swollmanstart_cmd="gbde_start"
18116456Swollmanstop_cmd="gbde_stop"
19116456Swollman
20116456Swollmanfind_gbde_devices()
21116456Swollman{
22116456Swollman	case "${gbde_devices-auto}" in
23116456Swollman	[Aa][Uu][Tt][Oo])
24125384Sdes		gbde_devices=""
25125384Sdes		;;
26116456Swollman	*)
27125384Sdes		return 0
28125384Sdes		;;
29116456Swollman	esac
30116456Swollman
31116456Swollman	case "$1" in
32125384Sdes	start)
33125384Sdes		fstab="/etc/fstab"
34125384Sdes		;;
35125384Sdes	stop)
36125384Sdes		fstab=$(mktemp /tmp/mtab.XXXXXX)
37116456Swollman		mount -p >${fstab}
38125384Sdes		;;
39116456Swollman	esac
40116456Swollman
41116456Swollman	#
42116456Swollman	# We can't use "mount -p | while ..." because when a shell loop
43116456Swollman	# is the target of a pipe it executes in a subshell, and so can't
44116456Swollman	# modify variables in the script.
45116456Swollman	#
46116456Swollman	while read device mountpt type options dump pass; do
47116456Swollman		case "$device" in
48116456Swollman		*.bde)
49116456Swollman			# Ignore swap devices
50116456Swollman			case "$type" in
51116456Swollman			swap)
52125385Sdes				continue
53125385Sdes				;;
54116456Swollman			esac
55116456Swollman
56116456Swollman			case "$options" in
57116456Swollman			*noauto*)
58116456Swollman				if checkyesno gbde_autoattach_all; then
59116456Swollman					gbde_devices="${gbde_devices} ${device}"
60116456Swollman				fi
61116456Swollman				;;
62116456Swollman			*)
63116456Swollman				gbde_devices="${gbde_devices} ${device}"
64116456Swollman				;;
65116456Swollman			esac
66116456Swollman			;;
67116456Swollman		esac
68116456Swollman	done <${fstab}
69116456Swollman
70116456Swollman	case "$1" in
71125384Sdes	stop)
72125384Sdes		rm -f ${fstab}
73125384Sdes		;;
74116456Swollman	esac
75116456Swollman
76116456Swollman	return 0
77116456Swollman}
78116456Swollman
79116456Swollmangbde_start()
80116456Swollman{
81116456Swollman	for device in $gbde_devices; do
82136212Spjd		parent=${device%.bde}
83136212Spjd		parent=${parent#/dev/}
84149049Spjd		parent_=`ltr ${parent} '/' '_'`
85140580Spjd		eval "lock=\${gbde_lock_${parent_}-\"${gbde_lockdir}/${parent_}.lock\"}"
86136212Spjd		if [ -e "/dev/${parent}" -a ! -e "/dev/${parent}.bde" ]; then
87136212Spjd			echo "Configuring Disk Encryption for ${parent}."
88132356Ssimon
89132356Ssimon			count=1
90132356Ssimon			while [ ${count} -le ${gbde_attach_attempts} ]; do
91136193Spjd				if [ -e "${lock}" ]; then
92136212Spjd					gbde attach ${parent} -l ${lock}
93136193Spjd				else
94136212Spjd					gbde attach ${parent}
95136193Spjd				fi
96136212Spjd				if [ -e "/dev/${parent}.bde" ]; then
97132356Ssimon					break
98132356Ssimon				fi
99132356Ssimon				echo "Attach failed; attempt ${count} of ${gbde_attach_attempts}."
100132356Ssimon				count=$((${count} + 1))
101132356Ssimon			done
102116456Swollman		fi
103116456Swollman	done
104116456Swollman}
105116456Swollman
106116456Swollmangbde_stop()
107116456Swollman{
108116456Swollman	for device in $gbde_devices; do
109136212Spjd		parent=${device%.bde}
110136212Spjd		parent=${parent#/dev/}
111136212Spjd		if [ -e "/dev/${parent}.bde" ]; then
112136212Spjd			umount "/dev/${parent}.bde" 2>/dev/null
113136212Spjd			gbde detach "${parent}"
114136212Spjd		fi
115116456Swollman	done
116116456Swollman}
117116456Swollman
118116456Swollmanload_rc_config $name
119116456Swollmanrun_rc_command "$1"
120