new package tgt
[openwrt/svn-archive/archive.git] / net / tgt / files / etc / init.d / tgt
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
14 handle_lun() {
15 local tgt_lun=$1
16 local tgtid=$2
17 local ro device type bstype sync direct
18 local my_tgtid=${tgt_lun%_*}
19 local lun=${tgt_lun#*_}
20 [ $my_tgtid -eq $tgtid ] || return 0
21
22 config_get device $1 device ""
23 [ "$device" ] || return 1
24
25 config_get type $1 type disk
26 config_get bstype $1 bstype rdwr
27 config_get_bool readonly $1 readonly 0
28 config_get_bool sync $1 sync 0
29 config_get_bool direct $1 direct 0
30 if [ $sync -ne 0 -o $direct -ne 0 ]; then
31 local bsoflags
32 [ $sync -ne 0 ] && bsoflags="sync"
33 [ $direct -ne 0 ] && bsoflags="direct"
34 [ $sync -ne 0 -a $direct -ne 0 ] && bsoflags="sync:direct"
35 bsoflags="--bsoflags $bsoflags"
36 fi
37
38 local _tgtadm="$tgtadm --mode logicalunit --tid $tgtid --lun $lun"
39 $_tgtadm --op new --backing-store $device --device-type $type --bstype $bstype --bstype $bstype $bsoflags || return 1
40 $_tgtadm --op update --param readonly=$readonly || return 1
41 }
42
43 handle_account() {
44 local tgtid=$2
45 local _tgtadm="$tgtadm --mode account"
46 local target user password outgoing
47
48 config_get target $1 target ""
49 [ "$target" ] || return 1
50 [ $target -eq $tgtid ] || return 0
51
52 config_get user $1 user ""
53 [ "$user" ] || return 1
54
55 config_get password $1 password ""
56 config_get_bool outgoing $1 outgoing 0
57 [ "$outgoing" -ne 0 ] && outgoing=--outgoing || outgoing=""
58
59 $_tgtadm --op new --user "$user" --password "$password" || return 1
60 $_tgtadm --op bind --tid $tgtid --user "$user" $outgoing || return 1
61 }
62
63 handle_target() {
64 local tgtid=$1
65 local tgtname allow
66 local _tgtadm="$tgtadm --mode target"
67
68 [ $tgtid -ge 0 ] || return 1
69
70 config_get tgtname $1 name iqn.2012-06.org.openwrt
71 config_get allow $1 allow ALL
72
73 $_tgtadm --op new --tid $tgtid --targetname $tgtname || return 1
74 $_tgtadm --op bind --tid $tgtid -I $allow || return 1
75
76 config_foreach handle_lun lun $tgtid || return 1
77 config_foreach handle_account account $tgtid || return 1
78 }
79
80 configure() {
81 config_load $NAME
82 $tgtadm --mode sys --op update --name State -v offline || return 1
83 config_foreach handle_target target || return 1
84 $tgtadm --mode sys --op update --name State -v ready || return 1
85 return 0
86 }
87
88 start_service() {
89 procd_open_instance
90 procd_set_param command $PROG -f
91 procd_set_param respawn
92 procd_close_instance
93 logger -t $NAME -s "Configuration will be loaded in seconds"
94 ( sleep 5; configure || { stop_service; exit 1; } ) &
95 }
96
97 stop_service() {
98 $tgtadm --mode sys --op update --name State -v offline
99 $tgtadm --mode target --op show \
100 | awk '$1 == "Target" {sub(/:/,"",$2); print $2}' \
101 | xargs -n1 $tgtadm --mode target --op delete --force --tid
102 $tgtadm --mode sys --op delete
103 }
104
105 show() {
106 $tgtadm --mode target --op show
107 }