Modifying init to create /dev/console ?

Denis Vlasenko vda.linux at googlemail.com
Wed Feb 28 22:38:47 UTC 2007


On Wednesday 28 February 2007 10:46, Terry Barnaby wrote:
> Denis Vlasenko wrote:
> > On Tuesday 27 February 2007 22:53, Terry Barnaby wrote:
> >> Denis Vlasenko wrote:
> >>> On Tuesday 27 February 2007 10:22, Terry Barnaby wrote:
> >>>> I am trying to create a basic Linux system based on Busybox and would 
> >>>> like the ability to create the root file system as a normal user and 
> >>>> without any /dev entries. The startup script would create the /dev 
> >>>> entries as needed.
> >>>> However, init and other programs obviously require /dev/console (and 
> >>>> other /dev entries).
> >>>>
> >>>> I was wondering about adding an option to init where it would create a 
> >>>> tmpfs file system, mount it on /dev and create a /dev/console node if no 
> >>>> /dev/console was found. I guess it could do a bit more and function as 
> >>>> udev as well.
> >>> I think you are on the right track, but move in the wrong direction
> >>> along that track. Code shouldn't be added to init, it should be removed.
> >>>
> >>> http://busybox.net/~vda/example_fs/README
> >>> --
> >>> vda
> >> Hi Denis,
> >>
> >> But then you end up with init implemented in several shell scripts.
> >> I guess it depends if you prefer 'C' or shell programs :)
> > 
> > Shell programs are easier to tailor for particular system without
> > messing up with compiler and toolchain - exactly what you need
> > for "I want to create /dev/console".
> > --
> > vda
> 
> Hi Denis,
> 
> I notice your http://busybox.net/~vda/example_fs has /dev/console and 
> /dev/null nodes. Have you managed to get a busybox system to boot and 
> run using shell scripts without these nodes ?

Yes, see below
 
> In my system the final rootfs will be a read-only NFS mount and I don't 
> want to have a /dev/console in the NFS mounted file system. So I need to 
> create this at boot time. My shell scripts do not appear to run from 
> init if I do not have a /dev/console though ...

This is the script which runs as /linuxrc (PID=1) in my initrd.
Notice that it mounts ramfs on /dev and creates console
and null there before chrooting into new ("real") root.
--
vda

#!/bin/sh

export PATH=/bin:/usr/bin

if ! test "$INIT"; then
    INIT=/sbin/init
fi

cd /
# devfs allows me to use /dev/ide/.../.../.. root= param
#mount -n -t devfs none /dev
mount -n -t proc none /proc
#mount -n -t sysfs none /sys

echo "# Mounting root fs"
/script/mount_root

# Clean up
#umount /sys
umount /proc

echo "# Chrooting into root fs"
test -d /new_root/dev -a -d /new_root/proc || {
    echo "Root fs (ls -l):"
    cd /new_root; ls -l
    echo "Root fs has no /dev and/or /proc!"
    echo "Fatal, cannot continue booting"
    while true; do sleep 32000; done
}
mount -n -t ramfs none /new_root/dev || {
    echo "! mount -n -t ramfs none /new_root/dev failed, continuing in 10 sec"
    sleep 10
}
cp -dp /dev/console /dev/null /new_root/dev
cd /new_root
# making sure we dont keep /dev busy
exec <dev/console >dev/console 2>&1
# proc/ in new root is used here as a temp mountpoint for old root
pivot_root . proc || {
    echo "pivot_root failed. Fatal, cannot continue booting"
    while true; do sleep 32000; done
}

exec \
chroot . \
sh -c '\
umount -n /proc || { echo "! error unmounting initrd fs, continuing"; sleep 10; }
exec /bin/env - $INIT
'

echo "chroot . sh failed. Fatal, cannot continue booting"
while true; do sleep 32000; done



More information about the busybox mailing list