Search:  
Gentoo Wiki

Unionfs-linuxrc

#!/bin/sh
#
# Mathias Laurin
# (c) 2006, Mathias Laurin, GPLv2, Tokyo
# NO WARRANTY
#
# mandatory argument: root= 
# optional arguments: init= shell verbose resume2
#
# The modules needed to boot are expected to be listed in 
# /etc/modules/boot, one per line

EDIT=
INIT="/sbin/init"
ARGS_ROOT_MOUNT="-o loop"
NEWROOT="/newroot"

TMP="/tmp"
DISK=$TMP"/disk.0"
SQUASH=$TMP"/disk.1"

SUSPEND=
VERBOSE=

verbose() {
	[ $VERBOSE ] && echo " * $@"
}

warning() {
	echo "WARNING: $@"
}

die() {
	verbose "Dying"
	[ $# -ne 0 ] && echo "$@"

	# Enable job control for the shell
	[ ! -c /dev/tty ] && \
	mknod /dev/tty c 5 0 && chown 0:tty /dev/tty

	# Try to find a minimal shell
	exec /bin/sh || \
	exec /bin/busybox
	
	echo "FATAL ERROR: Could not even die elegantly"
	echo "trying to force a reboot in 5 seconds"
	sleep 5
	reboot -f
}

do_bootsplash() {
	return 0
}

do_dev() {
	verbose "Populating /dev from sys"

	mdev -s
	# handle hotplug events
	echo /sbin/mdev > /proc/sys/kernel/hotplug

	return 0
}

do_lvm() {
	verbose "Enable LVM2 support"
	
	/sbin/lvm vgscan --mknodes --ignorelockingfailure
	/sbin/lvm vgchange -a y --ignorelockingfailure

	sleep 1

	return 0
}

do_parse_kernel() {
	#verbose "Parse kernel line"

	local CMD
	for CMD in $(cat /proc/cmdline)
	do
		case "$CMD" in
			init=*)
				INIT=${CMD#init=}
				;;
			shell)
				EDIT="yes"
				;;
			resume|resume2)
				SUSPEND="yes"
				;;
			ro|rw)
				ARGS_ROOT_MOUNT="-o $CMD"
				;;
			root=*)
				REALROOT=${CMD#root=}
				;;
			v|verbose)
				VERBOSE="yes"
				;;
		esac
	done

	return 0
}

do_realroot() {
	verbose "Mounting $REALROOT on $NEWROOT"
        # search disk by label
	do_findfs

	[ ! -d $TMP ] && mkdir $TMP
	mount -t tmpfs tmpfs $TMP

	[ ! -d $DISK ] && mkdir $DISK
	mount $REALROOT $DISK

	[ ! -d $SQUASH ] && mkdir $SQUASH
	mount -o loop,ro $DISK/.$LABEL/$LABEL.squashfs $SQUASH

	[ ! -d $NEWROOT ] && mkdir $NEWROOT
	mount -t aufs -o dirs=$DISK/.overlay=rw:$SQUASH=ro aufs $NEWROOT

	return $?
}

do_findfs(){
	LABEL=$REALROOT
	REALROOT=`findfs "LABEL=$REALROOT" 2>/dev/null`	
}

do_suspend() {
	return 0
}

do_switch() {
	verbose "Switching / to $NEWROOT"

	# Better make sure these exist before switching
	[ ! -c "$NEWROOT/dev/console" ] && mknod "$NEWROOT/dev/console" c 5 1
	[ ! -c "$NEWROOT/dev/null" ] && mknod "$NEWROOT/dev/null" c 1 3

	echo > /proc/sys/kernel/hotplug
	
	umount /dev  || umount -l /dev
	umount /sys  || umount -l /sys
	umount /proc || umount -l /proc
	mount -o move $TMP $NEWROOT/newroot	
	exec switch_root $NEWROOT $INIT
}

modprobe_group() {
	local GROUP="$1"
	local MOD
	verbose "Insert $GROUP modules"

	if [ -f "/etc/modules/$GROUP" ]
	then
		for MOD in $(cat "/etc/modules/$GROUP")
		do
			modprobe $MOD 1>/dev/null
		done
	else
		warning "File /etc/modules/$GROUP not found"
	fi

	return 0
}

main() {
	# mount proc ASAP
	/bin/busybox mount -t proc proc /proc

	# install busybox applets
	/bin/busybox --install -s
	umask 0077

	# finish mounting
	mount -t sysfs sysfs /sys
	mount -t tmpfs tmpfs /dev
	# parse kernel command line
	do_parse_kernel

	# load boot modules
	modprobe_group boot

	# populate dev from sys
	do_dev
	
	# splashscreen
	do_bootsplash

	# suspend2
	[ $SUSPEND ] && do_suspend

	# LVM2
	[ -x /sbin/lvm ] && do_lvm

	do_realroot
	
	# Switch unless a shell was requested
	[ "$EDIT" ] || do_switch
}

main
die
Retrieved from "http://www.gentoo-wiki.info/Unionfs-linuxrc"

Last modified: Fri, 05 Sep 2008 09:20:00 +0000 Hits: 1,067