#!/bin/bash
#
# /sbin/iscsi-gen-initiatorname
#
# Generate a default iSCSI Initiatorname for SUSE installations.
#
# Copyright (c) 2007 Hannes Reinecke, SUSE Linux Products GmbH.
# All rights reserved.
#

if [ "$1" ] ; then
    if [ "$1" = "-f" ] ; then
	FORCE=1
    else
	echo "Invalid option $1"
	echo "Usage: $0 [-f]"
	exit 1
    fi
fi

if [ -d /sys/firmware/ibft/initiator ] ; then
    read iSCSI_INITIATOR_NAME < /sys/firmware/ibft/initiator/initiator-name
fi

if [ -f /etc/iscsi/initiatorname.iscsi -a -z "$FORCE" ] ; then
    if [ "$iSCSI_INITIATOR_NAME" ] ; then
	eval $(cat /etc/iscsi/initiatorname.iscsi | sed -e '/^#/d')
	if [ "$iSCSI_INITIATOR_NAME" != "$InitiatorName" ] ; then
	    echo "iSCSI Initiatorname from iBFT is different from the current setting."
	    echo "Please call '/sbin/iscsi-gen-initiatorname -f' to update the iSCSI Initiatorname."
	    exit 1
	fi
    fi
fi

if [ "$iSCSI_INITIATOR_NAME" ] ; then
    cat << EOF >> /etc/iscsi/initiatorname.iscsi
##
## /etc/iscsi/iscsi.initiatorname
##
## iSCSI Initiatorname taken from iBFT BIOS tables.
##
## DO NOT EDIT OR REMOVE THIS FILE! 
## If you remove this file, the iSCSI daemon will not start.
## Any change here will not be reflected to the iBFT BIOS tables.
## If a different initiatorname is required please change the 
## initiatorname in the BIOS setup and call
## /sbin/iscsi-gen-initiatorname -f
## to recreate an updated version of this file.
##
InitiatorName=$iSCSI_INITIATOR_NAME
EOF
fi

if [ ! -f /etc/iscsi/initiatorname.iscsi ] ; then
    cat << EOF >> /etc/iscsi/initiatorname.iscsi
##
## /etc/iscsi/iscsi.initiatorname
##
## Default iSCSI Initiatorname.
##
## DO NOT EDIT OR REMOVE THIS FILE! 
## If you remove this file, the iSCSI daemon will not start.
## If you change the InitiatorName, existing access control lists
## may reject this initiator.  The InitiatorName must be unique
## for each iSCSI initiator.  Do NOT duplicate iSCSI InitiatorNames.
EOF
	ISSUEDATE="1996-04"
	INAME=$(/sbin/iscsi-iname -p iqn.$ISSUEDATE.de.suse:01)
	printf "InitiatorName=$INAME\n"  >>/etc/iscsi/initiatorname.iscsi
	chmod 0600 /etc/iscsi/initiatorname.iscsi
fi

