Skip to content
Permalink
Browse files

New service that waits on zvol links to be created

The zfs-volume service scans existing zvols and waits for their
links under /dev to be created. Any service that depends on zvol
links to be there should add a dependency on zfs-volume.service.

Signed-off-by: Pavel Zakharov <pzakharov@delphix.com>
  • Loading branch information...
pzakha committed Jul 1, 2019
1 parent 681a85c commit c32b0fd23fed5a415ad44ae35983c31f2faa3c0d
@@ -0,0 +1,84 @@
#!/bin/sh

count_zvols() {
if [ -z "$zvols" ]; then
echo 0
else
echo "$zvols" | wc -l
fi
}

filter_out_zvols_with_links() {
while read -r zvol; do
if [ ! -e "/dev/$zvol" ]; then
echo "$zvol"
fi
done
}

filter_out_deleted_zvols() {
while read -r zvol; do
if zfs list "$zvol" >/dev/null 2>&1; then
echo "$zvol"
fi
done
}

zvols=$(zfs list -t volume -H -o name)
zvols_count=$(count_zvols)
if [ "$zvols_count" -eq 0 ]; then
echo "No zvols found, nothing to do."
exit 0
fi

echo "Testing $zvols_count zvol links"

outer_loop=0
while [ "$outer_loop" -lt 20 ]; do
outer_loop=$((outer_loop + 1))

old_zvols_count=$(count_zvols)

inner_loop=0
while [ "$inner_loop" -lt 30 ]; do
inner_loop=$((inner_loop + 1))

zvols="$(echo "$zvols" | filter_out_zvols_with_links)"

zvols_count=$(count_zvols)
if [ "$zvols_count" -eq 0 ]; then
echo "All zvol links are now present."
exit 0
fi
sleep 1
done

echo "Still waiting on $zvols_count zvol links ..."
#
# Although zvols should normally not be deleted at boot time,
# if that is the case then their links will be missing and
# we would stall.
#
if [ "$old_zvols_count" -eq "$zvols_count" ]; then
echo "No progress since last loop."
echo "Checking if any zvols were deleted."

zvols=$(echo "$zvols" | filter_out_deleted_zvols)
zvols_count=$(count_zvols)

if [ "$old_zvols_count" -ne "$zvols_count" ]; then
echo "$((old_zvols_count - zvols_count)) zvol(s) deleted."
fi

if [ "$zvols_count" -ne 0 ]; then
echo "Remaining zvols:"
echo "$zvols"
else
echo "All zvol links are now present."
exit 0
fi
fi
done

echo "Timed out waiting on zvol links"
exit 1
@@ -5,4 +5,5 @@ enable zfs-import.target
enable zfs-mount.service
enable zfs-share.service
enable zfs-zed.service
enable zfs-volume.service
enable zfs.target
@@ -8,6 +8,7 @@ systemdunit_DATA = \
zfs-mount.service \
zfs-share.service \
zfs-import.target \
zfs-volume.service \
zfs.target

EXTRA_DIST = \
@@ -17,6 +18,7 @@ EXTRA_DIST = \
$(top_srcdir)/etc/systemd/system/zfs-mount.service.in \
$(top_srcdir)/etc/systemd/system/zfs-share.service.in \
$(top_srcdir)/etc/systemd/system/zfs-import.target.in \
$(top_srcdir)/etc/systemd/system/zfs-volume.service.in \
$(top_srcdir)/etc/systemd/system/zfs.target.in \
$(top_srcdir)/etc/systemd/system/50-zfs.preset.in

@@ -0,0 +1,13 @@
[Unit]
Description=Wait for ZFS Volume (zvol) links in /dev
DefaultDependencies=no
After=systemd-udev-settle.service
After=zfs-import.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/lib/zfs-linux/zvol-wait

[Install]
WantedBy=zfs.target
@@ -321,7 +321,7 @@ image which is ZFS aware.

%if 0%{?_systemd}
%define systemd --enable-systemd --with-systemdunitdir=%{_unitdir} --with-systemdpresetdir=%{_presetdir} --with-systemdmodulesloaddir=%{_modulesloaddir} --with-systemdgeneratordir=%{_systemdgeneratordir} --disable-sysvinit
%define systemd_svcs zfs-import-cache.service zfs-import-scan.service zfs-mount.service zfs-share.service zfs-zed.service zfs.target zfs-import.target
%define systemd_svcs zfs-import-cache.service zfs-import-scan.service zfs-mount.service zfs-share.service zfs-zed.service zfs.target zfs-import.target zfs-volume.service
%else
%define systemd --enable-sysvinit --disable-systemd
%endif

0 comments on commit c32b0fd

Please sign in to comment.
You can’t perform that action at this time.