Home | History | Annotate | Line # | Download | only in rc.d
      1 #!/bin/sh
      2 #
      3 # $NetBSD: iscsid_volumes,v 1.5 2023/12/25 08:29:05 kre 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 			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 
     91 iscsid_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" ] &&
    104 				   [ x"$key2" = x"Target"  ] &&
    105 				   [ x"$sesstarget" = x"$target" ]
    106 				then
    107 					/sbin/iscsictl logout -I "$num" |
    108 					    grep -v '^OK$'
    109 				fi
    110 			done
    111 
    112 			/sbin/iscsictl list_targets |
    113 			    while read num talias ttarget; do
    114 				if [ x"$ttarget" = x"$target" ]; then
    115 					/sbin/iscsictl remove_target -I "$num"
    116 				fi
    117 			done
    118 			;;
    119 		esac
    120 	done < /etc/iscsi/volumes
    121 }
    122 
    123 load_rc_config $name
    124 run_rc_command "$1"
    125