├── var └── rc │ ├── dtinit │ ├── nodown │ ├── run │ └── dtinit.sh │ ├── connmand │ └── run │ ├── sysctl │ └── run │ ├── elogind │ └── run │ ├── udev-settle │ └── run │ ├── tty2 │ └── run │ ├── tty3 │ └── run │ ├── tty4 │ └── run │ ├── tty5 │ └── run │ ├── tty6 │ └── run │ ├── udev-trigger │ └── run │ ├── tty1 │ └── run │ ├── sddm │ └── run │ ├── udevd │ └── run │ ├── dbus │ └── run │ └── cgroups │ ├── cgroups.conf │ ├── cgroup-release-agent.sh │ └── run ├── stuff └── artix-sinit.png ├── sbin └── shutdown ├── install-deps.sh ├── LICENSE ├── bin ├── rc.shutdown └── rc.init ├── install.sh └── README.md /var/rc/dtinit/nodown: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /var/rc/connmand/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | exec /usr/bin/connmand -n 3 | -------------------------------------------------------------------------------- /var/rc/dtinit/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | exec /var/rc/dtinit/dtinit.sh 3 | -------------------------------------------------------------------------------- /var/rc/sysctl/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | /usr/bin/sysctl --system 3 | lk_forever 3600 4 | -------------------------------------------------------------------------------- /var/rc/elogind/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | exec /usr/lib/elogind/elogind 3 | lk_forever 10 4 | -------------------------------------------------------------------------------- /var/rc/udev-settle/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | /usr/bin/udevadm settle 3 | lk_forever 3600 4 | -------------------------------------------------------------------------------- /var/rc/tty2/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo Starting tty2 3 | exec /sbin/agetty tty2 38400 linux 4 | -------------------------------------------------------------------------------- /var/rc/tty3/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo Starting tty3 3 | exec /sbin/agetty tty3 38400 linux 4 | -------------------------------------------------------------------------------- /var/rc/tty4/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo Starting tty4 3 | exec /sbin/agetty tty4 38400 linux 4 | -------------------------------------------------------------------------------- /var/rc/tty5/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo Starting tty5 3 | exec /sbin/agetty tty5 38400 linux 4 | -------------------------------------------------------------------------------- /var/rc/tty6/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo Starting tty6 3 | exec /sbin/agetty tty6 38400 linux 4 | -------------------------------------------------------------------------------- /var/rc/udev-trigger/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | udevadm trigger --action=add 3 | lk_forever 3600 4 | -------------------------------------------------------------------------------- /stuff/artix-sinit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Andrey0189/sinit-scripts/HEAD/stuff/artix-sinit.png -------------------------------------------------------------------------------- /var/rc/tty1/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo Starting tty1 3 | exec /sbin/agetty tty1 --noclear 38400 linux 4 | -------------------------------------------------------------------------------- /var/rc/sddm/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | mkdir -p /var/lib/sddm 3 | chown -R sddm:sddm /var/lib/sddm 4 | exec /usr/bin/sddm 5 | -------------------------------------------------------------------------------- /var/rc/udevd/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -x /usr/bin/udevd ]; then 4 | /usr/bin/udevd 5 | else 6 | /usr/lib/systemd/systemd-udevd 7 | fi 8 | -------------------------------------------------------------------------------- /var/rc/dbus/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | dbus-uuidgen --ensure=/etc/machine-id 3 | [ ! -d /run/dbus ] && install -m755 -g 81 -o 81 -d /run/dbus 4 | exec /usr/bin/dbus-daemon --system --nofork --nopidfile #--print-address=4 5 | -------------------------------------------------------------------------------- /sbin/shutdown: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [[ "$EUID" -ne 0 ]]; then 4 | echo "Must be executed by root" 5 | exit 1 6 | fi 7 | 8 | shutdown_mode=$(basename "$0") ## CAPTURE "poweroff" or "reboot" 9 | if [[ $(tty) =~ /dev/tty ]]; then 10 | /bin/rc.shutdown "$shutdown_mode" 11 | else 12 | nohup bash /bin/rc.shutdown "$shutdown_mode" | tee /dev/tty1 & 13 | fi 14 | -------------------------------------------------------------------------------- /var/rc/cgroups/cgroups.conf: -------------------------------------------------------------------------------- 1 | # cgroups mode 2 | # legacy mounts cgroups version 1 on /sys/fs/cgroup 3 | # unified mounts cgroups version 2 on /sys/fs/cgroup 4 | # hybrid mounts cgroups version 2 on /sys/fs/cgroup/unified and 5 | # cgroups version 1 on /sys/fs/cgroup 6 | 7 | CGROUP_MODE=hybrid 8 | 9 | # This is a list of controllers which should be enabled for cgroups version 2. 10 | # If hybrid mode is being used, controllers listed here will not be 11 | # available for cgroups version 1. none means no controllers will be used 12 | 13 | CGROUP_CONTROLLERS=none 14 | 15 | # This switch controls whether or not cgroups version 1 controllers are 16 | # individually mounted under 17 | # /sys/fs/cgroup in hybrid or legacy mode 18 | 19 | HAVE_CONTROLLER1_GROUPS=true 20 | -------------------------------------------------------------------------------- /var/rc/dtinit/dtinit.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo; 4 | 5 | log() { 6 | format="[ OK ]" 7 | echo "$format $1" > /dev/console 8 | } 9 | 10 | log "udevd" 11 | lk_runsvc /etc/rc/udevd 0 12 | log "udev-trigger" 13 | lk_runsvc /etc/rc/udev-trigger 0 14 | log "udev-settle" 15 | lk_runsvc /etc/rc/udev-settle 0 16 | 17 | log "cgroups" 18 | lk_runsvc /etc/rc/cgroups 0 19 | log "sysctl" 20 | lk_runsvc /etc/rc/sysctl 0 21 | log "dbus" 22 | lk_runsvc /etc/rc/dbus 0 23 | log "connmand" 24 | lk_runsvc /etc/rc/connmand 0 25 | log "elogind" 26 | lk_runsvc /etc/rc/elogind 0 27 | 28 | log "ttys" 29 | lk_runsvc /etc/rc/tty1 0 30 | lk_runsvc /etc/rc/tty2 0 31 | lk_runsvc /etc/rc/tty3 0 32 | lk_runsvc /etc/rc/tty4 0 33 | lk_runsvc /etc/rc/tty5 0 34 | lk_runsvc /etc/rc/tty6 0 35 | 36 | # Uncomment if needed 37 | # lk_runsvc /etc/rc/sddm 0 38 | 39 | lk_forever 3600 40 | 41 | -------------------------------------------------------------------------------- /var/rc/cgroups/cgroup-release-agent.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # This is run by the kernel after the last task is removed from a 3 | # control group in the openrc hierarchy. 4 | 5 | # Copyright (c) 2007-2015 The OpenRC Authors. 6 | # See the Authors file at the top-level directory of this distribution and 7 | # https://github.com/OpenRC/openrc/blob/master/AUTHORS 8 | # 9 | # This file is part of OpenRC. It is subject to the license terms in 10 | # the LICENSE file found in the top-level directory of this 11 | # distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE 12 | # This file may not be copied, modified, propagated, or distributed 13 | # except according to the terms contained in the LICENSE file. 14 | 15 | cgroup=/sys/fs/cgroup/openrc 16 | PATH=/bin:/usr/bin:/sbin:/usr/sbin 17 | if [ -d ${cgroup}/"$1" ]; then 18 | rmdir ${cgroup}/"$1" 19 | fi 20 | -------------------------------------------------------------------------------- /install-deps.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ "$(id -u)" -ne 0 ]; then 4 | echo "Must be executed as root" >&2 5 | exit 1 6 | fi 7 | 8 | if ! command -v git &> /dev/null; then 9 | echo "Git is not installed. Please install Git." >&2 10 | exit 1 11 | fi 12 | 13 | echo; 14 | echo " -< INSTALLING SINIT >-" 15 | git clone https://git.suckless.org/sinit 16 | cd sinit/ 17 | make 18 | make install 19 | cd .. 20 | 21 | echo; 22 | echo " -< INSTALLING DAEMONTOOLS-ENCORE >-" 23 | git clone https://github.com/bruceg/daemontools-encore 24 | cd daemontools-encore/ 25 | ./makemake 26 | make 27 | make install 28 | cd .. 29 | 30 | echo; 31 | echo " -< INSTALLING LITTKIT >-" 32 | curl -o littkit.tgz http://troubleshooters.com/projects/littkit/downloads/littkit_0_90.tgz 33 | tar xvf littkit.tgz 34 | cd littkit_0_90 35 | cp -v lk_* /usr/local/bin 36 | cd .. 37 | 38 | if [ $? -eq 0 ]; then 39 | echo; 40 | echo " -< Succeeded >-" 41 | fi 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Andrew 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /bin/rc.shutdown: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [[ "$EUID" -ne 0 ]]; then 4 | echo "Must be executed by root" 5 | exit 1 6 | fi 7 | 8 | shutdown_mode=$(basename "$1") ## CAPTURE "poweroff" or "reboot" 9 | 10 | echo "Killing non-tty daemontools-encore services..." 11 | for serv in /etc/rc/*; do 12 | servname=$(basename "$serv") 13 | 14 | if [[ ! "$servname" =~ ^tty|^dtinit ]]; then 15 | lk_killsvc "$serv" 0 16 | echo "[ !! ] Terminated $servname" 17 | fi 18 | done 19 | 20 | echo; 21 | echo "[ {} ] Unmounting /dev/pts and /dev/shm..." 22 | umount /dev/pts 23 | umount /dev/shm 24 | 25 | 26 | echo; 27 | interfaces=$(ip link show | awk -F': ' '{print $2}') 28 | for interface in $interfaces; do 29 | ip link set dev "$interface" down 30 | echo "[ <> ] Interface $interface is down" 31 | done 32 | 33 | 34 | echo; 35 | echo "[ !! ] pkill -15 (requesting)..." 36 | pkill -15 -e 1 37 | 38 | 39 | echo "[ !! ] pkill -9 (murdering)..." 40 | pkill -9 -e 1 41 | 42 | 43 | echo; 44 | echo "[ {} ] swapoff -a..." 45 | swapoff -a 46 | 47 | echo "[ {} ] Remounting filesystems as read-only..." 48 | echo "u" > /proc/sysrq-trigger 49 | sleep 3 50 | 51 | echo "[ {} ] Syncing filesystems..." 52 | sync 53 | echo "s" > /proc/sysrq-trigger 54 | sleep 3 55 | 56 | echo; 57 | if test "$shutdown_mode" = "reboot"; then 58 | echo "[ OK ] Rebooting..." 59 | sleep 1 60 | echo "b" > /proc/sysrq-trigger 61 | else 62 | echo "[ OK ] Powering off..." 63 | sleep 1 64 | echo "o" > /proc/sysrq-trigger 65 | fi 66 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ "$(id -u)" -ne 0 ]; then 4 | echo "Must be executed as root" >&2 5 | exit 1 6 | fi 7 | 8 | echo -e "It is highly recommended to run this script from chroot to avoid system crash in the middle of the process.\nThis script will wipe directories /var/rc/ and /etc/rc/ if you have these." 9 | read -p "Continue running the script? (y/N) " response 10 | 11 | if [ "$response" != "y" ] 12 | then 13 | exit 1 14 | fi 15 | 16 | echo; 17 | echo " -< Copying services... >-" 18 | rm -rf /var/rc &> /dev/null 19 | mkdir /var/rc 20 | cp -rv ./var/rc /var/ 21 | 22 | echo; 23 | echo " -< Making symlinks... >-" 24 | rm -rf /etc/rc &> /dev/null 25 | mkdir /etc/rc 26 | ln -sv /var/rc/* /etc/rc/ 27 | 28 | echo; 29 | echo " -< Creating reserve copies of important files... >-" 30 | files=("shutdown" "reboot" "poweroff") 31 | for file in "${files[@]}" 32 | do 33 | filename=$(basename "$file") 34 | mv -v /sbin/"$file" /sbin/"$file-old" 35 | done 36 | 37 | echo; 38 | echo " -< Copying rc.init and rc.shutdown... >-" 39 | cp -v ./bin/* /bin/ 40 | 41 | echo; 42 | echo " -< Copying shutdown script and making symlinks... >-" 43 | cp -v ./sbin/* /sbin/ 44 | ln -sv /sbin/shutdown /sbin/reboot 45 | ln -sv /sbin/shutdown /sbin/poweroff 46 | 47 | echo; 48 | echo " -< Installing suckless init as default... >-" 49 | mv -v /sbin/init /sbin/init-old 50 | cp -v /usr/local/bin/sinit /sbin/sinit 51 | cp -v /sbin/sinit /sbin/init 52 | 53 | if [ $? -eq 0 ]; then 54 | echo; 55 | echo " -< Succeeded. Now you can reboot >-" 56 | fi 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sinit-scripts 2 | 3 | Collection of services for [suckless init](https://core.suckless.org/sinit/) with [daemontools-encore](http://untroubled.org/daemontools-encore/) process manager and [LittKit](http://troubleshooters.com/projects/littkit/) for process ordering. Tested on Artix Linux, but can probably work on other distros. 4 | 5 | Insipired by [this](http://troubleshooters.com/linux/diy/suckless_init_on_plop.htm) guide. 6 | 7 | ![megainit](./stuff/artix-sinit.png) 8 | 9 | ## How it works? 10 | 11 | 1. Kernel executes `sinit` (suckless init). 12 | 2. `sinit` executes `/bin/rc.init`. 13 | 3. `/bin/rc.init` does some important stuff with pseudo filesystems and executes services in `/etc/rc/`. 14 | 4. `dtinit` (daemontools init) is the first service to start in `/etc/rc/` and it executes other services in `/etc/rc/` in proper order using LittKit's `lk_runsvc`. 15 | 16 | ## Adding new services 17 | ```bash 18 | mkdir /var/rc/service_name 19 | touch /var/rc/service_name/run # main execution script for the service. Note: daemontools-encore is going to restart the service every time it finishes execution of the run file. I you want to avoid this add "lk_forever 3600" at the end of the script. 20 | chmod u+x /var/rc/service_name/run 21 | ln -s /var/rc/service_name /etc/rc/ 22 | # Add "log 'service_name'" (optionally) and "lk_runsvc /etc/rc/service_name 0" to /etc/rc/dtinit/dtinit.sh 23 | ``` 24 | 25 | ## Installation 26 | First of all, clone, the repo: 27 | ```bash 28 | git clone https://github.com/Andrey0189/sinit-scripts 29 | cd sinit-scripts/ 30 | ``` 31 | Then you need to install suckless init, daemontools-encore and LittKit. You can do it automatically using file `install-deps.sh`, but doing it manually is safer, if for example, you get an error during compilation. 32 | ```bash 33 | # sinit 34 | git clone https://git.suckless.org/sinit 35 | cd sinit/ 36 | make 37 | sudo make install 38 | ``` 39 | ```bash 40 | # daemontools-encore 41 | git clone https://github.com/bruceg/daemontools-encore 42 | cd daemontools-encore/ 43 | ./makemake 44 | make 45 | sudo make install 46 | ``` 47 | ```bash 48 | # LittKit 49 | curl -o littkit.tgz http://troubleshooters.com/projects/littkit/downloads/littkit_0_90.tgz 50 | tar xvf littkit.tgz 51 | cd littkit_0_90 52 | sudo cp lk_* /usr/local/bin 53 | ``` 54 | \ 55 | **I highly recommend performing the next instructions from chroot via some livecd. You can try to do this on working machine, but chroot is much safer.** 56 | 57 | 58 | After you installed these dependencies, you can run the installation script for services, that will replace your current init with suckless init. 59 | ```bash 60 | # Assuming you are doing it from chroot 61 | ./install.sh 62 | exit 63 | umount -R /mnt 64 | reboot 65 | ``` 66 | If everything was done correctly, you can finally boot into your system with the suckless init! 67 | 68 | ## Possible issues 69 | * `/bin/rc.shutdown` is a little bit problematic and doesn't unmount filesystems properly on every shut down, which possibly may lead to filesystem damage. To be fixed soon. 70 | 71 | Currently only tested on these distos: 72 | 73 | - Artix 74 | - Arch 75 | 76 | Works perfectly on Artix, but on Arch you need to install elogind. If it works on your distro, let me know in pull requests. 77 | 78 | *** 79 | -------------------------------------------------------------------------------- /bin/rc.init: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin 4 | distro_name=$(grep "^PRETTY_NAME=" /etc/os-release | sed 's/^PRETTY_NAME=//; s/"//g') 5 | 6 | echo; 7 | echo "[ *** ] Welcome to $distro_name!" 8 | echo; 9 | echo "Started Suckless init..." 10 | 11 | echo "Mounting everything in /etc/fstab..." 12 | mount -a -O no_netdev 13 | 14 | echo "Mounting sys, efi, proc and dev..." 15 | mountpoint -q /sys || mount -t sysfs sys /sys -o nosuid,noexec,nodev 16 | mountpoint -q /sys/kernel/security || mount -n -t securityfs securityfs /sys/kernel/security 17 | [ -d /sys/firmware/efi ] && (mountpoint -q /sys/firmware/efi/efivars || mount -n -t efivarfs -o ro efivarfs /sys/firmware/efi/efivars) 18 | mountpoint -q /proc || mount -t proc proc /proc -o nosuid,noexec,nodev 19 | mountpoint -q /dev || mount -t devtmpfs dev /dev -o mode=0755,nosuid 20 | 21 | # seed /dev with some things that might be needed (for example, 22 | # xudev doesn't do this compared to eudev), code from OpenRC 23 | 24 | # creating /dev/console, /dev/tty and /dev/tty1 to be able to write 25 | # to $CONSOLE with/without bootsplash before udevd creates it 26 | echo "Preparing ttys..." 27 | [ -c /dev/console ] || mknod -m 600 /dev/console c 5 1 28 | [ -c /dev/tty1 ] || mknod -m 620 /dev/tty1 c 4 1 29 | [ -c /dev/tty ] || mknod -m 666 /dev/tty c 5 0 30 | 31 | # udevd will dup its stdin/stdout/stderr to /dev/null 32 | # and we do not want a file which gets buffered in ram 33 | echo "Creating /dev/null and /dev/kmsg..." 34 | [ -c /dev/null ] || mknod -m 666 /dev/null c 1 3 35 | 36 | # so udev can add its start-message to dmesg 37 | [ -c /dev/kmsg ] || mknod -m 660 /dev/kmsg c 1 11 38 | 39 | # extra symbolic links not provided by default 40 | echo "Making extra symbolic links not provided by default..." 41 | [ -e /dev/fd ] || ln -snf /proc/self/fd /dev/fd 42 | [ -e /dev/stdin ] || ln -snf /proc/self/fd/0 /dev/stdin 43 | [ -e /dev/stdout ] || ln -snf /proc/self/fd/1 /dev/stdout 44 | [ -e /dev/stderr ] || ln -snf /proc/self/fd/2 /dev/stderr 45 | [ -e /proc/kcore ] && ln -snf /proc/kcore /dev/core 46 | 47 | echo "Creating /dev/pts, /dev/shm and /run..." 48 | mkdir -p /dev/pts /dev/shm 49 | mountpoint -q /dev/pts || mount -t devpts devpts /dev/pts -o mode=0620,gid=5,nosuid,noexec 50 | mountpoint -q /dev/shm || mount -t tmpfs shm /dev/shm -o mode=1777,nosuid,nodev 51 | mountpoint -q /run || mount -t tmpfs run /run -o mode=0755,nosuid,nodev 52 | 53 | echo "Enabling swap..." 54 | swapon -a 55 | 56 | echo "Setting up hostname..." 57 | [ -s /etc/hostname ] && HOSTNAME="$(cat /etc/hostname)" 58 | [ "$HOSTNAME" ] && echo "$HOSTNAME" >| /proc/sys/kernel/hostname 59 | 60 | echo; 61 | echo "Starting daemontools-encore..." 62 | 63 | lk_prepare /etc/rc 64 | # svc -dx /etc/rc/* /etc/rc/*/log > /dev/null 2>&1 65 | 66 | env - PATH=$PATH svscan /etc/rc 2>&1 | \ 67 | env - PATH=$PATH readproctitle service errors: ............................................................................................................................................................................................................................................................................................................................................................................................................... 68 | -------------------------------------------------------------------------------- /var/rc/cgroups/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | [ -r /var/rc/cgroups/cgroups.conf ] && . /var/rc/cgroups/cgroups.conf 4 | 5 | CGROUP_OPTS=nodev,noexec,nosuid 6 | 7 | [ "$CGROUP_CONTROLLERS" = "none" ] && CGROUP_CONTROLLERS="" 8 | 9 | cgroup2_find_path() { 10 | if grep -qw cgroup2 /proc/filesystems; then 11 | case "${CGROUP_MODE}" in 12 | hybrid) printf "/sys/fs/cgroup/unified" ;; 13 | unified) printf "/sys/fs/cgroup" ;; 14 | esac 15 | fi 16 | return 0 17 | } 18 | 19 | cgroup1_base() { 20 | grep -qw cgroup /proc/filesystems || return 0 21 | if ! mountpoint -q /sys/fs/cgroup; then 22 | local opts="${CGROUP_OPTS},mode=755,size=${rc_cgroupsize:-10m}" 23 | mount -n -t tmpfs -o "${opts}" cgroup_root /sys/fs/cgroup 24 | fi 25 | 26 | if ! mountpoint -q /sys/fs/cgroup/openrc; then 27 | local agent 28 | agent="/var/rc/cgroups/cgroup-release-agent.sh" 29 | mkdir /sys/fs/cgroup/openrc 30 | mount -n -t cgroup -o none,${CGROUP_OPTS},name=openrc,release_agent="$agent" openrc /sys/fs/cgroup/openrc 31 | printf 1 > /sys/fs/cgroup/openrc/notify_on_release 32 | fi 33 | return 0 34 | } 35 | 36 | cgroup1_controllers() { 37 | ${HAVE_CONTROLLER1_GROUPS} && [ -e /proc/cgroups ] && grep -qw cgroup /proc/filesystems || return 0 38 | while read -r name _ _ enabled _; do 39 | case "${enabled}" in 40 | 1) if mountpoint -q "/sys/fs/cgroup/${name}";then continue;fi 41 | local x 42 | for x in $CGROUP_CONTROLLERS; do 43 | [ "${name}" = "blkio" ] && [ "${x}" = "io" ] && 44 | continue 2 45 | [ "${name}" = "${x}" ] && 46 | continue 2 47 | done 48 | mkdir "/sys/fs/cgroup/${name}" 49 | mount -n -t cgroup -o "${CGROUP_OPTS},${name}" "${name}" "/sys/fs/cgroup/${name}" 50 | ;; 51 | esac 52 | done < /proc/cgroups 53 | return 0 54 | } 55 | 56 | cgroup2_base() { 57 | grep -qw cgroup2 /proc/filesystems || return 0 58 | local base 59 | base="$(cgroup2_find_path)" 60 | mkdir -p "${base}" 61 | mount -t cgroup2 none -o "${CGROUP_OPTS},nsdelegate" "${base}" 2> /dev/null || 62 | mount -t cgroup2 none -o "${CGROUP_OPTS}" "${base}" 63 | return 0 64 | } 65 | 66 | cgroup2_controllers() { 67 | grep -qw cgroup2 /proc/filesystems || return 0 68 | local active cgroup_path x y 69 | cgroup_path="$(cgroup2_find_path)" 70 | [ -z "${cgroup_path}" ] && return 0 71 | [ -e "${cgroup_path}/cgroup.controllers" ] && read -r active < "${cgroup_path}/cgroup.controllers" 72 | for x in ${CGROUP_CONTROLLERS}; do 73 | for y in ${active}; do 74 | [ "$x" = "$y" ] && [ -e "${cgroup_path}/cgroup.subtree_control" ] && 75 | echo "+${x}" > "${cgroup_path}/cgroup.subtree_control" 76 | done 77 | done 78 | return 0 79 | } 80 | 81 | cgroups_hybrid() { 82 | cgroup1_base 83 | cgroup2_base 84 | cgroup2_controllers 85 | cgroup1_controllers 86 | return 0 87 | } 88 | 89 | cgroups_legacy() { 90 | cgroup1_base 91 | cgroup1_controllers 92 | return 0 93 | } 94 | 95 | cgroups_unified() { 96 | cgroup2_base 97 | cgroup2_controllers 98 | return 0 99 | } 100 | 101 | mount_cgroups() { 102 | case "${CGROUP_MODE}" in 103 | hybrid) cgroups_hybrid ;; 104 | legacy) cgroups_legacy ;; 105 | unified) cgroups_unified ;; 106 | esac 107 | return 0 108 | } 109 | 110 | mount_cgs() { 111 | if [ -d /sys/fs/cgroup ];then 112 | mount_cgroups 113 | return 0 114 | fi 115 | return 1 116 | } 117 | 118 | mount_cgs 119 | 120 | lk_forever 3600 121 | --------------------------------------------------------------------------------