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