define-alpine/modules/runit.lua

369 lines
9.9 KiB
Lua
Raw Normal View History

2022-06-18 00:06:19 +00:00
mkdir("/etc/runit/runsvdir/default")
add_symlink("/etc/runit/runsvdir/current", "/etc/runit/runsvdir/default")
add_symlink("/etc/service", "/etc/runit/runsvdir/current")
add_symlink("/etc/runit/stopit", "/run/runit/stopit")
2022-06-18 00:06:19 +00:00
local t = require "../utils/templater"
local function add_executable(path, script)
add_file(path, script)
chmod(path, "700")
end
2022-06-19 00:29:00 +00:00
local function add_service(name, script, _log)
local log = _log == nil and true or _log
2022-06-18 00:06:19 +00:00
local params = {name = name}
local run_script_path = t("/etc/sv/{{name}}/run", params)
2022-06-19 00:29:00 +00:00
if log then
local log_script_path = t("/etc/sv/{{name}}/log/run", params)
if log == true then
add_executable(log_script_path, t([[#!/bin/sh
exec logger -p daemon.info -t {{name}}]], params))
else
add_executable(log_script_path, log)
end
add_symlink(
t("/etc/sv/{{name}}/log/supervise", params),
t("/run/runit/supervise.{{name}}.log", params)
)
end
2022-06-18 00:06:19 +00:00
add_executable(run_script_path, script)
add_symlink("/etc/runit/runsvdir/default/"..name, "/etc/sv/"..name)
add_symlink(
t("/etc/sv/{{name}}/supervise", params),
t("/run/runit/supervise.{{name}}", params)
)
end
modules.runit = {
add_service = add_service,
}
-- Estos scripts fueron robados de Void Linux
add_executable("/etc/runit/functions", [[
msg() {
# bold
printf "\\033[1m=> $@\\033[m\\n"
}
msg_ok() {
# bold/green
printf "\\033[1m\\033[32m OK\\033[m\\n"
}
msg_error() {
# bold/red
printf "\\033[1m\\033[31mERROR: $@\\033[m\\n"
}
msg_warn() {
# bold/yellow
printf "\\033[1m\\033[33mWARNING: $@\\033[m\\n"
}
emergency_shell() {
echo
echo "Cannot continue due to errors above, starting emergency shell."
echo "When ready type exit to continue booting."
/bin/sh -l
}
detect_virt() {
# Detect LXC (and other) containers
[ -z "${container+x}" ] || export VIRTUALIZATION=1
}
deactivate_vgs() {
_group=${1:-All}
if [ -x /sbin/vgchange -o -x /bin/vgchange ]; then
vgs=$(vgs|wc -l)
if [ $vgs -gt 0 ]; then
msg "Deactivating $_group LVM Volume Groups..."
vgchange -an
fi
fi
}
deactivate_crypt() {
if [ -x /sbin/dmsetup -o -x /bin/dmsetup ]; then
msg "Deactivating Crypt Volumes"
for v in $(dmsetup ls --target crypt --exec "dmsetup info -c --noheadings -o open,name"); do
[ ${v%%:*} = "0" ] && cryptsetup close ${v##*:}
done
deactivate_vgs "Crypt"
fi
}
]])
add_executable("/etc/runit/core-services/00-pseudofs.sh", [[
msg "Mounting pseudo-filesystems..."
mountpoint -q /proc || mount -o nosuid,noexec,nodev -t proc proc /proc
mountpoint -q /sys || mount -o nosuid,noexec,nodev -t sysfs sys /sys
mountpoint -q /run || mount -o mode=0755,nosuid,nodev -t tmpfs run /run
mountpoint -q /dev || mount -o mode=0755,nosuid -t devtmpfs dev /dev
mkdir -p -m0755 /run/runit /run/lvm /run/user /run/lock /run/log /dev/pts /dev/shm
mountpoint -q /dev/pts || mount -o mode=0620,gid=5,nosuid,noexec -n -t devpts devpts /dev/pts
mountpoint -q /dev/shm || mount -o mode=1777,nosuid,nodev -n -t tmpfs shm /dev/shm
mountpoint -q /sys/kernel/security || mount -n -t securityfs securityfs /sys/kernel/security
if [ -d /sys/firmware/efi/efivars ]; then
mountpoint -q /sys/firmware/efi/efivars || mount -o nosuid,noexec,nodev -t efivarfs efivarfs /sys/firmware/efi/efivars
fi
if [ -z "$VIRTUALIZATION" ]; then
_cgroupv1=""
_cgroupv2=""
case "${CGROUP_MODE:-hybrid}" in
legacy)
_cgroupv1="/sys/fs/cgroup"
;;
hybrid)
_cgroupv1="/sys/fs/cgroup"
_cgroupv2="${_cgroupv1}/unified"
;;
unified)
_cgroupv2="/sys/fs/cgroup"
;;
esac
# cgroup v1
if [ -n "$_cgroupv1" ]; then
mountpoint -q "$_cgroupv1" || mount -o mode=0755 -t tmpfs cgroup "$_cgroupv1"
while read -r _subsys_name _hierarchy _num_cgroups _enabled; do
[ "$_enabled" = "1" ] || continue
_controller="${_cgroupv1}/${_subsys_name}"
mkdir -p "$_controller"
mountpoint -q "$_controller" || mount -t cgroup -o "$_subsys_name" cgroup "$_controller"
done < /proc/cgroups
fi
# cgroup v2
if [ -n "$_cgroupv2" ]; then
mkdir -p "$_cgroupv2"
mountpoint -q "$_cgroupv2" || mount -t cgroup2 -o nsdelegate cgroup2 "$_cgroupv2"
fi
fi]])
add_executable("/etc/runit/core-services/01-static-devnodes.sh", [[
# Some kernel modules must be loaded before starting udev(7).
# Load them by looking at the output of `kmod static-nodes`.
for f in $(kmod static-nodes 2>/dev/null|awk '/Module/ {print $2}'); do
modprobe -bq $f 2>/dev/null
done
]])
add_executable("/etc/runit/core-services/02-udev.sh", [[
[ -n "$VIRTUALIZATION" ] && return 0
if [ -x /sbin/udevd -o -x /bin/udevd ]; then
_udevd=udevd
else
msg_warn "cannot find udevd!"
fi
if [ -n "${_udevd}" ]; then
msg "Starting udev and waiting for devices to settle..."
${_udevd} --daemon
udevadm trigger --action=add --type=subsystems
udevadm trigger --action=add --type=devices
udevadm settle
fi
]])
add_executable("/etc/runit/core-services/03-filesystems.sh", [[
[ -n "$VIRTUALIZATION" ] && return 0
#msg "Remounting rootfs read-only..."
#mount -o remount,ro / || emergency_shell
if [ -x /sbin/dmraid -o -x /bin/dmraid ]; then
msg "Activating dmraid devices..."
dmraid -i -ay
fi
if [ -x /bin/mdadm ]; then
msg "Activating software RAID arrays..."
mdadm -As
fi
if [ -x /bin/btrfs ]; then
msg "Activating btrfs devices..."
btrfs device scan || emergency_shell
fi
if [ -x /sbin/vgchange -o -x /bin/vgchange ]; then
msg "Activating LVM devices..."
vgchange --sysinit -a ay || emergency_shell
fi
if [ -e /etc/crypttab ]; then
msg "Activating encrypted devices..."
awk -f /etc/runit/crypt.awk /etc/crypttab
if [ -x /sbin/vgchange -o -x /bin/vgchange ]; then
msg "Activating LVM devices for dm-crypt..."
vgchange --sysinit -a ay || emergency_shell
fi
fi
if [ -x /usr/bin/zpool -a -x /usr/bin/zfs ]; then
if [ -e /etc/zfs/zpool.cache ]; then
msg "Importing cached ZFS pools..."
zpool import -N -a -c /etc/zfs/zpool.cache
else
msg "Scanning for and importing ZFS pools..."
zpool import -N -a -o cachefile=none
fi
msg "Mounting ZFS file systems..."
zfs mount -a -l
msg "Sharing ZFS file systems..."
zfs share -a
# NOTE(dh): ZFS has ZVOLs, block devices on top of storage pools.
# In theory, it would be possible to use these as devices in
# dmraid, btrfs, LVM and so on. In practice it's unlikely that
# anybody is doing that, so we aren't supporting it for now.
fi
[ -f /fastboot ] && FASTBOOT=1
[ -f /forcefsck ] && FORCEFSCK="-f"
for arg in $(cat /proc/cmdline); do
case $arg in
fastboot) FASTBOOT=1;;
forcefsck) FORCEFSCK="-f";;
esac
done
if [ -z "$FASTBOOT" ]; then
msg "Checking filesystems:"
fsck -A -T -a -t noopts=_netdev $FORCEFSCK
if [ $? -gt 1 ]; then
s emergency_shell
fi
fi
msg "Mounting all non-network filesystems..."
mount -a -t "nosysfs,nonfs,nonfs4,nosmbfs,nocifs" -O no_netdev || emergency_shell
2022-06-18 15:05:18 +00:00
# data module
msg "Creating and mounting data directories..."
/usr/local/bin/mount-data || emergency_shell
2022-06-18 00:06:19 +00:00
]])
add_executable("/etc/runit/core-services/04-swap.sh", [[
[ -n "$VIRTUALIZATION" ] && return 0
msg "Initializing swap..."
swapon -a || emergency_shell
]])
add_executable("/etc/runit/core-services/05-misc.sh", [[
install -m0664 -o root -g utmp /dev/null /run/utmp
#halt -B # for wtmp
msg "Setting up loopback interface..."
ip link set up dev lo
2022-06-18 01:19:09 +00:00
msg "Setting hostname..."
hostname -F /etc/hostname
2022-06-18 00:06:19 +00:00
]])
-- Initial boot
add_executable("/etc/runit/1", [[#!/bin/sh
PATH=/bin:/usr/bin:/usr/sbin:/sbin
. /etc/runit/functions
msg "Welcome to Nulo!"
[ -r /etc/rc.conf ] && . /etc/rc.conf
# Start core services: one-time system tasks.
detect_virt
for f in /etc/runit/core-services/*.sh; do
[ -r $f ] && . $f
done
# create files for controlling runit
mkdir -p /run/runit
install -m000 /dev/null /run/runit/stopit
install -m000 /dev/null /run/runit/reboot
msg "Initialization complete, running stage 2..."
]])
add_executable("/etc/runit/2", [[#!/bin/sh
PATH=/bin:/usr/bin:/usr/sbin:/sbin
runlevel=default
for arg in $(cat /proc/cmdline); do
if [ -d /etc/runit/runsvdir/"$arg" ]; then
echo "Runlevel detected: '$arg' (via kernel cmdline)"
runlevel="$arg"
fi
done
[ -x /etc/rc.local ] && /etc/rc.local
runsvchdir "${runlevel}"
mkdir -p /run/runit/runsvdir
ln -s /etc/runit/runsvdir/current /run/runit/runsvdir/current
exec env - PATH=$PATH \
runsvdir -P /run/runit/runsvdir/current 'log: ................................'
]])
-- Shutdown
add_executable("/etc/runit/3", [[#!/bin/sh
PATH=/bin:/usr/bin:/usr/sbin:/sbin
. /etc/runit/functions
detect_virt
[ -r /etc/rc.conf ] && . /etc/rc.conf
echo
msg "Waiting for services to stop..."
sv force-stop /etc/service/*
sv exit /etc/service/*
[ -x /etc/rc.shutdown ] && /etc/rc.shutdown
if [ -z "$VIRTUALIZATION" -a -n "$HARDWARECLOCK" ]; then
hwclock --systohc ${HARDWARECLOCK:+--$(echo $HARDWARECLOCK |tr A-Z a-z)}
fi
halt -w # for wtmp
if [ -z "$VIRTUALIZATION" ]; then
msg "Stopping udev..."
udevadm control --exit
fi
if [ -z "$VIRTUALIZATION" ]; then
msg "Unmounting filesystems, disabling swap..."
swapoff -a
umount -r -a -t nosysfs,noproc,nodevtmpfs,notmpfs
fi
sync
if [ -z "$VIRTUALIZATION" ]; then
deactivate_vgs
deactivate_crypt
if [ -e /run/runit/reboot ] && command -v kexec >/dev/null; then
msg "Triggering kexec..."
kexec -e 2>/dev/null
# not reached when kexec was successful.
fi
fi
]])
add_packages({ "runit", "eudev" })
2022-06-18 00:06:19 +00:00
modules.runit.add_service("getty-tty1", [[#!/bin/sh
2022-06-19 00:29:00 +00:00
exec chpst -P getty 38400 tty1 linux]], false)
2022-06-18 00:06:19 +00:00
modules.runit.add_service("getty-tty2", [[#!/bin/sh
2022-06-19 00:29:00 +00:00
exec chpst -P getty 38400 tty2 linux]], false)