#!/bin/bash
# PXE BOOT SLAX FROM AN HTTP SERVER
# this is a script I created to automate the procedure of creating an initrd image for me
# this script was written by Jeff Sadowski
#
MYPWD=`pwd`
ipaddr=`ifconfig eth0|grep "inet addr"|cut -d":" -f2|cut -d" " -f1`
output=/tmp/prep
mkdir -p $output
mount -t tmpfs "" $output
#copy slax's initrd while uncompressing it
gunzip -c /mnt/live/mnt/*/boot/initrd.gz >$output/initrd
#create a place to mount slax's initrd
mkdir -p /mnt/initrd
#mount the initrd
mount $output/initrd /mnt/initrd/ -o loop
#create a temporary folder to work out of
TMP=/tmp/workinginitrd.`date +%Y-%m-%d.%H:%M`
mkdir -p $TMP
#copy all of the stuff from the old initrd into the working directory
cp -ra /mnt/initrd/* $TMP
umount /mnt/initrd
#copy all network modules
cp -r $BASE/$netmods $TMP/$modpath/kernel/drivers
#copy the fuse modules
cp -r $BASE/$modpath/kernel/fs/fuse $TMP/$modpath/kernel/fs
#copy the dependency file so I can load network modules and the fuse module I copied
cp $BASE/$modpath/modules.dep $TMP/$modpath/modules.dep
#copy alias file so I can lookup network cards
cp $BASE/$modpath/modules.alias $TMP/$modpath/modules.alias
httpfs \$httplocation /mnt/httpfs
mount -o loop,ro /mnt/httpfs/\`basename \$httplocation\` /mnt/vcdrom
DATA=/mnt/vcdrom/slax
fi
END
#find the place I want to patch
LINE=`grep -n "\\"\\$DATA\\" = \\"\\"" $TMP/linuxrc|head -n 1|cut -d: -f1`
let LINE=$LINE-1
head -n $LINE $TMP/linuxrc > linuxrc.head
TOTAL=`wc -l $TMP/linuxrc|cut -d " " -f1`
let REMANDER=$TOTAL-$LINE
tail -n $REMANDER $TMP/linuxrc > linuxrc.tail
#create patched linuxrc for my pxe boot implementation
cat linuxrc.head patch linuxrc.tail > $TMP/linuxrc
#find out how big I need to make the initrd image's filesystem
SIZE=`du --max-depth=0 $TMP|cut -f1`
#allow extra size for filesystem to sit on
let SIZE=$SIZE+1000
#create a blank file of needed size
dd if=/dev/zero of=$output/pxe_initrd bs=1024 count=$SIZE
#format the file
mkfs.ext2 -F $output/pxe_initrd
mount $output/pxe_initrd /mnt/initrd/ -o loop
cp -ra $TMP/* /mnt/initrd
umount /mnt/initrd
gzip $output/pxe_initrd
cp /boot/vmlinuz $output