#!/usr/bin/ksh
#
# History
#   19.01.2008 /bs 1.0.0 
#      initial release
#
# 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.0.0"

BASEDIR="$( pwd )"
DOMU_CFG_DIR="./xvm"

DOMU_DISKIMGFILE="${BASEDIR}/disk.img"
DOMU_TMPIMGFILE="${BASEDIR}/temp.img"

ORIGINAL_ROOTARCHIVE="/usr/sbin/root_archive"

NEW_ROOTARCHIVE="${BASEDIR}/root_archive.new"

DOMU_CFG_FILE64="${BASEDIR}/${DOMU_CFG_DIR}/embedded64_ram.cfg"
DOMU_CFG_FILE32="${BASEDIR}/${DOMU_CFG_DIR}/embedded32_ram.cfg"

DOM0_MENU_ENTRY_FILE="${BASEDIR}/${DOMU_CFG_DIR}/dom0_menu_add"

TMP_MOUNTPOINT="/mnt"

function die {
  typeset THISRC=$1
  shift
  typeset THISMSG="$*"
  echo "${THISMSG}"
  exit ${THISRC}
}

echo "buildDumU_ramdisk.sh - create a ram disk image for a 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 DomU disk image used is \"${DOMU_DISKIMGFILE}\""

echo "The temporary disk image file used is \"${DOMU_TMPIMGFILE}\""
[ -f \"${DOMU_TMPIMGFILE}\" ] && echo "  Warning: The file \"${DOMU_TMPIMGFILE}\" already exists"

echo "The DomU config file for the 64 Bit DomU will be \"${DOMU_CFG_FILE64}\""
[ -f  "${DOMU_CFG_FILE64}" ] && echo "  Warning: The file \"${DOMU_CFG_FILE64}\" already exists"

echo "The DomU config file for the 32 Bit DomU will be \"${DOMU_CFG_FILE32}\""
[ -f  "${DOMU_CFG_FILE32}" ] && echo "  Warning: The file \"${DOMU_CFG_FILE32}\" already exists"

echo "The file with the menu entries for the grub in the Dom0 will be \"${DOM0_MENU_ENTRY_FILE}\""
[ -f  "${DOM0_MENU_ENTRY_FILE}" ] && echo "  Warning: The file \"${DOM0_MENU_ENTRY_FILE}\" already exists"


echo "The DomU config directory used is \"${DOMU_CFG_DIR}\""
[ -d  "${DOMU_CFG_DIR}" ] && echo "  Warning: The directory \"${DOMU_CFG_DIR}\" already exists"

echo "The new rootarchive script is \"${NEW_ROOTARCHIVE}\" "
[ -f "${NEW_ROOTARCHIVE}" ] && echo "\"${NEW_ROOTARCHIVE}\" already exists - will not be overwritten."

echo "The mount point used is \"${TMP_MOUNTPOINT}\" "

echo ""
echo "Are these settings correct (n/Y)?"
read USERINPUT
[ "${USERINPUT}"x != "y"x -a "${USERINPUT}"x != "y"x  ]  &&  die 5 "Script aborted by the user "

echo "Checking the prerequisites ..."

if [ ! -f "${NEW_ROOTARCHIVE}" ] ; then
  [ ! -f "${ORIGINAL_ROOTARCHIVE}" ] && die 11 "\"${ORIGINAL_ROOTARCHIVE}\" not found"
  
  echo "Creating the new rootarchive script \"${NEW_ROOTARCHIVE}\" ..."
  sed "s/t \* 1.10/(t + (64 \* 1024 \* 1024)) \* 1.10/" "${ORIGINAL_ROOTARCHIVE}" >"${NEW_ROOTARCHIVE}"
  chmod 755 "${NEW_ROOTARCHIVE}"
fi

[ ! -f "${DOMU_DISKIMGFILE}" ] && die 6 "ERROR: The file \"${DOMU_DISKIMGFILE}\" does not exist!"

[ ! -d "${TMP_MOUNTPOINT}" ]  && die  7 "ERROR: The mountpoint \"${TMP_MOUNTPOINT}\" does not exist!"


echo "Creating the temporary disk image file ..."
cp ${DOMU_DISKIMGFILE} ${DOMU_TMPIMGFILE} || die 8 "Error creating the temporary disk image file"

echo "Mounting the temporary disk image file ..."

LOFI_DEVICE=$( lofiadm -a ${DOMU_TMPIMGFILE} ) 
 [ "${LOFI_DEVICE}"x = ""x ] && die 9 "Error creating the lofi device"

mount ${LOFI_DEVICE} ${TMP_MOUNTPOINT} || die 10 "Error mounting the lofi device"

echo "Correcting the devices links for the root disk in the image ..."
 # change the root device in the image
  rm ${TMP_MOUNTPOINT}/dev/dsk/c0d0s0 
 rm ${TMP_MOUNTPOINT}/dev/rdsk/c0d0s0
 ln -s ../../devices/ramdisk:a ${TMP_MOUNTPOINT}/dev/dsk/c0d0s0
 ln -s ../../devices/ramdisk:a,raw ${TMP_MOUNTPOINT}/dev/rdsk/c0d0s0

echo "Deleting not necessary files in the image ..."
[ -f ${TMP_MOUNTPOINT}/platform/i86pc/boot_archive ] && rm ${TMP_MOUNTPOINT}/platform/i86pc/boot_archive
[ -f ${TMP_MOUNTPOINT}/platform/i86pc/amd64/boot_archive ] && rm ${TMP_MOUNTPOINT}/platform/i86pc/amd64/boot_archive

echo "Creating and configuring the DomU directory ..."
mkdir -p ${DOMU_CFG_DIR}/amd64
set -x
"${NEW_ROOTARCHIVE}" pack ./${DOMU_CFG_DIR}/solaris.ramdisk ${TMP_MOUNTPOINT}
cp ${TMP_MOUNTPOINT}/platform/i86xpv/kernel/unix ${DOMU_CFG_DIR}
cp ${TMP_MOUNTPOINT}/boot/xen.gz ${DOMU_CFG_DIR}/
cp ${TMP_MOUNTPOINT}/platform/i86xpv/kernel/amd64/unix ${DOMU_CFG_DIR}/amd64/
cp ${TMP_MOUNTPOINT}/boot/amd64/xen.gz ${DOMU_CFG_DIR}/amd64/
set +x

echo "Removing the temporary disk image file ..."
umount ${TMP_MOUNTPOINT}
lofiadm -d ${LOFI_DEVICE}
rm "${DOMU_TMPIMGFILE}"

echo "Creating the 64 Bit DomU config file ..."
cat <<EOT >"${DOMU_CFG_FILE64}"
name = "embedded"
vcpus = 1
memory = "512"
kernel = "${BASEDIR}/${DOMU_CFG_DIR}/amd64/unix"
ramdisk = "${BASEDIR}/${DOMU_CFG_DIR}/solaris.ramdisk"
extra = "/platform/i86xpv/kernel/amd64/unix -k"
vif = ['']
EOT


echo "Creating the 32 Bit DomU config file ..."
cat <<EOT >"${DOMU_CFG_FILE32}"
name = "embedded"
vcpus = 1
memory = "512"
kernel = "${BASEDIR}/${DOMU_CFG_DIR}/unix"
ramdisk = "${BASEDIR}/${DOMU_CFG_DIR}/solaris.ramdisk"
extra = "/platform/i86xpv/kernel/unix -k"
vif = ['']
EOT


echo "Creating the file with the menu entries for grub ..."

cat <<EOT >"${DOM0_MENU_ENTRY_FILE}"
title Embedded Solaris xVM 64 Bit (serial console)
kernel$ /boot/\$ISADIR/xen.gz com1=96008n1 console=com1
module$ /boot/platform/i86xpv/kernel/\$ISADIR/unix  /platform/i86xpv/kernel/\$ISADIR/unix -k -B console=ttya
module$ /boot/solaris.ramdisk

title Embedded Solaris xVM 32 Bit (serial console)
kernel$ /boot/xen.gz com1=9600,8n1 console=com1
module$ /boot/platform/i86xpv/kernel/unix /platform/i86xpv/kernel/unix  -k -B console=ttya
module$ /boot/solaris.ramdisk

title Embedded Solaris xVM 64 Bit (vga)
kernel$ /boot/\$ISADIR/xen.gz 
module$ /boot/platform/i86xpv/kernel/\$ISADIR/unix  /platform/i86xpv/kernel/\$ISADIR/unix -k
module$ /boot/solaris.ramdisk

title Embedded Solaris xVM 32 Bit (vga) (2)
kernel$ /boot/xen.gz 
module$ /boot/platform/i86xpv/kernel/unix /platform/i86xpv/kernel/unix -k 
module$ /boot/solaris.ramdisk

EOT

cat <<EOT

Execute

cp "${BASEDIR}/${DOMU_CFG_DIR}/solaris.ramdisk" /boot

and add the contents of the file "${DOM0_MENU_ENTRY_FILE}" to the  grub menu for the Dom0
EOT

