#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Program to create DRBL/Clonezilla server live. You'd better to have 2 or more NICs in the machine, and they are configured so the created Clonezilla server live has preset DRBL environment.

# create a dummy /etc/fstab in Live CD so that nis/yp won's complain
touch /etc/fstab

# run drbl setup, we use DRBL SSI mode and clonezilla box mode
yes "" | /opt/drbl/sbin/drblsrv -i -l 0
# each ethernet card provide 20 clients.
yes "" | /opt/drbl/sbin/drblpush -i -l 0 -r 1 -z 1 -p 20
# set root passwd
echo "root:drbllive" | /usr/sbin/chpasswd

# add an account drbl
username=drbl
passwd_drbl=live
/usr/sbin/useradd -m $username
echo "$username:${passwd_drbl}" | /usr/sbin/chpasswd
# add user drbl to sudoers
chmod u+w /etc/sudoers
echo "drbl ALL=(ALL) ALL" >> /etc/sudoers
chmod u-w /etc/sudoers

# enable autologin
passwd_autologin=live
/opt/drbl/sbin/drbl-login-switch -l 0 -a -p ${passwd_autologin}

# stop all the services so that make-live can pack it.
/opt/drbl/sbin/drbl-all-service stop

# generate a template for tcp wrapper.
client_IP_list="$(/opt/drbl/bin/get-client-ip-list)"
client_IP_list="$(echo $client_IP_list)"   # convert to 1 line
echo "ALL: $client_IP_list" > /etc/hosts.allow.drbl-template

# Since there is an issue about squashfs + kernel nfs, we use user space nfs
# http://lists.alioth.debian.org/pipermail/debian-live-devel/2006-September/000470.html 
# This also has a benefit, the mounted /home/partimage is ready to be seen by client.
apt-get --yes remove nfs-kernel-server
apt-get --yes install unfs3

# preset some network setting
guess_uplink_port="$(route -n | awk '/^0.0.0.0/ {print $8}' | sort | head -n 1)"
ethernet_list="$(/opt/drbl/bin/get-all-nic-ip -d)"
ethernet_drbl="$(echo $ethernet_list | sed -e "s/$guess_uplink_port//g")"
cat <<-NET_END > /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

NET_END

for nic in $ethernet_drbl; do
  IP_ADR=""
  IP_ADR="$(/opt/drbl/bin/get_ip $nic)"
  if [ -n "$IP_ADR" ]; then
    cat <<-NET_END >> /etc/network/interfaces
auto $nic
iface $nic inet static
	address $IP_ADR
	netmask 255.255.255.0

NET_END
  fi
done

# create isolinux menu
# Load setting and functions
. /opt/drbl/sbin/drbl-conf-functions

# Load some necessary setting from drbl-ocs.conf
. /opt/drbl/conf/drbl-ocs.conf

# Load basic functions of ocs
. /opt/drbl/sbin/ocs-functions
#
mkdir -p isolinux
# we have to overwrite isolinux.bin since vesamenu.c32 should be same version with that.
etherboot_zlilo="$($query_pkglist_cmd drbl-etherboot | grep -E "eb-.*-etherboot-pci.zlilo$")"
cp -f $isolinux_file $pxelinux_simple_vesamenu $pxelinux_simple_menu $pxelinux_bg_img $ocs_logo_img_syslinux $pxelinux_chain_file /isolinux/
if [ -n "$etherboot_zlilo" ]; then
  # we have to force it name as etherboot.zdsk, since isolinux only uses the "plain" ISO 9660 filenames, i.e. it does not support Rock Ridge or Joliet filenames.
  # ref: http://syslinux.zytor.com/archives/2006-October/007440.html
  # "-" will be regards as "_" if you want to use "-" for isolinux.
  # In syslinux on vfat, etherboot.zlilo is too long, make it ever shorter as eb.zli
  cp -f $etherboot_zlilo /isolinux/eb.zli
fi
# generate the menu
/opt/drbl/sbin/ocs-live-boot-menu -l 0 --title "DRBL server live" isolinux /isolinux/

# do some dirty clean... no idea why "/etc/init.d/nfs-kernel-server stop" and "/"/etc/init.d/nfs-common stop" won't be able to release this:
# nfsd on /proc/fs/nfsd type nfsd (rw)
umount nfsd

# Run ocs_minimal_hook, this is the same as we do to create debian-live-for-ocs Live CD.
/ocs_minimal_hook

### THE END ###
# DO NOT PUT SCRIPT AFTHER THIS!!!
# kill this program before creating squashfs filesystem.
rm $0
