H. Peter Anvin wrote:> Please be aware that I HAVE NOT TESTED THIS PROGRAM... please try it in
> a "safe" environment since for all I know it could nuke your
"real root"
> by mistake.
A fixed and tested version of the run-init program is attached. To see
its real use example, put the attached "build.sh" script in the same
directory as udev-026.tar.bz2 and
klibc-0.129-with-fixed-run-init.tar.bz2 and run the build.sh script as
root (root is needed because of mknod commands). The result is a copy of
my working initramfs.
The check for not wiping out the real root filesystem is based on the
presence of the /init file. Rationale: it is impossible to distinguish
between initramfs and a chrooted environment based on the result of the
statfs call.
KNOWN BUG: my previous /init script left really nothing in initramfs.
Now /root is left there for some reason.
My initramfs does the following:
1) Disables hotplug in the kernel. It has been determined that the
assumption "userspace is always ready to handle hotplug events" is
inherently unsafe.
2) Interprets root=..., rootfstype=..., rootflags=..., init=..., ro and
rw kernel command line options
3) Mounts the root filesystem (no NFS yet, sorry)
4) Mounts a ramfs on top of the real root's /dev directory (like in
"mount devfs on boot") and creates /dev/null and /dev/console there
5) Wipes out initramfs contents and runs init from the root filesystem
Assumptions made upon initscripts (currently met in LFS CVS HEAD):
1) initscripts should be able to create all device nodes in /dev with
the correct permissions, perhaps with the help of udevstart
2) initscripts should handle the situation when ramfs is already mounted
on /dev
3) initscripts should echo /sbin/hotplug >/proc/sys/kernel/hotplug when
the system is ready to handle hotplug events
--
Alexander E. Patrakov
-------------- next part --------------
#!/bin/sh
if [ ! -d klibc-*/ ]
then
tar jxf klibc-*.tar.bz2
(
cd klibc-*/
ln -s /lib/modules/`uname -r`/build linux
#cp ../nuke.c utils
#sed -i 's,true false sleep ln,& nuke,' utils/Makefile
sed -i 's,^REGPARM_OPT.*$,# &,' klibc/arch/i386/MCONFIG
make
)
fi
if [ ! -d udev-*/ ]
then
tar jxf udev-*.tar.bz2
(
cd udev-*/
make USE_KLIBC=true USE_LOG=false # KLIBC_BASE=`echo $PWD/../klibc-*/`
)
fi
mkdir -p image/{dev,etc/udev,proc,root,sbin,sys}
ln -nsf sbin image/bin
mknod image/dev/console c 5 1
mknod image/dev/null c 1 3
cp klibc-*/ash/sh image/sbin
cp klibc-*/utils/static/{dd,fstype,run-init,mount,umount} image/sbin
cp udev-*/{udev,udevstart} image/sbin
cat >image/init <<"EOF"
#!/bin/sh
mount -t proc proc /proc
mount -t sysfs sysfs /sys
# Kernel is buggy by default
echo >/proc/sys/kernel/hotplug
echo "=================================="
echo " This kernel uses initramfs "
echo "=================================="
read cmdline </proc/cmdline
rw=0
init="/sbin/init"
for param in $cmdline
do
case "$param" in
root=*)
root=`echo "$param" | dd bs=1 skip=5 2>/dev/null`
if [ x"`echo "$root" | dd bs=1 count=5 2>/dev/null`" =
x/dev/ ]
then
root=`echo "$root" | dd bs=1 skip=5 2>/dev/null`
fi
;;
rootfstype=*|rootflags=*|init=*|kinit=*)
eval "$param"
;;
ro)
rw=0
;;
rw)
rw=1
;;
esac
done
if [ ! -z "$kinit" ]
then
exec "$kinit"
fi
if [ -z "$root" ]
then
echo "No root parameter specified!"
exit 1
fi
root="/dev/$root"
udevstart
if [ -z "$rootfstype" ]
then
eval $( fstype <"$root" )
rootfstype="$FSTYPE"
if [ "$rootfstype" = "unknown" ]
then
echo "Unknown root filesystem type!"
exit 1
else
echo "Guessed root filesystem type: $rootfstype"
fi
fi
if [ ! -z "$rootflags" ]
then
rootflags="$rootflags,"
fi
if [ "$rw" = 1 ]
then
rootflags="$rootflags"rw
else
rootflags="$rootflags"ro
fi
mount -t "$rootfstype" -o "$rootflags" "$root"
/root
mount -t ramfs ramfs /root/dev
dd if=/etc/udev/udev.conf.host of=/etc/udev/udev.conf 2>/dev/null
ACTION=add DEVPATH=/class/mem/null udev class
ACTION=add DEVPATH=/class/tty/console udev class
exec </root/dev/console >/root/dev/console 2>&1
umount /sys
umount /proc
exec run-init /root "$init" "$@"
EOF
chmod 755 image/init
cat >image/etc/udev/udev.conf <<"EOF"
udev_root="/dev/"
udev_db="/dev/.udev.tdb"
udev_rules="/etc/udev/udev.rules"
default_mode="0600"
default_owner="root"
default_group="root"
udev_log="no"
EOF
cat >image/etc/udev/udev.conf.host <<"EOF"
udev_root="/root/dev/"
udev_db="/dev/.udev.tdb"
udev_rules="/etc/udev/udev.rules"
default_mode="0600"
default_owner="root"
default_group="root"
udev_log="no"
EOF
cp udev-*/etc/udev/udev.rules image/etc/udev
(
cd image
find . | cpio -o -H newc | gzip -9 >../initramfs_data.cpio.gz
)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: run-init.c
Type: text/x-csrc
Size: 4748 bytes
Desc: not available
Url :
http://www.zytor.com/pipermail/klibc/attachments/20040608/4f7361fc/run-init-0001.bin