#!/bin/sh
# @(#)mknod.sh  2004-02-18  A.J.Travis

#
# Make ClusterNFS files for diskless node
#

if [ $# -ne 1 ]; then
	echo usage: mknode n
	exit 1
fi

NET=192.168
IPC=$NET.0.$1
NFS=$NET.1.$1
TAG='$$'IP=$NFS'$$'

#
# Check client tag files are in place
#
cd /

# Mountpoint for /proc filesystem
if [ ! -d 'proc$$CLIENT$$' ]; then
	mkdir 'proc$$CLIENT$$'
fi

#
# Client versions need to be edited manually
# inittab: initdefault 3
#
cd /etc

# update hosts database
fgrep -v $IPC hosts | fgrep -v $NFS > hosts.new
echo "$IPC	mpe$1" >> hosts.new
echo "$NFS	node$1" >> hosts.new
mv hosts hosts.bak
mv hosts.new hosts

if [ ! -f 'fstab$$CLIENT$$' ]; then
	echo warning: 'fstab$$CLIENT$$' missing
fi
if [ ! -f 'inittab$$CLIENT$$' ]; then
	echo warning: 'inittab$$CLIENT$$' missing
fi

#
# Enable only what is needed on clients
# The file DoNotExecuteOnClients should not exist.
#
cd /etc/init.d
rm -f DoNotExecuteOnClients

# Disable everything
for i in `ls | fgrep -v '$$CLIENT$$'`; do
	if [ ! -h ${i}'$$CLIENT$$' ]; then
		rm -f ${i}'$$CLIENT$$'
		ln -s DoNotExecuteOnClients ${i}'$$CLIENT$$'
	fi
done

# Enable only what is needed on clients
for i in \
	functions \
	autofs \
	halt \
	single \
	network \
	syslog \
	portmap \
	keytable \
	random \
	sshd \
	openmosix \
	ypbind
do
	rm ${i}'$$CLIENT$$'
done

# Prevent init 0, 1 and 6 from shutting down the network
cd /etc
for dir in rc0.d rc1.d rc6.d; do
	cd $dir
	for file in K*network K*netfs; do
		if [ ! -h ${file}'$$CLIENT$$' ]; then
			rm -f ${file}'$$CLIENT$$'
			ln -s DoNotExecuteOnClients ${file}'$$CLIENT$$'
		fi
	done
	cd ..
done

#
# Node-specific tag files and directories
#
cd /
if [ ! -h dev$TAG ]; then
	rm -rf dev$TAG
	ln -s /export/root/$NFS/dev dev$TAG
fi
if [ ! -h tmp$TAG ]; then
	rm -rf tmp$TAG
	ln -s /export/root/$NFS/tmp tmp$TAG
fi
if [ ! -h root$TAG ]; then
        rm -rf root$TAG
        ln -s /export/root/$NFS/root root$TAG
fi
if [ ! -d var$TAG ]; then
	rm -rf var$TAG
	ln -s /export/root/$NFS/var var$TAG
fi

# Mount table
cd /etc
if [ ! -h 'mtab$$CLIENT$$' ]; then
	rm -f 'mtab$$CLIENT$$'
	ln -s /proc/mounts 'mtab$$CLIENT$$'
fi

# Hostname and gateway
cd /etc/sysconfig
sed -e s/HOSTNAME=.*/HOSTNAME=mpe$1/ \
    -e s/GATEWAY=.*/GATEWAY=$IPC/ network > network$TAG

#
# Static IP addresses for eth0 and eth1 on clients
# Ignore eth2 on clients (LAN interface on head node)
#
cd /etc/sysconfig/network-scripts
if [ ! -h 'ifcfg-eth2$$CLIENT$$' ]; then
	rm -f 'ifcfg-eth2$$CLIENT$$'
	ln -s NoPresentOnClients 'ifcfg-eth2$$CLIENT$$'
fi

# Writeable directories for each client
cd /export
if [ ! -d root ]; then
	mkdir root
fi
cd root
if [ ! -d $NFS ]; then
        mkdir $NFS
fi
cd $NFS
if [ ! -d dev ]; then
        mkdir dev
	mkdir dev/pts
	cp -a /dev/MAKEDEV dev
	dev/MAKEDEV -d dev console generic
fi
if [ ! -d root ]; then
        mkdir root
	cp -a /root/.bashrc root
	cp -a /root/.ssh root
fi
if [ ! -d tmp ]; then
        mkdir tmp
fi
if [ ! -d var ]; then
        mkdir var
fi
cd var
if [ ! -d empty ]; then
        mkdir empty
	mkdir empty/sshd
fi
if [ ! -d lib ]; then
        mkdir lib
	mkdir lib/dhcp
	mkdir lib/rpm
fi
if [ ! -d lock ]; then
	mkdir lock
	mkdir lock/subsys
fi
if [ ! -d log ]; then
        mkdir log
	mkdir log/news
fi
if [ ! -d run ]; then
	mkdir run
	mkdir run/netreport
fi
if [ ! -d tmp ]; then
        mkdir tmp
fi
