#!/bin/sh
# Description: main script for new type of initial ramdisk for
# linux diskless clients version 4
#
# Author(s): Dirk von Suchodoletz <dirk@goe.net>, 12-04-2006
#
# Copyright: (c) 2006 - RZ Universitaet Freiburg
#
# Version: 0.2.4e
# functions common for all distros, messages contains all error and info
# output
. /etc/messages
. /etc/functions || ( echo -e $init_mff && exit 1 )
. /etc/distro-functions || ( echo -e $init_dff && exit 1 )
# configuration settings (several file and directory variables)
. /etc/sysconfig/config || ( echo -e $init_dscf && exit 1 )
export PATH=/bin:/sbin:/usr/bin/:/usr/sbin
export date="@@@DATE@@@"
export DEBUGLEVEL=0
export KERNEL="@@@KERNVER@@@"
export NWMODULES="@@@NWMODULES@@@"
export DISTRO="@@@DISTRO@@@"
# do not use dnbd cache file
nodnbdcache="yes"
DEVDIR="/dev"
mount -n -t tmpfs -o 'size=25%,mode=0755' initramfsdevs ${DEVDIR}
cp -a /tmp/null /tmp/console /dev
mkdir -p ${DEVDIR}/pts
mkdir -p ${DEVDIR}/shm
mkdir -p ${DEVDIR}/.udevdb
[ ! -f /proc/cpuinfo ] && mount -n -t proc proc /proc
echo 256 > /proc/sys/kernel/real-root-dev
[ ! -d /sys/class ] && mount -n -t sysfs sysfs /sys
# redirect console (after /dev/console is available, and /dev mounted)
exec < /dev/console > /dev/console 2>&1
# run pre init script
preinit
[ -x /bin/vendor_preinit ] && /bin/vendor_preinit
# start device auto discovery service - distro specific function
udev_hotplug
# set defaults and create waitfor files
TMPFSSIZE="50%"
COWSIZE="50%"
RWDIR=/dev/shm
echo "noldap" > /tmp/ldap-done
echo "nodhcp" > /tmp/dhcp-done
echo "nofile" > /tmp/file-done
# read kernel commandline
read KCMDLINE < /proc/cmdline
export KCMDLINE
for opts in ${KCMDLINE} ; do
case ${opts} in
# localization
country=*)
COUNTRY=${opts#country=}
echo -e "\n# localization information gotten via kernel command line \
in $0\ncountry=\"${COUNTRY}\"" >> /etc/machine-setup
;;
# single token for debugging
debug)
DEBUGLEVEL=1;;
# debug level
debug=*)
DEBUGLEVEL=${opts#debug=};;
# if configuration should be gathered by dhcp client
dhcp)
DHCP="yes"
rm /tmp/dhcp-done;;
# if ldap configuration should be triggered
ldap)
LDAP="yes"
rm /tmp/ldap-done;;
# ldap configuration with host and port to contact (base)
ldap*)
LDAP="yes"
rm /tmp/ldap-done;;
# if (external, via tftp) configuration file retrieval should
# be triggered, if no source is given try dhcp server and
# predefined standard path
file)
FILE="yes"
rm /tmp/file-done;;
# file source with tftp server and file location on the server
file*)
FILE="yes"
FILESRC=${opts#file=}
rm /tmp/file-done;;
# if ld.so.cache should not be generated
noldsc)
NOLDSC=yes;;
# additional source to unify root filesystem with
union=*)
UNIONFS=1
UNIONDIRS=${opts#union=};;
# if unionfs should be used
unionfs)
UNIONFS=1;;
# if cowloop should be used, only ontop of network block device and in
# combination with classical fs, like ext2 useful
cowloop*)
COWLOOP=1
#COWSIZE=${opts#cowloop=}
;;
# dnbd server:port
dnbdroot=*)
NBD=dnbd # name of kernel module
NBDOPT=${opts#dnbdroot=};;
dcsize=*)
# size of cache dnbd should use within ram
DNBDCACHESIZE=${opts#dcsize=};;
# nbd server:port,filesystem (filesystem is optional)
nbdroot=*)
NBD=nbd # name of kernel module
NBDOPT=${opts#nbdroot=};;
# ip configuration client-ip:server-ip:gateway:netmask
ip=*)
IPINFO=${opts#ip=};;
# nfs server and path
nfsroot=*)
NFSROOT=${opts#nfsroot=}
echo -e "\n# nfs root information gotten via kernel command line in $0\n\
nfsroot=\"${NFSROOT}\"\n" >> /etc/machine-setup
;;
# size of tempfs if not max. 50% of RAM should be used
tmpfssize=*)
TMPFSSIZE=${opts#tmpfssize=};;
# vendor code identifier for dhcp requests
vci=*)
VCI=${opts#vci=};;
esac
done
# load block device driver if needed
if [ -n "${NBD}" ] ; then
modprobe ${MODPRV} ${NBD} || error "$init_errnbd"
fi
# check if at least one type of IP configuration is availabe
if [ -z "$DHCP" -a -z "$LDAP" -a -z "$IPINFO" ] ; then
echo -e "# You did not specify any advanced configuration mode for your \
clients. You\nmight want to add the token 'dhcp' for DHCP configuration, \
'file(=source)'\nfor config file get via TFTP or 'ldap(=source)' for LDAP \
configuration to\nyour kernel command line." >> /etc/machine-setup
if strinfile "clientip" /etc/machine-setup ; then
# IP configuration seems to be present in machine-setup file
. /etc/machine-setup
IPINFO="ip=$clientip:$serverip:$gateway:$subnet_mask"
else
error "$init_errip"
fi
fi
# for ldap configuration at least basic IP setup is needed to contact the
# server
[ -z "$DHCP" -a -n "$LDAP" -a -z "$IPINFO" ] && error "$init_errldap"
# set debug level and logfile
echo "Setting debuglevel to ${DEBUGLEVEL}"
export MODPRV=" "
export LOGFILE
if [ "${DEBUGLEVEL}" -gt 0 ] ; then
# if LOGFILE should be used within initial ramdisk add '/mnt' in front
# of the variable
LOGFILE="/var/log/dxs-boot.log"
echo "1 4 1 7" >/proc/sys/kernel/printk
[ "${DEBUGLEVEL}" -ge 2 ] && MODPRV="-v"
[ "${DEBUGLEVEL}" -gt 3 ] && {
set -x
# and the kernel too
echo "7 7 7 7" >/proc/sys/kernel/printk; }
else
# switch off most of kernel debug output
echo "0 0 0 0" >/proc/sys/kernel/printk
# modprobe should stay quiet
MODPRV="-q"
LOGFILE="/dev/null"
fi
# load network adaptor modules
modprobe ${MODPRV} -a ${NWMODULES} || error "$init_errnwad"
# set up loopback networking (ipsetup - function defined in /etc/functions)
ipsetup 127.0.0.1 255.0.0.0 0.0.0.0 127.255.255.255 lo
# analyze ip information from the kernel command line and put parts
# of it into several variables
if [ -n "$IPINFO" ] ; then
getip () {
val=$IPINFO; i=$(expr $1 - 1);
while [ $i -gt 0 ] ; do
val=${val#*:} ; i=$(expr $i - 1);
done;
echo $val|sed "s/:.*//"; }
IP=$(getip 1)
SERVER=$(getip 2)
GW=$(getip 3)
NM=$(getip 4)
echo -e "# ip configuration written by $0 script\nclientip=$IP\n\
sub_netmask=$NM\ngateway=$GW\nserverip=$SERVER" \
>>/etc/machine-setup
# set static ip address via function ipsetup
ipsetup $IP $NM $GW 255.255.255.255 eth0
else
noipyet="yes"
fi
# get configuration data via dhcp (with vendor code identifier if present
# in ${VCI} (from kernel command line) ...
[ -n "$DHCP" ] && rundhcp ${VCI} &
if [ -n "$noipyet" ] ; then
waitfor /tmp/dhcp-done 20000
. /etc/machine-setup
[ -z "$clientip" -o -z "$subnet_mask" -o -z "$gateway" \
-o -z "$broadcast_address" ] && error "$init_errdhcp"
ipsetup $clientip $subnet_mask $gateway $broadcast_address eth0
fi
# ... or ldap if available (in background)
[ -n "$LDAP" ] && ldapconf &
# ... or via tftp file get (in background)
[ -n "$FILE" ] && fileget &
# if root filesystem should be imported via (d) network block device
if [ -n "${NBD}" ] ; then
nbdhost=${NBDOPT%:*}
nbdopt=${NBDOPT#*:}
nbdport=${nbdopt%,*}
nbdfs=${nbdopt#$nbdport*}
if [ -z "$nbdfs" ]; then
RFST=ext2;
else
RFST=${nbdfs#*,};
fi
echo "Diskless client using ${NBD} on $nbdhost:$nbdport with $RFST"
# fixme: check for fs in /proc/filesystems before trying to load the module
modprobe ${MODPRV} ${RFST} || error "$init_errnbd" nonfatal
case "${NBD}" in
# network block device present in standard kernel
nbd)
RDEV=/dev/nbd0
waitfor "${RDEV}" 30000 || error "$init_nbddev"
echo "0" > /sys/block/nbd0/queue/read_ahead_kb
nbd-client $nbdhost $nbdport ${RDEV} || error "$init_nbdcl"
# switch off unneeded block read ahead
echo "0" > /sys/block/nbd0/queue/read_ahead_kb
;;
# dnbd by Thorsten Zitterell
dnbd)
if [ -z $nodnbdcache ] ; then # variable not really used yet
mkdir /dnbd
mount -n -o 'size=10%' -t tmpfs tmpfs /dnbd
( cat /dev/zero > /dnbd/cache 2>/dev/null || echo "ok" >/tmp/cache ) &
clientopt="-c /dnbd/cache"
waitfor /tmp/cache 10000
fi
waitfor /dev/dnbd0 10000 || error "$init_nbddev"
while ! dnbd-client -b $nbdhost -d /dev/dnbd0 $clientopt ; do
usleep 10
done
echo "0" > /sys/block/dnbd0/queue/read_ahead_kb
RDEV=/dev/dnbd0
;;
esac
usleep 20
RWRO="ro"
if [ -n "${COWLOOP}" -a -x /bin/cowdev ] ; then
modprobe ${MODPRV} cowloop || {
error "$init_loadcow" nonfatal
COWLOOP=""; }
fi
if [ -n "${COWLOOP}" ] ; then
if [ -n "${UNIONFS}" ] ; then
error "$init_cownonbd"
UNIONFS="";
fi
echo "Using Copy-on-Write block device for rw access"
mount -n -t tmpfs -o size=${COWSIZE} ramfs ${RWDIR}
mkdir /dev/cow && cp -a /tmp/ctl /dev/cow
ln -s /dev/cowloop0 /dev/cow/0
cowdev -a /dev/nbd0 ${RWDIR}/nbd.cow
usleep 10
RWRO="rw"
RDEV=/dev/cow/0
# run ldconfig if not switched off via kernel command line
ldcfg
fi
# finally mount the block device
mount -n -t $RFST -o $RWRO $RDEV /mnt
fi
# if no type of root filesystem is passed via kernel command line try
# information gathered from dhcp process
if [ -z "${DNBDOPT}" -a -z "${NBDOPT}" -a -z "${NFSROOT}" ] ; then
# information has to be read from dhcp configuration
if [ -z "$noipyet" ] ; then
waitfor /tmp/dhcp-done 20000
fi
waitfor /tmp/ldap-done 20000
. /etc/machine-setup
NFSROOT=${serverip}:${root_path}
fi
# call function for nfs mounts
if [ -z "${DNBDOPT}" -a -z "${NBDOPT}" ] ; then
nfsmnt ${NFSROOT} || error "$init_nfs"
fi
# get the complete collection of kernel modules available
mount -n --bind /mnt/lib/modules/${KERNEL} /lib/modules/${KERNEL} || \
error "$init_moddir"
# start hardware configuration as background process
hwautocfg &
# try to use unionfs for rw access if available
if [ -n "${UNIONFS}" -o -n "${UNION}" ] ; then
modprobe ${MODPRV} unionfs || {
error "$init_loadufs" nonfatal
UNIONFS=""; }
fi
# setup of client root filesystem dependent on the availability of UnionFS
if [ -n "${UNIONFS}" ] ; then
echo "Using UnionFS for rw access"
[ -n ${NFSROOT} ] && NFSRO=nfs
mkdir -p ${RWDIR}/union ${RWDIR}/uniontmp
mount -n -t tmpfs none ${RWDIR}/uniontmp
mount -n -t unionfs -o dirs=${RWDIR}/uniontmp=rw:/mnt=${NFSRO}ro \
none /mnt
mkdir -p /mnt/uniontmp
mount -n --move ${RWDIR}/uniontmp /mnt/uniontmp
# if additional sources should be used for a combined root filesystem
# probably more sources should be merged into union (${UNION} is defined)
if [ -n "${UNION}" ] ; then
# unionctl
:
fi
# run ldconfig if not switched off via kernel command line
ldcfg
elif [ -z "$COWLOOP" ] ; then
echo "Using bind mounts to ramdisk for rw access"
mount -n -t tmpfs -o size=${TMPFSSIZE} ramfs ${RWDIR}
for path in ${D_BINDMPTS} ; do
mkdir -p ${RWDIR}/${path} >/dev/null 2>&1
mount -n --bind ${RWDIR}/${path} /mnt/${path}
done
# see above ...
ldcfg
for path in ${D_RODIRSINRW}; do
if [ -d /mnt/${path} ] ; then
LIST=${path}" "$LIST
# exclude them from etc copy process too
echo ${path}|sed -e "s,/root/,," >>/tmp/etc.exclude
mkdir -p /root/${path} >/dev/null 2>&1
#[ ${DEBUGLEVEL} -gt 0 ] && | #echo " ${path} (without /root path component)" >>$LOGFILE
mount -n --bind /mnt/${path} /root/${path} >/dev/null 2>&1
fi
done
for path in /etc/${D_SYSCONFDIR} ${D_DIRINBINDMNT} ${LIST}; do
mkdir -p ${RWDIR}/${path}
done
testmkd ${RWDIR}/var/tmp
chmod a+rwxt ${RWDIR}/var/tmp >/dev/null 2>&1
echo -e "${D_ETCEXCL} @@@COMETCEXCL@@@" >>/tmp/etc.exclude
# if ld.so.cache is to be generated then do not copy the file
[ -z "${NOLDSC}" ] && echo -e "ld.so.cache*" >>/tmp/etc.exclude
# for tar exclude lists might be used, more difficult for cp
cd /mnt
tar -X /tmp/etc.exclude -cp etc/* | \
tar -xp -C ${RWDIR} 2>/dev/null ;
cd /
mount -n --bind ${RWDIR}/etc /mnt/etc
mount -n --bind ${RWDIR}/var /mnt/var
# get the "covered" mounts back into filesystem structure
for i in ${LIST}; do
mount -n --move /root/$i /mnt/$i #>>$LOGFILE 2>&1
done
#rm -rf /root/* >/dev/null 2>&1
# run some specific stuff !?
fi
# script for stuff to execute during early bootup
d_mkrlscript init boot.ld "Running configuration postponed from InitRD"
echo "fs complete" >/tmp/fscmpl
# write debug file information after filesystem setup completed
echo -e "# /etc${D_SYSCONFDIR}/logfile - file created by $0 (initramfs \
from $date)\n#\n# logfile \
for linux diskless client specific debugging output\nLOGFILE=\"$LOGFILE\"\n#\
\n# debug level\nDEBUGLEVEL=\"$DEBUGLEVEL\"" \
> /mnt/etc/${D_SYSCONFDIR}/logfile || error "$init_errlog"
# run distribution independent and dependent configuration of files and
# services
servconfig &
for path in @@@COMDIRINDXS@@@ ${D_DIRINDXS} ${D_DIRINBINDMNT} ; do
testmkd /mnt/${path}
done
for i in /var/run/utmp /var/log/wtmp /var/log/lastlog /etc/mtab ; do
echo -n > /mnt/$i
done
chmod a+rwxt /mnt/tmp /mnt/tmp/scratch 2>/dev/null &
chown 65534:0 /mnt/var/lib/nobody &
echo -e "# /etc/fstab - file generated by $0 (initramfs from $date)\n#\
\tDirk von \
Suchodoletz, dirk@goe.net\n\nrootfs\t\t/\t\trootfs\t\tro\t\t 0 0\n\
proc\t\t/proc\t\tproc\t\tdefaults\t 0 0\ninitramdevs\t/dev\t\ttmpfs\
\t\trw\t\t 0 0\ndevpts\t\t/dev/pts\tdevpts\t\tmode=0620,gid=5\t 0 0\n\
usbfs\t\t/proc/bus/usb\tusbfs\t\tnoauto\t\t 0 0" >>/mnt/etc/fstab || \
error "$init_fstab"
echo -e "rootfs / rootfs rw 0 0" > /mnt/etc/mtab
# copy library cache if generated
if [ -z "${NOLDSC}" ] ; then
if waitfor /tmp/ldcfg 50000 ; then
test -s /mnt/tmp/ld.so.cache && {
cp /mnt/tmp/ld.so.cache /mnt/etc/ld.so.cache
rm /mnt/tmp/ld.so.cache; }
else
error "$init_errldcfg"
fi
else
error "$init_infldcfg" nonfatal
fi
# copy machine configuration (from global settings, additions made within
# here and from dhcp/ldap sources) to client /etc for later checks
cp /etc/machine-setup /mnt/etc
waitfor /tmp/hwcfg 20000 || error "$init_errhw"
waitfor /tmp/svcfg 20000 || error "$init_errsw"
# IP configuration is made and should not be updated automatically, udevd
# should be killed if started within init
killall -9 dhcpcd dhclient pump 2>/dev/null
# post init for some distro specific
postinit
[ -x /bin/vendor_postinit ] && /bin/vendor_postinit
# start a debug shell in higher debug levels
[ $DEBUGLEVEL -gt 2 ] && /bin/sh
# unmount the bind mounted modules directory
for i in 0 40 100 200 300 500 800 1000 1200; do
usleep $i && umount -n /lib/modules/${KERNEL} 2>/dev/null && break
error "$init_wait" nonfatal
done
[ $i -gt 1000 ] && error "$init_errumnt"
# check for inittab file
test -f /mnt/etc/inittab || error "$init_erritab"
# close runlevel script for stuff to execute during early bootup
d_mkrlscript close boot.ld ""
# preparations to leave initrd - umounting ...
umount -n /sys || error "$init_errsys" nonfatal
umount -n /proc/bus/usb >/dev/null 2>&1
mount -n --move /dev /mnt/dev
killall -9 udevd 2>/dev/null
umount -n /proc
# unset old environment variables
unset debug date initrd ip dnbdroot nbdroot nfsroot vci vga
unset BOOT_IMAGE KCMDLINE KERNEL MODPRV NWMODULES OLDPWD UDEVD_EVENT_TIMEOUT \
UDEVD_EXPECTED_SEQNUM
#strinstr "bash" "$(ls -la /bin/sh)" && EE="-c"
# new style of pivoting (exec -c would set an empty environment in bash)
exec run-init -c dev/console /mnt /sbin/init || error "$init_runinit"