Guest User

initfile

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