#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: 
#  start/stop services on the drbl clients to find swap partition

# must be root
ID=`id -u`
if [ "$ID" != "0" ]; then
  echo "You must be root to use this script."
  echo "Use sudo to enable it for users."
  exit 1
fi

drblhost="/var/lib/diskless/default"
swapfile_size=$2
if [ "$swapfile_size" = "" ]; then swapfile_size=64; fi
clean_swapfile=$3

case $1 in
  'start')
    echo -n "Configure to create $swapfile_size M swap files on client's hard disk.."
    for host in `ls $drblhost`; do
      if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then continue; fi
      tail +55 $0 > $drblhost/$host/etc/rc2.d/S99mkswapfile
      perl -p -i -e "s/SWAPFILE_SIZE=.*/SWAPFILE_SIZE=$swapfile_size/g" $drblhost/$host/etc/rc2.d/S99mkswapfile
      chmod 755 $drblhost/$host/etc/rc2.d/S99mkswapfile
      if [ "$clean_swapfile" != "" ]; then
        cp $drblhost/$host/etc/rc2.d/S99mkswapfile $drblhost/$host/etc/rc0.d/K01mkswapfile
        cp $drblhost/$host/etc/rc2.d/S99mkswapfile $drblhost/$host/etc/rc6.d/K01mkswapfile
      fi
    done
    echo "done.."
    ;;
  'stop')
    echo -n "Stop using swap files on client's hard disk and remove the files.."
    for host in `ls $drblhost`; do
      if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then continue; fi
      #rm -f $drblhost/$host/etc/rc2.d/S99mkswapfile
      perl -p -i -e "s/KILL_ME=0/KILL_ME=1/g" $drblhost/$host/etc/rc2.d/S99mkswapfile
      rm -f $drblhost/$host/etc/rc0.d/K01mkswapfile
      rm -f $drblhost/$host/etc/rc6.d/K01mkswapfile
    done
    echo "done.."
    ;;
  *)
    echo "Usage: "
    echo "  $0 start [swapfile_size] [clean_swapfile]"
    echo "  $0 stop"
    echo "  unit of swapfile_size is 'M', that is, 64 means 64M"
    echo "  default swapfile_size is 64M"
    ;;
esac
exit 0
#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: 
#  find the partition for swap file

SWAPFILE_SIZE=64
KILL_ME=0

mkswapfile() {

  swapfile_size=$1
  if [ "$swapfile_size" = "" ]; then swapfile_size=64; fi

  TMP="/tmp/mkswapfile.$$"
  total_swap_size=0

  # 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`
    total_swap_size=`expr $exist_swap_size_in_MB + $total_swap_size`
    swapfile_size=`expr $swapfile_size - $exist_swap_size_in_MB`
    [ $total_swap_size -ge $SWAPFILE_SIZE ] && 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
    d="/mnt/${p##*/}"
    f="$d/drbl.swap"
    mkdir -p $d
    # mount the partition, check the size, create the swap file
    if mount -o rw $p $d; then

      if [ -f $f ]; then
        echo "Using the existing swap file in $p"
        /sbin/swapon -v "$f"
        exist_swap_size=`cat /proc/swaps | grep $f | awk -F" " '{print $3}'`
        exist_swap_size_in_MB=`expr $exist_swap_size / 1000`
        total_swap_size=`expr $exist_swap_size_in_MB + $total_swap_size`
        swapfile_size=`expr $swapfile_size - $exist_swap_size_in_MB`
      else 
        echo "Creating a swap file in $p"
        ## check the partition size, use 60% for swap 
        AVAIL=$(df -m $d | awk '/^\/dev\//{print $4}')
        AVAIL=$(echo "scale=0; $AVAIL * 3 / 5 " |bc -l)

        if [ $AVAIL -le $swapfile_size ]; then
          ## create the swap file
          dd if=/dev/zero of="$f" bs=1000k count="$AVAIL"
          chmod 600 $f && /sbin/mkswap -v1 "$f" && /sbin/swapon -v "$f"
          total_swap_size=`expr $AVAIL + $total_swap_size`
          swapfile_size=`expr $swapfile_size - $AVAIL`
        else 
          dd if=/dev/zero of="$f" bs=1000k count="$swapfile_size"
          chmod 600 $f && /sbin/mkswap -v1 "$f" && /sbin/swapon -v "$f"
          total_swap_size=$swapfile_size
        fi
      fi
    fi
    # check if the total_swap_size is greater than SWAPFILE_SIZE
    if [ $total_swap_size -ge $SWAPFILE_SIZE ]; then break; fi
    if [ $swapfile_size -le 0 ]; then break; fi
  done
  rm -f $TMP
}

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="/mnt/${p##*/}"
    f="$d/drbl.swap"
    mkdir -p $d
    if [ "$(mount | grep "$d")" = "" ]; then
      mount -o rw $p $d
    fi
    if [ -f $f ]; then
      /sbin/swapoff -v "$f"
      rm -f $f
    fi
    umount $d
  done
}

killme() {
  rmswapfile
  rm -f $0
  exit 0
}

###
case "$1" in
  "start")
    if [ $KILL_ME -eq 1 ]; then killme; fi
    echo "Create swap file (This may take several minitues)"
    $0 mkswapfile &
    ;;
  "stop")
    echo "Remove swap file (This may take several minitues)"
    $0 rmswapfile
    ;;
  "mkswapfile")
    mkswapfile $SWAPFILE_SIZE
    ;;
  "rmswapfile")
    rmswapfile
    ;;
  *)
    ;;
esac

exit 0
