init.in 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #!/bin/sh
  2. PATH=/bin:/usr/bin:/sbin:/usr/sbin
  3. export PATH
  4. problem()
  5. {
  6. printf "Encountered a problem!\n\nDropping you to a shell.\n\n"
  7. sh
  8. }
  9. no_device()
  10. {
  11. printf "The device %s, which is supposed to contain the\n" $1
  12. printf "root file system, does not exist.\n"
  13. printf "Please fix this problem and exit this shell.\n\n"
  14. }
  15. no_mount()
  16. {
  17. printf "Could not mount device %s\n" $1
  18. printf "Sleeping forever. Please reboot and fix the kernel command line.\n\n"
  19. printf "Maybe the device is formatted with an unsupported file system?\n\n"
  20. printf "Or maybe filesystem type autodetection went wrong, in which case\n"
  21. printf "you should add the rootfstype=... parameter to the kernel command line.\n\n"
  22. printf "Available partitions:\n"
  23. }
  24. do_mount_root()
  25. {
  26. [ -n "$rootflags" ] && rootflags="$rootflags,"
  27. rootflags="$rootflags$ro"
  28. case "$root" in
  29. /dev/* ) device=$root ;;
  30. UUID=* ) eval $root; device="/dev/disk/by-uuid/$UUID" ;;
  31. LABEL=*) eval $root; device="/dev/disk/by-label/$LABEL" ;;
  32. "" ) echo "No root device specified." ; problem ;;
  33. esac
  34. while [ ! -b "$device" ] ; do
  35. no_device $device
  36. problem
  37. done
  38. if [ "$rootfstype" != "auto" ]; then
  39. rootfstype=`print_file_system $device`
  40. fi
  41. if [ "$rootfstype" == "no" ]; then
  42. no_mount $device
  43. cat /proc/partitions
  44. while true ; do sleep 10000 ; done
  45. fi
  46. [ -z "$overlayroot" ] && mountpoint=/.root || mountpoint=/.roRoot
  47. mkdir -p $mountpoint
  48. if ! mount -n -t "$rootfstype" -o "$rootflags" "$device" $mountpoint ; then
  49. no_mount $device
  50. cat /proc/partitions
  51. while true ; do sleep 10000 ; done
  52. else
  53. log_info_msg "Mounting root file system ${root} in read-only mode..."
  54. evaluate_retval
  55. fi
  56. if [ -n "$overlayroot" ]; then
  57. overlaydevice=$overlayroot; # /dev/mapper/<vg>/<lv>
  58. mkdir -p /mnt/OVL
  59. if ! mount -n "$overlaydevice" /mnt/OVL; then
  60. no_mount $overlaydevice
  61. cat /proc/partitions
  62. while true ; do sleep 10000 ; done
  63. else
  64. log_info_msg "Mounting overlay upper file system ${overlayroot}..."
  65. evaluate_retval
  66. fi
  67. mkdir -p /mnt/OVL/{upper,work}
  68. if ! mount -n -t overlay -o lowerdir=$mountpoint,upperdir=/mnt/OVL/upper,workdir=/mnt/OVL/work overlayrootfs /.root; then
  69. no_mount "overlayrootfs"
  70. cat /proc/partitions
  71. while true ; do sleep 10000 ; done
  72. else
  73. log_info_msg "Mounting overlay root filesystem..."
  74. evaluate_retval
  75. fi
  76. fi
  77. }
  78. do_install()
  79. {
  80. echo "Start installation on $root "
  81. umount /.root
  82. ro="rw"
  83. mkdir /etc
  84. ln -s /proc/self/mounts /etc/mtab
  85. do_mount_root
  86. for DIR in home proc sys dev srv mnt
  87. do
  88. echo -n "."
  89. if [ ! -d /.root/$DIR ]; then
  90. mkdir /.root/$DIR
  91. fi
  92. done
  93. echo -n "."
  94. ln -s lib /.root/lib64
  95. if [ ! -d /.root/tmp ]; then
  96. echo -n "."
  97. mkdir /.root/tmp
  98. chmod 1777 /.root/tmp
  99. fi
  100. for DIR in var run usr etc root lib sbin bin opt
  101. do
  102. echo -n "."
  103. if [ -f /.root/boot/$DIR.squashfs ]; then
  104. mkdir /DIR
  105. mount -o loop -t squashfs /.root/boot/$DIR.squashfs /DIR
  106. cp -a /DIR /.root/$DIR
  107. umount /DIR
  108. rm -r /DIR
  109. if [ "$DIR" == "root" ]; then
  110. chmod 750 /.root/root
  111. fi
  112. fi
  113. done
  114. # We need to check if entry exist in /etc/fstab
  115. if [ "`cat /.root/etc/fstab |sed "s/ \+/ /g"|sed "s/\t\+/ /g"|sed "s/ /:/g"|cut -d ":" -f1-2|grep $root|cut -d ":" -f2`" != "/" ]; then
  116. # blkid will tell us the file system
  117. TYPE=`print_file_system $root`
  118. echo -n "."
  119. echo "$root / $TYPE defaults 0 1" >> /.root/etc/fstab
  120. do_stop_udev
  121. fi
  122. umount /.root
  123. ro="ro"
  124. do_mount_root
  125. echo "Successfully Installed on device $root"
  126. }
  127. do_mount_in_ram()
  128. {
  129. # squasfs files are in ${1} on the initrd
  130. # RW folders
  131. modprobe squashfs
  132. modprobe loop
  133. for DIR in var run etc root
  134. do
  135. if [ -f ${1}/$DIR.squashfs ];then
  136. mkdir /DIR
  137. [ "$verbose" == "no" ] || echo "mount -o loop -t squashfs ${1}/$DIR.squashfs /DIR"
  138. mount -o loop -t squashfs ${1}/$DIR.squashfs /DIR || problem
  139. mkdir -p /$DIR > /dev/null 2>&1
  140. cp -a /DIR/* /$DIR/ > /dev/null 2>&1
  141. umount /DIR
  142. rm -r /DIR
  143. if [ "$DIR" == "root" ]; then
  144. chmod 750 /root
  145. fi
  146. fi
  147. done
  148. # RO folders
  149. for DIR in usr lib sbin bin opt
  150. do
  151. if [ -f ${1}/$DIR.squashfs ];then
  152. mkdir /$DIR > /dev/null 2>&1
  153. [ "$verbose" == "no" ] || echo "mount -o loop -t squashfs ${1}/$DIR.squashfs /$DIR"
  154. mount -o loop -t squashfs ${1}/$DIR.squashfs /$DIR || problem
  155. fi
  156. done
  157. }
  158. find_media() {
  159. EXPECT_LABEL="milis_calisan"
  160. for SYS in /sys/block/sd* /sys/block/sr* ; do
  161. [ "$verbose" == "no" ] || echo "Looking in $SYS for media"
  162. if [ ! -d "$SYS" ]; then continue; fi
  163. DEV=/dev/${SYS##*/}
  164. LABEL=`dd if=$DEV bs=1 skip=32808 count=32 2>/dev/null`
  165. if [ ${LABEL} == ${EXPECT_LABEL} ] 2>/dev/null ; then
  166. ln -s $DEV /dev/cdrom
  167. mkdir -p /media/cdrom 2>/dev/null
  168. [ "$verbose" == "no" ] || echo "mount -n /dev/cdrom /media/cdrom"
  169. mount -n /dev/cdrom /media/cdrom
  170. [ -d /media/cdrom/depot ] && echo $DEV > /tmp/depot
  171. break
  172. fi
  173. done
  174. }
  175. MainLoop(){
  176. while [ ! -f /root/boot ];
  177. do
  178. [ "$verbose" == "no" ] || echo "Launch /etc/rc.d/init.d/milis-kur ..."
  179. /etc/rc.d/init.d/milis-kur || problem
  180. done
  181. }
  182. # **********
  183. # Begin here
  184. # **********
  185. init=/sbin/init
  186. overlayroot=
  187. root=
  188. rootdelay=
  189. rootfstype=auto
  190. ro="ro"
  191. rootflags=
  192. device=
  193. verbose="no"
  194. . /lib/lsb/init-functions
  195. do_mount_virtualfs
  196. read -r cmdline < /proc/cmdline
  197. for param in $cmdline ; do
  198. case $param in
  199. init=* ) init=${param#init=} ;;
  200. root=* ) root=${param#root=} ;;
  201. rootdelay=* ) rootdelay=${param#rootdelay=} ;;
  202. overlayroot=*) overlayroot=${param#overlayroot=} ;;
  203. rootfstype=* ) rootfstype=${param#rootfstype=} ;;
  204. rootflags=* ) rootflags=${param#rootflags=} ;;
  205. ro ) ro="ro" ;;
  206. rw ) ro="rw" ;;
  207. quiet ) export verbose="no" ;;
  208. testiso ) export verbose="yes" ;;
  209. esac
  210. done
  211. if [ -n "$rootdelay" ] && [ -f /sbin/mdadm ]; then
  212. /sbin/mdadm --auto-detect
  213. ### First we activate lvm if possible
  214. if [ -f /sbin/lvm ]; then
  215. /sbin/lvm vgchange --ignorelockingfailure -ay
  216. if [ $? = 0 ]; then
  217. /sbin/lvm vgscan --ignorelockingfailure --mknodes
  218. fi
  219. fi
  220. fi
  221. if [ -d /lib/modules ]; then
  222. do_start_localnet
  223. do_start_udev
  224. fi
  225. if [ -n "$rootdelay" ] ; then
  226. [ "$verbose" == "no" ] || echo "... wait $rootdelay seconds"
  227. sleep "$rootdelay"
  228. fi
  229. if [ "$root" == "" ]; then
  230. if [ ! -f /etc/rc.d/init.d/milis-kur ]; then
  231. find_media
  232. if [ ! -L /dev/cdrom ]; then
  233. [ "$verbose" == "no" ] || echo "Media not found yet, trying again in 5 sec"
  234. sleep 5
  235. find_media
  236. fi
  237. if [ -d /media/cdrom/isolinux/boot ]; then
  238. do_mount_in_ram /media/cdrom/isolinux/boot
  239. else
  240. do_mount_in_ram /boot
  241. fi
  242. else
  243. do_mount_in_ram /boot
  244. fi
  245. if [ ! -f /etc/rc.d/init.d/milis-kur ]; then
  246. no_mount
  247. sh
  248. else
  249. if [ "$verbose" == "yes" ]; then
  250. echo "/etc/rc.d/init.d/milis-kur exit ..."
  251. ls -lt /etc/rc.d/init.d/milis-kur
  252. fi
  253. fi
  254. # TODO This doesn't works FIXME
  255. [ "$verbose" == "no" ] || echo "Stop udev ..."
  256. do_stop_udev
  257. [ "$verbose" == "no" ] || echo "Start udev ..."
  258. do_start_udev
  259. [ "$verbose" == "no" ] || echo "Wait 5 second ..."
  260. [ "$verbose" == "no" ] || sleep 5
  261. [ "$verbose" == "no" ] && clear
  262. MainLoop
  263. root=`cat /root/boot`
  264. [ "$verbose" == "no" ] || echo "Stop udev ..."
  265. do_stop_udev
  266. killall -w /sbin/udevd
  267. for DIR in usr lib sbin bin opt
  268. do
  269. [ "$verbose" == "no" ] || echo "umount /$DIR ..."
  270. umount /$DIR > /dev/null 2>&1
  271. done
  272. fi
  273. mkdir /.root
  274. do_mount_root
  275. if ! [ -d /.root/bin -a -d /.root/dev -a -d /.root/etc -a -d /.root/lib \
  276. -a -d /.root/sbin -a -d /.root/tmp -a -d /.root/root -a -d /.root/proc \
  277. -a -d /.root/usr -a -d /.root/var -a -d /.root/run -a -d /.root/sys ]; then
  278. do_install
  279. fi
  280. do_stop_udev
  281. cd /.root
  282. exec switch_root /.root "$init" "$@" > /dev/null 2>&1