tgt (5816B)
1 #!/bin/sh /etc/rc.common 2 START=91 3 STOP=10 4 5 EXTRA_COMMANDS="show" 6 EXTRA_HELP=" show Show current configuration of tgtd" 7 8 NAME=tgt 9 PROG=/usr/sbin/tgtd 10 USE_PROCD=1 11 12 tgtadm="/usr/sbin/tgtadm --lld iscsi" 13 logger="logger -p daemon.err -s -t $NAME" 14 15 validate_lun_section() { 16 uci_validate_section tgt lun $1 \ 17 'device:or(file, device)' \ 18 'type:or("disk", "cd", "pt"):disk' \ 19 'bstype:or("rdwr", "aio", "sg"):rdwr' \ 20 'sync:bool:0' \ 21 'direct:bool:0' \ 22 'blocksize:uinteger' \ 23 'mode_page:string' \ 24 'product_id:string' \ 25 'product_rev:string' \ 26 'readonly:bool:0' \ 27 'removable:bool' \ 28 'scsi_id:string' \ 29 'scsi_sn:string' \ 30 'sense_format:range(0, 1)' \ 31 'vendor_id:string' 32 } 33 34 handle_lun() { 35 local tgt_lun=$1 36 local tgtid=$2 37 local readonly device type bstype sync direct 38 local my_tgtid=${tgt_lun%_*} 39 local lun=${tgt_lun#*_} 40 41 [ $my_tgtid -eq $tgtid ] || return 0 42 43 validate_lun_section $tgt_lun || { 44 $logger "Validation failed for LUN $tgt_lun" 45 return 1 46 } 47 [ "$device" ] || { 48 $logger "Device is required for target $tgt_lun" 49 return 1 50 } 51 52 if [ $sync -ne 0 -o $direct -ne 0 ]; then 53 local bsoflags 54 [ $sync -ne 0 ] && bsoflags="sync" 55 [ $direct -ne 0 ] && bsoflags="direct" 56 [ $sync -ne 0 -a $direct -ne 0 ] && bsoflags="sync:direct" 57 bsoflags="--bsoflags $bsoflags" 58 fi 59 60 blocksize=${blocksize+--blocksize=$blocksize} 61 local params='' i 62 for i in mode_page product_id product_rev readonly removable scsi_id scsi_sn sense_format vendor_id; do 63 eval params=\${$i+$i=\$$i,}\$params 64 done 65 66 local _tgtadm="$tgtadm --mode logicalunit --tid $tgtid --lun $lun" 67 $_tgtadm --op new --backing-store $device --device-type $type --bstype $bstype --bstype $bstype $bsoflags $blocksize || { 68 $logger "Failed to create lun $tgt_lun" 69 return 1 70 } 71 $_tgtadm --op update --param $params || { 72 $logger "Failed to update lun $tgt_lun" 73 return 1 74 } 75 } 76 77 validate_account_section () { 78 uci_validate_section tgt account $1 \ 79 'target:list(uinteger)' \ 80 'user:string' \ 81 'password:string' \ 82 'outgoing:bool:0' 83 } 84 85 handle_account() { 86 local _tgtadm="$tgtadm --mode account" 87 local user password target outgoing 88 89 validate_account_section $1 || { 90 $logger "Validation failed for account ${user:-$1}" 91 return 1 92 } 93 [ "$user" ] || { 94 $logger "User is required for account $1. Run 'uci show tgt.$1' and check options" 95 return 1 96 } 97 [ "$target" ] || { 98 $logger "Target is required for account $user" 99 return 1 100 } 101 [ "$password" ] || { 102 $logger "Password is required for account $user" 103 return 1 104 } 105 $_tgtadm --op new --user "$user" --password "$password" || { 106 $logger "Failed to create user $username" 107 return 1 108 } 109 } 110 111 bind_account_to_target() { 112 local _tgtadm="$tgtadm --mode account" 113 local tgtid=$2 user password outgoing 114 115 validate_account_section $1 || { 116 $logger "Validation failed for account ${user:-$1}" 117 return 1 118 } 119 120 [ "$outgoing" -ne 0 ] && outgoing=--outgoing || outgoing="" 121 local t 122 for t in $target; do 123 [ "$t" -eq "$tgtid" ] && { 124 $_tgtadm --op bind --tid $tgtid --user "$user" $outgoing || { 125 $logger "Failed to bind user $username to target $tgtid" 126 return 1 127 } 128 } 129 done 130 return 0 131 } 132 133 validate_target_section() { 134 uci_validate_section tgt target $1 \ 135 'name:string:iqn.2012-06.org.openwrt' \ 136 'allow_address:list(string):ALL' \ 137 'allow_name:list(string)' 138 } 139 140 handle_target() { 141 local tgtid=$1 142 local _tgtadm="$tgtadm --mode target" 143 local name allow 144 145 [ $tgtid -ge 0 ] || return 1 146 validate_target_section $tgtid || { 147 $logger "Validation failed for target $tgtid" 148 return 1 149 } 150 $_tgtadm --op new --tid $tgtid --targetname $name || { 151 $logger "Failed to create target $tgtid" 152 return 1 153 } 154 local i 155 for i in $allow_address; do 156 $_tgtadm --op bind --tid $tgtid --initiator-address $i || { 157 $logger "Failed to set allow $i to connect to target $tgtid" 158 return 1 159 } 160 done 161 for i in $allow_name; do 162 $_tgtadm --op bind --tid $tgtid --initiator-name $i || { 163 $logger "Failed to set allow $i to connect to target $tgtid" 164 return 1 165 } 166 done 167 config_foreach handle_lun lun $tgtid || return 1 168 config_foreach bind_account_to_target account $tgtid || return 1 169 } 170 171 configure() { 172 config_load $NAME 173 $tgtadm --mode sys --op update --name State -v offline || { 174 $logger "Failed to set system state to Offline" 175 return 1 176 } 177 config_foreach handle_account account || return 1 178 config_foreach handle_target target || return 1 179 $tgtadm --mode sys --op update --name State -v ready || { 180 $logger "Failed to set system state to Ready" 181 return 1 182 } 183 return 0 184 } 185 186 validate_tgt_section() { 187 uci_validate_section tgt options $1 \ 188 'iothreads:uinteger' \ 189 'portal:list(string)' \ 190 'nop_interval:uinteger' \ 191 'nop_count:uinteger' 192 } 193 194 start_service() { 195 local iothreads portal nop_interval nop_count 196 validate_tgt_section tgt || { 197 $logger "Validation failed for tgt options" 198 return 1 199 } 200 procd_open_instance 201 procd_set_param command $PROG -f 202 [ "$iothreads" ] && procd_append_param command -t $iothreads 203 [ "${portal}${nop_interval}${nop_count}" ] && { 204 local iscsi="" i 205 for i in nop_interval nop_count; do 206 eval iscsi=\${$i+$i=\$$i,}\$iscsi 207 done 208 for i in $portal; do 209 iscsi="portal=$i,$iscsi" 210 done 211 procd_append_param command --iscsi $iscsi 212 } 213 procd_set_param respawn 214 procd_close_instance 215 logger -p daemon.info -t $NAME -s "Configuration will be loaded in seconds" 216 ( sleep 5; configure || { stop_service; exit 1; } ) & 217 } 218 219 stop_service() { 220 $tgtadm --mode sys --op update --name State -v offline || { 221 $logger "Failed to set system state to Offline" 222 return 1 223 } 224 $tgtadm --mode target --op show \ 225 | awk '$1 == "Target" {sub(/:/,"",$2); print $2}' \ 226 | xargs -r -n1 $tgtadm --mode target --op delete --force --tid 227 $tgtadm --mode sys --op delete 228 } 229 230 reload_service() { 231 stop_service 232 start_service 233 } 234 235 show() { 236 $tgtadm --mode target --op show 237 }