Skip to content

Instantly share code, notes, and snippets.

@2510
Last active May 11, 2023 22:30
Show Gist options
  • Save 2510/eb9c83c7e8949f857228a4602c14a27a to your computer and use it in GitHub Desktop.
Save 2510/eb9c83c7e8949f857228a4602c14a27a to your computer and use it in GitHub Desktop.
FreeBSD rc.script for activating ZFS pool over iSCSI
#!/bin/sh
# PROVIDE: iscsi-zfs
# REQUIRE: iscsid iscsictl mountcritlocal var
. /etc/rc.subr
name="iscsi_zfs"
start_cmd="iscsi_zfs_start"
stop_cmd="iscsi_zfs_stop"
rcvar="iscsi_zfs_enable"
iscsi_zfs_enable=${iscsi_zfs_enable:-"NO"}
iscsi_zfs_wait_iqn=${iscsi_zfs_wait_iqn:-""}
iscsi_zfs_wait_pool=${iscsi_zfs_wait_pool:-""}
iscsi_zfs_wait_retry=${iscsi_zfs_wait_retry:-"30"}
iscsi_zfs_start()
{
retry=0
while [ "$retry" -lt "${iscsi_zfs_wait_retry}" ]
do
if iscsictl | fgrep "${iscsi_zfs_wait_iqn}" | fgrep "Connected: " >/dev/null; then
break
fi
retry=`expr ${retry} + 1`
sleep 1
done
while [ "$retry" -lt "${iscsi_zfs_wait_retry}" ]
do
if zpool status "${iscsi_zfs_wait_pool}" >/dev/null 2>&1; then
break
fi
retry=`expr ${retry} + 1`
sleep 1
done
while [ "$retry" -lt "${iscsi_zfs_wait_retry}" ]
do
zfs mount -a
if mount | fgrep "${iscsi_zfs_wait_pool}" >/dev/null 2>&1; then
break
fi
retry=`expr ${retry} + 1`
sleep 1
done
}
load_rc_config $name
run_rc_command "$1"
@amikhail74
Copy link

amikhail74 commented Feb 11, 2022

Hi

Thanks for your script. I have an issue I can not resolve. Script does not work for some reason. I appreciate your help

root@iscsi:/usr/local/etc/rc.d # sh ./iscsi_zfs start
export: -zfs_env: bad variable name
root@iscsi:/usr/local/etc/rc.d # uname -a
FreeBSD iscsi 13.0-RELEASE FreeBSD 13.0-RELEASE #0 releng/13.0-n244733-ea31abc261f: Fri Apr 9 04:24:09 UTC 2021 root@releng1.nyi.freebsd.org:/usr/obj/usr/src/amd64.amd64/sys/GENERIC amd64
root@iscsi:/usr/local/etc/rc.d #
root@iscsi:/usr/local/etc/rc.d # iscsictl
Target name Target portal State
iqn.2005-10.org.freenas.ctl:test 192.168.4.7 Connected: da1
root@iscsi:/usr/local/etc/rc.d # zpool status
no pools available
root@iscsi:/usr/local/etc/rc.d #

@2510
Copy link
Author

2510 commented Feb 11, 2022

@amikhail74

name="iscsi-zfs"

This line is causing the problem. It seems name should not contain hyphens nowadays.

Also, a dependency (BEFORE) to xtreemfs-osd, that is useless unless you are using xtreemfs, can be removed.

@amikhail74
Copy link

amikhail74 commented Feb 11, 2022

Thanks a lot for your help! Now it starts but it does not seem to work. The problem that the script expects pools already be imported. But the fact is that after reboot the list of pools is empty. It requres zpool import to be done first. Maybe either the issue with the local iscsictl configuration, or remote iscsi share, or zfs pool requires some special setting.

Upd: It seems that there is a difference between FreeBSD 13 and FreeBSD 12.2. 12.2 works as expected. After reboot 12.2 has a zfs pool in place

@2510
Copy link
Author

2510 commented Feb 11, 2022

Yes, this script expects target pools to be imported already (and imported state persists over reboots).

I'm not sure whether imported pools persists over reboot, but adding zpool import "${iscsi_zfs_wait_pool}" in retry-loop may help.

     while [ "$retry" -lt "${iscsi_zfs_wait_retry}" ]
     do
         if zpool status "${iscsi_zfs_wait_pool}" >/dev/null 2>&1; then
             break
         fi
+        zpool import "${iscsi_zfs_wait_pool}"
         retry=`expr ${retry} + 1`
         sleep 1

@amikhail74
Copy link

It can be even more simple. 'zpool import -a' mounts all imported pools.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment