]> Pileus Git - mkinit/commitdiff
Adding initial import of mkinit
authorAndy Spencer <andy753421@gmail.com>
Tue, 13 Oct 2009 22:07:07 +0000 (22:07 +0000)
committerAndy Spencer <andy753421@gmail.com>
Tue, 13 Oct 2009 22:07:07 +0000 (22:07 +0000)
COPYING [new file with mode: 0644]
README [new file with mode: 0644]
init.mk [new file with mode: 0644]
mkfile [new file with mode: 0644]
src/mkinit [new file with mode: 0755]
src/respawn [new file with mode: 0755]
src/service [new file with mode: 0755]

diff --git a/COPYING b/COPYING
new file mode 100644 (file)
index 0000000..8a8ff9b
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,17 @@
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..e6327b6
--- /dev/null
+++ b/README
@@ -0,0 +1,34 @@
+mkinit is a tiny init daemon that relies on mk for determining the order init
+services need to be executed. 
+
+
+Init scripts
+------------
+Init scripts take the form of mk rules contained in /etc/init.mk. Services are
+started by calling  the `start-<service>' rule and stopped by calling
+`stop-service'. Services should use `:Pservice -[ud]:' to determine whether the
+script needs to be invoked.
+
+For simple services consisting of a single start and stop command which no
+dependencies other than boot, the default start-stop rules can be used by
+setting `<service>_start_cmd' and `<service>_stop_cmd'.
+
+Dependencies are handled by adding `start-<dependency>' to the rules list of
+dependencies. When stopping a service, `stop-<service>' should be added to the
+list of dependencies for the dependency.
+
+
+Files
+-----
+COPYING - Licencing terms (MIT)
+README  - This file
+mkfile  - For [un]installing mkinit
+init.mk - Example database of init scripts
+
+bin/mkinit  - The actual init program it runs main loop and calls mk on
+              init.mk whenever services need to be started and stopped.
+bin/respawn - Used to respawn gettys and such
+bin/service - Set or query the state of a service
+
+
+# tw=80
diff --git a/init.mk b/init.mk
new file mode 100644 (file)
index 0000000..710acdd
--- /dev/null
+++ b/init.mk
@@ -0,0 +1,163 @@
+# Copyright (C) 2009 Andy Spencer
+# See COPYING for terms
+
+# Config
+PATH=/lib/mkinit/bin:/bin:/sbin:/usr/bin:/usr/sbin
+MKSHELL=/usr/lib/plan9/bin/rc
+NPROC=8
+
+# Example
+#start-test:VQPservice -u: start-foo
+#      echo starting test
+#      service -U $target
+#
+#stop-test:VQPservice -d: /
+#      echo stopping test
+#      service -D $target
+
+# Runlevels
+default:V: bare
+
+desktop:V: console `{echo start-^(alsa polipo spam sshd)}
+console:V: bare    `{echo start-^(at cron syslog hddtemp i8k hwclock swap)}
+bare:V:            `{echo start-^(mounts uevents hostname fsclean getty qingy localhost keymap)}
+
+# Initial setup/shutdown for mkinit
+boot:QVEPservice -u: /
+       echo Starting init
+       $P mount -o remount,rw /
+       rm -f /lib/mkinit/state/*
+       service -U $target
+
+# Kill all process, then remount and sync
+halt:QVE: stop-hwclock stop-alsa
+       echo Stopping init
+       rm -f /lib/mkinit/state/*
+       
+       echo TERMinating all processes
+       $P pkill -15 -vg0
+       for (i in 1 2 3 4 5)
+               $P pgrep -vg0 >/dev/null && $P sleep 1
+       
+       echo KILLing all processes
+       $P pkill  -9 -vg0
+       for (i in 1 2 3)
+               $P pgrep -vg0 >/dev/null && $P sleep 1
+       
+       $P mount -o remount,ro /
+       $P sync
+
+# Proc, mtab, udev, fstab
+start-mounts:QVPservice -u: boot
+       echo Starting mounts
+       $P mount -t proc proc /proc
+       $P cat /proc/mounts > /etc/mtab
+       $P udevd --daemon
+       $P mount -a 
+       service -U $target
+
+# Load kernel modules
+start-modules:QVEPservice -u: boot
+       echo Starting modules
+       $P modprobe uvesafb
+       service -U $target
+
+# Trigger udev uevents
+start-uevents:QVEPservice -u:  start-mounts
+       echo Starting uevents
+       $P udevadm trigger
+       $P udevadm settle '--timeout=10'
+       service -U $target
+
+# Clean out /tmp and /var/run directories
+start-fsclean:QVPservice -u: boot
+       echo Starting fsclean
+       $P rm -rf /tmp/* 
+       $P rm -rf /var/run/*
+       service -U $target
+
+# Spawn gettys for tty[456]
+start-getty:QVPservice -u: start-hostname
+       echo Starting getty
+       $P respawn /sbin/agetty 38400 tty4 linux &
+       $P respawn /sbin/agetty 38400 tty5 linux &
+       $P respawn /sbin/agetty 38400 tty6 linux &
+       service -U $target
+
+# Spawn qingys for tty[23]
+start-qingy:QVPservice -u: start-hostname start-modules start-uevents
+       echo Starting qingy
+       $P respawn /sbin/qingy tty2 &
+       $P respawn /sbin/qingy tty3 &
+       service -U $target
+
+# Keymap (us-cc = us with ctrl-capslock switched)
+keymap_start_cmd=loadkeys -u us-cc
+
+# Localhost
+localhost_start_cmd=ifconfig lo 127.0.0.1
+localhost_stop_cmd=ifconfig lo down
+
+# Set hostname
+hostname_start_cmd=hostname b
+
+# Kernel parameters
+sysctl_start_cmd=sysctl -p
+
+
+# Console
+# -------
+at_start_cmd=atd
+cron_start_cmd=cron
+hwclock_start_cmd=hwclock --hctosys --utc
+hwclock_stop_cmd=hwclock --systohc --utc
+swap_start_cmd=swapon -a
+swap_stop_cmd=swapoff -a
+start-syslog:QVPservice -u: start-mounts
+       echo Starting syslog;
+       $P syslog-ng
+       service -U $target
+start-hddtemp:QVPservice -u: start-localhost
+       echo Starting hddtemp
+       $P hddtemp -d -l 127.0.0.1 /dev/sda
+       service -U $target
+hddtemp_stop_cmd=pkill hddtemp
+
+
+# Desktop
+# -------
+alsa_start_cmd=alsactl restore
+alsa_stop_cmd=alsactl store
+sshd_start_cmd=/usr/sbin/sshd
+start-spam:QVPservice -u: start-localhost
+       echo Starting spam
+       $P spamd -d
+       service -U $target
+start-polipo:QVPservice -u: start-localhost
+       echo Starting poliop
+       $P polipo
+       service -U $target
+polipo_stop_cmd=pkill polipo
+
+
+# Library 
+# -------
+start-%:QVPservice -u: boot
+       if (~ $#($stem^_start_cmd) 0)
+               exit 0
+       echo Starting $stem
+       $P $($stem^_start_cmd)
+       service -U $target
+
+stop-%:QVPservice -d: /
+       if (~ $#($stem^_stop_cmd) 0)
+               exit 0
+       echo Stopping $stem
+       $P $($stem^_stop_cmd)
+       service -D $target
+
+zap-%:QVPservice -d: /
+       service -D $target
+
+status-%:QV:
+       service -q $target
diff --git a/mkfile b/mkfile
new file mode 100644 (file)
index 0000000..e762874
--- /dev/null
+++ b/mkfile
@@ -0,0 +1,20 @@
+# Copyright (C) 2009 Andy Spencer
+# See COPYING for terms
+
+install:V:
+       install -d \
+               $DESTDIR/etc \
+               $DESTDIR/sbin \
+               $DESTDIR/lib/mkinit/bin \
+               $DESTDIR/lib/mkinit/state
+       install -t $DESTDIR/lib/mkinit/bin \
+               ./src/{mkinit,service,respawn}
+       install -t $DESTDIR/etc  ./init.mk       
+       ln -sf $DESTDIR/lib/mkinit/bin/mkinit $DESTDIR/sbin
+
+uninstall:VE:
+       rm -rf /lib/mkinit/bin/
+       rm /lib/mkinit/cmd
+       rm /sbin/mkinit
+       rmdir /lib/mkinit/state/
+       rmdir /lib/mkinit/
diff --git a/src/mkinit b/src/mkinit
new file mode 100755 (executable)
index 0000000..c2b4ec0
--- /dev/null
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+# Copyright (C) 2009 Andy Spencer
+# See ../COPYING for terms
+
+echo
+echo "mkinit -- starting"
+
+function runamk {
+       /usr/lib/plan9/bin/mk \
+               -f /etc/init.mk \
+               -i -k "$@"
+}
+
+# Handle arguments
+TEST=false
+cmd=boot
+case "$1" in
+-t )
+       TEST=true
+       export P="true"
+       ;;
+-n )
+       cmd=""
+       ;;
+esac
+
+
+# Main loop
+echo "mkinit -- entering main loop"
+while true; do
+       case "$cmd" in
+       boot )
+               echo "mkinit -- booting"
+               if runamk -a && ! $TEST; then
+                       # booted successuflly, redirect input
+                       echo "skipping redirect"
+                       #pipe=/lib/mkinit/cmd
+                       #[ -p pipe ] || mkfifo $pipe
+                       #exec 0< $pipe
+               fi
+               ;;
+       halt )
+               echo "mkinit -- halting"
+               runamk "halt"
+               # exit
+               ;;
+       restart )
+               if [ "$args" ]; then
+                       echo "mkinit -- restarting $args"
+                       runamk "stop-$args"
+                       runamk "start-$args"
+               fi
+               ;;
+       start|stop|zap|status )
+               if [ "$args" ]; then
+                       echo "mkinit -- ${cmd}ing $args"
+                       runamk "${cmd}-$args"
+               fi
+               ;;
+       mk )
+               if [ "$args" ]; then
+                       echo "mkinit -- running mk cmd [$args]"
+                       runamk "$args"
+               fi
+               ;;
+       reload )
+               exec /sbin/mkinit -n
+               ;;
+       eval )
+               $args
+               ;;
+       ?* )
+               echo "unknown command [$cmd $args]"
+               ;;
+       esac
+       echo -n "mkinit> "
+       read cmd args
+done
diff --git a/src/respawn b/src/respawn
new file mode 100755 (executable)
index 0000000..9917c44
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+# Copyright (C) 2009 Andy Spencer
+# See ../COPYING for terms
+
+while (true); do
+       "$@"
+       echo Process $! died, waiting 1 second to respawn
+done
diff --git a/src/service b/src/service
new file mode 100755 (executable)
index 0000000..034df51
--- /dev/null
@@ -0,0 +1,41 @@
+#!/bin/bash
+
+# Copyright (C) 2009 Andy Spencer
+# See ../COPYING for terms
+
+function usage {
+cat - <<EOF
+usage: $0 <action> [start-|stop-]<service>
+Action:
+  -u,--is-up    Test is service is running
+  -d,--is-down  Test is service is stopped
+  -U,--set-up   Set service to running
+  -D,--set-down Set service to stopped
+  -q,--query    Print the state of a service
+EOF
+exit
+}
+
+# Main
+STATE=/lib/mkinit/state
+
+action="$1"
+service="${2/*-}"
+
+[ "$action" -a "$service" ] || usage
+
+# echo "$0 $@"
+
+case "$1" in
+       -u|--is-up    ) test   -e "$STATE/$service" ;;
+       -d|--is-down  ) test ! -e "$STATE/$service" ;;
+       -U|--set-up   ) touch     "$STATE/$service" ;;
+       -D|--set-down ) rm        "$STATE/$service" ;;
+       -q|--query    ) 
+               echo -n "Service \`$service' is "
+               test -e "$STATE/$service" \
+                       && echo running \
+                       || echo stopped
+               ;;
+       * ) usage ;;
+esac