From 38a2e3f0ce51f335c63bf77646a827e216fc3025 Mon Sep 17 00:00:00 2001 From: Cem Keylan Date: Mon, 13 Jan 2020 11:37:16 +0300 Subject: allow user hooks and make it more portable --- Makefile | 12 ++- README | 10 +++ rc.boot | 271 +++++++++++++++++++++++++++++------------------------------- rc.conf | 3 - rc.lib | 29 +++++++ rc.local | 9 ++ rc.shutdown | 71 ++++++++-------- 7 files changed, 226 insertions(+), 179 deletions(-) create mode 100644 rc.lib create mode 100755 rc.local diff --git a/Makefile b/Makefile index 358118f..ee33c77 100644 --- a/Makefile +++ b/Makefile @@ -8,12 +8,18 @@ install: sed 's#INITDIR#${INITDIR}#g' < inittab > ${DESTDIR}/etc/inittab chmod 644 ${DESTDIR}/etc/inittab install -Dm644 rc.conf ${DESTDIR}/etc/init/rc.conf - install -Dm755 rc.boot ${DESTDIR}${INITDIR}/rc.boot - install -Dm755 rc.shutdown ${DESTDIR}${INITDIR}/rc.shutdown + install -Dm644 rc.lib ${DESTDIR}${INITDIR}/rc.lib + install -Dm755 rc.local ${DESTDIR}/etc/init/rc.local + sed 's#INITDIR#${INITDIR}#g' < rc.boot > ${DESTDIR}${INITDIR}/rc.boot + sed 's#INITDIR#${INITDIR}#g' < rc.shutdown > ${DESTDIR}${INITDIR}/rc.shutdown + chmod 755 ${DESTDIR}${INITDIR}/rc.boot ${DESTDIR}${INITDIR}/rc.shutdown + install -Dm644 README ${DESTDIR}${INITDIR}/README uninstall: rm -f ${DESTDIR}/etc/inittab rm -f ${DESTDIR}/etc/init/rc.conf + rm -f ${DESTDIR}/etc/init/rc.local rm -f ${DESTDIR}${INITDIR}/rc.boot rm -f ${DESTDIR}${INITDIR}/rc.shutdown - + rm -f ${DESTDIR}${INITDIR}/rc.lib + rm -f ${DESTDIR}${INITDIR}/README diff --git a/README b/README index 0111ee7..dd47e08 100644 --- a/README +++ b/README @@ -1,2 +1,12 @@ carbs init scripts ================== + +these init scripts can be configured from +your /etc/init directory. + + +Hooks +----- + +You can add and configure hooks from + diff --git a/rc.boot b/rc.boot index 0f33985..45c8d75 100755 --- a/rc.boot +++ b/rc.boot @@ -1,178 +1,169 @@ #!/bin/sh . /etc/init/rc.conf +. INITDIR/rc.lib -welcome() { printf '\033[1;36m->\033[m\033[1m Welcome to\033[35m Carbs Linux\033[m\033[1m!\n' ;} -mnt() { - mountpoint -q "$1" || { - dir=$1 - shift - mount "$@" "$dir" - } -} -emergency_shell() { - error \ - "Cannot continue init due to errors above, starting emergency shell" \ - "When ready, type 'exit' to continue the boot." +PATH=/usr/bin:/usr/sbin +old_ifs=$IFS +set -f - /bin/sh -l -} +welcome -main() { - PATH=/usr/bin:/usr/sbin - old_ifs=$IFS - set -f - - welcome - - log "Mounting pseudo filesystems..."; { - mnt /proc -o nosuid,noexec,nodev -t proc proc - mnt /sys -o nosuid,noexec,nodev -t sysfs sys - mnt /run -o mode=0755,nosuid,nodev -t tmpfs run - mnt /dev -o mode=0755,nosuid -t devtmpfs dev - - mkdir -pm 0755 /run/runit \ - /run/lvm \ - /run/user \ - /run/lock \ - /run/log \ - /dev/pts \ - /dev/shm - - mnt /dev/pts -o mode=0620,gid=5,nosuid,noexec -nt devpts devpts - mnt /dev/shm -o mode=1777,nosuid,nodev -nt tmpfs shm - } +out "Mounting pseudo filesystems..."; { + mnt /proc -o nosuid,noexec,nodev -t proc proc + mnt /sys -o nosuid,noexec,nodev -t sysfs sys + mnt /run -o mode=0755,nosuid,nodev -t tmpfs run + mnt /dev -o mode=0755,nosuid -t devtmpfs dev - log "Setting dmesg level..."; { - [ -n "$dmesg_level" ] && dmesg -n$dmesg_level - } + mkdir -pm 0755 /run/runit \ + /run/lvm \ + /run/user \ + /run/lock \ + /run/log \ + /dev/pts \ + /dev/shm - log "Starting eudev..."; { - command -v udevd >/dev/null && { - udevd --daemon - udevadm trigger --action=add --type=subsystems - udevadm trigger --action=add --type=devices - udevadm settle - } - } + mnt /dev/pts -o mode=0620,gid=5,nosuid,noexec -nt devpts devpts + mnt /dev/shm -o mode=1777,nosuid,nodev -nt tmpfs shm +} + +out "Setting dmesg level..."; { + [ -n "$dmesg_level" ] && dmesg -n$dmesg_level +} - log "Remounting rootfs as ro..."; { - mount -o remount,ro / || emergency_shell +out "Starting eudev..."; { + command -v udevd >/dev/null && { + udevd --daemon + udevadm trigger --action=add --type=subsystems + udevadm trigger --action=add --type=devices + udevadm settle } +} - log "Activating encrypted devices (if any exist)..."; { - [ -e /etc/crypttab ] && [ -x /bin/cryptsetup ] && { - exec 3<&0 +out "Remounting rootfs as ro..."; { + mount -o remount,ro / || emergency_shell +} - while read -r name dev pass opts err; do - [ "${name##\#*}" ] || continue +out "Activating encrypted devices (if any exist)..."; { + [ -e /etc/crypttab ] && [ -x /bin/cryptsetup ] && { + exec 3<&0 - # Break on invalid crypttab. - [ "$err" ] && { - printf 'error: A valid crypttab has only 4 columns.\n' - break - } + while read -r name dev pass opts err; do + [ "${name##\#*}" ] || continue - # Turn 'UUID=*' lines into device names. - [ "${dev##UUID*}" ] || dev=$(blkid -l -o device -t "$dev") + # Break on invalid crypttab. + [ "$err" ] && { + printf 'error: A valid crypttab has only 4 columns.\n' + break + } - # Parse options by turning list into a pseudo array. - IFS=, - set -- $opts - IFS=$old_ifs + # Turn 'UUID=*' lines into device names. + [ "${dev##UUID*}" ] || dev=$(blkid -l -o device -t "$dev") - copts="cryptsetup luksOpen" + # Parse options by turning list into a pseudo array. + IFS=, + set -- $opts + IFS=$old_ifs - # Create an argument list (no other way to do this in sh). - for opt; do case $opt in - discard) copts="$copts --allow-discards" ;; - readonly|read-only) copts="$copts -r" ;; - tries=*) copts="$copts -T ${opt##*=}" ;; - esac; done + copts="cryptsetup luksOpen" - # If password is 'none', '-' or empty ask for it. - case $pass in - none|-|"") $copts "$dev" "$name" <&3 ;; - *) $copts -d "$pass" "$dev" "$name" ;; - esac - done < /etc/crypttab + # Create an argument list (no other way to do this in sh). + for opt; do case $opt in + discard) copts="$copts --allow-discards" ;; + readonly|read-only) copts="$copts -r" ;; + tries=*) copts="$copts -T ${opt##*=}" ;; + esac; done - exec 3>&- + # If password is 'none', '-' or empty ask for it. + case $pass in + none|-|"") $copts "$dev" "$name" <&3 ;; + *) $copts -d "$pass" "$dev" "$name" ;; + esac + done < /etc/crypttab - [ "$copts" ] && [ -x /bin/vgchance ] && { - log "Activating LVM devices for dm-crypt..." - vgchange --sysinit -a y || emergency_shell - } + exec 3>&- + + [ "$copts" ] && [ -x /bin/vgchance ] && { + out "Activating LVM devices for dm-crypt..." + vgchange --sysinit -a y || emergency_shell } } +} - log "Checking filesystems..."; { - fsck -ATat noopts=_netdev - [ $? -gt 1 ] && emergency_shell - } +out "Checking filesystems..."; { + fsck -ATat noopts=_netdev + [ $? -gt 1 ] && emergency_shell +} - log "Mounting rootfs rw..."; { - mount -o remount,rw / || emergency_shell - } +out "Mounting rootfs rw..."; { + mount -o remount,rw / || emergency_shell +} - log "Mounting all local filesystems..."; { - mount -at nosysfs,nonfs,nonfs4,nosmbfs,nocifs -O no_netdev || - emergency_shell - } +out "Mounting all local filesystems..."; { + mount -at nosysfs,nonfs,nonfs4,nosmbfs,nocifs -O no_netdev || + emergency_shell +} - log "Enabling swap..."; { - swapon -a || emergency_shell - } +out "Enabling swap..."; { + swapon -a || emergency_shell +} - log "Seeding random..."; { - if [ -f /var/random.seed ]; then - cat /var/random.seed > /dev/urandom - else - log "This may hang." - log "Mash the keyboard to generate entropy..." +out "Seeding random..."; { + if [ -f /var/random.seed ]; then + cat /var/random.seed > /dev/urandom + else + out "This may hang." + out "Mash the keyboard to generate entropy..." - dd count=1 bs=512 if=/dev/random of=/var/random.seed - fi - } + dd count=1 bs=512 if=/dev/random of=/var/random.seed + fi +} - log "Setting up loopback..."; { - ip link set up dev lo - } +out "Setting up loopback..."; { + ip link set up dev lo +} - log "Setting hostname..."; { - read -r hostname < /etc/hostname - printf '%s\n' "${hostname:-carbs-linux}" > /proc/sys/kernel/hostname - } 2>/dev/null +out "Setting hostname..."; { + read -r hostname < /etc/hostname + printf '%s\n' "${hostname:-carbs-linux}" > /proc/sys/kernel/hostname +} 2>/dev/null - log "Getting keymap settings..."; { - [ -n "$keymap" ] && loadkmap < "$keymap" - } +out "Getting keymap settings..."; { + [ -n "$keymap" ] && loadkmap < "$keymap" +} - log "Loading sysctl settings..."; { - find /run/sysctl.d \ - /etc/sysctl.d \ - /usr/local/lib/sysctl.d \ - /usr/lib/sysctl.d \ - /lib/sysctl.d \ - /etc/sysctl.conf \ - -name \*.conf -type f 2>/dev/null \ - | while read -r conf; do - seen="$seen ${conf##*/}" - - case $seen in - *" ${conf##*/} "*) ;; - *) printf '%s\n' "* Applying $conf ..." - sysctl -p "$conf" ;; - esac - done - } - command -v udevd >/dev/null && - udevadm control --exit +out "Loading sysctl settings..."; { + find /run/sysctl.d \ + /etc/sysctl.d \ + /usr/local/lib/sysctl.d \ + /usr/lib/sysctl.d \ + /lib/sysctl.d \ + /etc/sysctl.conf \ + -name \*.conf -type f 2>/dev/null \ + | while read -r conf; do + seen="$seen ${conf##*/}" + + case $seen in + *" ${conf##*/} "*) ;; + *) printf '%s\n' "* Applying $conf ..." + sysctl -p "$conf" ;; + esac + done +} + +out "Running boot hooks..."; { + run_hooks /etc/init/*.boot +} - log "Boot stage complete..." +out "Running rc.local..."; { + [ -r "/etc/init/rc.local" ] && \ + . /etc/init/rc.local } -main +command -v udevd >/dev/null && + udevadm control --exit + +out "Boot stage complete..." diff --git a/rc.conf b/rc.conf index 37a414f..7a8ce97 100644 --- a/rc.conf +++ b/rc.conf @@ -2,9 +2,6 @@ # CARBS init configuration # -log() { printf "\033[1;36m->\033[m\033[1m %s\n\033[0;00m" "$@" ;} -error() { printf "\033[1;31m-> ERROR: %s\n\033[0;00m" "$@" ;} - # Set your keymap (full path to your keymap e.g. /usr/share/bkeymaps) keymap= diff --git a/rc.lib b/rc.lib new file mode 100644 index 0000000..c7c8f84 --- /dev/null +++ b/rc.lib @@ -0,0 +1,29 @@ +# +# rc.lib +# + +# vim:filetype=sh +# shellcheck disable=1090 + +# Functions for rc.boot and rc.shutdown + +out() { printf "\033[1;36m->\033[0;00m\033[1m$1\033[0;00m\n" ;} +error() { printf "\033[1;31m-> ERROR: \033[0;00m\033[1m$1\033[0;00m\n" ;} +welcome() { printf '\033[1;36m->\033[m\033[1m Welcome to\033[35m Carbs Linux\033[m\033[1m!\n' ;} + +mnt() { + mountpoint -q "$1" || { + dir=$1 + shift + mount "$@" "$dir" + } +} + +emergency_shell() { + error "Cannot continue init due to errors above, starting emergency shell" + error "When ready, type 'exit' to continue the boot." + + /bin/sh -l +} + +run_hooks() { for file in "$@" ; do [ -f "$file" ] && . "$file"; done ;} diff --git a/rc.local b/rc.local new file mode 100755 index 0000000..1cd0051 --- /dev/null +++ b/rc.local @@ -0,0 +1,9 @@ +# +# rc.local +# + +# vim:filetype=sh + +# rc.local is executed after the main functions +# of rc.boot so you can add your own scripts and +# run them from here. diff --git a/rc.shutdown b/rc.shutdown index f47fdf7..83b6572 100755 --- a/rc.shutdown +++ b/rc.shutdown @@ -1,48 +1,53 @@ #!/bin/sh . /etc/init/rc.conf +. INITDIR/rc.lib -main() { - PATH=/sbin:/bin:/usr/sbin:/usr/bin +PATH=/sbin:/bin:/usr/sbin:/usr/bin - log "Waiting for services to stop..."; { - sv -w196 force-stop /var/service/* - sv exit /var/service/* - } +out "Waiting for services to stop..."; { + sv -w196 force-stop /var/service/* + sv exit /var/service/* +} - log "Saving random seed..."; { - dd count=1 bs=512 if=/dev/random of=/var/random.seed - } +out "Running pre shutdown hooks..."; { + run_hooks /etc/init/*.pre.shutdown +} - halt -w +out "Saving random seed..."; { + dd count=1 bs=512 if=/dev/random of=/var/random.seed +} - log "Sending TERM signal to all processes..."; { - killall5 -TERM - sleep 1 - } +halt -w - log "Sending KILL signal to all processes..."; { - killall5 -KILL - } +out "Sending TERM signal to all processes..."; { + killall5 -TERM + sleep 1 +} - log "Unmounting filesystems and disabling swap..." \ - "Remounting rootfs as readonly"; { - swapoff -a - umount -rat nosysfs,noproc,nodevtmpfs,notmpfs - mount -o remount,ro / +out "Sending KILL signal to all processes..."; { + killall5 -KILL +} - sync - } +out "Unmounting filesystems and disabling swap..." +out "Remounting rootfs as readonly"; { + swapoff -a + umount -rat nosysfs,noproc,nodevtmpfs,notmpfs + mount -o remount,ro / - log "Deactivating dmcrypt devices (if any exist)."; { - [ -x /bin/cryptsetup ] && [ -x /bin/dmsetup ] && { - dmsetup ls --target crypt \ - --exec "dmsetup info -c --noheadings -o open,name" | - while read -r drive; do - [ "${drive%%:*}" = "0" ] && cryptsetup close "${drive##*:}" - done - } + sync +} + +out "Deactivating dmcrypt devices (if any exist)."; { + [ -x /bin/cryptsetup ] && [ -x /bin/dmsetup ] && { + dmsetup ls --target crypt \ + --exec "dmsetup info -c --noheadings -o open,name" | + while read -r drive; do + [ "${drive%%:*}" = "0" ] && cryptsetup close "${drive##*:}" + done } } -main +out "Running post shutdown hooks..."; { + run_hooks /etc/rc.d/*.post.shutdown +} -- cgit v1.2.3