Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/busybox sh
- rescue_shell() {
- echo "$@"
- echo "Something went wrong. Dropping you to a shell."
- busybox --install -s
- exec /bin/sh
- }
- pretty_menu() {
- num=0
- while read menuitem ; do
- if [ $num -gt 0 ] ; then
- echo -n "${num}: "
- echo $menuitem | cut -d "|" -f 4
- else
- defaultitem=$(echo $menuitem | cut -d "|" -f 1)
- menutimeout=$(echo $menuitem | cut -d "|" -f 2)
- fi
- num=$(expr $num + 1)
- done < /mnt/boot/menu.lst
- echo
- }
- read_selection() {
- while [[ -z "${selecteditemindex}" || -z "${selectedline}" ]] ; do
- read -t $menutimeout -p "Select an option (default is ${defaultitem} timeout is ${menutimeout}): " selecteditem || selecteditem=$defaultitem
- if [[ $selecteditem -eq $selecteditem ]]; then
- selecteditemindex=$(expr $selecteditem + 1)
- selectedline=$(sed -n ${selecteditemindex}p /mnt/boot/menu.lst)
- selectedtype=$(echo $selectedline | cut -d "|" -f 1)
- selectedroot=$(echo $selectedline | cut -d "|" -f 2)
- selectedoptions=$(echo $selectedline | cut -d "|" -f 3)
- selectedname=$(echo $selectedline | cut -d "|" -f 4)
- fi
- done
- }
- show_bootmenu() {
- # mountimg /boot
- mkdir -p /mnt/boot
- mount /dev/mmcblk0p1 /mnt/boot || rescue_shell "Error while mounting /boot"
- # print boot menu
- pretty_menu || rescue_shell "Error while printing boot menu"
- # read selected menu item
- read_selection || rescue_shell "Error while reading selection"
- # umounting /boot
- umount /mnt/boot || rescue_shell "Error while umounting /boot"
- }
- activate_vgs() {
- /sbin/pvscan --uuid --verbose || rescue_shell "Error while scanning physical volumes"
- /sbin/vgscan --mknodes --verbose || rescue_shell "Error while scanning volume groups"
- for vg in $(/sbin/vgscan | grep "Found volume group" | cut -d\" -f2) ; do
- echo "Activating volume group: $vg"
- /sbin/vgchange -ay $vg --sysinit || rescue_shell "Error while activating $vg"
- /sbin/vgmknodes --ignorelockingfailure $vg || rescue_shell "Error while making nodes for $vg"
- done
- }
- find_rootfs() {
- if [ -z "${selectedoptions}" ] ; then
- mountoptions="ro"
- else
- mountoptions="ro,${selectedoptions}"
- fi
- case $selectedtype in
- phy|lvm)
- fstype=$(echo $selectedroot | cut -d "=" -f 1)
- if [ "${fstype}" == "LABEL" ] || [ "${fstype}" == "UUID" ] ; then
- fsname=$(echo $selectedroot | cut -d "=" -f 2)
- echo "Mounting rootfs: mount -o $mountoptions $(findfs "${fstype}=${fsname}") /mnt/root"
- mount -o $mountoptions $(findfs "${fstype}=${fsname}") /mnt/root
- return
- else
- echo "Mounting rootfs: mount -o $mountoptions ${selectedroot} /mnt/root"
- mount -o $mountoptions $selectedroot /mnt/root
- return
- fi
- ;;
- nfs)
- echo "Mounting rootfs: mount -o ${selectedoptions} ${selectedroot} /mnt/root"
- mount -o $mountoptions $selectedroot /mnt/root
- return
- ;;
- esac
- rescue_shell "No root partition found"
- }
- pretty_bootmsg() {
- echo
- echo "Booting ${selectedname} (${selectedroot})..."
- }
- # temporarily mount proc and sys
- mount -t proc none /proc
- mount -t sysfs none /sys
- mount -t devtmpfs none /dev
- # disable kernel messages from popping onto the screen
- echo 0 > /proc/sys/kernel/printk
- # clear the screen
- clear
- # display the boot menu
- show_bootmenu || rescue_shell "Error while reading boot menu"
- # scan for vgs and activate each and every one of them
- activate_vgs || rescue_shell "Error while activating volume groups"
- # mounting rootfs on /mnt/root
- find_rootfs || rescue_shell "Error while finding root filesystem"
- # print boot message
- pretty_bootmsg || rescue_shell "Error while printing boot message"
- # find and check mount points
- find_mountpoints || rescue_shell "Error while reading and checking mount points"
- echo "All done. Switching to real root."
- # clean up. The init process will remount proc sys and dev later
- umount /proc
- umount /sys
- umount /dev
- # switch to the real root and execute init
- exec switch_root /mnt/root /sbin/init
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement