#!/bin/bash # # /linuxrc.usb.usb: init script to load initrd kernel modules # # # For More Ramdisk Options ... # ---------------------------- # linuxrc.Commands.Help.txt # # # # 02-Nov-04 amo Create a minimal initrd.gz to load the rootfs.gz as / # 13-Nov-04 amo Split off ramdisk options into linuxrc.Commands.Help.txt # 20-Nov-04 amo Split off common mount and checking to linuxrc.common.sh # 23-Nov-04 amo Use CFRW to debug mount CF as rw vs ro options -> linuxrc -> /etc/fstab # 26-Mar-05 amo Modified for CF ( hda1 ) or USB ( sda1 ) # 27-Mar-05 amo "root=/dev/ram0 1" has no single user, root=/dev/loop0 is readonly # 29-Mar-05 amo use /dev/sda2 ( usb or cf ) to uncompress rootfs.gz into and chroot into it # 02-Apr-05 amo Explicity set PATH and remount / # 03-Apr-05 amo Fixed localtime w/ hwclock # 15-Apr-05 amo Make sure /proc exists # 16-Apr-05 amo Update for Booting CF vs Booting USB # 20-Aug-05 amo WRAP does not have battery for time ( fix date ) # 21-Aug-05 amo Merge to Support CF ( hda1 ) or USB ( sda1 ) # 01-Sep-05 amo 2005.0901 Use PreInstalled LoopFiles for RootFS # 03-Sep-05 amo Use new bash.common.sh ( aka linuxrc.common.sh ) # 04-Sep-05 amo BootMsg is NOT yet available in initrd ( /linuxrc ) # 09-Sep-05 amo Use LoopFile instead of rootfs.tgz # 15-Sep-05 amo Check for -x before executing rc.usb-stick # # # # Include the common functions and definitions # -------------------------------------------- # define docmd(), BootMsg() mount_proc() #ource /LinuxRC/linuxrc.common.sh source /etc/bash.common.sh # # # To turn on debug messages # ------------------------- DebugMsg="anything" # # # -------------------------------------------------------- # Define the devices for CompactFlash vs USB-memory-stick # -------------------------------------------------------- # # # CF #mount /dev/hda1 # USBCFdev="/dev/hda" USBCFmnt="/mnt/CF" # # # USB #mount /dev/sda1 # USBCFdev="/dev/sda" USBCFmnt="/mnt/USB" # # USBCFloop="/mnt/loop0" # # -------------------------- # docmd "#" docmd "# Executing /linurc" docmd "# linuxrc: Boot=$DebugMsg..run=$runlevel.." docmd "#" # # # leave a " " in front of mount, to avoid the debug output of an empty /proc/mounts # ----------------------------- # # # Mount /proc # =========== # mount -n -o rw,remount / # # make sure /proc exists if [ ! -d /proc ]; then mkdir /proc ; fi # mount -t proc proc /proc # # # Make / writable ( need "-n" for /etc/mtab ) # --------------- docmd " mount -n -o rw,remount -t ext2 /dev/ram0 /" # # # Remount it again so that it shows up in the now writable /etc/mtab # ---------------- docmd " mount -o rw,remount -t ext2 /dev/ram0 /" # # # Read/Write Tests # ----------------- docmd " touch /Read-Write-Test.txt " # if [ ! -f "/Read-Write-Test.txt" ]; then # docmd "# FATAL ERROR /dev/ram0 is NOT read write.." # exit 1 # else # rm -f "/Read-Write-Test.txt" # fi # # docmd " ls -la /mnt " # # # Load the kernel modules if any # # for module in /lib/modules/`uname -r`/*.*o ; do # insmod $module # done # # # Load the USB modules # -------------------- # # # # booting from GRUB requires the usb modules # # # # vfat is in the kernel, but it won't boot if vfat for /dev/sda1 is not installed here # # # if [ -x "/etc/rc.d/rc.usb-stick" ]; # then # # required for "rootfs 1" even if the kernel has the usb and vfat/fat modules compiled in # docmd "/etc/rc.d/rc.usb-stick start " # # # for vfat partitions on /dev/sda1 ( aka /boot ) # -------------------------------- docmd "modprobe msdos " docmd "modprobe fat " docmd "modprobe vfat " # # fi # # # == # == i donno why /dev/hda1 needs to be rw here # == # # Mount the compact flash ( CF:/boot/rootfs.gz ) # ----------------------------------------------- # == # == pivot_root/chroot works if both /mnt/CF and *.img are "ro" # == # ----------------------------------------------- # - with "ro", system boots into RootFS with "ro" but is in fact ro # ----------------------------------------------- # CFRW="ro" docmd " mount -o $CFRW ${USBCFdev}1 $USBCFmnt " # # # ERREXIT="exit-on-critical-error" # # no point in continuing if the rootfs is not found # docmd "ls -la $USBCFmnt/boot/rootfs.tgz" # # # # rootfs is 32MB # -------------- # - # - we need initrd to create the 32MB rootfs # - pivot_root expects a directory name # - when we pivot_root and chroot into /mnt/loop0, we want to rw into /dev/tty, /var/* # - # # # This make the rootfs "ro" # cmd " mount -o $CFRW,loop /mnt/USB/boot/rootfs.img /mnt/loop0" # # # ------------------------------------------ # 01-Sep-05 amo use the PreInstalled RootFS # ------------------------------------------ if [ ! -d /mnt/RootFS ]; then mkdir /mnt/RootFS else umount /mnt/RootFS fi umount $USBCFloop # # # * jfs ( sda2 ) works on the 1st pass, but seems to be corrupted on subsequent mounts # * trying xfs instead # # 09-Sep-05 amo Use LoopFile instead of rootfs.tgz # docmd " mount -t auto ${USBCFdev}2 $USBCFloop" # # no point in continuing if the rootfs ( its LoopFile ) is not found docmd "ls -la $USBCFloop/LoopFile" # docmd " losetup /dev/loop0 $USBCFloop/LoopFile " # docmd " mount /dev/loop0 /mnt/RootFS " # # # not critical anymore ERREXIT="" # # # view the rootfs # docmd "ls -la /mnt/RootFS" # # final sanity check - remove debug messages, /linuxrc doesn't listen to BootMsg variable # ------------------ # docmd "df | sort " # # docmd "cat /proc/mounts | sort " # # sync # # # --------------------- # /dev/ram is loaded -- linuxrc should pass control to /sbin/init by default # --------------------- # # # docmd "#" # docmd "# Done: /linuxrc ..." # #ocmd "# start /linux.loop in lieu of /sbin/init since" # #ocmd "# /bin, /dev, /etc, /sbin is a preformatted loop device " # docmd "#" # # # Tell the kernel, / is already mounted # -------------------------------------- # /dev/loop0 7,0 # /dev/loop1 7,1 # docmd "# /dev/loop0 == 0x700 == 1792" docmd "# /dev/loop1 == 0x701 == 1793" docmd "echo '0x700' > /proc/sys/kernel/real-root-dev" docmd "#" # # # # Attempt to Get rid of "pivot_root: Device or Resource is busy" # --------------------------------- # - "busy error" seems to be non-fatal when "mount ro" # - "busy error" exits if "mount rw" # # # # # pivot_root does not seem to be needed # # # docmd "cd /mnt/RootFS" # docmd "umount /proc" # docmd "pivot_root . /initrd" # docmd "#" # # # to free up memory # # umount /initrd/proc # # umount /initrd # # --------------------------------------------------- # use a variable to make sure chroot is done properly # --------------------------------------------------- MntRootFS="/mnt/RootFS" # docmd "cd $MntRootFS" # # # # Invoking chroot from inside linuxrc causes: # ------------------------------------------ # "error opening/writing control channel /dev/initctl" # # - use pivot-root to avoid the above error # # exec /usr/sbin/chroot . /sbin/init dev/console # # # Continue with normal bootup # ============================ # PID=1 should be init # BootMsg "# " docmd "ps axuw " BootMsg "# " # # initid=` ps auwx | grep " init " | awk '{ print $2 }' ` # # BootMsg "# " BootMsg "# Debug=$DebugMsg...run1=$runlevel..initid=$initid.." # if [ "$runlevel" = "" ]; then runlevel=3 ; fi # BootMsg "# Debug=$DebugMsg...run2=$runlevel..initid=$initid.." BootMsg "#" # # # ====================================================================== # lilo and grub does NOT need to chroot to boot properly with /dev/loop # ====================================================================== # # Upon exiting /linuxrc, normal /sbin/init is supposed to be executed # # if [ "$1" = "" -o "$InitLevel" = "3" -o "$InitLevel" = "1" ]; # # if [ "$initid" = "1" ]; # then # # # InitCMD="exec chroot $MntRootFS /sbin/init $runlevel" # # # else # # # InitCMD="exec chroot $MntRootFS /etc/rc.d/rc.S && /etc/rc.d/rc.M " # # # fi # # # Work aroound the problem that syslinux cannot find /dev/loop # # if [ "$SysLinux" = "WorkAround" ]; # then # InitCMD="exec chroot $MntRootFS /etc/rc.d/rc.SM " # fi # BootMsg "#" docmd "df" BootMsg "#" BootMsg "WorkAroundInitCmd=$InitCMD" BootMsg "#" # sleep 5 # $InitCMD # # Creating RootFS worked # exit 0 # # # End of file