#!/bin/sh
# netapp_init:
# This script creates a TMPFS on /newroot, copies over all the files in 
# the JFFS2 root-fs to this dir, mounts devfs and procfs, does a 
# pivot_root to /newroot, and chroot's /sbin/init on /newroot
# The RLM will now be running entirely from a RAM based root-fs. This
# will facilitate in-place updates of the RLM's file system while it 
# is running, giving us the advantages of having the initrd mechanism 
# and that of having the JFFS2 mechanism with 25% less RAM utilization
#
# Kiran Kamity, 09/11/2004
# Sreen Tallam, 08/07/2008
#

echo "Starting netapp_init process"
# Creating a TMPFS of max size 32 MB and mounting /newroot on it

mount -t tmpfs none /newroot -o size=48m		
cd /newroot
mkdir oldroot

# Copying contents from / to /newroot
cp -a /bin .
cp -a /boot .
cp -a /etc .
cp -a /home .
cp -a /lib .
cp -a /opt .
cp -a /root .
cp -a /sbin .
cp -a /usr .
cp -a /var .
	
# Creating udev in /newroot
mkdir dev		

# Creating all the other necessary dirs in /newroot
mkdir proc
mkdir sys
mkdir tmp
mkdir var/dhcp
mkdir var/run
mkdir var/log
mkdir mnt
mkdir mnt/logs
mkdir mnt/nfsxfer
mkdir mnt/jffs2_rootfs_primary
mkdir mnt/jffs2_rootfs_backup
mkdir netapp
mkdir netapp/run
mkdir install
cp -a /install/Metadata install/.

# Change permissions as needed
chmod a+rwx netapp
chmod a+rwx netapp/run

# pivot_root'ing to the new root
# This moves the old root (/) to the /oldroot dir
/sbin/pivot_root . oldroot
echo $?
		
# Mounting /proc in the new root
/bin/chroot . mount /proc
/bin/chroot . mount -t sysfs nodev /sys

# Chroot'ing and running /sbin/init in the new root
/bin/chroot . mknod -m 0600 /dev/console c 5 1
/bin/chroot . mknod -m 0666 /dev/null c 1 3

exec /bin/chroot . /sbin/init < /dev/console > /dev/console 2>&1

# This shouldn't return
exit
