#!/bin/sh

#
# "SystemImager"
#
#  Copyright (C) 1999-2001 Brian Elliott Finley <brian@bgsw.net>
#  Copyright (C) 2002-2003 Bald Guy Software <brian@bgsw.net>
#
# This master autoinstall script was created with SystemImager v3.2.3

# Load functions and other variables
. /etc/init.d/functions

get_arch

if [ -z $NO_LISTING ]; then
    VERBOSE_OPT="v"
else
    VERBOSE_OPT=""
fi

# Pull in variables left behind by the linuxrc script.
# This information is passed from the linuxrc script on the autoinstall media 
# via /tmp/variables.txt.  Apparently the shell we use in BOEL is not 
# intelligent enough to take a "set -a" parameter.
#
. /tmp/variables.txt || shellout

[ -z $IMAGENAME ] && IMAGENAME=gridnode
[ -z $OVERRIDES ] && OVERRIDES="gridnode"

### BEGIN Check to be sure this not run from a working machine ###
# Test for mounted SCSI or IDE disks
mount | grep [hs]d[a-z][1-9] > /dev/null 2>&1
[ $? -eq 0 ] &&  echo Sorry.  Must not run on a working machine... && shellout

# Test for mounted software RAID devices
mount | grep md[0-9] > /dev/null 2>&1
[ $? -eq 0 ] &&  echo Sorry.  Must not run on a working machine... && shellout

# Test for mounted hardware RAID disks
mount | grep c[0-9]+d[0-9]+p > /dev/null 2>&1
[ $? -eq 0 ] &&  echo Sorry.  Must not run on a working machine... && shellout
### END Check to be sure this not run from a working machine ###


################################################################################
#
#   Stop RAID devices before partitioning begins
#
# Q1) Why did they get started in the first place?  
# A1) So we can pull a local.cfg file off a root mounted software RAID system.
#     They may not be started on your system -- they would only be started if
#     you did the stuff in Q3 below.
#
# Q2) Why didn't my local.cfg on my root mounted software RAID work for me 
#     with the standard kernel flavour?
# A2) The standard kernel flavour uses modules for the software RAID drivers --
#     therefore, software RAID is not available at the point in the boot process
#     where BOEL needs to read the local.cfg file.  They are only pulled over 
#     when this script is run, which is, of course, only runnable if it was
#     pulled over the network using the settings that you would have wanted it
#     to get from the local.cfg file, which it couldn't.  Right?
#
# Q3) Whatever.  So how do I make it work with a local.cfg file on my root
#     mounted software RAID?  
# A3) Compile an autoinstall kernel with software RAID, and any other drivers 
#     you might need built in (filesystem, SCSI drivers, etc.).
#
if [ -f /proc/mdstat ]; then
  RAID_DEVICES=` cat /proc/mdstat | grep ^md | sed 's/ .*$//g' `

  # raidstop will not run unless a raidtab file exists
  echo "" >> /etc/raidtab || shellout

  # turn dem pesky raid devices off!
  for RAID_DEVICE in ${RAID_DEVICES}
  do
    DEV="/dev/${RAID_DEVICE}"
    # we don't do a shellout here because, well I forgot why, but we don't.
    echo "raidstop ${DEV}" && raidstop ${DEV}
  done
fi
#
################################################################################

### BEGIN partition /dev/sda ###
echo "Partitioning /dev/sda..."
echo "Old partition table for /dev/sda:"
parted -s -- /dev/sda print

# Create disk label.  This ensures that all remnants of the old label, whatever
# type it was, are removed and that we're starting with a clean label.
echo "parted -s -- /dev/sda mklabel msdos || shellout"
parted -s -- /dev/sda mklabel msdos || shellout

# Get the size of the destination disk so that we can make the partitions fit properly.
DISK_SIZE=`parted -s /dev/sda print | grep 'Disk geometry for' | sed 's/^.*-//g' | sed 's/\..*$//' `
[ -z $DISK_SIZE ] && shellout
if [ "$ARCH" = "alpha" ]; then
    END_OF_LAST_PRIMARY=1
else
    END_OF_LAST_PRIMARY=0
fi


echo "Creating partition /dev/sda1."
START_MB=$END_OF_LAST_PRIMARY
END_MB=$(echo "scale=3; ($START_MB + 31)" | bc)
echo "parted -s -- /dev/sda mkpart primary ext2 $START_MB $END_MB || shellout"
parted -s -- /dev/sda mkpart primary ext2 $START_MB $END_MB || shellout
END_OF_LAST_PRIMARY=$END_MB

echo "Creating partition /dev/sda2."
START_MB=$END_OF_LAST_PRIMARY
END_MB=$(echo "scale=3; ($START_MB + 30443)" | bc)
echo "parted -s -- /dev/sda mkpart primary ext2 $START_MB $END_MB || shellout"
parted -s -- /dev/sda mkpart primary ext2 $START_MB $END_MB || shellout
END_OF_LAST_PRIMARY=$END_MB

echo "Creating partition /dev/sda3."
START_MB=$END_OF_LAST_PRIMARY
END_MB=$(echo "scale=3; ($START_MB + 102)" | bc)
echo "parted -s -- /dev/sda mkpart primary ext2 $START_MB $END_MB || shellout"
parted -s -- /dev/sda mkpart primary ext2 $START_MB $END_MB || shellout
END_OF_LAST_PRIMARY=$END_MB
echo parted -s -- /dev/sda set 3 boot on || shellout
parted -s -- /dev/sda set 3 boot on || shellout

echo "Creating partition /dev/sda4."
START_MB=$END_OF_LAST_PRIMARY
END_MB=$(( $DISK_SIZE - 0 ))
echo "parted -s -- /dev/sda mkpart extended $START_MB $END_MB || shellout"
parted -s -- /dev/sda mkpart extended $START_MB $END_MB || shellout
END_OF_LAST_PRIMARY=$END_MB
END_OF_LAST_LOGICAL=$START_MB
echo parted -s -- /dev/sda set 4 lba on || shellout
parted -s -- /dev/sda set 4 lba on || shellout

echo "Creating partition /dev/sda5."
START_MB=$END_OF_LAST_LOGICAL
END_MB=$(echo "scale=3; ($START_MB + 25369)" | bc)
echo "parted -s -- /dev/sda mkpart logical ext2 $START_MB $END_MB || shellout"
parted -s -- /dev/sda mkpart logical ext2 $START_MB $END_MB || shellout
END_OF_LAST_LOGICAL=$END_MB

echo "Creating partition /dev/sda6."
START_MB=$END_OF_LAST_LOGICAL
END_MB=$(echo "scale=3; ($START_MB + 509)" | bc)
echo "parted -s -- /dev/sda mkpart logical ext2 $START_MB $END_MB || shellout"
parted -s -- /dev/sda mkpart logical ext2 $START_MB $END_MB || shellout
END_OF_LAST_LOGICAL=$END_MB

echo "Creating partition /dev/sda7."
START_MB=$END_OF_LAST_LOGICAL
END_MB=$(echo "scale=3; ($START_MB + 509)" | bc)
echo "parted -s -- /dev/sda mkpart logical ext2 $START_MB $END_MB || shellout"
parted -s -- /dev/sda mkpart logical ext2 $START_MB $END_MB || shellout
END_OF_LAST_LOGICAL=$END_MB

echo "Creating partition /dev/sda8."
START_MB=$END_OF_LAST_LOGICAL
END_MB=$(echo "scale=3; ($START_MB + 2048)" | bc)
echo "parted -s -- /dev/sda mkpart logical ext2 $START_MB $END_MB || shellout"
parted -s -- /dev/sda mkpart logical ext2 $START_MB $END_MB || shellout
END_OF_LAST_LOGICAL=$END_MB

echo "Creating partition /dev/sda9."
START_MB=$END_OF_LAST_LOGICAL
END_MB=$(echo "scale=3; ($START_MB + 15068)" | bc)
echo "parted -s -- /dev/sda mkpart logical ext2 $START_MB $END_MB || shellout"
parted -s -- /dev/sda mkpart logical ext2 $START_MB $END_MB || shellout
END_OF_LAST_LOGICAL=$END_MB

echo "New partition table for /dev/sda:"
echo "parted -s -- /dev/sda print"
parted -s -- /dev/sda print
### END partition /dev/sda ###



echo "Load additional filesystem drivers."
modprobe reiserfs
modprobe ext2
modprobe ext3
modprobe jfs
modprobe xfs

### BEGIN swap and filesystem creation commands ###
echo "mke2fs -j /dev/sda9 || shellout"
mke2fs -j /dev/sda9 || shellout
echo "tune2fs -L / /dev/sda9"
tune2fs -L / /dev/sda9
echo "mkdir -p /a/ || shellout"
mkdir -p /a/ || shellout
echo "mount /dev/sda9 /a/ -t ext3 -o defaults || shellout"
mount /dev/sda9 /a/ -t ext3 -o defaults || shellout

echo "mke2fs -j /dev/sda3 || shellout"
mke2fs -j /dev/sda3 || shellout
echo "tune2fs -L /boot /dev/sda3"
tune2fs -L /boot /dev/sda3
echo "mkdir -p /a/boot || shellout"
mkdir -p /a/boot || shellout
echo "mount /dev/sda3 /a/boot -t ext3 -o defaults || shellout"
mount /dev/sda3 /a/boot -t ext3 -o defaults || shellout

echo "mke2fs -j /dev/sda6 || shellout"
mke2fs -j /dev/sda6 || shellout
echo "tune2fs -L /tmp /dev/sda6"
tune2fs -L /tmp /dev/sda6
echo "mkdir -p /a/tmp || shellout"
mkdir -p /a/tmp || shellout
echo "mount /dev/sda6 /a/tmp -t ext3 -o defaults || shellout"
mount /dev/sda6 /a/tmp -t ext3 -o defaults || shellout

echo "mke2fs -j /dev/sda5 || shellout"
mke2fs -j /dev/sda5 || shellout
echo "tune2fs -L /usr /dev/sda5"
tune2fs -L /usr /dev/sda5
echo "mkdir -p /a/usr || shellout"
mkdir -p /a/usr || shellout
echo "mount /dev/sda5 /a/usr -t ext3 -o defaults || shellout"
mount /dev/sda5 /a/usr -t ext3 -o defaults || shellout

echo "mke2fs -j /dev/sda7 || shellout"
mke2fs -j /dev/sda7 || shellout
echo "tune2fs -L /var /dev/sda7"
tune2fs -L /var /dev/sda7
echo "mkdir -p /a/var || shellout"
mkdir -p /a/var || shellout
echo "mount /dev/sda7 /a/var -t ext3 -o defaults || shellout"
mount /dev/sda7 /a/var -t ext3 -o defaults || shellout

echo "mkswap -v1 /dev/sda8 || shellout"
mkswap -v1 /dev/sda8 || shellout
echo "swapon /dev/sda8 || shellout"
swapon /dev/sda8 || shellout

echo "mke2fs -j automount(pid2557) || shellout"
#mke2fs -j automount(pid2557) || shellout
echo "tune2fs -L /home-25552 automount(pid2557)"
#tune2fs -L /home-25552 automount(pid2557)
echo "mkdir -p /a/home || shellout"
#mkdir -p /a/home || shellout
echo "mount automount(pid2557) /a/home -t ext3 -o defaults || shellout"
#mount automount(pid2557) /a/home -t ext3 -o defaults || shellout

### END swap and filesystem creation commands ###

### BEGIN mount proc in image for tools like System Configurator ###
echo "mkdir -p /a/proc || shellout"
mkdir -p /a/proc || shellout
echo "mount proc /a/proc -t proc -o defaults || shellout"
mount proc /a/proc -t proc -o defaults || shellout
### END mount proc in image for tools like System Configurator ###

################################################################################
#
#   Lay the image down on the freshly formatted disk(s)
#
if [ ! -z $FLAMETHROWER_DIRECTORY_PORTBASE ]; then 

    # Use multicast 
    MODULE_NAME="${IMAGENAME}"
    DIR=/a
    RETRY=7
    flamethrower_client
else 
    # Use rsync 
    if [ $NO_LISTING ]; then
        echo "Quietly installing image... "
        start_spinner
    fi
    if [ "${TMPFS_STAGING}" = "yes" ]; then 

        # Deposit image into tmpfs
        DIR=/tmp/tmpfs_staging
        echo
        echo "TMPFS_STAGING=${TMPFS_STAGING} -- Staging in ${DIR}"
        mkdir -p ${DIR}

        echo "rsync -aHS${VERBOSE_OPT} --exclude=lost+found/ --numeric-ids ${IMAGESERVER}::${IMAGENAME}/ ${DIR}/" 
        rsync -aHS${VERBOSE_OPT} --exclude=lost+found/ --numeric-ids ${IMAGESERVER}::${IMAGENAME}/ ${DIR}/ || shellout 

        # Move from staging in tmpfs to disk
        rsync -aHS${VERBOSE_OPT} --exclude=lost+found/ --numeric-ids ${DIR}/ /a/
    else
        echo "rsync -aHS${VERBOSE_OPT} --exclude=lost+found/ --numeric-ids ${IMAGESERVER}::${IMAGENAME}/ /a/" 
        rsync -aHS${VERBOSE_OPT} --exclude=lost+found/ --numeric-ids ${IMAGESERVER}::${IMAGENAME}/ /a/ || shellout 
    fi
fi 

beep

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


if [ $NO_LISTING ]; then
    stop_spinner
fi

# Leave notice of which image is installed on the client
echo $IMAGENAME > /a/etc/systemimager/IMAGE_LAST_SYNCED_TO || shellout


### BEGIN generate new fstab file from autoinstallscript.conf ###
cat <<'EOF' > /a/etc/fstab
LABEL=/	/	ext3	defaults	1	1
LABEL=/boot	/boot	ext3	defaults	1	2
none	/dev/pts	devpts	gid=5,mode=620	0	0
none	/proc	proc	defaults	0	0
none	/dev/shm	tmpfs	defaults	0	0
LABEL=/tmp	/tmp	ext3	defaults	1	2
LABEL=/usr	/usr	ext3	defaults	1	2
LABEL=/var	/var	ext3	defaults	1	2
/dev/sda8	swap	swap	defaults	0	0
LABEL=/home-25552	/home	ext3	defaults	1	2
/dev/cdrom	/mnt/cdrom	udf,iso9660	noauto,owner,kudzu,ro	0	0
/dev/fd0	/mnt/floppy	auto	noauto,owner,kudzu	0	0
EOF
### END generate new fstab file from autoinstallscript.conf ###

################################################################################
#
#   Process override directories
#
for OVERRIDE in $OVERRIDES
do
    if [ ! -z $FLAMETHROWER_DIRECTORY_PORTBASE ]; then
        # Use multicast
        MODULE_NAME="override_${OVERRIDE}"
        DIR=/a
        RETRY=7
        flamethrower_client
    else
        # Use rsync
        echo "rsync -av --numeric-ids $IMAGESERVER::overrides/$OVERRIDE/ /a/"
        rsync -av --numeric-ids $IMAGESERVER::overrides/$OVERRIDE/ /a/ || echo "Override directory $OVERRIDE doesn't seem to exist, but that may be OK."
    fi
done

beep

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


##################################################################
#
# Uncomment the line below to leave your hostname blank.
# Certain distributions use this as an indication to take on the
# hostname provided by a DHCP server.  The default is to have
# SystemConfigurator assign your clients the hostname that
# corresponds to the IP address the use during the install.
# (If you used to use the static_dhcp option, this is your man.)
#
#HOSTNAME=""


################################################################################
#
# mount /dev /a/dev -o bind if needed
#
#not needed for this image
#
################################################################################


################################################################################
#
#   System Configurator
#
# Configure the client's hardware, network interface, and boot loader.
#
chroot /a/ systemconfigurator --excludesto=/etc/systemimager/systemconfig.local.exclude --configsi --stdin << EOL || shellout
[NETWORK]
HOSTNAME = $HOSTNAME
DOMAINNAME = $DOMAINNAME

[INTERFACE0]
DEVICE = eth0
TYPE = dhcp
EOL
#
################################################################################


################################################################################
#
#   Unmount filesystems
#
echo "umount /a/var || shellout"
umount /a/var || shellout

echo "umount /a/usr || shellout"
umount /a/usr || shellout

echo "umount /a/tmp || shellout"
umount /a/tmp || shellout

echo "umount /a/proc || shellout"
umount /a/proc || shellout

echo "umount /a/home || shellout"
umount /a/home || shellout

echo "umount /a/boot || shellout"
umount /a/boot || shellout

echo "umount /a/ || shellout"
umount /a/ || shellout

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


################################################################################
#
#   Tell the image server we're done
#   
rsync $IMAGESERVER::scripts/imaging_complete > /dev/null 2>&1

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

# Take network interface down
ifconfig eth0 down || shellout

# Announce completion (even for non beep-incessantly --post-install options)
beep 3

beep_incessantly
