#!/bin/bash

# from: http://gentoo-wiki.com/HOWTO_network_boot_without_write_access_on_server
# also see: http://usher.ucsd.edu/downloads/scripts/

### create ramdisks ###
echo "creating ramdisk for /tmp"
mount -n -t tmpfs tmpfs /tmp

QUIET=1

### save current dirs ###
echo "Saving current state:"
for i in $(egrep -v '^([#]|[ ]*$)' /tmpfs.lst); do
	if [ -d "$i" ]; then
		tmpfile=/tmp/${i//\//.}.tar
		cd "$i" && tar cpf "$tmpfile" .
		mount -n -t tmpfs tmpfs "$i" && tar xpf "$tmpfile" -C "$i"
#		rm -f "$tmpfile"
		echo -n " D  $i"
	elif [ -L "$i" ]; then
		# check if symlink points to /tmp
		lnk=$(/usr/bin/readlink "$i" | grep '^/tmp/')
		echo -n " @  $i"
		if [ -z "$lnk" ]; then
			#[ $QUIET -eq 0 ] && echo "WARNING: $i: not a symbolic link that points to /tmp"
			echo -n "    :WARNING: not a symbolic link that points to /tmp"
		else
			touch "$i"
			echo -n " -> $lnk"
		fi
	elif [ -f "$i" ]; then
		#[ $QUIET -eq 0 ] && echo "WARNING: $i: is a file, skipping"
		echo -n " F   $i"
		echo -n "    :WARNING: is a file, skipping"
	else
		echo -n " ?   $i"
	fi
	echo
done

echo "creating ramdisk for /root"
mount -n -t tmpfs tmpfs /root

#exec /sbin/init </dev/console >/dev/console 2>&1
exec /bin/bash </dev/console >/dev/console 2>&1
