#!/bin/ksh
#
# History
# 	20.01.2008 v1.0.0 /bs 
#        initial release
#
#      03.02.2008 v1.1.0 /bs
#        added changes for Solaris snv_81 (search for "snv81" in the code)
#
# Credits
#
# This script is based on the instructions from Mark Johnsons forum entry:
# 		http://www.opensolaris.org/jive/thread.jspa?threadID=49309&tstart=0
#
# Minor changes by: Bernd Schemmer
#
#
version=1.1.0

PKGADD=/usr/sbin/pkgadd

# log file for the installation ($$ added by /bs)
PKGLOG=/tmp/packages.$$.log

# admin file for pkgadd
PKGADMIN=/tmp/pkgadmin

# mount point for the disk image file
ROOTDIR=/mnt

# directory with the Solaris packages to isntall
#
PROD=/export/install/i386/Solaris_11/Product

PROD_OVERRIDE=/tank/ws/xvm-remote/packages-nondebug

# code added by /bs  -- start --
#

thisscript="$( basename $0 )"

MOUNT_DEVICE=$( mount | grep "^${ROOTDIR} " | cut -f3 -d " " ) 
if [ "${MOUNT_DEVICE}"x != ""x ] ; then
  IMAGEFILE=$( lofiadm ${MOUNT_DEVICE} | grep -v "^Block Device"  )
else
  IMAGEFILE=""
fi  

# xen DomU config file
XEN_CONFIGFILE="${IMAGEFILE%/*}/embbed_solaris.cfg"
#

echo "buildDumU_ramdisk.sh - create a disk image for a a minimal Solaris DomU "
echo "Source: http://www.opensolaris.org/jive/thread.jspa?threadID=49309&tstart=0"
echo "        http://home.arcor.de/bnsmb/public/htdocs/Xen_and_Solaris.html"
echo ""

echo "The settings used are:"
echo ""
echo "The logfile is \"${PKGLOG}\" "
echo "The root dir of the image file is \"${ROOTDIR}\""

echo "The image file used is: \"${IMAGEFILE}\""

echo "The path to the OpenSolaris installation image product directory is: \"${PROD}\" "

echo "The DomU config file to create is \"${XEN_CONFIGFILE}\""
[ -f "${XEN_CONFIGFILE}" ] && echo "  Warning: The file \"${XEN_CONFIGFILE}\" already exist"

echo ""
echo "Note: You must create the image file for the DomU manually, create a lofi"
echo "       device for it, and mount it before invoking this script"
echo ""

echo "Are these settings correct (y/N)? "
read userinput
if [ "${userinput}"x != "Y"x -a "${userinput}"x != "y"x ] ; then
  echo "Script aborted by the user"
  echo "Note: Edit \"$0\" to change the settings."
  exit 5
fi

echo "Checking the settings ... "
settings_ok=0

echo "Checking the root directory \"${ROOTDIR}\" ..."
if [ "${IMAGEFILE}"x  = ""x ] ; then
  df -k ${ROOTDIR}
  echo "WARNING: \"${ROOTDIR}\" is NOT mounted on a lofi device"  
  settings_ok=1
fi

echo "Checking the package directory \"${PROD}\" ..."
if [ ! -f ${PROD}/.clustertoc ]  ; then
  echo "WARNING: The directory \"${PROD}\" seems not to be a package directory"
  settings_ok=1
fi

touch "${PKGLOG}"
if [ $? -ne 0 ] ; then
  echo "WARNING: Can not write to the log file \"${PKGLOG}\" "
  settings_ok=1
fi

touch "${XEN_CONFIGFILE}"
if [ $? -ne 0 ] ; then
  echo "WARNING: Can not write to the Xen DomU config file \"${XEN_CONFIGFILE}\" "
  settings_ok=1
fi

if [ ${settings_ok} != 0 ] ; then
  echo "Possible errors detected - continue anyway (y/N)?"
  read userinput
  if [ "${userinput}"x != "Y"x -a "${userinput}"x != "y"x ] ; then
    echo "Script aborted by the user"
    echo "Note: Edit \"$0\" to change the settings."
    exit 10
  fi
fi

# code added by /bs  -- end --

#
# Minimum list of packages that boots to login prompt on text console.
# Add additional packages to get more functionality (e.g. add SUNWmdbr
# for kernel debugging via kmdb).
#
BASE="
SUNWcar.i
SUNWcarx.i
SUNWcakr.i
SUNWcakrx.i
SUNWkvm.i
SUNWcsr
SUNWcsd
SUNWos86r
SUNWrmodr
SUNWpsdcr
SUNWpsdir
SUNWckr
SUNWcnetr
SUNWcsl
SUNWcsu
SUNWcslr
SUNWesu
SUNWkey
SUNWlibms
SUNWlibmsr
SUNWusb
SUNWpr
SUNWtls
SUNWlibsasl
SUNWlxml
SUNWopenssl-libraries
SUNWusbs
SUNWmdr
SUNWmdu
SUNWtecla
SUNWzlib
SUNWuprl
SUNWsmapi
SUNWkrbr
SUNWkrbu
SUNWgss
SUNWbipr
SUNWbip
SUNWzfskr
SUNWzfsr
SUNWzfsu
SUNWbash
SUNWipfr
SUNWipfu
SUNWmkcdr
SUNWmkcd
"

SYSID="
SUNWadmap
SUNWadmlib-sysid
SUNWadmr
"

SSH="
SUNWsshcu
SUNWsshdr
SUNWsshdu
SUNWsshr
SUNWsshu
"

NFSCLIENT="
SUNWnfsckr
SUNWnfscr
SUNWnfscu
"

NFSSERVER="
SUNWnfsskr
SUNWnfssr
SUNWnfssu
"

NIS="
SUNWnisr
SUNWnisu
"
AUTOFS="
SUNWatfsr
SUNWatfsu
"

MDB="
SUNWmdb
SUNWmdbr
"

DOM0="
SUNWPython
SUNWPython-extra
SUNWvirtinst
SUNWurlgrabber
SUNWlibvirt
SUNWxvmhvm
SUNWxvmdomr
SUNWxvmdomu
SUNWxvmu
SUNWxvmr
SUNWgccruntime
SUNWgnutls
SUNWlibsdl
SUNWxwplt
SUNWxwrtl
"

NICS="
SUNWbge
SUNWnge
SUNWrge
SUNWxge
SUNWintgige
"

DISK_HBAS="
SUNWahci
"

#
# Create a pkg admin file - see man admin(4)
#
sed 's/ask/nocheck/' /var/sadm/install/admin/default > $PKGADMIN

echo "adding packages to $ROOTDIR"

# changed PKGLOG to $PKGLOG /bs
#
for pkg in $BASE $SYSID $SSH $MDB $NFSCLIENT $AUTOFS $DOM0 $NIS $DISK_HBAS; do
       echo "Installing the package \"${pkg}\" ..."
       
	if [ -d "$PROD_OVERRIDE/$pkg" ]; then
		$PKGADD -a $PKGADMIN -d $PROD_OVERRIDE -R $ROOTDIR $pkg >> $PKGLOG 2>&1
	elif [ -d "$PROD/$pkg" ]; then
		$PKGADD -a $PKGADMIN -d $PROD -R $ROOTDIR $pkg >> $PKGLOG 2>&1
	else
		echo " $pkg not found: skipped"
	fi
done

# seed the SMF repository
/usr/bin/cp $ROOTDIR/lib/svc/seed/global.db $ROOTDIR/etc/svc/repository.db
chmod 600 $ROOTDIR/etc/svc/repository.db
cd $ROOTDIR/var/svc/profile/
rm -f generic.xml name_service.xml
ln -s generic_limited_net.xml generic.xml
ln -s ns_files.xml name_service.xml


# snv81
# disable TCP checksum in the DomU
#
if [ "$( uname -v )"x = "snv_81" ] ; then
  echo "Disabling TCP checksum in the DomU ..."
  echo "set xnf:xnf_cksum_offload = 0" >>$ROOTDIR/etc/system
fi

#
# Setup DHCP on all NICs
#
#cp /tank/ws/embedded/devices-net $ROOTDIR/lib/svc/method/
#cp /tank/ws/embedded/devices-net.xml $ROOTDIR/var/svc/manifest/system/device/

# save away vfstab for domU and ramdisk
/usr/bin/cp $ROOTDIR/etc/vfstab $ROOTDIR/etc/vfstab.disk
/usr/bin/cp $ROOTDIR/etc/vfstab $ROOTDIR/etc/vfstab.ramdisk
echo "/devices/ramdisk:a - / ufs - no nologging" >> $ROOTDIR/etc/vfstab.ramdisk
echo "/dev/dsk/c0d0s0 /dev/rdsk/c0d0s0 / ufs 1 no -" >> $ROOTDIR/etc/vfstab.disk

# build up /dev. This causes some minor problems but will self correct.
# XXX - This would be better if we had a seed tarball.
/usr/sbin/devfsadm -R $ROOTDIR

# make the disk links for domU
cd $ROOTDIR/dev/dsk
rm -f c0d0s0
/usr/bin/ln -s ../../devices/xpvd/xdf@0:a c0d0s0
cd $ROOTDIR/dev/rdsk
rm -f c0d0s0
/usr/bin/ln -s ../../devices/xpvd/xdf@0:a,raw c0d0s0
/usr/bin/cp $ROOTDIR/etc/vfstab.disk $ROOTDIR/etc/vfstab

# spinkle a little magic sysconfig fairy dust
echo "/lib/svc/method/sshd\n\
/usr/sbin/sysidkbd\n\
/usr/sbin/sysidpm\n\
/l ib/svc/method/net-nwam\n\
/usr/lib/cc-ccr/bin/eraseCCRRepository" > $ROOTDIR/etc/.sysidconfig.apps

# setup the image so we need to configure it on first boot
# XXX: if we setup timezone, could we save the reboot??
/usr/sbin/sys-unconfig -R $ROOTDIR

echo "build boot_archive"
cp /boot/solaris/bin/root_archive $ROOTDIR/boot/solaris/bin/root_archive

# mkdir command added by /bs
mkdir $ROOTDIR/boot/grub

/usr/sbin/bootadm update-archive -R $ROOTDIR

# code added by /bs
#
echo "Creating the Xen config file for the Domu \"${XEN_CONFIGFILE}\" ..."
cat <<EOT >"${XEN_CONFIGFILE}"
name = "embedded-disk"
vcpus = 1
memory = "512"
root = "/dev/dsk/c0d0s0"
disk = ['file:${IMAGEFILE},0,w']
vif = ['']
EOT


echo "The logfile used was \"${PKGLOG}\"."

