File: iscsi

package info (click to toggle)
partman-iscsi 70
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 832 kB
  • sloc: sh: 356; makefile: 2
file content (95 lines) | stat: -rwxr-xr-x 2,790 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /bin/sh
# Detect iSCSI devices and leave notes about them for later. Ideally, udev
# would do most of this for us ...

. /lib/partman/lib/base.sh

# We can't work any of this out without udevadm.
type udevadm >/dev/null 2>&1 || exit 0

cat_null () {
	local text="$(cat "$1")"
	if [ "$text" != "<NULL>" ]; then
		echo "$text"
	fi
}

for dev in $DEVICES/*; do
	[ -d "$dev" ] || continue
	cd "$dev"
	rm -f iscsi_portal iscsi_target \
		iscsi_username iscsi_password \
		iscsi_username_in iscsi_password_in

	devnode="$(cat device 2>/dev/null)" || continue
	devpath="$(udevadm info -n "$devnode" -q property | sed -n 's/^DEVPATH=//p')" || continue
	[ "$devpath" ] || continue

	parent="$devpath"
	while :; do
		parent="${parent%/*}"
		[ "$parent" ] || break
		[ -L "/sys/$parent/subsystem" ] || continue
		subsys="$(readlink "/sys/$parent/subsystem")"
		subsys="${subsys##*/}"
		if [ "$subsys" = scsi ]; then
			devtype="$(sed -n 's/^DEVTYPE=//p' /sys/$parent/uevent)" || continue
			[ "$devtype" = scsi_device ] || continue

			# We found a SCSI device. Now, is it iSCSI?
			echo "/sys/$parent" | grep -q /session || continue

			# Look for the iSCSI transport device.
			transportdev="$parent"
			while :; do
				transportdev="${transportdev%/*}"
				[ "$transportdev" ] || break
				sysname="${transportdev##*/}"
				if [ "${sysname#session}" != "$sysname" ]; then
					break
				fi
			done
			[ "$transportdev" ] || continue

			# Confirm that it's really iSCSI: look for the session.
			sessiondev=
			for trydev in "subsystem/iscsi_session/$sysname/devices" \
				      "bus/iscsi_session/$sysname/devices" \
				      "class/iscsi_session/$sysname"; do
				if [ -e "/sys/$trydev" ]; then
					sessiondev="$trydev"
					break
				fi
			done
			[ "$sessiondev" ] || continue

			# Look for the portal.
			connectiondev="/sys/$transportdev"
			while [ ! -f "$connectiondev/persistent_address" ]; do
				for trydev in "$connectiondev"/*; do
					[ -d "$trydev" ] || continue
					trydev="${trydev##*/}"
					if [ "${trydev#connection*}" != "$trydev" ] || \
					   [ "$trydev" = iscsi_connection ]; then
						connectiondev="$connectiondev/$trydev"
						continue 2
					fi
				done
				break
			done
			if [ -f "$connectiondev/persistent_address" ]; then
				# Hooray!
				echo "$(cat "$connectiondev/persistent_address"):$(cat "$connectiondev/persistent_port"),$(cat "/sys/$sessiondev/tpgt")" >iscsi_portal
				cat "/sys/$sessiondev/targetname" >iscsi_target
				cat_null "/sys/$sessiondev/username" >iscsi_username
				cat_null "/sys/$sessiondev/password" >iscsi_password
				cat_null "/sys/$sessiondev/username_in" >iscsi_username_in
				cat_null "/sys/$sessiondev/password_in" >iscsi_password_in
				chmod 600 iscsi_password iscsi_password_in
				break
			fi
		fi
	done
done

exit 0