summaryrefslogtreecommitdiff
path: root/etc/rc.d/iscsid_volumes
blob: 57efed4f0c33a28060598b671ec6b70e9cdf8f00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/sh
#
# $NetBSD: iscsid_volumes,v 1.3 2023/02/18 07:51:52 mlelstv Exp $
#

# PROVIDE: iscsid_volumes
# REQUIRE: iscsid
# BEFORE:  securelevel mountcritremote

$_rc_subr_loaded . /etc/rc.subr

name="iscsid_volumes"
rcvar=$name
start_cmd="iscsid_volumes_start"
stop_cmd="iscsid_volumes_stop"

iscsid_volumes_start()
{
	test -f /etc/iscsi/volumes || return 0

	while read host target digest auth user alias; do
		case $host in
		\#*|"") ;;
		*)
			topts=''
			case $digest in
			*d*) topts="$topts -d";;
			esac
			case $digest in
			*h*) topts="$topts -h";;
			esac

			pass="-"
			mpass="-"

			if [ -f /etc/iscsi/auths ]; then
				while read entry dummy; do
					case $entry in
					\#*|"") ;;
					"$user":*) pass=${entry#*:} ;;
					"$target":*) mpass=${entry#*:} ;;
					esac
				done < /etc/iscsi/auths
			fi

			case $host in
			*:*)
				port=${host#*:}
				host=${host%%:*}
				;;
			*)
				port=3260
				;;
			esac

			echo "Add target ${alias:-$target}"

			out=$(/sbin/iscsictl add_target$topts \
				-a "$host" \
				-p "$port" \
				-n "$target" \
				-t "$auth" \
				-u "$user" \
				-s "$pass" \
				-S "$mpass" \
				-N "${alias:--}")
			echo "$out"

			case $out in
			Added\ Target\ [1-9]*,\ Portal\ [1-9]*\ )
				out=${out% }
				portal=${out##* }
				echo "Login $target via Portal $portal"
				/sbin/iscsictl login -P "$portal"
				;;
			esac
		esac
	done < /etc/iscsi/volumes
}

iscsid_volumes_stop()
{
	test -f /etc/iscsi/volumes || return 0

	while read host target digest auth user alias; do
		case $host in
		\#*|"") ;;
		*)
			echo "Remove target ${alias:-$target}"

			/sbin/iscsictl list_sessions \
			| while read key1 num key2 sesstarget; do
				if [ x"$key1" = x"Session" -a \
				     x"$key2" = x"Target" -a \
				     x"$sesstarget" = x"$target" ]; then
					/sbin/iscsictl logout -I "$num" | grep -v '^OK$'
				fi
			done

			/sbin/iscsictl list_targets \
			| while read num talias ttarget; do
				if [ x"$ttarget" = x"$target" ]; then
					/sbin/iscsictl remove_target -I "$num"
				fi
			done
			;;
		esac
	done < /etc/iscsi/volumes
}

load_rc_config $name
run_rc_command "$1"