initNEW 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/sh
  2. #(c) Copyright Barry Kauler 2010, bkhome.org
  3. #Lesser GPL license v2 (/usr/share/doc/legal/lgpl-2.1.txt
  4. #note, 3builddistro (in Woof) renames rootfs-skeleton/sbin/initNEW to init.
  5. #perform a f.s. check if file /fsckme.flg exits, otherwise normal startup.
  6. #/etc/rc.d/rc.sysinit creates /fsckme.flg at bootup, rc.shutdown deletes it.
  7. #this script is /sbin/init in the main Puppy f.s., which is the very first to run.
  8. #in the case of not a full hd install, this script just passes execution to busybox init.
  9. #100911 fsckme.err filename changed to fsckme.flg. see also init (initramfs), rc.sysinit, rc.shutdown.
  10. #101020 no longer have /etc/modprobe.conf.
  11. #120408 support minit
  12. #120516 gentoo: libgcc_s.so.1 in wrong place.
  13. #120627 check ramdisk big enough.
  14. #120716 revert 120627, /sys not yet loaded. do it a different way. fix for weird 'tr' missing.
  15. #120716 raspi: no battery-backed hardware clock, so date is wrong, workaround. fixes for rc.sysinit in ramdisk.
  16. #120717 f.s. maximal mount count reached, perform f.s. check.
  17. #120718 try force unmount.
  18. #120729 needed to unmount /old_root/dev, as raspi kernel configured to automount fs on /dev.
  19. #130203 /bin/probedisk2 now /bin/probedisk only.
  20. #130418 support kmod (replaces module-init-tools).
  21. INITEXE='/bin/busybox init'
  22. [ -f /sbin/minit ] && INITEXE='/sbin/minit'
  23. [ -d /initrd ] && exec ${INITEXE} #not a full hd install.
  24. [ ! -e /fsckme.flg ] && exec ${INITEXE} #no error.
  25. [ ! -f /var/local/ram_size_bytes ] && exec ${INITEXE} #created in rc.sysinit
  26. if [ `cat /var/local/ram_size_bytes` -lt 20000 ];then #was 32768
  27. rm -f /fsckme.flg
  28. exec ${INITEXE} #unable to do f.s. check.
  29. fi
  30. PATH='/bin:/sbin'
  31. export LANG=C
  32. #only come here if full hd install, and previous improper shutdown.
  33. PDEV1="`cat /fsckme.flg | cut -f 1 -d ','`" #file format: sda1,ext3,
  34. DEV1FS="`cat /fsckme.flg | cut -f 2 -d ','`"
  35. CHECK="`cat /fsckme.flg | cut -f 3 -d ','`" #120717 see rc.shutdown.
  36. case $DEV1FS in
  37. ext2|ext3|ext4)
  38. echo -e "\\033[1;31m" >/dev/console #34=blue, 33=yellow, 32=green, 31=red, 35=purple, 36=aquablue, 38=black.
  39. if [ "$CHECK" = "MAXIMAL" ];then #120717
  40. echo "Performing filesystem check of '${PDEV1}', maximal mount count" > /dev/console
  41. else
  42. echo "Performing filesystem check of '${PDEV1}' after improper shutdown" > /dev/console
  43. fi
  44. echo -n "NOTE: no progress indication, may take awhile, please wait patiently..."
  45. echo -e "\\033[0;39m" >/dev/console
  46. echo >/dev/console
  47. busybox mount -t proc none /proc
  48. busybox mount -o remount,rw /
  49. rm -f /fsckme.flg
  50. #creat a ramdisk...
  51. #note, Puppy kernels are compiled with 13MB ramdisk, so need to be careful not to fill it up...
  52. echo 'Creating a ramdisk and populating with a Linux environment...' >/dev/console
  53. mkfs.ext2 -q -m 0 /dev/ram0
  54. mkdir -p /mnt/ram0
  55. busybox mount -t ext2 /dev/ram0 /mnt/ram0
  56. #populate the ramdisk...
  57. mkdir -p /mnt/ram0/old_root
  58. mkdir /mnt/ram0/bin
  59. for ONEBIN in "[" "[[" ash autologinroot basename busybox cat chmod chown clear cp cut date dc dd df dirname dmesg du ed expr false find free grep head kill ln login ls lsmod mkdir mknod more mv nice ntfs-3g pidof pwd readlink rev rm rmdir sed sleep sort stat su sync tail tar touch tr true uname usleep waitmax wc which xargs
  60. do
  61. if [ -e /bin/$ONEBIN ];then
  62. cp -a /bin/$ONEBIN /mnt/ram0/bin/
  63. else
  64. ln -s busybox /mnt/ram0/bin/$ONEBIN
  65. fi
  66. done
  67. ln -snf busybox /mnt/ram0/bin/ps
  68. ln -snf busybox /mnt/ram0/bin/rdev
  69. ln -snf busybox /mnt/ram0/bin/sh
  70. ln -snf busybox /mnt/ram0/bin/mount
  71. ln -snf busybox /mnt/ram0/bin/umount
  72. mkdir /mnt/ram0/sbin
  73. for ONEBIN in depmod dumpe2fs e2fsck elspci fdisk freeramdisk fsck.ext2 fsck.ext3 fsck.ext4 getty guess_fstype halt hdparm hotplug2stdout hotplug2stdout_notimeout hwclock ifconfig ifplugstatus insmod klogd kmod ldconfig loadkmap losetup lsmod mingetty mount.ntfs-3g pivot_root probedisk probepart swapon #130418
  74. do
  75. if [ -e /sbin/$ONEBIN ];then
  76. cp -a /sbin/$ONEBIN /mnt/ram0/sbin/
  77. else
  78. ln -s ../bin/busybox /mnt/ram0/sbin/$ONEBIN
  79. fi
  80. done
  81. ln -snf ../bin/busybox /sbin/chroot #must match
  82. ln -snf ../bin/busybox /mnt/ram0/sbin/chroot #must match
  83. ln -snf ../bin/busybox /mnt/ram0/sbin/init
  84. ln -snf ../bin/busybox /mnt/ram0/sbin/poweroff
  85. ln -snf ../bin/busybox /mnt/ram0/sbin/reboot
  86. mkdir /mnt/ram0/etc
  87. #101020 no longer have /etc/modprobe.conf...
  88. for ONEFILE in codepage DISTRO_SPECS eventmanager fdprm floppy fontmap group gshadow host.conf hostname hosts hosts.allow hosts.deny inittab inoutrc issue keymap localtime mailcap mime.types mke2fs.conf modules.conf mtab networks nscd.conf nsswitch.conf passwd printcap protocols services shadow
  89. do
  90. cp -a /etc/$ONEFILE /mnt/ram0/etc/ 2>/dev/null
  91. done
  92. cp -a /etc/modprobe.d /mnt/ram0/etc/ 2>/dev/null #101020
  93. echo '/lib' > /mnt/ram0/etc/ld.so.conf
  94. echo 'export PATH="/bin:/sbin"
  95. export LD_LIBRARY_PATH="/lib"
  96. ulimit -c 0
  97. umask 022
  98. export USER=root
  99. export LOGNAME=root
  100. export TERM=linux' > /mnt/ram0/etc/profile
  101. LOCALTIME="/usr/share`readlink /etc/localtime | sed 's%^.*/zoneinfo%/zoneinfo%'`"
  102. LOCALTDIR="`dirname $LOCALTIME`"
  103. mkdir -p /mnt/ram0/$LOCALTDIR
  104. cp -a $LOCALTIME /mnt/ram0/$LOCALTDIR/
  105. mkdir /mnt/ram0/etc/rc.d
  106. #120716 raspi: no battery-backed hardware clock, so date is wrong...
  107. mkdir -p /mnt/ram0/var/local/
  108. #120718 find the date...
  109. HWDATE0="`hwclock --show 2>/dev/null`" #ex: Fri 29 Jun 2012 07:45:28 AM WST -0.725833 seconds
  110. if [ "$HWDATE0" = "" ];then
  111. if [ -f /var/local/shutdown_date_saved ];then #see /etc/rc.d/rc.shutdown
  112. HWDATE1="`cat /var/local/shutdown_date_saved`"
  113. else
  114. #either of these formats can set the date: '29 JUN 2012 10:00:00' '2012-06-28 16:20:08'
  115. #120718 busybox date says '29 JUN 2012 10:00:00' invalid format
  116. HWDATE1="`stat --format=%z /etc/DISTRO_SPECS | cut -f 1 -d '.' | cut -f 1,2 -d ' '`" #creation date of build. ex: '2012-06-28 16:20:08'
  117. fi
  118. HWDATE0="$HWDATE1"
  119. fi
  120. echo "#!/bin/sh
  121. echo 'Now running in ramdisk, performing filesystem check of ${PDEV1}...'
  122. /bin/busybox mount -t proc none /proc
  123. HWDATE=\"\`/sbin/hwclock --show 2>/dev/null\`\" #ex: Fri 29 Jun 2012 07:45:28 AM WST -0.725833 seconds
  124. if [ \"${HWDATE0}\" != \"\" ];then
  125. /bin/date -s \"${HWDATE0}\"
  126. else
  127. /sbin/hwclock --hctosys --localtime
  128. fi
  129. /bin/busybox umount /old_root/dev 2>/dev/null #if kernel configured to mount fs on /dev
  130. /bin/busybox umount /old_root
  131. if [ \$? -ne 0 ];then
  132. echo 'Not so good, was unable to unmount ${PDEV1}. Trying forced unmount...'
  133. /bin/busybox umount -f /old_root #120718 try force unmount.
  134. if [ \$? -ne 0 ];then
  135. echo 'Unable to unmount, aborting filesystem check.'
  136. else
  137. echo 'Unmount successful, continuing with f.s. check...'
  138. UMOUNTFLAG='yes'
  139. fi
  140. else
  141. UMOUNTFLAG='yes'
  142. fi
  143. if [ \"\$UMOUNTFLAG\" = \"yes\" ];then
  144. /sbin/e2fsck -p -f /dev/${PDEV1}
  145. /bin/busybox sync
  146. echo 'Filesystem check completed!'
  147. fi
  148. echo 'The computer now has to be restarted.'
  149. echo 'Computer will reboot in 4 minutes, else press ENTER for immediate reboot.'
  150. echo \"(Developers only: press 'c' then ENTER for shell in ramdisk)\"
  151. echo
  152. echo -n 'Press ENTER key to reboot:'
  153. read -t 240 abcdef
  154. [ \"\$abcdef\" = \"c\" ] && exit #go to shell in ramdisk.
  155. /bin/busybox umount /proc
  156. /bin/busybox reboot" > /mnt/ram0/etc/rc.d/rc.sysinit
  157. chmod 755 /mnt/ram0/etc/rc.d/rc.sysinit
  158. cp -a /dev /mnt/ram0/
  159. mkdir /mnt/ram0/lib
  160. cp -a /lib/lib*.so* /mnt/ram0/lib/
  161. cp -a /lib/ld*.so* /mnt/ram0/lib/
  162. [ ! -e /mnt/ram0/lib/libgcc_s.so.1 ] && cp -a -f --remove-destination /usr/lib/libgcc_s.so.* /mnt/ram0/lib/ 2>/dev/null #120516 gentoo fix.
  163. cp -a /lib/e2initrd_helper /mnt/ram0/lib/ 2>/dev/null #dunno what this is.
  164. mkdir /mnt/ram0/tmp
  165. mkdir -p /mnt/ram0/var/run
  166. mkdir /mnt/ram0/proc
  167. mkdir -p /mnt/ram0/usr/share
  168. cp -a /usr/share/terminfo /mnt/ram0/usr/share/
  169. #cleanup files...
  170. rm -f /var/log/X*
  171. rm -rf /tmp/*
  172. rm -f /var/lock/LCK*
  173. rm -f /var/run/*.pid
  174. rm -rf /root/tmp 2>/dev/null
  175. rm -rf /root/.thumbnails/* 2>/dev/null
  176. #120716 weird bug raspi, can't find 'tr', so use 'busybox tr'...
  177. rm -f `find /var -mount -type s | busybox tr '\n' ' '` #remove sockets.
  178. rm -f `find /var -mount -type p | busybox tr '\n' ' '` #remove pipes.
  179. sync
  180. #pivot_root to the ramdisk...
  181. echo 'Performing a pivot_root to the ramdisk...' >/dev/console
  182. busybox mount -o remount,ro /
  183. busybox umount /proc
  184. cd /mnt/ram0
  185. pivot_root . old_root
  186. exec chroot . sh -c "exec /bin/busybox init" <dev/console >dev/console 2>&1
  187. #...the ramdisk is setup so that /etc/rc.d/rc.sysinit will run, which does the f.s. check.
  188. ;;
  189. *)
  190. echo -e "\\033[1;31m" >/dev/console #34=blue, 33=yellow, 32=green, 31=red, 35=purple, 36=aquablue, 38=black.
  191. echo -n "Previous improper shutdown, sorry can only do f.s. check of ext2/3/4. Rebooting..." > /dev/console
  192. echo -e "\\033[0;39m" >/dev/console
  193. sleep 5
  194. exec ${INITEXE}
  195. ;;
  196. esac
  197. ###END###