]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/95iscsi/module-setup.sh
686ed570af50e25907166538b465543e06ea5ab4
[thirdparty/dracut.git] / modules.d / 95iscsi / module-setup.sh
1 #!/bin/bash
2
3 # called by dracut
4 check() {
5 local _rootdev
6 # If our prerequisites are not met, fail anyways.
7 require_binaries iscsi-iname iscsiadm iscsid || return 1
8
9 # If hostonly was requested, fail the check if we are not actually
10 # booting from root.
11
12 is_iscsi() {
13 local _dev=$1
14
15 [[ -L "/sys/dev/block/$_dev" ]] || return
16 cd "$(readlink -f "/sys/dev/block/$_dev")"
17 until [[ -d sys || -d iscsi_session ]]; do
18 cd ..
19 done
20 [[ -d iscsi_session ]]
21 }
22
23 [[ $hostonly ]] || [[ $mount_needs ]] && {
24 pushd . >/dev/null
25 for_each_host_dev_and_slaves is_iscsi
26 local _is_iscsi=$?
27 popd >/dev/null
28 [[ $_is_iscsi == 0 ]] || return 255
29 }
30 return 0
31 }
32
33 get_ibft_mod() {
34 local ibft_mac=$1
35 local iface_mac iface_mod
36 # Return the iSCSI offload module for a given MAC address
37 for iface_desc in $(iscsiadm -m iface | cut -f 2 -d ' '); do
38 iface_mod=${iface_desc%%,*}
39 iface_mac=${iface_desc#*,}
40 iface_mac=${iface_mac%%,*}
41 if [ "$ibft_mac" = "$iface_mac" ] ; then
42 echo $iface_mod
43 return 0
44 fi
45 done
46 }
47
48 install_ibft() {
49 # When iBFT / iscsi_boot is detected:
50 # - Use 'ip=ibft' to set up iBFT network interface
51 # Note: bnx2i is using a different MAC address of iSCSI offloading
52 # so the 'ip=ibft' parameter must not be set
53 # - specify firmware booting cmdline parameter
54
55 for d in /sys/firmware/* ; do
56 if [ -d ${d}/ethernet0 ] ; then
57 read ibft_mac < ${d}/ethernet0/mac
58 ibft_mod=$(get_ibft_mod $ibft_mac)
59 fi
60 if [ -z "$ibft_mod" ] && [ -d ${d}/ethernet1 ] ; then
61 read ibft_mac < ${d}/ethernet1/mac
62 ibft_mod=$(get_ibft_mod $ibft_mac)
63 fi
64 if [ -d ${d}/initiator ] ; then
65 if [ ${d##*/} = "ibft" ] && [ "$ibft_mod" != "bnx2i" ] ; then
66 echo -n "rd.iscsi.ibft=1 "
67 fi
68 echo -n "rd.iscsi.firmware=1"
69 fi
70 done
71 }
72
73 install_iscsiroot() {
74 local devpath=$1
75 local scsi_path iscsi_lun session c d conn host flash
76 local iscsi_session iscsi_address iscsi_port iscsi_targetname iscsi_tpgt
77 local bootproto
78
79 scsi_path=${devpath%%/block*}
80 [ "$scsi_path" = "$devpath" ] && return 1
81 iscsi_lun=${scsi_path##*:}
82 [ "$iscsi_lun" = "$scsi_path" ] && return 1
83 session=${devpath%%/target*}
84 [ "$session" = "$devpath" ] && return 1
85 iscsi_session=${session##*/}
86 [ "$iscsi_session" = "$session" ] && return 1
87 host=${session%%/session*}
88 [ "$host" = "$session" ] && return 1
89 iscsi_host=${host##*/}
90
91 for flash in ${host}/flashnode_sess-* ; do
92 [ -f "$flash" ] || continue
93 [ ! -e "$flash/is_boot_target" ] && continue
94 is_boot=$(cat $flash/is_boot_target)
95 if [ $is_boot -eq 1 ] ; then
96 # qla4xxx flashnode session; skip iBFT discovery
97 iscsi_initiator=$(cat /sys/class/iscsi_host/${iscsi_host}/initiatorname)
98 echo "rd.iscsi.initiator=${iscsi_initiator}"
99 return;
100 fi
101 done
102
103 for d in ${session}/* ; do
104 case $d in
105 *connection*)
106 c=${d##*/}
107 conn=${d}/iscsi_connection/${c}
108 if [ -d ${conn} ] ; then
109 iscsi_address=$(cat ${conn}/persistent_address)
110 iscsi_port=$(cat ${conn}/persistent_port)
111 fi
112 ;;
113 *session)
114 if [ -d ${d}/${iscsi_session} ] ; then
115 iscsi_initiator=$(cat ${d}/${iscsi_session}/initiatorname)
116 iscsi_targetname=$(cat ${d}/${iscsi_session}/targetname)
117 fi
118 ;;
119 esac
120 done
121
122 [ -z "$iscsi_address" ] && return
123 local_address=$(ip -o route get to $iscsi_address | sed -n 's/.*src \([0-9a-f.:]*\).*/\1/p')
124 ifname=$(ip -o route get to $iscsi_address | sed -n 's/.*dev \([^ ]*\).*/\1/p')
125
126 # follow ifcfg settings for boot protocol
127 for _path in \
128 "/etc/sysconfig/network-scripts/ifcfg-$ifname" \
129 "/etc/sysconfig/network/ifcfg-$ifname" \
130 ; do
131 [ -f "$_path" ] && bootproto=$(sed -n "s/BOOTPROTO='\?\([[:alpha:]]*6\?\)4\?/\1/p" "$_path")
132 done
133
134 if [ $bootproto ]; then
135 printf 'ip=%s:%s ' ${ifname} ${bootproto}
136 else
137 printf 'ip=%s:static ' ${ifname}
138 fi
139
140 if [ -e /sys/class/net/$ifname/address ] ; then
141 ifmac=$(cat /sys/class/net/$ifname/address)
142 printf 'ifname=%s:%s ' ${ifname} ${ifmac}
143 fi
144
145 if [ -n "$iscsi_address" -a -n "$iscsi_targetname" ] ; then
146 if [ -n "$iscsi_port" -a "$iscsi_port" -eq 3260 ] ; then
147 iscsi_port=
148 fi
149 if [ -n "$iscsi_lun" -a "$iscsi_lun" -eq 0 ] ; then
150 iscsi_lun=
151 fi
152 # In IPv6 case rd.iscsi.initatior= must pass address in [] brackets
153 case "$iscsi_address" in
154 *:*)
155 iscsi_address="[$iscsi_address]"
156 ;;
157 esac
158 # Must be two separate lines, so that "sort | uniq" commands later
159 # can sort out rd.iscsi.initiator= duplicates
160 echo "rd.iscsi.initiator=${iscsi_initiator}"
161 echo "netroot=iscsi:${iscsi_address}::${iscsi_port}:${iscsi_lun}:${iscsi_targetname}"
162 echo "rd.neednet=1"
163 fi
164 return 0
165 }
166
167
168 install_softiscsi() {
169 [ -d /sys/firmware/ibft ] && return 0
170
171 is_softiscsi() {
172 local _dev=$1
173 local iscsi_dev
174
175 [[ -L "/sys/dev/block/$_dev" ]] || return
176 iscsi_dev=$(cd -P /sys/dev/block/$_dev; echo $PWD)
177 install_iscsiroot $iscsi_dev
178 }
179
180 for_each_host_dev_and_slaves_all is_softiscsi || return 255
181 return 0
182 }
183
184 # called by dracut
185 depends() {
186 echo network rootfs-block
187 }
188
189 # called by dracut
190 installkernel() {
191 local _arch=$(uname -m)
192 local _funcs='iscsi_register_transport'
193
194 instmods bnx2i qla4xxx cxgb3i cxgb4i be2iscsi qedi
195 hostonly="" instmods iscsi_tcp iscsi_ibft crc32c iscsi_boot_sysfs
196
197 if [ "$_arch" = "s390" -o "$_arch" = "s390x" ]; then
198 _s390drivers="=drivers/s390/scsi"
199 fi
200
201 dracut_instmods -o -s ${_funcs} =drivers/scsi ${_s390drivers:+"$_s390drivers"}
202 }
203
204 # called by dracut
205 cmdline() {
206 local _iscsiconf=$(install_ibft)
207 {
208 if [ "$_iscsiconf" ] ; then
209 echo ${_iscsiconf}
210 else
211 install_softiscsi
212 fi
213 } | sort | uniq
214 }
215
216 # called by dracut
217 install() {
218 inst_multiple -o iscsiuio
219 inst_libdir_file 'libgcc_s.so*'
220 inst_multiple umount iscsi-iname iscsiadm iscsid
221
222 inst_multiple -o \
223 $systemdsystemunitdir/iscsid.socket \
224 $systemdsystemunitdir/iscsid.service \
225 $systemdsystemunitdir/iscsiuio.service \
226 $systemdsystemunitdir/iscsiuio.socket \
227 $systemdsystemunitdir/sockets.target.wants/iscsid.socket \
228 $systemdsystemunitdir/sockets.target.wants/iscsiuio.socket
229
230 if [[ $hostonly ]]; then
231 inst_dir $(/usr/bin/find /etc/iscsi)
232 else
233 inst_simple /etc/iscsi/iscsid.conf
234 fi
235
236 # Detect iBFT and perform mandatory steps
237 if [[ $hostonly_cmdline == "yes" ]] ; then
238 local _iscsiconf=$(cmdline)
239 [[ $_iscsiconf ]] && printf "%s\n" "$_iscsiconf" >> "${initdir}/etc/cmdline.d/95iscsi.conf"
240 fi
241
242 inst_hook cmdline 90 "$moddir/parse-iscsiroot.sh"
243 inst_hook cleanup 90 "$moddir/cleanup-iscsi.sh"
244 inst "$moddir/iscsiroot.sh" "/sbin/iscsiroot"
245 if ! dracut_module_included "systemd"; then
246 inst "$moddir/mount-lun.sh" "/bin/mount-lun.sh"
247 else
248 inst_multiple -o \
249 $systemdsystemunitdir/iscsi.service \
250 $systemdsystemunitdir/iscsid.service \
251 $systemdsystemunitdir/iscsid.socket \
252 $systemdsystemunitdir/iscsiuio.service \
253 $systemdsystemunitdir/iscsiuio.socket \
254 iscsiadm iscsid
255
256 mkdir -p "${initdir}/$systemdsystemunitdir/sockets.target.wants"
257 for i in \
258 iscsid.socket \
259 iscsiuio.socket \
260 ; do
261 ln_r "$systemdsystemunitdir/${i}" "$systemdsystemunitdir/sockets.target.wants/${i}"
262 done
263
264 mkdir -p "${initdir}/$systemdsystemunitdir/basic.target.wants"
265 for i in \
266 iscsid.service \
267 iscsiuio.service \
268 ; do
269 ln_r "$systemdsystemunitdir/${i}" "$systemdsystemunitdir/basic.target.wants/${i}"
270 done
271
272 # Make sure iscsid is started after dracut-cmdline and ready for the initqueue
273 mkdir -p "${initdir}/$systemdsystemunitdir/iscsid.service.d"
274 (
275 echo "[Unit]"
276 echo "After=dracut-cmdline.service"
277 echo "Before=dracut-initqueue.service"
278 ) > "${initdir}/$systemdsystemunitdir/iscsid.service.d/dracut.conf"
279
280 # The iscsi deamon does not need to wait for any storage inside initrd
281 mkdir -p "${initdir}/$systemdsystemunitdir/iscsid.socket.d"
282 (
283 echo "[Unit]"
284 echo "DefaultDependencies=no"
285 echo "Conflicts=shutdown.target"
286 echo "Before=shutdown.target sockets.target"
287 ) > "${initdir}/$systemdsystemunitdir/iscsid.socket.d/dracut.conf"
288 mkdir -p "${initdir}/$systemdsystemunitdir/iscsiuio.socket.d"
289 (
290 echo "[Unit]"
291 echo "DefaultDependencies=no"
292 echo "Conflicts=shutdown.target"
293 echo "Before=shutdown.target sockets.target"
294 ) > "${initdir}/$systemdsystemunitdir/iscsiuio.socket.d/dracut.conf"
295
296 fi
297 inst_dir /var/lib/iscsi
298 dracut_need_initqueue
299 }