[BusyBox] pivot_root and BusyBox's init.

W. Michael Petullo mike at flyn.org
Sat Apr 14 11:47:32 UTC 2001


I am trying to modify Gratien D'haese's Make CD-ROM Recovery (mkcdrec)
utility to use the newish Linux kernel pivot_root interface.

Mkcdrec uses an initrd, populated with a small number of system binaries,
and a root disk, populated by BusyBox.

The initrd contains a linuxrc shell script which loads some modules,
mounts the real root filesystem, calls pivot_root, and execs BusyBox's
init applet.

The problem is that by the time the init process is execed, its PID
is somewhere around 9.  BusyBox's init exits if its PID != 1.  To get
around this BusyBox is used, but the init program is called as linuxrc,
which causes the PID check to be skipped.

I am having trouble finding a pivot_root, initrd, and BusyBox example.
Though my linuxrc script works, I don't think I have it quite right.

Does anyone have any pointers for me.  Perhaps an example?

Thank you.

Here is my linuxrc script:

================================================================================

#!/bin/sh
# Copyright (c) 2000-2001 Gratien D'haese - IT3 Consultants
# Please read LICENSE in the source directory

# look through all the cdroms for the boot cd
mountBootDev()
{
    for bootdev in $*; do
        echo -n "$bootdev "
	# -n skips lock file for romfs.
        mount -n -o ro -t iso9660 /dev/$bootdev /mnt 2> /dev/null
        if [ -f /mnt/rd-base.img.bz2 ]; then
            echo "."
            cdbootdev=$bootdev
            return
        else
            umount /dev/$bootdev
        fi
    done
}

# -n skips lock file for romfs.
mount -n -t proc proc /proc  2> /dev/null

# unpack modules (if any)
OLDPWD=$PWD
cd /lib/modules
 MODULES="`ls *.bz2 2> /dev/null`"
 if [ ! -z "${MODULES}" ]; then
    /bin/bzip2 -d ${MODULES}
 fi

 # load modules needed to load the cd-rom
 . /etc/modules
cd $OLDPWD

# look through all the ide devices for cdroms
for idedev in `cd /proc/ide; echo hd?`; do
    grep "cdrom" /proc/ide/$idedev/media > /dev/null && cddev="$cddev $idedev"
done
cddev="$cddev "`cd /dev; echo scd*`
cddev="$cddev "`cd /dev; echo cdroms/*`

mountBootDev $cddev

if [ "X$cdbootdev" != "X" ]; then 
    echo I believe the CD device is /dev/$cdbootdev
    # save the CD device for remounting later
    if [ -b /dev/ram2 ]; then
	    echo /dev/$cdbootdev > /dev/ram2
    elif [ -b /dev/rd/2 ]; then
	    echo /dev/$cdbootdev > /dev/rd/2
    else
            echo "Can not find a RAM disk device /dev/ram2 or /dev/rd/2!"
	    echo I will not be able to automatically determine CD-ROM device.
    fi
else
    echo I couldn\'t find the CD!  It isn\'t one of \"$cddev\".
    echo "Being desparate I'll try to open a /bin/sh"
	/bin/sh
fi

echo Remounting root from /mnt/rd-base.img.bz2
if [ -b /dev/ram1 ]; then
       bzip2 -dc /mnt/rd-base.img.bz2 > /dev/ram1
       mount -n -o ro /dev/ram1 /new_root
elif [ -b /dev/rd/1 ]; then
       bzip2 -dc /mnt/rd-base.img.bz2 > /dev/rd/1
       mount -n -o ro /dev/rd/1 /new_root
else
       echo Can not find a RAM disk device for root at /dev/ram1 or /dev/rd/1!
       echo "Being desparate I'll try to open a /bin/sh"
       /bin/sh
fi

cd /new_root
mount -n -t devfs none dev
mount -n -t proc none proc
pivot_root . initrd

# FIXME: The following should not be necessary.
# echo 257 > /proc/sys/kernel/real-root-dev

umount initrd/proc
umount initrd/dev/$cdbootdev

echo My PID is $$
# exec /usr/sbin/chroot . /sbin/init < dev/console > dev/console 2>&1
# Okay, try this:
exec /usr/sbin/chroot . /linuxrc < dev/console > dev/console 2>&1

echo Init failed!
echo "Being desparate I'll try to open a /bin/sh"
/bin/sh

exit 0

================================================================================

-- 
W. Michael Petullo

:wq





More information about the busybox mailing list