RSS Git Download  Clone
Raw Blame History
#!/bin/bash
################################################################################
#	Mknfsinitrd		Creates an initial ramdisk image for NFS boot
#				T.Barnaby,	BEAM Ltd,	14/2/04
#				Some functions taken from /sbin/mkinitrd
################################################################################
#
busyboxProgs="[ ash awk basename bunzip2 bzcat cat chgrp chmod\
        chown chroot chvt clear cmp cp cut date dd deallocvt\
        df dirname dmesg du echo egrep env expr false fgrep\
        find free grep gunzip gzip halt head hexdump hostid hostname\
        id ifconfig init insmod install ipaddr kill killall klogd\
        linuxrc ln logger ls lsmod mkdir mknod mkswap mktemp\
        modprobe more mount mv openvt pidof ping pivot_root poweroff\
        ps pwd readlink reboot reset rm rmdir rmmod route sed\
        sh sleep sort strings swapoff swapon sync syslogd tail\
        tar tee test time touch tr true tty udhcpc umount uname uniq\
        unzip uptime usleep vi wc wget which whoami xargs yes\
        zcat stat switch_root"

baseDir=""
kernelVersion=""
verbose="-v"
target=""
builtins=""

##############################################################################
#	Script files
##############################################################################
#

fileInit(){
cat <<"EOF"
#!/bin/sh
################################################################################
#	Init.rc		Diskless NFS boot init script
#			T.Barnaby,	BEAM Ltd,	16/2/05
################################################################################
#

modules="e1000 e1000e igb nfsv3 nfsv4"

echo "BEAM Diskless boot starting ..."

#mount -n -o remount,rw /
mount -n -t proc /proc /proc
mount -n -t sysfs none /sys
mount -n -t tmpfs /dev/shm /tmp

#/bin/sh

echo "Loading modules"
for m in $modules; do
	modprobe $m
done

#/bin/sh

echo "Seting up network interface"
while true; do
	if udhcpc -s /dhcpConfig -f -n -q -O 17 -i eth0; then break; fi
	if udhcpc -s /dhcpConfig -f -n -q -O 17 -i eth1; then break; fi
done
. /tmp/dhcpInfo

if [ -n "$dhcp_hostname" ]; then
	hostname $dhcp_hostname
fi

#/bin/sh

echo "Mounting root filesystem"
mount -n -t rpc_pipefs sunrpc /var/lib/nfs/rpc_pipefs
echo "Hostname: $dhcp_hostname"
echo "Rootpath: $dhcp_rootpath"
mount -n -r -t nfs  -o async,nolock $dhcp_rootpath /sysroot

# Debug
#mount -n -r -t nfs  -o async,nolock 192.168.202.1:/home/terry/cern/rootfs /sysroot
#mount -n -r -t nfs  -o async,nolock 192.168.201.1:/usr/tms/rootfs-1 /sysroot
#/bin/sh

echo "Switching to new root"
echo 0x100 > /proc/sys/kernel/real-root-dev
umount /var/lib/nfs/rpc_pipefs
umount /sys
umount /proc
umount /tmp

#/bin/sh

echo "Doing switch_root"
exec switch_root /sysroot /sbin/init

EOF
}

fileDhcpConfig(){
cat <<"EOF"
#!/bin/sh
################################################################################
#	dhcpConfig	Sets up network interface
#			T.Barnaby,	BEAM Ltd,	16/2/05
################################################################################
#

configureNetwork(){
	ifconfig $interface $ip
	if [ -n "$router" ]; then
		route add default gw $router
	fi
	
	echo "dhcp_interface=$interface" > /tmp/dhcpInfo
	echo "dhcp_ip=$ip" >> /tmp/dhcpInfo
	echo "dhcp_mask=$mask" >> /tmp/dhcpInfo
	echo "dhcp_subnet=$subnet" >> /tmp/dhcpInfo
	echo "dhcp_broadcast=$broadcast" >> /tmp/dhcpInfo
	echo "dhcp_router=$router" >> /tmp/dhcpInfo
	echo "dhcp_dns=$dns" >> /tmp/dhcpInfo
	echo "dhcp_namesvr=$namesvr" >> /tmp/dhcpInfo
	echo "dhcp_hostname=$hostname" >> /tmp/dhcpInfo
	echo "dhcp_domain=$domain" >> /tmp/dhcpInfo
	echo "dhcp_siaddr=$siaddr" >> /tmp/dhcpInfo
	echo "dhcp_rootpath=$rootpath" >> /tmp/dhcpInfo
	echo "dhcp_timesvr=$timesvr" >> /tmp/dhcpInfo
	echo "dhcp_ntpsrv=$ntpsrv" >> /tmp/dhcpInfo
	echo "dhcp_tftp=$tftp" >> /tmp/dhcpInfo
	echo "dhcp_timezone=$timezone" >> /tmp/dhcpInfo
	echo "dhcp_logsvr=$logsvr" >> /tmp/dhcpInfo
	echo "dhcp_swapsvr=$swapsvr" >> /tmp/dhcpInfo
}

case $1 in
	deconfig)	ifconfig $interface 0.0.0.0;;
	bound)		configureNetwork;;
esac

exit 0
EOF
}

##############################################################################
#	Functions
##############################################################################
#

inst() {
    if [ "$#" != "2" ];then
        echo "usage: inst <file> <destination>"
        return
    fi 
    [ -n "$verbose" ] && echo "cp $1 -> $2"
    cp $1 $2
}

usage () {
    echo "usage: `basename $0` [-v][-d baseDir] <initrd-image> <kernel-version>" >&2
    echo "" >&2
    echo "       (ex: `basename $0` initrd-2.6.10-1.760_FC3 2.6.10-1.760_FC3)" >&2
    exit 1
}

##############################################################################
#	Main code
##############################################################################
#

while [ $# -gt 0 ]; do
    case $1 in
	-v)
	    verbose=-v
	    ;;
	-d)
	    shift
	    baseDir=$1
	    ;;
	*)
	    if [ -z "$target" ]; then
		target=$1
	    elif [ -z "$kernelVersion" ]; then
		kernelVersion=$1
	    else
		usage
	    fi
	    ;;
    esac

    shift
done

echo "BaseDir: $baseDir";

if [ -z "$target" -o -z "$kernelVersion" ]; then
    usage
fi

if [ ! -d $baseDir/lib/modules/$kernelVersion ]; then
    echo "$baseDir/lib/modules/$kernelVersion is not a directory." >&2
    exit 1
fi

if false; then
if [ $UID != 0 ]; then
    echo "mknfsinitrd must be run as root"
    exit 1
fi
fi

# find a temporary directory which doesn't use tmpfs
TMPDIR=""
for t in /tmp /var/tmp /root ${PWD}; do
    if [ ! -d $t ]; then continue; fi
    if ! echo access -w $t; then continue; fi

    fs=$(df -T $t 2>/dev/null | awk '{line=$1;} END {printf $2;}')
    if [ "$fs" != "tmpfs" ]; then 
	TMPDIR=$t
	break
    fi
done

if [ -z "$TMPDIR" ]; then
    echo "no temporary directory could be found" >&2
    exit 1
fi

if [ $TMPDIR = "/root" -o $TMPDIR = "${PWD}" ]; then 
    echo "WARNING: using $TMPDIR for temporary files" >&2
fi


#echo "TMPDIR: $TMPDIR"
#exit 0

##############################################################################
#	Main wor
##############################################################################
#
MNTIMAGE=`mktemp -d ${TMPDIR}/initrd.XXXXXX`
IMAGE=`mktemp ${TMPDIR}/initrd.img.XXXXXX`

#MNTIMAGE=/tmp/initrd

if [ -z "$MNTIMAGE" -o -z "$IMAGE" ]; then
    echo "Error creating temporaries.  Try again" >&2
    exit 1
fi

echo "MNTIMAGE: $MNTIMAGE"

mkdir -p $MNTIMAGE
mkdir -p $MNTIMAGE/lib
mkdir -p $MNTIMAGE/lib64
mkdir -p $MNTIMAGE/bin
mkdir -p $MNTIMAGE/etc
mkdir -p $MNTIMAGE/dev
mkdir -p $MNTIMAGE/proc
mkdir -p $MNTIMAGE/sys
mkdir -p $MNTIMAGE/sysroot
mkdir -p $MNTIMAGE/usr
mkdir -p $MNTIMAGE/usr/bin
mkdir -p $MNTIMAGE/usr/sbin
mkdir -p $MNTIMAGE/var
mkdir -p $MNTIMAGE/var/lib/nfs/rpc_pipefs
mkdir -p $MNTIMAGE/tmp
rm -f $MNTIMAGE/sbin
ln -s bin $MNTIMAGE/sbin

# Install needed programs
#inst /sbin/nash "$MNTIMAGE/bin/nash"
#inst /sbin/insmod.static "$MNTIMAGE/bin/insmod"
#ln -s /sbin/nash $MNTIMAGE/sbin/modprobe
#inst /bin/bash $MNTIMAGE/bin
#ln -s bash $MNTIMAGE/bin/sh

# Test progs
if false; then
# Install libs
inst /lib/libtermcap.so.2 $MNTIMAGE/lib
inst /lib/libdl.so.2 $MNTIMAGE/lib
inst /lib/libc.so.6 $MNTIMAGE/lib
inst /lib/ld-linux.so.2 $MNTIMAGE/lib

#inst /sbin/ifconfig $MNTIMAGE/bin/ifconfig1
#inst /bin/dmesg $MNTIMAGE/bin
inst /usr/bin/strace $MNTIMAGE/bin
#inst /sbin/lsmod $MNTIMAGE/bin
fi

# Install busybox
inst /src/cern/tms-mcsys/rootfs/bin/busybox $MNTIMAGE/bin/busybox
for p in $busyboxProgs; do
	ln -sf busybox $MNTIMAGE/bin/$p
done
if false; then
	inst /lib64/linux-vdso.so.1 $MNTIMAGE/lib64
	inst /lib64/libm.so.6 $MNTIMAGE/lib64
	inst /lib64/libc.so.6 $MNTIMAGE/lib64
	inst /lib64/ld-linux-x86-64.so.2 $MNTIMAGE/lib64
fi
if true; then
	#inst /lib/linux-gate.so.1 $MNTIMAGE/lib
	inst /lib/libm.so.6 $MNTIMAGE/lib
	inst /lib/libc.so.6 $MNTIMAGE/lib
	inst /lib/ld-linux.so.2 $MNTIMAGE/lib
fi

# Copy modules
mkdir -p $MNTIMAGE/lib/modules
cp -a $baseDir/lib/modules/$kernelVersion $MNTIMAGE/lib/modules

#touch $MNTIMAGE/etc/fstab

# Setup udev
#inst /sbin/udev.static $MNTIMAGE/sbin/udev
#ln -s udev $MNTIMAGE/sbin/udevstart
#mkdir -p $MNTIMAGE/etc/udev
#inst /etc/udev/udev.conf $MNTIMAGE/etc/udev/udev.conf
#ln -s /sbin/nash $MNTIMAGE/sbin/hotplug

# Create rc script filea
fileInit > $MNTIMAGE/init
fileDhcpConfig > $MNTIMAGE/dhcpConfig
chmod +x $MNTIMAGE/init
chmod +x $MNTIMAGE/dhcpConfig

(cd $MNTIMAGE; find . | cpio --quiet -c -o) > $IMAGE
gzip -9 < $IMAGE > $target || rc=1

rm -rf $MNTIMAGE $IMAGE

exit 0