Advertisement
sandyd

initrd

Sep 8th, 2011
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. [ -d /dev ] || mkdir -m 0755 /dev
  4. [ -d /root ] || mkdir -m 0700 /root
  5. [ -d /sys ] || mkdir /sys
  6. [ -d /proc ] || mkdir /proc
  7. [ -d /tmp ] || mkdir /tmp
  8. mkdir -p /var/lock
  9. mount -t sysfs -o nodev,noexec,nosuid none /sys
  10. mount -t proc -o nodev,noexec,nosuid none /proc
  11. # Some things don't work properly without /etc/mtab.
  12. ln -sf /proc/mounts /etc/mtab
  13.  
  14. grep -q '\<quiet\>' /proc/cmdline || echo "Loading, please wait..."
  15.  
  16. # Note that this only becomes /dev on the real filesystem if udev's scripts
  17. # are used; which they will be, but it's worth pointing out
  18. if ! mount -t devtmpfs -o mode=0755 none /dev; then
  19. echo "W: devtmpfs not available, falling back to tmpfs for /dev"
  20. mount -t tmpfs -o mode=0755 udev /dev
  21. [ -e /dev/console ] || mknod -m 0600 /dev/console c 5 1
  22. [ -e /dev/null ] || mknod /dev/null c 1 3
  23. fi
  24. mkdir /dev/pts
  25. mount -t devpts -o noexec,nosuid,gid=5,mode=0620 none /dev/pts || true
  26. > /dev/.initramfs-tools
  27. mkdir /dev/.initramfs
  28.  
  29. # Export the dpkg architecture
  30. export DPKG_ARCH=
  31. . /conf/arch.conf
  32.  
  33. # Set modprobe env
  34. export MODPROBE_OPTIONS="-qb"
  35.  
  36. # Export relevant variables
  37. export ROOT=
  38. export ROOTDELAY=
  39. export ROOTFLAGS=
  40. export ROOTFSTYPE=
  41. export IP=
  42. export BOOT=
  43. export BOOTIF=
  44. export UBIMTD=
  45. export break=
  46. export init=/sbin/init
  47. export quiet=n
  48. export readonly=y
  49. export rootmnt=/root
  50. export debug=
  51. export panic=
  52. export blacklist=
  53. export resume=
  54. export resume_offset=
  55.  
  56. # mdadm needs hostname to be set. This has to be done before the udev rules are called!
  57. if [ -f "/etc/hostname" ]; then
  58. /bin/hostname -b -F /etc/hostname 2>&1 1>/dev/null
  59. fi
  60.  
  61. # Bring in the main config
  62. . /conf/initramfs.conf
  63. for conf in conf/conf.d/*; do
  64. [ -f ${conf} ] && . ${conf}
  65. done
  66. . /scripts/functions
  67.  
  68. # Parse command line options
  69. for x in $(cat /proc/cmdline); do
  70. case $x in
  71. init=*)
  72. init=${x#init=}
  73. ;;
  74. root=*)
  75. ROOT=${x#root=}
  76. case $ROOT in
  77. LABEL=*)
  78. ROOT="${ROOT#LABEL=}"
  79.  
  80. # support any / in LABEL= path (escape to \x2f)
  81. case "${ROOT}" in
  82. */*)
  83. if command -v sed >/dev/null 2>&1; then
  84. ROOT="$(echo ${ROOT} | sed 's,/,\\x2f,g')"
  85. else
  86. if [ "${ROOT}" != "${ROOT#/}" ]; then
  87. ROOT="\x2f${ROOT#/}"
  88. fi
  89. if [ "${ROOT}" != "${ROOT%/}" ]; then
  90. ROOT="${ROOT%/}\x2f"
  91. fi
  92. IFS='/'
  93. newroot=
  94. for s in $ROOT; do
  95. newroot="${newroot:+${newroot}\\x2f}${s}"
  96. done
  97. unset IFS
  98. ROOT="${newroot}"
  99. fi
  100. esac
  101. ROOT="/dev/disk/by-label/${ROOT}"
  102. ;;
  103. UUID=*)
  104. ROOT="/dev/disk/by-uuid/${ROOT#UUID=}"
  105. ;;
  106. /dev/nfs)
  107. [ -z "${BOOT}" ] && BOOT=nfs
  108. ;;
  109. esac
  110. ;;
  111. rootflags=*)
  112. ROOTFLAGS="-o ${x#rootflags=}"
  113. ;;
  114. rootfstype=*)
  115. ROOTFSTYPE="${x#rootfstype=}"
  116. ;;
  117. rootdelay=*)
  118. ROOTDELAY="${x#rootdelay=}"
  119. case ${ROOTDELAY} in
  120. *[![:digit:].]*)
  121. ROOTDELAY=
  122. ;;
  123. esac
  124. ;;
  125. resumedelay=*)
  126. RESUMEDELAY="${x#resumedelay=}"
  127. ;;
  128. loop=*)
  129. LOOP="${x#loop=}"
  130. ;;
  131. loopflags=*)
  132. LOOPFLAGS="-o ${x#loopflags=}"
  133. ;;
  134. loopfstype=*)
  135. LOOPFSTYPE="${x#loopfstype=}"
  136. ;;
  137. cryptopts=*)
  138. cryptopts="${x#cryptopts=}"
  139. ;;
  140. nfsroot=*)
  141. NFSROOT="${x#nfsroot=}"
  142. ;;
  143. netboot=*)
  144. NETBOOT="${x#netboot=}"
  145. ;;
  146. ip=*)
  147. IP="${x#ip=}"
  148. ;;
  149. boot=*)
  150. BOOT=${x#boot=}
  151. ;;
  152. ubi.mtd=*)
  153. UBIMTD=${x#ubi.mtd=}
  154. ;;
  155. resume=*)
  156. RESUME="${x#resume=}"
  157. ;;
  158. resume_offset=*)
  159. resume_offset="${x#resume_offset=}"
  160. ;;
  161. noresume)
  162. noresume=y
  163. ;;
  164. panic=*)
  165. panic="${x#panic=}"
  166. case ${panic} in
  167. *[![:digit:].]*)
  168. panic=
  169. ;;
  170. esac
  171. ;;
  172. quiet)
  173. quiet=y
  174. ;;
  175. ro)
  176. readonly=y
  177. ;;
  178. rw)
  179. readonly=n
  180. ;;
  181. debug)
  182. debug=y
  183. quiet=n
  184. exec >/dev/.initramfs/initramfs.debug 2>&1
  185. set -x
  186. ;;
  187. debug=*)
  188. debug=y
  189. quiet=n
  190. set -x
  191. ;;
  192. break=*)
  193. break=${x#break=}
  194. ;;
  195. break)
  196. break=premount
  197. ;;
  198. blacklist=*)
  199. blacklist=${x#blacklist=}
  200. ;;
  201. netconsole=*)
  202. netconsole=${x#netconsole=}
  203. ;;
  204. BOOTIF=*)
  205. BOOTIF=${x#BOOTIF=}
  206. ;;
  207. hwaddr=*)
  208. BOOTIF=${x#BOOTIF=}
  209. ;;
  210. esac
  211. done
  212.  
  213. if [ -n "${noresume}" ]; then
  214. export noresume
  215. unset resume
  216. else
  217. resume=${RESUME:-}
  218. fi
  219.  
  220. [ -n "${netconsole}" ] && modprobe netconsole netconsole="${netconsole}"
  221.  
  222. maybe_break top
  223.  
  224. # export BOOT variable value for compcache,
  225. # so we know if we run from casper
  226. export BOOT
  227.  
  228. # Don't do log messages here to avoid confusing usplash
  229. run_scripts /scripts/init-top
  230.  
  231. maybe_break modules
  232. [ "$quiet" != "y" ] && log_begin_msg "Loading essential drivers"
  233. load_modules
  234. [ "$quiet" != "y" ] && log_end_msg
  235.  
  236. maybe_break premount
  237. [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/init-premount"
  238. run_scripts /scripts/init-premount
  239. [ "$quiet" != "y" ] && log_end_msg
  240.  
  241. maybe_break mount
  242. log_begin_msg "Mounting root file system"
  243. . /scripts/${BOOT}
  244. parse_numeric ${ROOT}
  245. maybe_break mountroot
  246. mountroot
  247. log_end_msg
  248.  
  249. maybe_break bottom
  250. [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/init-bottom"
  251. run_scripts /scripts/init-bottom
  252. [ "$quiet" != "y" ] && log_end_msg
  253.  
  254. # Move virtual filesystems over to the real filesystem
  255. mount -n -o move /sys ${rootmnt}/sys
  256. mount -n -o move /proc ${rootmnt}/proc
  257.  
  258. validate_init() {
  259. checktarget="${1}"
  260.  
  261. # Work around absolute symlinks
  262. if [ -d "${rootmnt}" ] && [ -h "${rootmnt}${checktarget}" ]; then
  263. case $(readlink "${rootmnt}${checktarget}") in /*)
  264. checktarget="$(chroot ${rootmnt} readlink ${checktarget})"
  265. ;;
  266. esac
  267. fi
  268.  
  269. # Make sure the specified init can be executed
  270. if [ ! -x "${rootmnt}${checktarget}" ]; then
  271. return 1
  272. fi
  273.  
  274. # Upstart uses /etc/init as configuration directory :-/
  275. if [ -d "${rootmnt}${checktarget}" ]; then
  276. return 1
  277. fi
  278. }
  279.  
  280. # Check init bootarg
  281. if [ -n "${init}" ]; then
  282. if ! validate_init "$init"; then
  283. echo "Target filesystem doesn't have requested ${init}."
  284. init=
  285. fi
  286. fi
  287.  
  288. # Common case: /sbin/init is present
  289. if [ ! -x "${rootmnt}/sbin/init" ]; then
  290. # ... if it's not available search for valid init
  291. if [ -z "${init}" ] ; then
  292. for inittest in /sbin/init /etc/init /bin/init /bin/sh; do
  293. if validate_init "${inittest}"; then
  294. init="$inittest"
  295. break
  296. fi
  297. done
  298. fi
  299.  
  300. # No init on rootmount
  301. if ! validate_init "${init}" ; then
  302. panic "No init found. Try passing init= bootarg."
  303. fi
  304. fi
  305.  
  306. maybe_break init
  307.  
  308. # don't leak too much of env - some init(8) don't clear it
  309. # (keep init, rootmnt)
  310. unset debug
  311. unset MODPROBE_OPTIONS
  312. unset DPKG_ARCH
  313. unset ROOTFLAGS
  314. unset ROOTFSTYPE
  315. unset ROOTDELAY
  316. unset ROOT
  317. unset IP
  318. unset BOOT
  319. unset BOOTIF
  320. unset UBIMTD
  321. unset blacklist
  322. unset break
  323. unset noresume
  324. unset panic
  325. unset quiet
  326. unset readonly
  327. unset resume
  328. unset resume_offset
  329.  
  330. # Chain to real filesystem
  331. exec run-init ${rootmnt} ${init} "$@" <${rootmnt}/dev/console >${rootmnt}/dev/console 2>&1
  332. panic "Could not execute run-init."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement