#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description: /tmp configuration

drblhost="/var/lib/diskless/default/"

usage() {
  echo "$0 [tmpfs|nbd] [tmpdir_size]"
  echo "unit of tmpdir_size is 'M', that is, 16 means 16M"
  echo "default tmpdir_size is 16M"
}

tmpdir_size=$2
if [ "$tmpdir_size" = "" ]; then tmpdir_size=16; fi

### create all hosts before setup ..
if [ -f /etc/default/drbl ]; then
  . /etc/default/drbl
fi

if [ "$NO_DRBLMGRD" = "true" -o "$NO_DRBLMGRD" = "" ]; then
  ## no drblmgrd
  if [ ! -f /opt/drbl/sbin/drbl_deploy.sh ]; then
    echo "DRBL Configuration is not correct!"
    echo "Please use '/opt/drbl/sbin/drblpush-desktop' to setup!!"
    exit
  else
    for ip in `cat /etc/dhcp3/dhcpd.conf | grep "fixed-address" | cut -d\; -f1 | awk '{ print $2; }'`
    do
      ## if one host lost in $imagehost, regenerate all ..
      if [ ! -d $imagehost/$ip ]; then
        /opt/drbl/sbin/drbl_deploy.sh
        break
      fi
    done
  fi
else
  ## drblmgrd
  drblmgrd=`/sbin/ifconfig eth0 | grep "inet addr" | cut -d: -f2 | awk '{ print $1; }'`
  drblmgrd_ps=`ps -e 2> /dev/null | grep "drblmgrd"`
  if [ "$drblmgrd" = "" -o "$drblmgrd_ps" = "" ]; then
    echo "DRBL Configuration is not correct!"
    echo "Please use '/opt/drbl/bin/drblpush-desktop' to setup!!"
    exit
  fi

  for ip in `cat /etc/dhcp3/dhcpd.conf | grep "fixed-address" | cut -d\; -f1 | awk '{ print $2; }'`
  do
    if [ ! -d $imagehost/$ip ]; then
      echo "create host: $ip"
      echo "$ip" | /usr/bin/socket $drblmgrd 6460 
    fi
  done
fi

## main ##
case "$1" in
  "tmpfs")
    echo -n "Configure to use tmpfs on /tmp of clients.."
    for host in `ls $drblhost`; do
      ## skip some host ..
      if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then continue; fi
      skip=0
      for netdev in `cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}'`
      do
        ip=`/sbin/ifconfig $netdev | grep "inet addr" | cut -d: -f2 | awk '{ print $1; }'`
        if [ "$host" = "$ip" ]; then skip=1; fi
      done
      if [ $skip -eq 1 ]; then continue; fi
      ## fstab
      rm -f /tmp/fstab.$host
      while read spec file vfstype others; do
        if [ "$spec" = "#tmpfs" -a "$file" = "/tmp" ]; then spec="tmpfs";
        elif [ "$spec" = "/dev/nbd0" -a "$file" = "/tmp" ]; then spec="#/dev/nbd0";
        fi
        echo "$spec $file $vfstype $others" >> /tmp/fstab.$host
      done < $drblhost/$host/etc/fstab
      cp /tmp/fstab.$host $drblhost/$host/etc/fstab
      rm -f /tmp/fstab.$host
      ## nbd-client
      rm -f $drblhost/$host/etc/nbd-client
    done
    ## nbd-server
    rm -f /etc/nbd-server
    echo "done.."
    ;;
  "nbd")
    echo "Configure to use network block device (NBD) on /tmp of clients.." 
    i=0
    for host in `ls $drblhost`; do
      ## skip some host ..
      if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then continue; fi
      skip=0
      for netdev in `cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}'`
      do
        ip=`/sbin/ifconfig $netdev | grep "inet addr" | cut -d: -f2 | awk '{ print $1; }'`
        if [ "$host" = "$ip" ]; then skip=1; fi
      done
      if [ $skip -eq 1 ]; then continue; fi

      ## fstab
      rm -f /tmp/fstab.$host
      while read spec file vfstype others; do
        if [ "$spec" = "tmpfs" -a "$file" = "/tmp" ]; then spec="#tmpfs"; 
        elif [ "$spec" = "#/dev/nbd0" -a "$file" = "/tmp" ]; then spec="/dev/nbd0";
        fi
        echo "$spec $file $vfstype $others" >> /tmp/fstab.$host
      done < $drblhost/$host/etc/fstab
      cp /tmp/fstab.$host $drblhost/$host/etc/fstab
      rm -f /tmp/fstab.$host
      ## nbd-client
      nbd_port=`expr $i + 1077`
      nbd_server=""
      while read spec file others; do
        if [ "$file" = "/" ]; then nbd_server="$(echo "$spec" | cut -d: -f1)"; fi
        if [ "$nbd_server" != "" ]; then break; fi
      done < $drblhost/$host/etc/fstab 
      echo "NBD_DEVICE[0]=\"/dev/nbd0\"" > $drblhost/$host/etc/nbd-client
      echo "NBD_TYPE[0]=\"f\"" >> $drblhost/$host/etc/nbd-client
      echo "NBD_HOST[0]=\"$nbd_server\"" >> $drblhost/$host/etc/nbd-client
      echo "NBD_PORT[0]=\"$nbd_port\"" >> $drblhost/$host/etc/nbd-client
      ## nbd-server
      mk_nbd_remote=1
      echo "NBD_PORT[$i]=$nbd_port" >> /etc/nbd-server
      echo "NBD_FILE[$i]=$drblhost/$host/tmp/nbd-remote" >> /etc/nbd-server
      if [ -f $drblhost/$host/tmp/nbd-remote ]; then
        nbd_remote_size=`du --block-size 1000k $drblhost/$host/tmp/nbd-remote | awk '{ print $1; }'`
        if [ $nbd_remote_size -eq $tmpdir_size ]; then mk_nbd_remote=0; fi
      fi
      if [ $mk_nbd_remote -eq 1 ]; then
        echo "Create a NBD for $host.. This will take several minitues"
        dd if=/dev/zero of=$drblhost/$host/tmp/nbd-remote bs=1000k count=$tmpdir_size
        mke2fs -q -F $drblhost/$host/tmp/nbd-remote
      else
        echo "Use the NBD created before for $host"
      fi

      i=`expr $i + 1`
    done
    /etc/init.d/nbd-server restart
    echo "done.."
    ;;
  *) usage ;;
esac
