#!/bin/sh
# mkswapfile    This shell script takes care of starting and stopping
#               the mkswapfile.
#
# chkconfig: 2345 15 85
# description: mkswapfile is a script to create swap file for DRBL client.
#
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: 
#  start|stop drbl cluster mode
#  disk is used to install the cluster node (the same as drblcd) on local disk
#
# Modified by Steven Shiau <steven@nchc.org.tw> to use it in DRBL for Redhat.
# This file is borrowed from Knoppix (http://www.knoppix.org).

# The MaxSwapSize (MB)
# MaxSwapSize=512
# Source function library.
. /etc/rc.d/init.d/functions

# Source mkswapfile configuration.
[ ! -f /etc/sysconfig/mkswapfile ] && exit 1

. /etc/sysconfig/mkswapfile


newswapfile() {
  total_swap_size=0
  # Maximum swap file size
  MaxSwapSize=$1
  # Find exist swap partitions
  for p in `/sbin/fdisk -l | grep "Linux swap" | cut -d" " -f1`; do
    echo "Using the existing swap partition in $p"
    /sbin/swapon $p
    exist_swap_size=`cat /proc/swaps | grep $p | awk -F" " '{print $3}'`
    exist_swap_size_in_MB=`expr $exist_swap_size / 1000`
    echo "$p with size $exist_swap_size_in_MB (MB)."
    total_swap_size=`expr $exist_swap_size_in_MB + $total_swap_size`
    [ $total_swap_size -ge $MaxSwapSize ] && return 0
  done

  # Create swap files in exist partitions
  for p in `/sbin/fdisk -l | grep "^\/dev\/" | awk '{ if($2=="*" && $6!="f" && $6!="0") print $1; if($2!="*" && $5!="f" && $5!="0") print $1; }'`
  do
    #initial value
    Swap_2_create=0
    SwapSize_Need=0
    exist_size_in_MB=0
    #
    SwapSize_Need=`expr $MaxSwapSize - $total_swap_size`
    d="/tmp/${p##*/}"
    f="$d/drbl-cluster.swap"
    mkdir -p $d
    # mount the partition, check the size, create the swap file
    if mount $p $d; then
      if [ -f "$f" ]; then
         exist_size_in_MB=`du --block-size=1M /tmp/hda1/drbl-cluster.swap | awk -F" " '{print $1}'`
         echo "exist_size_in_MB = $exist_size_in_MB"
      fi
      ## check the partition size, use 60% for swap 
      AVAIL=$(df --block-size=1M $d | awk '/^\/dev\//{print $4}')
      AVAIL=`expr $AVAIL + $exist_size_in_MB`
      AVAIL=$(echo "scale=0; $AVAIL * 3 / 5 " |bc -l)
      #[ $AVAIL -gt $SwapSize_Need ] && AVAIL=$SwapSize_Need
      if [ $SwapSize_Need -gt $AVAIL ]; then
	 Swap_2_create=$AVAIL
      else
	 Swap_2_create=$SwapSize_Need
      fi
      # when the swap file if not exist, or the existing swapfile size
      # is not matched, create it.
      if [ ! -f "$f" -o "$exist_size_in_MB" != "$Swap_2_create" ]; then
         dd if=/dev/zero of="$f" bs=1M count="$Swap_2_create"
         chmod 600 $f 2>/dev/null
      fi
      # force to format it as swapfile no matter it is or not.
      /sbin/mkswap -v1 "$f" && /sbin/swapon -v "$f"
    fi

    # check if the total_swap_size is greater than $MaxSwapSize
    total_swap_size=`expr $Swap_2_create + $total_swap_size`
    [ $total_swap_size -ge $MaxSwapSize ] && return 0
  done
    if [ $total_swap_size -eq 0 ]; then
      echo "Can NOT find available partition, NO swap file will be created!"
    fi
}

rmswapfile() {
  for p in `/sbin/fdisk -l | grep "^\/dev\/" | awk '{ if($2=="*" && $6!="f" && $6!="0") print $1; if($2!="*" && $5!="f" && $5!="0") print $1; }'`
  do
    d="/tmp/${p##*/}"
    f="$d/drbl-cluster.swap"
    if [ -f $f ]; then
      /sbin/swapoff -v "$f"
      # do not remove, so that next time we can use, this should be better,
      # otherwise client has to spend some time to dd again.
      #rm -f $f
    fi
    if [ "$(mount | grep "$d")" != "" ]; then
      umount $d
    fi
  done
}

###
case "$1" in
  "start")
    #echo "Creating swap file (Upto $maxswapsize Mb) if the harddisk is available... This may take few minitues."
    action $"Starting mkswapfile services: " $0 newswapfile $maxswapsize
    RETVAL=$?
    [ "$RETVAL" = 0 ] && touch /var/lock/subsys/mkswapfile
    ;;
  "stop")
    #echo "Removing swap file... This may take few minitues."
    action $"Stoping mkswapfile services: " $0 rmswapfile
    RETVAL=$?
    [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/mkswapfile
    ;;
  "newswapfile")
    newswapfile $maxswapsize
    ;;
  "rmswapfile")
    rmswapfile
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart}"
    exit 1
esac

exit 0
