]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/rc.d/gbde
This commit was generated by cvs2svn to compensate for changes in r145857,
[FreeBSD/FreeBSD.git] / etc / rc.d / gbde
1 #!/bin/sh
2 #
3 # This file, originally written by Garrett A. Wollman, is in the public
4 # domain.
5 #
6 # $FreeBSD$
7 #
8
9 # PROVIDE: disks
10 # KEYWORD: nojail
11
12 . /etc/rc.subr
13
14 name="gbde"
15 start_precmd="find_gbde_devices start"
16 stop_precmd="find_gbde_devices stop"
17 start_cmd="gbde_start"
18 stop_cmd="gbde_stop"
19
20 # Change every ${_src} in ${_str} to ${_dst}.
21 # We cannot use tr(1), sed(1) nor awk(1) here, because this script is executed
22 # before /usr is mounted.
23 local_tr()
24 {
25         _str=$1
26         _src=$2
27         _dst=$3
28         _out=""
29
30         IFS=${_src}
31         for _com in ${_str}; do
32                 if [ -z "${_out}" ]; then
33                         _out="${_com}"
34                 else
35                         _out="${_out}${_dst}${_com}"
36                 fi
37         done
38         echo "${_out}"
39 }
40
41 find_gbde_devices()
42 {
43         case "${gbde_devices-auto}" in
44         [Aa][Uu][Tt][Oo])
45                 gbde_devices=""
46                 ;;
47         *)
48                 return 0
49                 ;;
50         esac
51
52         case "$1" in
53         start)
54                 fstab="/etc/fstab"
55                 ;;
56         stop)
57                 fstab=$(mktemp /tmp/mtab.XXXXXX)
58                 mount -p >${fstab}
59                 ;;
60         esac
61
62         #
63         # We can't use "mount -p | while ..." because when a shell loop
64         # is the target of a pipe it executes in a subshell, and so can't
65         # modify variables in the script.
66         #
67         while read device mountpt type options dump pass; do
68                 case "$device" in
69                 *.bde)
70                         # Ignore swap devices
71                         case "$type" in
72                         swap)
73                                 continue
74                                 ;;
75                         esac
76
77                         case "$options" in
78                         *noauto*)
79                                 if checkyesno gbde_autoattach_all; then
80                                         gbde_devices="${gbde_devices} ${device}"
81                                 fi
82                                 ;;
83                         *)
84                                 gbde_devices="${gbde_devices} ${device}"
85                                 ;;
86                         esac
87                         ;;
88                 esac
89         done <${fstab}
90
91         case "$1" in
92         stop)
93                 rm -f ${fstab}
94                 ;;
95         esac
96
97         return 0
98 }
99
100 gbde_start()
101 {
102         for device in $gbde_devices; do
103                 parent=${device%.bde}
104                 parent=${parent#/dev/}
105                 parent_=`local_tr ${parent} '/' '_'`
106                 eval "lock=\${gbde_lock_${parent_}-\"${gbde_lockdir}/${parent_}.lock\"}"
107                 if [ -e "/dev/${parent}" -a ! -e "/dev/${parent}.bde" ]; then
108                         echo "Configuring Disk Encryption for ${parent}."
109
110                         count=1
111                         while [ ${count} -le ${gbde_attach_attempts} ]; do
112                                 if [ -e "${lock}" ]; then
113                                         gbde attach ${parent} -l ${lock}
114                                 else
115                                         gbde attach ${parent}
116                                 fi
117                                 if [ -e "/dev/${parent}.bde" ]; then
118                                         break
119                                 fi
120                                 echo "Attach failed; attempt ${count} of ${gbde_attach_attempts}."
121                                 count=$((${count} + 1))
122                         done
123                 fi
124         done
125 }
126
127 gbde_stop()
128 {
129         for device in $gbde_devices; do
130                 parent=${device%.bde}
131                 parent=${parent#/dev/}
132                 if [ -e "/dev/${parent}.bde" ]; then
133                         umount "/dev/${parent}.bde" 2>/dev/null
134                         gbde detach "${parent}"
135                 fi
136         done
137 }
138
139 load_rc_config $name
140 run_rc_command "$1"