Advertisement
Guest User

/etc/runit/1

a guest
Jan 20th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.86 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # /etc/runit/1:  System one time tasks (Stage 1).
  4. #
  5. # Version:       (#) 1.00  2009-12-07 (MAF)
  6. #                (#) 1.01  2010-03-12 (MAF)
  7. #                (#) 1.02  2010-06-16 (MAF)
  8. #                (#) 1.03  2010-06-17 (MAF)
  9. #                (#) 1.04  2010-06-22 (MAF)
  10. #                (#) 1.05  2010-06-30 (MAF)
  11. #                (#) 1.06  2010-07-02 (MAF)
  12. #                (#) 2.00  2010-07-09 (MAF)
  13. #                (#) 2.01  2010-07-21 (MAF)
  14. #                (#) 2.02  2010-07-29 (MAF)
  15. #                (#) 2.03  2010-07-30 (MAF)
  16. #                (#) 2.04  2010-12-21 (MAF)
  17. #                (#) 2.05  2011-03-30 (MAF)
  18. #                (#) 2.06  2011-04-14 (MAF)
  19. #                (#) 2.07  2011-05-11 (MAF)
  20. #                (#) 2.08  2011-06-03 (MAF)
  21. #                (#) 2.09  2011-06-07 (MAF)
  22. #                (#) 2.10  2012-01-14 (MAF)
  23. #                (#) 2.11  2012-02-02 (MAF)
  24. #                (#) 2.12  2012-02-28 (MAF)
  25. #                (#) 2.13  2012-03-01 (MAF)
  26. #                (#) 2.14  2012-04-06 (MAF)
  27. #
  28. # Author:        Matias A. Fonzo, <selk@dragora.org>.
  29. #
  30. # Under the terms of the GNU General Public License.
  31.  
  32. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  33. export PATH
  34.  
  35. # Functions.
  36.  
  37. # A function for display a message:
  38. msg() { printf '%s\n' "$@"; }
  39.  
  40. msg "Mounting kernel-based filesystems ..."
  41. mount -vn -t proc  proc  /proc
  42. mount -vn -t sysfs sysfs /sys
  43.  
  44. # Initialize the dynamic device directory system (udev) #
  45.  
  46. # Sanity creation to mount:
  47. mkdir -p --mode=0755 /run /dev/pts /dev/shm
  48.  
  49. # Mount tmpfs over /run (required by udev):
  50. mount -vn -t tmpfs tmpfs /run -o mode=0755
  51.  
  52. # Mount devpts over /dev/pts:
  53. mount -vn -t devpts devpts /dev/pts -o mode=0620,gid=4
  54.  
  55. # Mount POSIX shared memory:
  56. mount -vn -t tmpfs none /dev/shm
  57.  
  58. # Udev handles the uevents itself, so we don't need to have
  59. # the kernel call out to any binary in response to them:
  60. if [ -f /sys/kernel/uevent_helper ]; then
  61.   msg "" >/sys/kernel/uevent_helper
  62. elif [ -f /proc/sys/kernel/hotplug ]; then
  63.   msg "" >/proc/sys/kernel/hotplug
  64. fi
  65.  
  66. # Start the daemon:
  67. msg "Starting udev daemon:  /sbin/udevd --daemon"
  68. udevd --daemon
  69.  
  70. # Create the "/dev/root" symlink:
  71. mkdir -p --mode=0755 /run/udev/rules.d
  72. eval $(udevadm info --export --device-id-of-file=/ --export-prefix=ROOT_)
  73. msg 'ACTION=="add|change", SUBSYSTEM=="block", ENV{MAJOR}=="'$ROOT_MAJOR'", ENV{MINOR}=="'$ROOT_MINOR'", SYMLINK+="root"' \
  74.  >/run/udev/rules.d/61-dev-root-link.rules
  75.  
  76. # Request device uevents to replay events at system coldplug:
  77. udevadm trigger --type=subsystems
  78. udevadm trigger --type=devices
  79. # End of Initialize the dynamic device directory system (udev) #
  80.  
  81. # Initialize LVM2 (Logical Volume Manager version 2):
  82. #
  83. # See the backup directory:
  84. if [ -r /etc/lvm/lvm.conf ]; then
  85.   BACKUP_DIR="$(awk '/backup_dir/ && !/^#/' /etc/lvm/lvm.conf | awk -F '["]' '{ print $2 }')"
  86. else  # This is the default directory:
  87.   BACKUP_DIR=/etc/lvm/backup
  88. fi
  89. # Check the backup directory:
  90. if [ -d "$BACKUP_DIR" ]; then  # Load the device-mapper module:
  91.   modprobe -q dm-mod
  92.   # Scan all volume groups and make the attributes:
  93.   vgscan --mknodes --ignorelockingfailure 1>/dev/null 2>&1 && \
  94.    vgchange -ay --ignorelockingfailure 1>/dev/null 2>&1
  95. fi
  96.  
  97. # Check if the filesystem is still in read-only mode:
  98. if touch /if-not-read-only$$ 2>/dev/null ; then
  99.   rm -f /if-not-read-only$$
  100.   mount -vn -o remount,ro /
  101. fi
  102.  
  103. msg "*** Checking the root filesystem ..."
  104. if [ ! -e /etc/fastboot ]; then
  105.   if [ -r /etc/forcefsck ]; then
  106.     FORCE_CHK=-f
  107.   fi
  108.  
  109.   fsck -a $FORCE_CHK -C /
  110.   ERR_CODE=$?
  111.  
  112.   if [ $ERR_CODE -eq 0 ]; then
  113.     msg "*** Checking the rest of the filesystems ..."
  114.     fsck -a $FORCE_CHK -R -A -T -C
  115.   fi
  116.  
  117.   if [ $ERR_CODE -eq 2 ] || [ $ERR_CODE -eq 3 ]; then
  118.     msg \
  119.      "o=====================CHECK FAILED======================o" \
  120.      "An error ocurred while the root filesystem was checked."   \
  121.      ""                                                          \
  122.      "The system should be rebooted or halted."                  \
  123.      ""                                                          \
  124.      "ERROR CODE: $ERR_CODE"                                     \
  125.      "o=======================================================o" \
  126.      ""
  127.     read -p "Press ENTER to reboot (or halt the system)..."
  128.     exit 100;
  129.   fi
  130.  
  131.   if [ $ERR_CODE -gt 3 ] && [ $ERR_CODE -lt 16 ]; then
  132.     msg \
  133.      "o=====================CHECK FAILED======================o" \
  134.      "An error ocurred while the root filesystem was checked."   \
  135.      ""                                                          \
  136.      "This is your chance to repair the system manually."        \
  137.      "For help, check the *fsck utilities according to"          \
  138.      "your filesystem."                                          \
  139.      ""                                                          \
  140.      "Note:"                                                     \
  141.      ""                                                          \
  142.      "The system is currently mounted in read-only mode."        \
  143.      "To remount the system in read-write mode, type:"           \
  144.      ""                                                          \
  145.      "mount -vn -o remount,rw /"                                 \
  146.      ""                                                          \
  147.      "ERROR CODE: $ERR_CODE"                                     \
  148.      "o=======================================================o" \
  149.      ""
  150.     read -p "Press ENTER to continue..."
  151.     exec env - PS1='Repairme:\w# ' /bin/su --shell /bin/sh --login
  152.   fi
  153.  
  154.   if [ $ERR_CODE -ge 16 ]; then
  155.     msg "" "Unexpected fsck error: $ERR_CODE" ""
  156.   fi
  157. else
  158.   msg "/etc/fastboot is present: Dodging checking."
  159. fi
  160.  
  161. msg "Remounting root filesystem in read-write mode ..."
  162. mount -vn -o remount,rw /
  163.  
  164. # In a modern GNU/Linux distribution,
  165. # /etc/mtab is a symlink to /proc/mounts:
  166. rm -f /etc/mtab*
  167. ln -s /proc/mounts /etc/mtab
  168.  
  169. # Enable devices for paging and swapping are to take place:
  170. if [ -f /etc/rc.d/rc.swap ]; then
  171.   sh /etc/rc.d/rc.swap start
  172. fi
  173.  
  174. # Load kernel parameters:
  175. if [ -r /etc/sysctl.conf ]; then
  176.   msg "[-] Loading kernel parameters via /etc/sysctl.conf ..."
  177.   sysctl -q -p /etc/sysctl.conf
  178. fi
  179.  
  180. # Refresh kernel modules:
  181. msg "Refreshing kernel modules:  depmod -A"
  182. depmod -A
  183.  
  184. # Load modules from /etc/rc.d/rc.modules:
  185. if [ -L /etc/rc.d/rc.modules ] && [ -e /etc/rc.d/rc.modules ]; then
  186.   RC_MODULES="$(readlink /etc/rc.d/rc.modules)"
  187.   msg "[-] Loading modules from /etc/rc.d/${RC_MODULES} ..."
  188.   . /etc/rc.d/${RC_MODULES}
  189. fi
  190.  
  191. # Set ISA Plug-And-Play devices (if any):
  192. if [ -x /sbin/isapnp ]; then
  193.   if [ -r /etc/isapnp.conf ]; then
  194.     /sbin/isapnp /etc/isapnp.conf
  195.   else
  196.     if [ -x /sbin/pnpdump ]; then
  197.       msg "Detecting ISA Plug-And-Play devices (wait) ..."
  198.       /sbin/pnpdump >/etc/isapnp.conf
  199.  
  200.       msg "Establishing ISA Plug-And-Play devices ..."
  201.       /sbin/isapnp /etc/isapnp.conf
  202.     fi
  203.   fi
  204. fi
  205.  
  206. # Mount USB filesystem (if supported):
  207. msg "Mounting usbfs (if supported) ..."
  208. if grep -qw usbfs /proc/filesystems ; then
  209.   if ! grep -wq usbfs /proc/mounts ; then
  210.     if ! mount -f /proc/bus/usb 2>/dev/null ; then
  211.       mount -v -t usbfs usbfs /proc/bus/usb
  212.     else  # Mount from fstab:
  213.       mount -v /proc/bus/usb
  214.     fi
  215.   fi
  216. fi
  217.  
  218. # Try to mount FUSE:
  219. msg "Trying to mount fusectl ..."
  220. if modprobe -q fuse 2>/dev/null ; then
  221.   mount -v -t fusectl fusectl /sys/fs/fuse/connections 2>/dev/null
  222. fi
  223.  
  224. # Mount the Control Groups filesystem:
  225. msg "Trying to mount cgroups ..."
  226. if grep -q cgroup /proc/filesystems ; then
  227.   mkdir -p --mode=0755 /dev/cgroup
  228.   mount -v -t cgroup cpuset -o cpuset /dev/cgroup
  229. fi
  230.  
  231. msg "Mounting local filesystems:"
  232. mount -v -a -t no,fs,rootfs,usbfs,proc,sysfs,devpts
  233.  
  234. # Seek & destroy temporary files:
  235. find /var/lock /var/run \( ! -type d -a ! -name 'utmp' \) -delete
  236. rm -rf \
  237.  /etc/fastboot \
  238.  /etc/forcefsck \
  239.  /etc/nologin \
  240.  /tmp/.ICE-unix/* \
  241.  /tmp/.X11-unix/* \
  242.  /tmp/.*-ICE-* \
  243.  /tmp/.Xauth* \
  244.  /tmp/.X?-lock \
  245.  /tmp/serverauth.* \
  246.  /tmp/cron.* \
  247.  /tmp/pulse-* \
  248.  /tmp/gpg-* \
  249.  /tmp/orbit-* \
  250.  /tmp/mc-* \
  251.  /tmp/gnash-cookies.*
  252.  
  253. # Initialize random number generator #
  254. msg "/etc/random-seed:  Initializing random number generator ..."
  255. if [ -r /etc/random-seed ]; then
  256.   cat /etc/random-seed 1>/dev/urandom
  257. else
  258.   touch /etc/random-seed
  259. fi
  260. chmod 0600 /etc/random-seed
  261.  
  262. # Set the pool size:
  263. printf -v bytes "$(sysctl -n -e kernel.random.poolsize)"
  264.  
  265. # Test variable to assign a new value if needed:
  266. if [ "$bytes" = "" ]; then
  267.   bytes=1024
  268. fi
  269.  
  270. # Save the seed file:
  271. dd if=/dev/urandom of=/etc/random-seed count=1 bs="$bytes" 2>/dev/null
  272. # End of Initialize random number generator #
  273.  
  274. # Save dmesg output:
  275. dmesg &>/var/log/dmesg
  276.  
  277. # Update shared libraries:
  278. msg "Updating recent shared libraries:  ldconfig"
  279. ldconfig
  280.  
  281. touch /etc/runit/stopit
  282. chmod 0 /etc/runit/stopit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement