#!/bin/bash
# Generate a random iSCSI IQN

set -e

defaultdomain="com.example"
XE=/opt/xensource/bin/xe
GENIQN=/opt/xensource/bin/xe-set-iscsi-iqn

# generate and reverse the hosts DNS domain name
domain=$(dnsdomainname || true)

if [ "$domain" = "" ] || [ "$domain" = "(none)" ]; then
  echo Warning: no DNS domain name specified, defaulting to ${defaultdomain}
  domain=${defaultdomain}
fi

revdomain=$(echo $domain | perl -ne \
  'chomp; print join(".",reverse(split(/\./,$_)));')

uuid=$(uuidgen | cut -d- -f1)
date=$(date +"%Y-%m")
iqn="iqn.${date}.${revdomain}:${uuid}"

${GENIQN} ${iqn}
echo Generated iSCSI IQN: ${iqn}
