iscsid_volumes revision 1.3
1#!/bin/sh
2#
3# $NetBSD: iscsid_volumes,v 1.3 2023/02/18 07:51:52 mlelstv Exp $
4#
5
6# PROVIDE: iscsid_volumes
7# REQUIRE: iscsid
8# BEFORE:  securelevel mountcritremote
9
10$_rc_subr_loaded . /etc/rc.subr
11
12name="iscsid_volumes"
13rcvar=$name
14start_cmd="iscsid_volumes_start"
15stop_cmd="iscsid_volumes_stop"
16
17iscsid_volumes_start()
18{
19	test -f /etc/iscsi/volumes || return 0
20
21	while read host target digest auth user alias; do
22		case $host in
23		\#*|"") ;;
24		*)
25			topts=''
26			case $digest in
27			*d*) topts="$topts -d";;
28			esac
29			case $digest in
30			*h*) topts="$topts -h";;
31			esac
32
33			pass="-"
34			mpass="-"
35
36			if [ -f /etc/iscsi/auths ]; then
37				while read entry dummy; do
38					case $entry in
39					\#*|"") ;;
40					"$user":*) pass=${entry#*:} ;;
41					"$target":*) mpass=${entry#*:} ;;
42					esac
43				done < /etc/iscsi/auths
44			fi
45
46			case $host in
47			*:*)
48				port=${host#*:}
49				host=${host%%:*}
50				;;
51			*)
52				port=3260
53				;;
54			esac
55
56			echo "Add target ${alias:-$target}"
57
58			out=$(/sbin/iscsictl add_target$topts \
59				-a "$host" \
60				-p "$port" \
61				-n "$target" \
62				-t "$auth" \
63				-u "$user" \
64				-s "$pass" \
65				-S "$mpass" \
66				-N "${alias:--}")
67			echo "$out"
68
69			case $out in
70			Added\ Target\ [1-9]*,\ Portal\ [1-9]*\ )
71				out=${out% }
72				portal=${out##* }
73				echo "Login $target via Portal $portal"
74				/sbin/iscsictl login -P "$portal"
75				;;
76			esac
77		esac
78	done < /etc/iscsi/volumes
79}
80
81iscsid_volumes_stop()
82{
83	test -f /etc/iscsi/volumes || return 0
84
85	while read host target digest auth user alias; do
86		case $host in
87		\#*|"") ;;
88		*)
89			echo "Remove target ${alias:-$target}"
90
91			/sbin/iscsictl list_sessions \
92			| while read key1 num key2 sesstarget; do
93				if [ x"$key1" = x"Session" -a \
94				     x"$key2" = x"Target" -a \
95				     x"$sesstarget" = x"$target" ]; then
96					/sbin/iscsictl logout -I "$num" | grep -v '^OK$'
97				fi
98			done
99
100			/sbin/iscsictl list_targets \
101			| while read num talias ttarget; do
102				if [ x"$ttarget" = x"$target" ]; then
103					/sbin/iscsictl remove_target -I "$num"
104				fi
105			done
106			;;
107		esac
108	done < /etc/iscsi/volumes
109}
110
111load_rc_config $name
112run_rc_command "$1"
113