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