#!/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

# set root passwd, I do not like root no passwd.
echo "root:drbllive" | /usr/sbin/chpasswd

# add the casper account with UID=999 first, otherwise if we add accounts later, when Debian live boots, scripts/casper-bottom/10adduser will kill casper account first (if it's not UID=999, 10adduser can remove that successfully), then when it try to add it, it will fail. Then there is no casper account at all. This will result client's /sbin/casper-getty fails in /etc/inittab. Therefore we can not login in console 1-6.
casper_uid="999"
casper_passwd="live"
/usr/sbin/useradd -m -u $casper_uid casper
echo "casper:$casper_passwd" | /usr/sbin/chpasswd

# 2007/03/11. Comment this. Account casper should be enough.
# add super accounts: drbl
# It seems if we add any account (ex. drbl), the default account casper (UID: 999) will disapper.
#users="drbl"
## Give them an universal passwd: live
#passwd_uni=live
#for username in $users; do
#  /usr/sbin/useradd -m $username
#  echo "$username:${passwd_uni}" | /usr/sbin/chpasswd
#  # add user to sudoers
#  chmod u+w /etc/sudoers
#  echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
#  chmod u-w /etc/sudoers
#done

# 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

# 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

# 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

# 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

# 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

# Append the PATH and default LANG in system.
echo "export PATH=/opt/drbl/sbin:/opt/drbl/bin:\$PATH" >> /etc/profile
echo "export PATH=/opt/drbl/sbin:/opt/drbl/bin:\$PATH" >> /etc/bash.bashrc

# 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
