2 # shellcheck disable=SC2154
4 # zfs-import This script will import ZFS pools
6 # chkconfig: 2345 01 99
7 # description: This script will perform a verbatim import of ZFS pools
12 # Provides: zfs-import
13 # Required-Start: mtab
14 # Required-Stop: $local_fs mtab
17 # X-Start-Before: checkfs
18 # X-Stop-After: zfs-mount
19 # Short-Description: Import ZFS pools
20 # Description: Run the `zpool import` command.
23 # NOTE: Not having '$local_fs' on Required-Start but only on Required-Stop
24 # is on purpose. If we have '$local_fs' in both (and X-Start-Before=checkfs)
25 # we get conflicts - import needs to be started extremely early,
26 # but not stopped too late.
28 # Released under the 2-clause BSD license.
30 # This script is based on debian/zfsutils.zfs.init from the
31 # Debian GNU/kFreeBSD zfsutils 8.1-3 package, written by Aurelien Jarno.
33 # Source the common init script
34 . @sysconfdir@/zfs/zfs-functions
36 # ----------------------------------------------------
42 keyword -lxc -openvz -prefix -vserver
45 # Use the zpool cache file to import pools
48 if [ -f "$ZPOOL_CACHE" ]
50 zfs_action "Importing ZFS pool(s)" \
51 "$ZPOOL" import -c "$ZPOOL_CACHE" -N -a
55 # Support function to get a list of all pools, separated with ';'
60 pools=$("$@" 2> /dev/null | \
61 sed -Ee '/pool:|^[a-zA-Z0-9]/!d' -e 's@.*: @@' | \
65 echo "${pools%%;}" # Return without the last ';'.
68 # Find and import all visible pools, even exported ones
69 do_import_all_visible()
71 local already_imported available_pools pool npools
72 local exception dir ZPOOL_IMPORT_PATH RET=0 r=1
74 # In case not shutdown cleanly.
75 # shellcheck disable=SC2154
76 [ -n "$init" ] && rm -f /etc/dfs/sharetab
78 # Just simplify code later on.
79 if [ -n "$USE_DISK_BY_ID" ] && [ "$USE_DISK_BY_ID" != 'yes' ]
81 # It's something, but not 'yes' so it's no good to us.
85 # Find list of already imported pools.
86 already_imported=$(find_pools "$ZPOOL" list -H -oname)
87 available_pools=$(find_pools "$ZPOOL" import)
89 # Just in case - seen it happen (that a pool isn't visible/found
90 # with a simple "zpool import" but only when using the "-d"
91 # option or setting ZPOOL_IMPORT_PATH).
92 if [ -d "/dev/disk/by-id" ]
94 npools=$(find_pools "$ZPOOL" import -d /dev/disk/by-id)
97 # Because we have found extra pool(s) here, which wasn't
98 # found 'normally', we need to force USE_DISK_BY_ID to
99 # make sure we're able to actually import it/them later.
102 if [ -n "$available_pools" ]
104 # Filter out duplicates (pools found with the simpl
105 # "zpool import" but which is also found with the
106 # "zpool import -d ...").
107 npools=$(echo "$npools" | sed "s,$available_pools,,")
109 # Add the list to the existing list of
111 available_pools="$available_pools;$npools"
113 available_pools="$npools"
118 # Filter out any exceptions...
119 if [ -n "$ZFS_POOL_EXCEPTIONS" ]
123 OLD_IFS="$IFS" ; IFS=";"
125 for pool in $available_pools
127 for exception in $ZFS_POOL_EXCEPTIONS
129 [ "$pool" = "$exception" ] && continue 2
137 apools="$apools;$pool"
145 available_pools="$apools"
148 # For backwards compatibility, make sure that ZPOOL_IMPORT_PATH is set
149 # to something we can use later with the real import(s). We want to
150 # make sure we find all by* dirs, BUT by-vdev should be first (if it
152 if [ -n "$USE_DISK_BY_ID" ] && [ -z "$ZPOOL_IMPORT_PATH" ]
155 dirs="$(for dir in $(echo /dev/disk/by-*)
157 # Ignore by-vdev here - we want it first!
158 echo "$dir" | grep -q /by-vdev && continue
159 [ ! -d "$dir" ] && continue
162 done | sed 's,:$,,g')"
164 if [ -d "/dev/disk/by-vdev" ]
166 # Add by-vdev at the beginning.
167 ZPOOL_IMPORT_PATH="/dev/disk/by-vdev:"
170 # Help with getting LUKS partitions etc imported.
171 if [ -d "/dev/mapper" ]; then
172 if [ -n "$ZPOOL_IMPORT_PATH" ]; then
173 ZPOOL_IMPORT_PATH="$ZPOOL_IMPORT_PATH:/dev/mapper:"
175 ZPOOL_IMPORT_PATH="/dev/mapper:"
179 # ... and /dev at the very end, just for good measure.
180 ZPOOL_IMPORT_PATH="$ZPOOL_IMPORT_PATH$dirs:/dev"
183 # Needs to be exported for "zpool" to catch it.
184 [ -n "$ZPOOL_IMPORT_PATH" ] && export ZPOOL_IMPORT_PATH
186 # Mount all available pools (except those set in ZFS_POOL_EXCEPTIONS.
188 # If not interactive (run from init - variable init='/sbin/init')
189 # we get ONE line for all pools being imported, with just a dot
190 # as status for each pool.
191 # Example: Importing ZFS pool(s)... [OK]
193 # If it IS interactive (started from the shell manually), then we
194 # get one line per pool importing.
195 # Example: Importing ZFS pool pool1 [OK]
196 # Importing ZFS pool pool2 [OK]
198 [ -n "$init" ] && zfs_log_begin_msg "Importing ZFS pool(s)"
199 OLD_IFS="$IFS" ; IFS=";"
200 for pool in $available_pools
202 [ -z "$pool" ] && continue
204 # We have pools that haven't been imported - import them
207 # Not interactive - a dot for each pool.
208 # Except on Gentoo where this doesn't work.
209 zfs_log_progress_msg "."
211 # Interactive - one 'Importing ...' line per pool
212 zfs_log_begin_msg "Importing ZFS pool $pool"
215 # Import by using ZPOOL_IMPORT_PATH (either set above or in
216 # the config file) _or_ with the 'built in' default search
217 # paths. This is the preferred way.
218 # shellcheck disable=SC2086
219 "$ZPOOL" import -N ${ZPOOL_IMPORT_OPTS} "$pool" 2> /dev/null
220 r="$?" ; RET=$((RET + r))
223 # Output success and process the next pool
224 [ -z "$init" ] && zfs_log_end_msg 0
227 # We don't want a fail msg here, we're going to try import
228 # using the cache file soon and that might succeed.
229 [ ! -f "$ZPOOL_CACHE" ] && zfs_log_end_msg "$RET"
231 if [ "$r" -gt 0 ] && [ -f "$ZPOOL_CACHE" ]
233 # Failed to import without a cache file. Try WITH...
234 if [ -z "$init" ] && check_boolean "$VERBOSE_MOUNT"
236 # Interactive + Verbose = more information
237 zfs_log_progress_msg " using cache file"
240 # shellcheck disable=SC2086
241 "$ZPOOL" import -c "$ZPOOL_CACHE" -N ${ZPOOL_IMPORT_OPTS} \
243 r="$?" ; RET=$((RET + r))
246 [ -z "$init" ] && zfs_log_end_msg 0
247 continue 3 # Next pool
249 zfs_log_end_msg "$RET"
252 [ -n "$init" ] && zfs_log_end_msg "$RET"
255 [ -n "$already_imported" ] && [ -z "$available_pools" ] && return 0
262 if check_boolean "$ZPOOL_IMPORT_ALL_VISIBLE"
264 do_import_all_visible
266 # This is the default option
271 # Output the status and list of pools
274 check_module_loaded "zfs" || exit 0
276 "$ZPOOL" status && echo "" && "$ZPOOL" list
281 if check_boolean "$VERBOSE_MOUNT"
283 zfs_log_begin_msg "Checking if ZFS userspace tools present"
288 check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
290 check_boolean "$VERBOSE_MOUNT" && \
291 zfs_log_begin_msg "Loading kernel ZFS infrastructure"
293 if ! load_module "zfs"
295 check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 1
298 check_boolean "$VERBOSE_MOUNT" && zfs_log_end_msg 0
300 do_import && udev_trigger # just to make sure we get zvols.
308 # ----------------------------------------------------
310 if [ ! -e /sbin/openrc-run ]
322 force-reload|condrestart|reload|restart)
326 [ -n "$1" ] && echo "Error: Unknown command $1."
327 echo "Usage: $0 {start|status}"
334 # Create wrapper functions since Gentoo don't use the case part.
335 depend() { do_depend; }
336 start() { do_start; }
337 status() { do_status; }