Mercurial > vinit
changeset 4:3db6ca1fe07d
misc: merge scripts from vinit-script
While here, make /etc tunable.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 10 Nov 2021 16:34:41 +0100 |
parents | 55c59f0c8cbd |
children | d2a58a85bec2 |
files | Makefile vinit.7 vinit.8 vinit.c vinit.conf vinit.conf.5 vinit.net vinit.shutdown vinit.start |
diffstat | 9 files changed, 546 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Thu Jul 29 09:50:27 2021 +0200 +++ b/Makefile Wed Nov 10 16:34:41 2021 +0100 @@ -19,15 +19,20 @@ .POSIX: CC= cc -CFLAGS= -Wall -Wextra -O3 -DNDEBUG -D_POSIX_C_SOURCE=200809L +CFLAGS= -Wall -Wextra -O3 -DNDEBUG PREFIX= /usr/local BINDIR= ${PREFIX}/sbin MANDIR= ${PREFIX}/share/man +SHAREDIR= ${PREFIX}/share +SYSCONFDIR= ${PREFIX}/etc VERSION= 0.1.0 SRCS= vinit.c OBJS= ${SRCS:.c=.o} +SED= -e "s,@SHAREDIR@,${SHAREDIR},g" \ + -e "s,@SYSCONFDIR@,${SYSCONFDIR},g" +FLAGS= -D_POSIX_C_SOURCE=200809L -DSYSCONFDIR=\"${SYSCONFDIR}\" .SUFFIXES: .SUFFIXES: .c .o @@ -35,7 +40,7 @@ all: vinit .c.o: - ${CC} ${CFLAGS} -c $< -o $@ + ${CC} ${FLAGS} ${CFLAGS} -c $< -o $@ vinit: ${OBJS} ${CC} ${CFLAGS} -o $@ ${OBJS} ${LDFLAGS} @@ -44,7 +49,14 @@ mkdir -p ${DESTDIR}${BINDIR} cp vinit ${DESTDIR}${BINDIR} mkdir -p ${DESTDIR}${MANDIR}/man8 - cp vinit.8 ${DESTDIR}${MANDIR}/man8 + sed ${SED} < vinit.conf > ${DESTDIR}${SYSCONFDIR}/vinit.conf + sed ${SED} < vinit.7 > ${DESTDIR}${MANDIR}/man7/vinit.7 + sed ${SED} < vinit.8 > ${DESTDIR}${MANDIR}/man8/vinit.8 + sed ${SED} < vinit.conf.5 > ${DESTDIR}${MANDIR}/man5/vinit.conf.5 + mkdir -p ${DESTDIR}${SYSCONFDIR} + sed ${SED} < vinit.start > ${DESTDIR}${SYSCONFDIR}/vinit.start + sed ${SED} < vinit.net > ${DESTDIR}${SYSCONFDIR}/vinit.net + sed ${SED} < vinit.shutdown > ${DESTDIR}${SYSCONFDIR}/vinit.shutdown dist: rm -rf vinit-${VERSION}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vinit.7 Wed Nov 10 16:34:41 2021 +0100 @@ -0,0 +1,59 @@ +.\" +.\" Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> +.\" +.\" Permission to use, copy, modify, and/or distribute this software for any +.\" purpose with or without fee is hereby granted, provided that the above +.\" copyright notice and this permission notice appear in all copies. +.\" +.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +.\" +.Dd July 29, 2021 +.Dt VINIT 7 +.Os +.Sh NAME +.Nm vinit +.Nd vanilla init scripts +.Sh DESCRIPTION +The following files are provided for use with the +.Xr vinit 8 +init system. It provides a default set of features to startup a fully working +system. +.Pp +List of available scripts and files: +.Bl -tag -width 18n +.It Pa @SYSCONFDIR@/vinit.conf +The global configuration file for system initialization and basic configuration. +See +.Xr vinit.conf 5 +for more details. +.It Pa @SYSCONFDIR@/vinit.start +File executed by boot process to perform the machine initialization. It mounts +filesystem, checks devices and finally sets configuration from +.Pa @SYSCONFDIR@/vinit.conf +file. +.It Pa @SYSCONFDIR@/vinit.net +Enable network interfaces and configure them. Called just before starting user +services and mostly used for static address scenarios. +.It Pa @SYSCONFDIR@/vinit.shutdown +This file is executed when the machine is going off. Its purpose is to shutdown +all processed, unmount filesystem and halts the machine. It takes an argument +which can be one of: +.Dq halt , +.Dq poweroff +or +.Dq reboot . +See +.Xr vinit 8 +for more information. +.El +.Pp +All files are used editable and not replaced on upgrade. +.Sh SEE ALSO +.Xr vinit.conf 5 , +.Xr vinit 8
--- a/vinit.8 Thu Jul 29 09:50:27 2021 +0200 +++ b/vinit.8 Wed Nov 10 16:34:41 2021 +0100 @@ -20,21 +20,21 @@ .Os .Sh NAME .Nm vinit -.Nd the Vanilla Linux init +.Nd the vanilla minimalist init .Sh DESCRIPTION .Nm -is the official default init system for Vanilla Linux. It's sole purpose is to -start init scripts and shutdown the system. +is the official default init system for vanilla. It's sole purpose is to start +init scripts and shutdown the system. .Ss Startup When the system starts, .Nm invokes -.Pa /etc/rc.init +.Pa @SYSCONFDIR@/vinit.start which setups the system and starts some services. There is not respawning process, an external tool should be used for that purpose. .Ss Shutdown When a signal is received the script -.Pa /etc/rc.shutdown +.Pa @SYSCONFDIR@/vinit.shutdown is invoked with a unique argument defined as follow: .Bl -tag .It USR1
--- a/vinit.c Thu Jul 29 09:50:27 2021 +0200 +++ b/vinit.c Wed Nov 10 16:34:41 2021 +0100 @@ -26,10 +26,10 @@ #include <string.h> #include <unistd.h> -static char * const rcinit[] = { "/etc/rc.init", NULL }; -static char * const rchalt[] = { "/etc/rc.shutdown", "halt", NULL }; -static char * const rcpoweroff[] = { "/etc/rc.shutdown", "poweroff", NULL }; -static char * const rcreboot[] = { "/etc/rc.shutdown", "reboot", NULL }; +static char * const rcinit[] = { SYSCONFDIR "/vinit.start", NULL }; +static char * const rchalt[] = { SYSCONFDIR "/vinit.shutdown", "halt", NULL }; +static char * const rcpoweroff[] = { SYSCONFDIR "/vinit.shutdown", "poweroff", NULL }; +static char * const rcreboot[] = { SYSCONFDIR "/vinit.shutdown", "reboot", NULL }; static sigset_t set;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vinit.conf Wed Nov 10 16:34:41 2021 +0100 @@ -0,0 +1,22 @@ +# +# @SYSCONFDIR@/vinit.conf: system configuration +# + +# System hostname. +HOSTNAME="localhost" + +# System timezone. +TIMEZONE="UTC" + +# Services to start at boot. +SERVICES="" + +# Console font and keymap. +FONT="" +KEYMAP="" + +# Networking. +INTERFACES="lo" + +# Defaults ttys. +TTYS="tty1 tty2 tty3 tty4 tty5 tty6 tty7 tty8"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vinit.conf.5 Wed Nov 10 16:34:41 2021 +0100 @@ -0,0 +1,122 @@ +.\" +.\" Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> +.\" +.\" Permission to use, copy, modify, and/or distribute this software for any +.\" purpose with or without fee is hereby granted, provided that the above +.\" copyright notice and this permission notice appear in all copies. +.\" +.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +.\" +.Dd July 29, 2021 +.Dt VINIT.CONF 5 +.Os +.Sh NAME +.Nm vinit.conf +.Nd system configuration +.Sh DESCRIPTION +The file +.Nm +contains information about the system. +.Pp +It is evaluated at boot time when +.Pa @SYSCONFDIR@/vinit.start +is started from +.Xr vinit 8 +and performs initialization. The file is sourced from a shell script and +therefore can contain shell code. +.Pp +The following options are available: +.Bl -tag -width indent-two +.It Va HOSTNAME +Sets the machine hostname. (Default: localhost) +.It Va TIMEZONE +Sets the time zone. You can get a list of timezones in the +.Pa @SHAREDIR@/zoneinfo +directory. The variable must be in the form +.Ar Area/Region . +(Default: empty) +.Pp +Note: this requires package +.Ar tzdata +to be installed. +.It Va SERVICES +This variable contains services to be started at boot. (Default: empty). +.Pp +In Vanilla Linux, services have no dependencies and therefore user is +responsible of starting them is a specific order if needed. Thus, services +marked in this list separated by spaces are executed in order. +.Pp +Entries in this variable should contain file names located in +.Pa @SYSCONFDIR@/vinit.d +directory. +.Pp +Some services offer tunables to pass additional configuration to them. See the +service file header for more information. +.Pp +Note: don't forget to mark the service file as executable or it won't be +executed by init process. +.It Va FONT +Sets the console font. A list is available in +.Pa @SHAREDIR@/kbd/consolefonts . +(Default: empty). +.Pp +Note: this requires package +.Ar kbd +to be installed. +.It Va KEYMAP +Sets the console keymap. A list is available in +.Pa @SHAREDIR@/kbd/keymaps . +(Default: empty). +.Pp +Note: this requires package +.Ar kbd +to be installed. +.It Va INTERFACES +Interfaces to pass to +.Pa @SYSCONFDIR@/vinit.net +script during bootup and shutdown. Mostly used for static IP addresses. It's +strongly advised to keep +.Ar lo +interface in this variable. (Default: lo) +.Pp +See the documentation in the +.Pa @SYSCONFDIR@/vinit.net +file. +.It Va TTYS +List of ttys to configure. (Default: tty1 to tty8). +.Pp +For each tty listed in this variable two variables +.Va <TTY>_ARGS +and +.Va <TTY>_FONT +can be specified where <TTY> is the uppercase tty number (e.g. TTY1 for tty1). +The first one specifies the arguments to pass to the getty program which is set +to +.Dq 38400 +by default. If the boolean _FONT isn't set to +.Dq no , +its console font will be changed according to +.Va FONT +value. +.El +.Sh EXAMPLES +.Ss Starting sysklogd package +The +.Ar sysklogd +package understand the variable +.Va SYSKLOGD_ARGS +and may be used instead. However, please note that setting this variable replace +the original arguments so double check the service file before setting any +variable. +.Bd -literal +SYSKLOGD_ARGS="-4" +SERVICES="sysklogd" +.Ed +.Sh SEE ALSO +.Xr vinit 7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vinit.net Wed Nov 10 16:34:41 2021 +0100 @@ -0,0 +1,105 @@ +#!/bin/sh +# +# @SYSCONFDIR@/vinit.net -- basic networking script +# +# Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +# +# This script is provided mostly for static configuration and/or more complex +# scenarios where the system administrator requires full control over the +# networking interfaces. Most people would prefer using dhcpcd for simple +# configurations or NetworkManager on desktop systems. +# +# The file @SYSCONFDIR@/vinit.net allows you to define functions for each interface +# and will automatically call "start" and "stop" at boot and shutdown +# respectively. +# +# See below for basic examples and uncomment the appropriate one. +# + +if [ -f @SYSCONFDIR@/vinit.conf ]; then + . @SYSCONFDIR@/vinit.conf +fi + +lo() +{ + printf "Setting network interface: lo." + + case $1 in + "start") + ip link set dev lo up + ip addr add 127.0.0.1/8 dev lo + ip -6 addr add ::1/128 dev lo + ;; + "stop") + ip -6 addr del ::1/128 dev lo + ip addr del 127.0.0.1 dev lo + ip link set dev lo down + ;; + esac +} + +# +# Example 1: static IP on eth0 interface. +# +# eth0() +# { +# printf "Setting network interface: eth0." +# +# case $1 in +# "start") +# ip link set dev eth0 up +# ip addr add 192.168.0.1 dev eth0 +# ;; +# "stop") +# ip addr del 192.168.0.1 dev eth0 +# ip link set dev eth0 down +# ;; +# esac +# } +# + +usage() +{ + echo "usage: $(basename $0) start|stop [interface]" 1>&2 + exit 1 +} + +if [ $# -eq 0 ]; then + usage + # NOTREACHED +fi + +case $1 in +"start"|"stop") + # + # If no interface is given, iterate over all defined in + # INTERFACES from @SYSCONFDIR@/vinit.conf. + # + if [ $# -eq 1 ]; then + for iface in $INTERFACES; do + $iface $1 + done + else + $2 $1 + fi + + ;; +*) + usage + # NOTREACHED + ;; +esac
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vinit.shutdown Wed Nov 10 16:34:41 2021 +0100 @@ -0,0 +1,50 @@ +#!/bin/sh +# +# @SYSCONFDIR@/rc.shutdown -- reboot and halt script +# +# Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +trap "" INT TERM + +if [ -f @SYSCONFDIR@/vinit.conf ]; then + . @SYSCONFDIR@/vinit.conf +fi + +# Stop networking. +if [ -x @SYSCONFDIR@/vinit.net ]; then + @SYSCONFDIR@/vinit.net stop +fi + +echo "Terminating all processes." +killall5 -15 + +echo "Unmounting all filesystems." +halt -w +swapoff -a + +echo "Remounting / read-only" +umount -a -d -r -t nosysfs,noproc,nodevtmpfs +umount -a -r +mount -o remount,ro / + +case "$1" in +"halt") + halt -f ;; +"poweroff") + poweroff -f ;; +"reboot") + reboot -f ;; +esac
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vinit.start Wed Nov 10 16:34:41 2021 +0100 @@ -0,0 +1,164 @@ +#!/bin/sh +# +# @SYSCONFDIR@/vinit.start -- system initialization script +# +# Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +if [ -f @SYSCONFDIR@/vinit.conf ]; then + . @SYSCONFDIR@/vinit.conf +fi + +# Set font as early as possible for a better output. +for t in $TTYS; do + eval font=\$$(printf $t | tr "[:lower:]" "[:upper:]")_FONT + + if [ "$font" != "no" ] && [ -n "$FONT" ]; then + setfont -C /dev/$t $FONT + fi +done + +if [ "${BANNER:-yes}" = "yes" ]; then + printf "\nWelcome to \033[34;1mvanilla\033[0m\n\n" +fi + +if [ -n "$HOSTNAME" ]; then + echo "Setting hostname: $HOSTNAME" + hostname "$HOSTNAME" +fi + +echo "Setting kernel parameters." +sysctl -p >/dev/null 2>&1 + +echo -n "Mounting filesystems:" + +if ! mountpoint /dev >/dev/null 2>&1; then + mount -t devtmpfs devtmpfs /dev >/dev/null 2>&1 + printf " /dev" +fi + +if ! mountpoint /dev/pts >/dev/null 2>&1; then + mkdir -p /dev/pts 2>/dev/null + mount -t devpts devpts /dev/pts >/dev/null 2>&1 + printf " /dev/pts" +fi + +if ! mountpoint /proc >/dev/null 2>&1; then + mount -t proc proc /proc >/dev/null 2>&1 + printf " /proc" +fi + +if ! mountpoint /sys >/dev/null 2>&1; then + mount -t sysfs sysfs /sys >/dev/null 2>&1 + printf " /sys" +fi + +if ! mountpoint /run >/dev/null 2>&1; then + mount -t tmpfs tmpfs /run >/dev/null 2>&1 + printf " /run" +fi + +printf ".\n" + +# +# Start a device service. Both eudev and busybox's mdev are supported but only +# one at a time must be enabled. +# +if [ -x @SYSCONFDIR@/vinit.d/udevd ] && [ -x @SYSCONFDIR@/vinit.d/busybox.mdev ]; then + echo "Warning: both @SYSCONFDIR@/vinit.d/udevd and @SYSCONFDIR@/vinit.d/busybox.mdev are enabled" 1>&2 + echo "Warning: using udevd instead" 1>&2 +fi + +if [ -x @SYSCONFDIR@/vinit.d/udevd ]; then + @SYSCONFDIR@/vinit.d/udevd start +elif [ -x @SYSCONFDIR@/vinit.d/busybox.mdev ]; then + @SYSCONFDIR@/vinit.d/busybox.mdev start +fi + +# Make sure / is ro in case of initrd. +mount -o remount,ro / + +# If @SYSCONFDIR@/forcefsck is there, force check. +if [ -f @SYSCONFDIR@/forcefsck ]; then + force="-f" +fi + +# +# There is no way to shut off fsck entirely, so put everything in a file and +# output it on error. +# +fsck $force -yAT >/run/fsck.log 2>&1 + +if [ "$?" -gt 1 ]; then + cat /run/fsck.log && rm -f /run/fsck.log + echo "* Filesystem check failed" + echo "* Emergency shell spawned, type exit to reboot" + login -f root + reboot +else + rm -f /run/fsck.log +fi + +# Remount / and enable swap as the system is ready. +mount -o remount,rw / +mount -a +swapon -a + +# Clean pid files before starting any services. +find /var/run -type f -name "*.pid" -delete + +if [ -n "$TIMEZONE" ] && [ "$TIMEZONE" != "UTC" ]; then + printf "Setting timezone: " + + if [ -f /usr/share/zoneinfo/$TIMEZONE ]; then + ln -sf /usr/share/zoneinfo/$TIMEZONE @SYSCONFDIR@/localtime + printf "$TIMEZONE.\n" + else + printf "$TIMEZONE not found.\n" + fi +fi + +# Keymap. +if [ -n "$KEYMAP" ]; then + printf "Setting keymap: $KEYMAP." + loadkeys $KEYMAP +fi + +# Cleanup some files. +rm -f @SYSCONFDIR@/forcefsck + +# Startup networking. +if [ -x @SYSCONFDIR@/vinit.net ]; then + @SYSCONFDIR@/vinit.net start +fi + +# Start user services requested in vinit.conf(5). +for s in $SERVICES; do + if [ -x @SYSCONFDIR@/vinit.d/$s ]; then + @SYSCONFDIR@/vinit.d/$s start + fi +done + +# Spawn ttys. +printf "Starting ttys:" + +for t in $TTYS; do + eval args=\$$(printf $t | tr "[:lower:]" "[:upper:]")_ARGS + + printf " $t" + getty -L $t ${args:-38400} & +done + +printf ".\n"