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