iscsid_volumes revision 1.4
1#!/bin/sh
2#
3# $NetBSD: iscsid_volumes,v 1.4 2023/07/22 10:31:35 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
39					case $entry in
40					*:chap:*|\
41					*:CHAP:*|\
42					*:none:*)
43						dummy=${entry#*:}
44						entry=${entry%%:*}:${dummy#*:}
45						;;
46					esac
47
48					case $entry in
49					\#*|"") ;;
50					"$user":*) pass=${entry#*:} ;;
51					"$target":*) mpass=${entry#*:} ;;
52					esac
53				done < /etc/iscsi/auths
54			fi
55
56			case $host in
57			*:*)
58				port=${host#*:}
59				host=${host%%:*}
60				;;
61			*)
62				port=3260
63				;;
64			esac
65
66			echo "Add target ${alias:-$target}"
67
68			out=$(/sbin/iscsictl add_target$topts \
69				-a "$host" \
70				-p "$port" \
71				-n "$target" \
72				-t "$auth" \
73				-u "$user" \
74				-s "$pass" \
75				-S "$mpass" \
76				${alias:+-N} ${alias:+"$alias"})
77			echo "$out"
78
79			case $out in
80			Added\ Target\ [1-9]*,\ Portal\ [1-9]*\ )
81				out=${out% }
82				portal=${out##* }
83				echo "Login $target via Portal $portal"
84				/sbin/iscsictl login -P "$portal"
85				;;
86			esac
87		esac
88	done < /etc/iscsi/volumes
89}
90
91iscsid_volumes_stop()
92{
93	test -f /etc/iscsi/volumes || return 0
94
95	while read host target digest auth user alias; do
96		case $host in
97		\#*|"") ;;
98		*)
99			echo "Remove target ${alias:-$target}"
100
101			/sbin/iscsictl list_sessions \
102			| while read key1 num key2 sesstarget; do
103				if [ x"$key1" = x"Session" -a \
104				     x"$key2" = x"Target" -a \
105				     x"$sesstarget" = x"$target" ]; then
106					/sbin/iscsictl logout -I "$num" | grep -v '^OK$'
107				fi
108			done
109
110			/sbin/iscsictl list_targets \
111			| while read num talias ttarget; do
112				if [ x"$ttarget" = x"$target" ]; then
113					/sbin/iscsictl remove_target -I "$num"
114				fi
115			done
116			;;
117		esac
118	done < /etc/iscsi/volumes
119}
120
121load_rc_config $name
122run_rc_command "$1"
123