#!/bin/sh
# $Id: disksetup.sh,v 1.1 1998/12/30 19:53:04 danw Exp $
# Create partitions, disklabels, etc.

# /tmp/vars inputs:
#	disk		disk device containing NetBSD partition
#	start		starting sector of NetBSD partition
#	size		ending sector of NetBSD partition
#	disksize	total number of sectors on $disk
#	create		whether we need to create the partition
#	rootsize	sectors for root subpartition
#	swapsize	sectors for swap subpartition
#	cachesize	sectors for cache subpartition
#	u1size		sectors for u1 subpartition
#	u1cachesize	megabytes for cache if located in /u1 partition.

. /tmp/vars

if [ -z "$disk" ]; then
	echo "Please run /disksetup to select or create a NetBSD partition."
	exit 1
fi

case "$disk" in
	wd*)	type=ST506	;;
	sd*)	type=SCSI	;;
esac
swapoffset=`expr "$start" + "$rootsize"`
cacheoffset=`expr "$swapoffset" + "$swapsize"`
u1offset=`expr "$cacheoffset" + "$cachesize"`

# We will use a made-up geometry of 16 heads and 63 sectors per track
# in the disklabel (which doesn't have to agree with the BIOS, and in
# fact shouldn't due to FFS limitations).
cylinders=`expr "$disksize" / 63 / 16`

cat >> /etc/disktab << EOM
temp|NetBSD installation generated:\\
	:dt=${type}:ty=winchester:\\
	:nc#${cylinders}:ns#63:nt#16:se#512:su#${disksize}:\\
	:pa#${rootsize}:oa#${start}:ta=4.2BSD:ba#8192:fa#1024:\\
	:pb#${swapsize}:ob#${swapoffset}:tb=swap:\\
	:pc#${size}:oc#${start}:\\
	:pd#${disksize}:od#0:\\
	:pe#${cachesize}:oe#${cacheoffset}:te=4.2BSD:be#8192:fe#1024:\\
	:pf#${u1size}:of#${u1offset}:tf=4.2BSD:bf#8192:ff#1024:
EOM

if [ "$create" = "yes" ]; then
	# Find a free partition on the selected disk.
	eval `fdisk -S /dev/r${disk}d 2> /dev/null | sed -n '/=/p'`
	for i in 0 1 2 3; do
	    eval partsize='$PART'${i}SIZE
	    if [ $partsize -eq 0 ]; then
		freepart=$i
		break
	    fi
	done
	if [ -z "$freepart" ]; then
		echo "Couldn't find a free partition on ${disk}."
		exit 1
	fi
	# Set the free partition to the given values.	
	fdisk -ufa "-$freepart" -s "165/$start/$size" /dev/r${disk}d || exit 1
else
	echo ""
	echo "                        DESTROYS"
	echo -n "Warning: This operation DESTROYS the NetBSD partition "
	echo "on disk $disk".
	echo "                        DESTROYS"
	echo "Please confirm that you wish to overwrite the NetBSD partition"
	echo -n "on disk $disk by typing 'yes': "
	read resp
	if [ "$resp" != "yes" ]; then
		exit 1
	fi
fi

echo ""
echo "Initializing disk ${disk}..."

# Write the NetBSD partition table
disklabel -w -r "$disk" temp || exit 1

# Make the root subpartition.  /tmp will be a symlink to /u1/tmp; make sure
# that /tmp is available (as /rtmp) in single-user mode by making a /u1/tmp
# symlink to /rtmp before mounting /u1.  Also make other directories where
# filesystems will be mounted.
newfs "/dev/r${disk}a" || exit 1
mount -o async "/dev/${disk}a" /hd || exit 1
mkdir /hd/u1 /hd/afs /hd/kern /hd/proc
mkdir /hd/rtmp
chmod 1777 /hd/rtmp
ln -s ../rtmp /hd/u1/tmp

# Make the /u1 subpartition.
newfs "/dev/r${disk}f" || exit 1
mount -o async "/dev/${disk}f" /hd/u1 || exit 1
mkdir -m 755 /hd/u1/var /hd/u1/usr
mkdir /hd/u1/tmp
chmod 1777 /hd/u1/tmp 

# Make /tmp, /usr, and /var symlinks into /u1.
ln -s u1/tmp /hd/tmp
ln -s u1/usr /hd/usr
ln -s u1/var /hd/var

# Make the cache subpartition.
mkdir -p /hd/u1/usr/vice/cache
newfs -m 0 "/dev/r${disk}e" || exit 1
mount -o async "/dev/${disk}e" /hd/u1/usr/vice/cache || exit 1


# Install boot blocks
/os/usr/mdec/installboot -v /os/usr/mdec/biosboot.sym "/dev/r${disk}a" ||exit 1
