#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: Program to dump AoE image to DRBL server.

DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/opt/drbl/}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. $DRBL_SCRIPT_PATH/conf/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

#
device=$1
img_name_prefix=$2

#
USAGE() {
   echo "Usage: $0 disk_device image_name"
   echo "Ex: $0 /dev/hda winaoe"
}

# Old version use 3 parameters, now only 2. We have to show warning message if mornot 2.
if [ "$#" -ne 2 ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "You must assign 2 parameters. No more, no less!!!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    USAGE
    echo "Program terminited!"
    exit 1
fi
#
for i in device img_name_prefix; do
  eval var_tmp=\$$i
  if [ -z "$var_tmp" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "$i is nothing!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    USAGE
    echo "Program terminited!"
    exit 1
  fi
done

# Stop local mounting service
/etc/init.d/mkswapfile stop

# Check if the image name contains reserved characters
if [ -n "$(echo $img_name_prefix | grep -iE "aoe-[[:digit:]]+-[[:digit:]]+")" ]; then
  img_name_p1="$(echo $img_name_prefix | sed -e "s/aoe-[[:digit:]]*-[[:digit:]]*//g")"
  img_name_p2="$(echo $img_name_prefix | sed -e "s/$img_name_p1//g")"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "The input name contains reserved characters: $img_name_p2"
  echo "Please reassign the input name without $img_name_p2"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "Program terminiated!"
  exit 1
fi

# Disk /dev/sda: 160.0 GB, 160041885696 bytes
# 255 heads, 63 sectors/track, 19457 cylinders
# Units = cylinders of 16065 * 512 = 8225280 bytes
# 
#    Device Boot      Start         End      Blocks   Id  System
# /dev/sda1   *           1       11519    92526336    7  HPFS/NTFS
# /dev/sda2           11520       12735     9767520   83  Linux
# /dev/sda3           12736       12978     1951897+  82  Linux swap / Solaris
# /dev/sda4           12979       19457    52042567+   5  Extended
# /dev/sda5           12979       19457    52042536   83  Linux

mkdir -p $sanboot_img_dir

bs="$(LANG=C fdisk -l $device | grep -iE '^Units = cylinders' | awk -F'=' '{print $3}' | sed -e 's/bytes//g' -e 's/[[:space:]]//g')"
# Find the largest cylinder in the "End" column. By doing this, we can dump multiple partitions.
count="$(LANG=C fdisk -l $device | grep -A10000 -Ei "Device[[:space:]]*Boot" | grep -vEi "Device[[:space:]]*Boot" | sed -e "s/*//g" | awk -F' ' '{print $3}' | sort -rn | head -n 1)"
disk_size="$(echo "scale=2; $count * $bs /1000/1000/1000" | bc -l) GB" # unit: GB

for i in bs count; do
 eval var_tmp=\$$i
 if [ -z "$var_tmp" ]; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "$i is nothing!"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   USAGE
   exit 1
 fi
 if [ -n "$(echo $var_tmp | grep -iE '[^[:digit:]]')" ]; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "$i not digits! Program terminated!"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   exit 1
 fi
done

# Assign vbladed <shelf> <slot> to image name. Find a free one
# Here we will set the file name as IMAGENMAE.aoe-<shelf>-<slot>, e.g. image1.aoe-1-1
shelf_no=""
slot_no=""
for i in `seq 0 $aoe_shelf_max`; do
  for j in `seq 0 $aoe_slot_max`; do
    if [ -z "$(unalias ls &>/dev/null; ls -alFh $sanboot_img_dir/*.aoe-$i-$j-for-* 2>/dev/null)" ]; then
        shelf_no=$i
	slot_no=$j
    fi
    [ -n "$slot_no" ] && break
  done
  [ -n "$shelf_no" ] && break
done
echo "Available AoE shelf #: $shelf_no, slot #:$slot_no"

# MAC address of the ethx linked to drbl server
ethx_link_2_drbl="$(get-port-link-2-drbl-srv)"
if [ -n "$ethx_link_2_drbl" ]; then
  # To avoid strange character ":" in the file name, we use "-"
  mac_add="$(drbl-get-macadd $ethx_link_2_drbl | tr "[A-Z]" "[a-z]" | tr ":" "-")"
else
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "The ethernet port connected to DRBL server was NOT found!"
  echo "Something went wrong!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "Program terminated!"
  exit 1
fi
aoe_img_name="${img_name_prefix}.aoe-${shelf_no}-${slot_no}-for-${mac_add}"
#
echo "Disk $device partition table:"
echo ================================================
LANG=C fdisk -l $device
echo ================================================
echo "Dumping $device (size: $disk_size) as image $sanboot_img_dir/$aoe_img_name on server by:"
echo dd if=$device of=$sanboot_img_dir/${aoe_img_name} bs=$bs count=$count
echo "This might take a while... "
trigger_dd_status_report "$device" "$disk_size" &
dd_report_sig_pid=$!
start_time="$(date +%s%N)"
dd if=$device of=$sanboot_img_dir/${aoe_img_name} bs=$bs count=$count
end_time="$(date +%s%N)"
kill -9 $dd_report_sig_pid &>/dev/null
echo "done!"
