1226031Sstas#!/bin/sh
2226031Sstas#
3# Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27#
28
29# PROVIDE: disks
30# KEYWORD: nojail
31
32. /etc/rc.subr
33
34name="geli"
35desc="GELI disk encryption"
36start_precmd='[ -n "$(geli_make_list)" -o -n "${geli_groups}" ]'
37start_cmd="geli_start"
38stop_cmd="geli_stop"
39required_modules="geom_eli:g_eli"
40
41geli_start()
42{
43	devices=`geli_make_list`
44
45	if [ -z "${geli_tries}" ]; then
46		if [ -n "${geli_attach_attempts}" ]; then
47			geli_tries=${geli_attach_attempts}
48		else
49			geli_tries=`${SYSCTL_N} kern.geom.eli.tries`
50		fi
51	fi
52
53	for provider in ${devices}; do
54		provider_=`ltr ${provider} '/-' '_'`
55
56		eval "flags=\${geli_${provider_}_flags}"
57		if [ -z "${flags}" ]; then
58			flags=${geli_default_flags}
59		fi
60		if [ -e "/dev/${provider}" -a ! -e "/dev/${provider}.eli" ]; then
61			echo "Configuring Disk Encryption for ${provider}."
62			count=1
63			while [ ${count} -le ${geli_tries} ]; do
64				geli attach ${flags} ${provider}
65				if [ -e "/dev/${provider}.eli" ]; then
66					break
67				fi
68				echo "Attach failed; attempt ${count} of ${geli_tries}."
69				count=$((count+1))
70			done
71		fi
72	done
73
74	for group in ${geli_groups}; do
75		group_=`ltr ${group} '/-' '_'`
76
77		eval "flags=\${geli_${group_}_flags}"
78		if [ -z "${flags}" ]; then
79			flags=${geli_default_flags}
80		fi
81
82		eval "providers=\${geli_${group_}_devices}"
83		if [ -z "${providers}" ]; then
84			echo "No devices listed in geli group ${group}."
85			continue
86		fi
87
88		if [ -e "/dev/${providers%% *}" -a ! -e "/dev/${providers%% *}.eli" ]; then
89			echo "Configuring Disk Encryption for geli group ${group}, containing ${providers}."
90			count=1
91			while [ ${count} -le ${geli_tries} ]; do
92				geli attach ${flags} ${providers}
93				if [ -e "/dev/${providers%% *}.eli" ]; then
94					break
95				fi
96				echo "Attach failed; attempt ${count} of ${geli_tries}."
97				count=$((count+1))
98			done
99		fi
100	done
101}
102
103geli_stop()
104{
105	devices=`geli_make_list`
106
107	for group in ${geli_groups}; do
108		group_=`ltr ${group} '/-' '_'`
109
110		eval "providers=\${geli_${group_}_devices}"
111
112		devices="${devices} ${providers}"
113	done
114
115	for provider in ${devices}; do
116		if [ -e "/dev/${provider}.eli" ]; then
117			umount "/dev/${provider}.eli" 2>/dev/null
118			geli detach "${provider}"
119		fi
120	done
121}
122
123load_rc_config $name
124
125# doesn't make sense to run in a svcj: config setting
126geli_svcj="NO"
127
128run_rc_command "$1"
129