#! /bin/sh
# Install iSCSI packages in the target if necessary. iSCSI volumes
# containing the root filesystem also need some special settings for
# robustness.

. /lib/partman/lib/base.sh

have_iscsi=
root_on_iscsi=
for dev in $DEVICES/*; do
	[ -d "$dev" ] || continue
	[ -e "$dev/iscsi_portal" ] || continue
	have_iscsi=1
	cd "$dev"
	portal="$(cat "$dev/iscsi_portal")"
	target="$(cat "$dev/iscsi_target")"

	contains_root=
	open_dialog PARTITIONS
	while { read_line num id size type fs path name; [ "$id" ]; }; do
		[ "$fs" != free ] || continue
		[ -f "$id/method" ] || continue
		[ -f "$id/acting_filesystem" ] || continue
		[ -f "$id/mountpoint" ] || continue
		mountpoint="$(cat "$id/mountpoint")"
		[ "$mountpoint" = / ] || continue
		contains_root=1
	done
	close_dialog

	[ "$contains_root" ] || continue
	iscsiadm -m node --portal "${portal%,*}" --targetname "$target" -o update -n 'node.conn[0].timeo.noop_out_interval' -v 0
	iscsiadm -m node --portal "${portal%,*}" --targetname "$target" -o update -n 'node.conn[0].timeo.noop_out_timeout' -v 0
	iscsiadm -m node --portal "${portal%,*}" --targetname "$target" -o update -n 'node.session.timeo.replacement_timeout' -v 86400
	root_on_iscsi="$portal $target"
done

if [ "$have_iscsi" ]; then
	apt-install open-iscsi || true
fi

if [ "$root_on_iscsi" ]; then
	portal="${root_on_iscsi%% *}"
	target="${root_on_iscsi#* }"
	group="${portal##*,}"
	portal="${portal%,*}"
	ip="${portal%%:*}"
	port="${portal#*:}"
	mkdir -p /target/etc/iscsi
	cat >>/target/etc/iscsi/iscsi.initramfs <<EOF
ISCSI_TARGET_NAME="$target"
ISCSI_TARGET_IP="$ip"
ISCSI_TARGET_PORT="$port"
ISCSI_TARGET_GROUP="$group"
EOF
	chmod 600 /target/etc/iscsi/iscsi.initramfs

	# This isn't ideal, but ifupdown doesn't expose any interface that
	# might allow the initramfs to dynamically tell it to avoid a
	# particular interface on this boot.
	if db_get netcfg/choose_interface; then
		# Work around netcfg/choose_interface not always being set:
		#   https://bugs.launchpad.net/ubuntu/+source/netcfg/+bug/430820
		[ "$RET" ] || RET=eth0
		sed -i "s/\(iface ${RET%%:*} inet\) dhcp/\1 manual/" \
			/etc/network/interfaces || true
	fi
else
	rm -f /target/etc/iscsi/iscsi.initramfs
fi

exit 0
