#!/bin/busybox sh rescue_shell() { echo "$@" echo "Something went wrong. Dropping you to a shell." # have time to read the message /bin/sleep 20 /bin/busybox --install -s exec /bin/sh } # allow the use of UUIDs or filesystem lables uuidlabel_root() { for cmd in $(/bin/cat /proc/cmdline) ; do case $cmd in root=*) type=$(echo $cmd | /bin/cut -d= -f2) echo $type echo "Mounting rootfs" if [ $type == "LABEL" ] || [ $type == "UUID" ] ; then uuid=$(echo $cmd | /bin/cut -d= -f3) /bin/mount -o ro $(/bin/findfs "$type"="$uuid") /mnt/root else /bin/mount -o ro $(echo $cmd | /bin/cut -d= -f2) /mnt/root fi #echo "Root contains" #ls -l /mnt/root #/bin/sleep 20 ;; esac done } check_filesystem() { # most of code coming from /etc/init.d/fsck local fsck_opts= check_extra= RC_UNAME=$(uname -s) # FIXME : get_bootparam forcefsck if [ -e /forcefsck ]; then fsck_opts="$fsck_opts -f" check_extra="(check forced)" fi echo "Checking local filesystem $check_extra : $1" #/bin/sleep 20 if [ "$RC_UNAME" = Linux ]; then fsck_opts="$fsck_opts -C0 -T" fi trap : INT QUIT # using our own fsck, not the builtin one from busybox /sbin/fsck -p $fsck_opts $1 ret_val=$? case $ret_val in 0) return 0;; 1) echo "Filesystem repaired"; return 0;; 2|3) if [ "$RC_UNAME" = Linux ]; then echo "Filesystem repaired, but reboot needed" reboot -f else rescue_shell "Filesystem still have errors; manual fsck required" fi;; 4) if [ "$RC_UNAME" = Linux ]; then rescue_shell "Fileystem errors left uncorrected, aborting" else echo "Filesystem repaired, but reboot needed" reboot -f fi;; 8) echo "Operational error"; return 0;; 16) echo "Use or Syntax Error"; return 16;; 32) echo "fsck interrupted";; 127) echo "Shared Library Error"; sleep 20; return 0;; *) echo $ret_val; echo "Some random fsck error - continuing anyway"; sleep 20; return 0;; esac # rescue_shell can't find tty so its broken rescue_shell } # start for real here # temporarily mount proc and sys PATH=/sbin:/bin export PATH #echo "/bin" #/bin/ls -l /bin #echo "/sbin" #/bin/ls -l /sbin #/bin/sleep 20 #echo "Mount proc" /bin/mount -t proc proc /proc #/bin/busybox sleep 20 #echo "Mount sysfs" /bin/mount -t sysfs sysfs /sys #/bin/ls /dev #/bin/sleep 20 # Don't be silly - /dev is static and its on / # echo "Mount /dev" # /bin/busybox mount -t devtmpfs none /dev # /bin/busybox sleep 20 # /bin/busybox ls /dev # /bin/busybox sleep 20 # disable kernel messages from popping onto the screen ###echo 0 > /proc/sys/kernel/printk # clear the screen ###clear # assemble the raid set(s) - they got renumbered from md1, md5 and md6 #/dev/sda1: UUID="9392926d-6408-6e7a-8663-82834138a597" TYPE="linux_raid_member" PARTUUID="0553caf4-01" #/dev/sda2: UUID="b6633d8e-41ef-4485-9bbe-c4c2d69f4e8c" TYPE="swap" PARTUUID="0553caf4-02" #/dev/sda5: UUID="5e3cadd4-cfd2-665d-9690-1ac76d8f5a5d" TYPE="linux_raid_member" PARTUUID="0553caf4-05" #/dev/sda6: UUID="9657e667-5b60-f6a3-0391-65e6dcf662fa" TYPE="linux_raid_member" PARTUUID="0553caf4-06" # not needed on SSD but we may want to maintain it # spinney /boot /sbin/mdadm --assemble /dev/md125 --uuid=9392926d-6408-6e7a-8663-82834138a597 # /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1 # don't care if /boot fails to assemble # not needed on SSD, so don't fail for not assembling spinney root # / (root) I wimped out of root on lvm for this box /sbin/mdadm --assemble /dev/md126 --uuid=5e3cadd4-cfd2-665d-9690-1ac76d8f5a5d # /dev/sda5 /dev/sdb5 /dev/sdc5 /dev/sdd5 || rescue_shell "Root Assembly Failed" # LVM for everything else # /home and everything portge related /sbin/mdadm --assemble /dev/md127 --uuid=9657e667-5b60-f6a3-0391-65e6dcf662fa #/dev/sda6 /dev/sdb6 /dev/sdc6 /dev/sdd6 || rescue_shell "LVM Assembly Failed" # and if the LVM space won't assemble there is no /usr or /var so we are really in a mess # TODO could auto cope with degraded raid operation # start the vg volume group - /home and everything for portage - need not die here /sbin/vgchange -ay vg || rescue_shell "lvchange vg failed" ## WARNING Order is important!!! or the /dev/dm-x devices come up in the wrong places. # if this failed we have no, / /usr or /var # everything on the SDD /sbin/vgchange -ay static || rescue_shell "lvchange static failed" # get here with raid sets assembled and logical volumes available # mounting rootfs on /mnt/root uuidlabel_root || rescue_shell "Error with uuidlabel_root" # space separated list of mountpoints that ... mountpoints="/usr /var" # ... we want to find in /etc/fstab ... /bin/ln -s /mnt/root/etc/fstab /etc/fstab #/bin/ls -l /etc #/bin/ls -l /mnt/root #/bin/sleep 20 # ... to check filesystems and mount our devices. for m in $mountpoints ; do #echo $m #/bin/sleep 20 check_filesystem $m echo "Mounting $m" # mount the device and ... /bin/mount $m || rescue_shell "Error while mounting $m" # ... move the tree to its final location /bin/mount --move $m "/mnt/root"$m || rescue_shell "Error while moving $m" done echo "All done. Switching to real root." # clean up. The init process will remount proc sys and dev later /bin/umount /proc /bin/umount /sys # we don't have a /dev to umount # switch to the real root and execute init exec /sbin/switch_root /mnt/root /sbin/init