#!/bin/sh
#
# Copyright 2001 2002 Edson Brandi <ebrandi.home@uol.com.br>
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY EDSON BRANDI ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT EDSON BRANDI BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.

############################################

echo "Creating  /dev under MFS..."
mount_mfs -s 24000 -c 20 -o noatime swap /dev 2>&1
tar -xzpf /mfs/dev.tgz -C / >/dev/null 2>&1
cd /dev
./MAKEDEV all  >/dev/null 2>&1
#

# Now we will try to detect all IDE disks and existing slices and try to mount
# everyone that is FAT, NTFS, UFS or EXT2FS type
#
#
echo "Checking for slices..."
#
for i in `dmesg | egrep "ad0:|ad1:|ad2:|ad3:" | awk -F":" '{print $1}' | sort | uniq`
do
  a=1
  for x in `fdisk /dev/$i | grep sysid | awk -F"," '{print $1}' | awk '{print $2}'`
  do
    case ${x} in
    5)
        echo "FAT16/32 partition found at /dev/${i}s${a}, i'll mount it under /mnt/dos.${a} ..."
        mount -t msdos /dev/${i}s${a} /mnt/dos.${a}
        ;;
    6)
        echo "FAT16/32 partition found at /dev/${i}s${a}, i'll mount it under /mnt/dos.${a} ..."
        mount -t msdos /dev/${i}s${a} /mnt/dos.${a}
        ;;
    12)
        echo "FAT16/32 partition found at /dev/${i}s${a}, i'll mount it under /mnt/dos.${a} ..."
        mount -t msdos /dev/${i}s${a} /mnt/dos.${a}
        ;;
    7)
        echo "NTFS partition found at /dev/${i}s${a}, i'll mount it under /mnt/ntfs.${a} ..."
        mount -t ntfs /dev/${i}s${a} /mnt/ntfs.${a}
        ;;
    131)
        echo "EXT2FS partition found at /dev/${i}s${a}, i'll mount it under /mnt/ext2fs.${a} ..."
        mount -t ext2fs /dev/${i}s${a} /mnt/ext2fs.${a}
        ;;
    165)
        echo "UFS partition found at /dev/${i}s${a}, i'll mount it under /mnt/ufs.${a} ..."
        mount  /dev/${i}s${a} /mnt/ufs.${a}
        ;;
    esac
    a=`expr $a + 1`
    done
done
####
####
#
# Now, at boot time, we will try to find any freebsd/ directory under every slice
# we have mounted. Our goal is, if we find the directory, we will look for 
# .flp files to attach them as Virtual Nodes ;-)
#

echo "Checking for Virtual Nodes..."
#

for i in `df -k | egrep "ufs|dos|ntfs|ext2fs" | awk '{print $6}'`
do
if [ -r ${i}/freebsd ]; then

if [ -r ${i}/freebsd/tmp.flp ]; then
        vnconfig -c /dev/vn1c   ${i}/freebsd/tmp.flp
        sleep 2
        mount /tmp
fi

if [ -r ${i}/freebsd/var.flp ]; then
        vnconfig -c /dev/vn3c  ${i}/freebsd/var.flp
        sleep 2
        mount /var
fi

if [ -r ${i}/freebsd/etc.flp ]; then
        vnconfig -c /dev/vn4c  ${i}/freebsd/etc.flp
        sleep 2
        mount /etc
        . /etc/rc.conf
fi

if [ -r ${i}/freebsd/home.flp ]; then
        vnconfig -c /dev/vn5c  ${i}/freebsd/home.flp
        sleep 2
        mount /home
fi

if [ -r ${i}/freebsd/root.flp ]; then
        vnconfig -c /dev/vn6c  ${i}/freebsd/root.flp
        sleep 2
        mount /root
fi

if [ -r ${i}/freebsd/local.flp ]; then
        vnconfig -c /dev/vn7c  ${i}/freebsd/local.flp
        sleep 2
        mount /usr/local
fi

if [ -r ${i}/freebsd/X11R6.flp ]; then
        vnconfig -c /dev/vn8c  ${i}/freebsd/X11R6.flp
        sleep 2
        mount /usr/X11R6
fi
fi
done

# Now we will check if virtual nodes were mount (will check for it via a control file) 
# If we find the partition file, its mounted, if not, we will generate it under MFS
# and will unpack the necessery files.
#

if [ -r /tmp/XYZ123_ ]; then
        echo "/tmp partition was mounted via virtual node"
        else
        mount_mfs -s 32768 -c 20 -o nodev,noexec,nosuid,noatime swap /tmp 2>&1
        echo "Generating /tmp under MFS"
fi

if [ -r /var/XYZ123_ ]; then
        echo "/var partition was mounted via virtual node"
        else
        mount_mfs -s 32768 -c 20 -o noatime swap /var 2>&1
        /usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var >/dev/null 2>&1
 	logs=`/usr/bin/awk '$1 != "#" { printf "%s ", $1 } ' /etc/newsyslog.conf`	
	if [ -n "$logs" ]; then
        	/usr/bin/touch $logs
	fi
	/usr/bin/touch /var/log/lastlog
        echo "Generating /var under MFS"
fi

if [ -r /etc/XYZ123_ ]; then
        echo "/etc partition was mounted via virtual node"
        else
        mount_mfs -s 32768 -c 20 -o noatime swap /etc 2>&1
        tar -xzpf /mfs/etc.tgz -C / >/dev/null 2>&1
        echo "Generating /etc under MFS"
fi

if [ -r /home/XYZ123_ ]; then
        echo "/home partition was mounted via virtual node"
        else
        mount_mfs -s 32768 -c 20 -o noatime swap /home 2>&1
        echo "Generating /home under MFS"
fi

if [ -r /root/XYZ123_ ]; then
        echo "/root partition was mounted via virtual node"
        else
        mount_mfs -s 32768 -c 20 -o noatime swap /root 2>&1
        tar -xzpf /mfs/root.tgz -C / >/dev/null 2>&1
        echo "Generating /root under MFS"
fi

if [ -r /usr/local/XYZ123_ ]; then
        echo "/usr/local partition was mounted via virtual node"
        else
        mount_mfs -s 32768 -c 20 -o noatime swap /usr/local/etc 2>&1
        tar -xzpf /mfs/local_etc.tgz -C / >/dev/null 2>&1
        echo "Generating /usr/local/etc under MFS"
fi

mount proc

############################################


syntax highlighted by Code2HTML, v. 0.9.1