]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/bsdconfig/share/media/ufs.subr
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / bsdconfig / share / media / ufs.subr
1 if [ ! "$_MEDIA_UFS_SUBR" ]; then _MEDIA_UFS_SUBR=1
2 #
3 # Copyright (c) 2012-2013 Devin Teske
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 AUTHOR 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 AUTHOR 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 # $FreeBSD$
28 #
29 ############################################################ INCLUDES
30
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." media/ufs.subr
34 f_include $BSDCFG_SHARE/device.subr
35 f_include $BSDCFG_SHARE/dialog.subr
36 f_include $BSDCFG_SHARE/media/common.subr
37 f_include $BSDCFG_SHARE/struct.subr
38 f_include $BSDCFG_SHARE/variable.subr
39
40 BSDCFG_LIBE="/usr/libexec/bsdconfig"
41 f_include_lang $BSDCFG_LIBE/include/messages.subr
42
43 ############################################################ GLOBALS
44
45 UFS_MOUNTED=
46
47 ############################################################ FUNCTIONS
48
49 # f_media_set_ufs
50 #
51 # Return success if we both found and set the media type to be a UFS partition.
52 # Variables from variable.subr that can be used to script user input:
53 #
54 #       VAR_UFS_PATH
55 #               Path to a UFS character device node to be used with mount(8) in
56 #               mounting a UFS formatted partition. Valid examples include:
57 #                       /dev/da0s1a
58 #                       /dev/ad4s1e
59 #               However, other forms may be valid (see mount(8) for additional
60 #               information).
61 #
62 f_media_set_ufs()
63 {
64         local ufs
65
66         f_media_close
67
68         local devs ndevs
69         f_device_find "" $DEVICE_TYPE_UFS devs
70         ndevs=$( set -- $devs; echo $# )
71
72         if [ ${ndevs:=0} -eq 0 ]; then
73                 f_variable_get_value $VAR_UFS_PATH \
74                     "$msg_enter_the_device_name_of_a_ufs_formatted_partition"
75                 f_getvar $VAR_UFS_PATH ufs
76                 [ "$ufs" ] || return $FAILURE
77
78                 local fstype
79                 fstype=$( df -nT $ufs 2> /dev/null |
80                         awk '!/Type/{print $2;exit}' )
81
82                 f_struct_new DEVICE device_ufs
83                 device_ufs set   name     ${fstype:-ufs}
84                 device_ufs set   devname  "$ufs"
85                 device_ufs set   get      f_media_get_ufs
86                 device_ufs set   init     f_media_init_ufs
87                 device_ufs set   shutdown f_media_shutdown_ufs
88                 device_ufs unset private
89
90                 f_struct_copy device_ufs device_media
91                 f_struct_free device_ufs
92         elif [ $ndevs -gt 1 ]; then
93                 local title="$msg_choose_a_ufs_partition"
94                 local prompt="$msg_please_select_ufs_partition"
95                 local hline=""
96
97                 local dev retval
98                 dev=$( f_device_menu \
99                         "$title" "$prompt" "$hline" $DEVICE_TYPE_UFS \
100                         2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
101                 retval=$?
102                 [ "$dev" ] || return $FAILURE
103
104                 f_struct_copy device_$dev device_media
105                 [ $retval -eq $SUCCESS ] || return $FAILURE
106         else
107                 f_struct_copy device_$devs device_media
108         fi
109
110         f_struct device_media || return $FAILURE
111 }
112
113 # f_media_init_ufs $device
114 #
115 # Initializes the UFS media device. Returns success if able to mount the UFS
116 # partition device using mount(1).
117 #
118 f_media_init_ufs()
119 {
120         local dev="$1" devname err
121
122         device_$dev get devname devname || return $FAILURE
123         f_dprintf "Init routine called for UFS device. devname=[%s]" \
124                   "$devname"
125
126         if [ "$UFS_MOUNTED" ]; then
127                 f_dprintf "UFS device already mounted."
128                 return $SUCCESS
129         fi
130
131         if [ ! -e "$devname" ]; then
132                 f_show_msg "$msg_no_such_file_or_directory" \
133                            "f_media_init_ufs" "$devname"
134                 return $FAILURE
135         fi
136
137         if [ ! -e "$MOUNTPOINT" ] &&
138            ! err=$( mkdir -p "$MOUNTPOINT" 2>&1 )
139         then
140                 f_dialog_msgbox "$err"
141                 return $FAILURE
142         fi
143
144         if ! err=$( mount "$devname" "$MOUNTPOINT" 2>&1 )
145         then
146                 err="${err#mount: }"; err="${err#$devname : }"
147                 f_show_msg "$msg_error_mounting_device" \
148                            "$devname" "$MOUNTPOINT" "$err"
149                 return $FAILURE
150         fi
151         UFS_MOUNTED=1
152         return $SUCCESS
153 }
154
155 # f_media_get_ufs $device $file [$probe_only]
156 #
157 # Returns data from $file on a mounted UFS partition device. Similar to cat(1).
158 # If $probe_only is present and non-NULL, returns success if $file exists.
159 #
160 f_media_get_ufs()
161 {
162         local dev="$1" file="$2" probe_only="$3"
163
164         f_dprintf "f_media_get_ufs: dev=[%s] file=[%s] probe_only=%s" \
165                   "$dev" "$file" "$probe_only"
166
167         f_media_generic_get "$MOUNTPOINT" "$file" "$probe_only"
168 }
169
170 # f_media_shutdown_ufs $device
171 #
172 # Shuts down the UFS device using umount(8). Return status should be ignored.
173 #
174 f_media_shutdown_ufs()
175 {
176         local dev="$1" err
177
178         [ "$UFS_MOUNTED" ] || return
179
180         if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); then
181                 err="${err#umount: }"; err="${err#*: }"
182                 f_show_msg "$msg_could_not_unmount_the_ufs_partition" \
183                            "$MOUNTPOINT" "$err"
184         else
185                 UFS_MOUNTED=
186         fi
187 }
188
189 ############################################################ MAIN
190
191 f_dprintf "%s: Successfully loaded." media/ufs.subr
192
193 fi # ! $_MEDIA_UFS_SUBR