Share Pastebin
Guest
Public paste!

initramfs_init

By: a guest | Mar 22nd, 2010 | Syntax: Bash | Size: 1.18 KB | Hits: 73 | Expires: Never
Copy text to clipboard
  1. #!/bin/sh
  2.  
  3. #Mount things needed by this script
  4. mount -t proc proc /proc
  5. mount -t sysfs sysfs /sys
  6.  
  7. #Disable kernel messages from popping onto the screen
  8. echo 0 > /proc/sys/kernel/printk
  9.  
  10. #Clear the screen
  11. clear
  12.  
  13. #Create all the symlinks to /bin/busybox
  14. busybox --install -s
  15.  
  16. #Create device nodes
  17. mknod /dev/null c 1 3
  18. mknod /dev/tty c 5 0
  19. mdev -s
  20.  
  21. #Function for parsing command line options with "=" in them
  22. # get_opt("init=/sbin/init") will return "/sbin/init"
  23. get_opt() {
  24.         echo "$@" | cut -d "=" -f 2
  25. }
  26.  
  27. #Defaults
  28. init="/sbin/init"
  29. root="/dev/hda1"
  30.  
  31. #Process command line options
  32. for i in $(cat /proc/cmdline); do
  33.         case $i in
  34.                 root\=*)
  35.                         root=$(get_opt $i)
  36.                         ;;
  37.                 init\=*)
  38.                         init=$(get_opt $i)
  39.                         ;;
  40.         esac
  41. done
  42.  
  43. #Mount the root device
  44. mount "${root}" /newroot
  45.  
  46. #Check if $init exists and is executable
  47. if [[ -x "/newroot/${init}" ]] ; then
  48.         #Unmount all other mounts so that the ram used by
  49.         #the initramfs can be cleared after switch_root
  50.         umount /sys /proc
  51.        
  52.         #Switch to the new root and execute init
  53.         exec switch_root /newroot "${init}"
  54. fi
  55.  
  56. #This will only be run if the exec above failed
  57. echo "Failed to switch_root, dropping to a shell"
  58. exec sh