Advertisement
Guest User

init

a guest
Oct 12th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #!/bin/bash
  2. PATH=/bin:/usr/bin:/usr/sbin:/sbin
  3. udevd_running=0
  4.  
  5. if [ -x /usr/bin/systemd-timestamp ]; then
  6. RD_TIMESTAMP=$(systemd-timestamp)
  7. fi
  8.  
  9. . /usr/lib/initcpio/init_functions
  10.  
  11. mount -t proc proc /proc -o nosuid,noexec,nodev
  12. mount -t sysfs sys /sys -o nosuid,noexec,nodev
  13. mount -t devtmpfs dev /dev -o mode=0755,nosuid
  14. mount -t tmpfs run /run -o nosuid,nodev,mode=0755
  15. mkdir -m755 /run/initramfs
  16.  
  17. # parse the kernel command line
  18. parse_cmdline
  19.  
  20. for d in ${disablehooks//,/ }; do
  21. [ -e "/hooks/$d" ] && chmod 644 "/hooks/$d"
  22. done
  23.  
  24. #. /config
  25.  
  26. run_hookfunctions 'run_earlyhook' 'early hook' $EARLYHOOKS
  27.  
  28. [ -n "${earlymodules//[[:space:]]}" ] && modprobe -qab ${earlymodules//,/ }
  29. [ -n "${MODULES//[[:space:]]}" ] && modprobe -qab $MODULES
  30.  
  31. # If rootdelay is empty or not a non-negative integer, set it to 10
  32. if [ -z "${rootdelay}" ] || ! [ "${rootdelay}" -ge 0 ]; then
  33. rootdelay=10
  34. fi
  35.  
  36. run_hookfunctions 'run_hook' 'hook' $HOOKS
  37.  
  38. # honor the old behavior of break=y as a synonym for break=premount
  39. if [ "${break}" = "y" ] || [ "${break}" = "premount" ]; then
  40. echo ":: Pre-mount break requested, type 'exit' to resume operation"
  41. launch_interactive_shell
  42. fi
  43.  
  44. rootdev=$(resolve_device "$root") && root=$rootdev
  45. unset rootdev
  46.  
  47. #fsck_root
  48.  
  49.  
  50. ######################################################
  51.  
  52. # Make /new_root
  53. mkdir /new_root
  54.  
  55. # Mount root at /new_root
  56. #${mount_handler:-default_mount_handler} /new_root
  57. echo -e "Switching / to tmpfs..."
  58.  
  59. mount -t tmpfs tmpfs /new_root
  60.  
  61. run_hookfunctions 'run_latehook' 'late hook' $LATEHOOKS
  62. run_hookfunctions 'run_cleanuphook' 'cleanup hook' $CLEANUPHOOKS
  63.  
  64. # Stop udevd if is running
  65. if [ "${udevd_running}" -eq 1 ]; then
  66. udevadm control --exit
  67. udevadm info --cleanup-db
  68. fi
  69.  
  70. # Copy root
  71. echo -e "Copying root..."
  72. for i in $(ls -a /); do
  73. case "$i" in
  74. .|..) ;;
  75. mnt) mkdir /new_root/mnt;;
  76. proc) mkdir /new_root/proc;;
  77. sys) mkdir /new_root/sys;;
  78. dev) mkdir /new_root/dev;;
  79. run) mkdir /new_root/run;;
  80. new_root) ;;
  81. *) cp -a /$i /new_root
  82. esac
  83. done
  84.  
  85. # Create mountpoints
  86. mkdir /new_root/mnt/sda1
  87. mkdir /new_root/mnt/sda2
  88. mkdir /new_root/mnt/sda3
  89. mkdir /new_root/mnt/sda4
  90.  
  91. init=${init:-/sbin/init}
  92.  
  93. echo -e "Switching root..."
  94. exec env -i \
  95. "TERM=$TERM" \
  96. "RD_TIMESTAMP=$RD_TIMESTAMP" \
  97. /sbin/switch_root /new_root $init "$@"
  98.  
  99.  
  100. ######################################################
  101.  
  102.  
  103. ## Mount root at /new_root
  104. #${mount_handler:-default_mount_handler} /new_root
  105.  
  106. #run_hookfunctions 'run_latehook' 'late hook' $LATEHOOKS
  107. #run_hookfunctions 'run_cleanuphook' 'cleanup hook' $CLEANUPHOOKS
  108.  
  109. init=${init:-/sbin/init}
  110. if [ "$(stat -c %D /)" = "$(stat -c %D /new_root)" ]; then
  111. # Nothing got mounted on /new_root. This is the end, we don't know what to do anymore
  112. # We fall back into a shell, but the shell has now PID 1
  113. # This way, manual recovery is still possible.
  114. err "Failed to mount the real root device."
  115. echo "Bailing out, you are on your own. Good luck."
  116. echo
  117. launch_interactive_shell --exec
  118. elif [ ! -x "/new_root${init}" ]; then
  119. # Successfully mounted /new_root, but ${init} is missing
  120. # The same logic as above applies
  121. err "Root device mounted successfully, but ${init} does not exist."
  122. echo "Bailing out, you are on your own. Good luck."
  123. echo
  124. launch_interactive_shell --exec
  125. fi
  126.  
  127. if [ "${break}" = "postmount" ]; then
  128. echo ":: Post-mount break requested, type 'exit' to resume operation"
  129. launch_interactive_shell
  130. fi
  131.  
  132. exec env -i \
  133. "TERM=$TERM" \
  134. "RD_TIMESTAMP=$RD_TIMESTAMP" \
  135. /sbin/switch_root /new_root $init "$@"
  136.  
  137. # vim: set ft=sh ts=4 sw=4 et:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement