#!/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 SP will now be running entirely from a RAM based root-fs. This
# will facilitate in-place updates of the SP'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

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

mount -t tmpfs none /newroot -o size=160M
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 /root .
cp -a /sbin .
cp -a /usr .
cp -a /var .
cp -a /conf .
cp -a /dev .
		
# Creating all the other necessary dirs in /newroot
mkdir proc
mkdir sys
mkdir tmp
mkdir tmp/oem
mkdir tmp/auth
mkdir tmp/firmware
mkdir tmp/firmware/battery
mkdir var/dhcp
mkdir var/log
mkdir mnt
mkdir mnt/logs
mkdir mnt/nfsxfer
mkdir mnt/jffs2_rootfs_primary
mkdir mnt/jffs2_rootfs_backup
mkdir mnt/sapps
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
chmod a+rwx tmp
chmod a+rwx tmp/oem
chmod a+rwx tmp/firmware
chmod a+rwx tmp/firmware/battery

# 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
/usr/sbin/chroot . mount /proc
# Mounting /sys in the new root
/usr/sbin/chroot . mount -t sysfs sysfs /sys
# Mount /dev/pts
/usr/sbin/chroot . mount -t devpts devpts /dev/pts -o gid=5,mode=620 

#exec /usr/sbin/chroot . /sbin/init < /dev/console > /dev/console 2>&1
# removed /dev/console redirection to support silent boot option
exec /usr/sbin/chroot . /sbin/init  

# This shouldn't return
exit
