注意:

The Funtoo Linux project has transitioned to "Hobby Mode" and this wiki is now read-only.

Difference between revisions of "Runit"

From Funtoo
Jump to navigation Jump to search
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''''runit''''' is a cross-platform Unix init scheme with service supervision, a replacement for sysvinit, and other init schemes. It can be used together with OpenRC as an alternative to sysvinit or replace OpenRC completely.
'''''[http://smarden.org/runit/ runit]''''' is a cross-platform Unix init scheme with service supervision, a replacement for sysvinit, and other init schemes. It can be used together with OpenRC as an alternative to sysvinit or replace OpenRC completely.


== Installation ==
== Installation ==
Line 7: Line 7:


== Configuration over OpenRC ==
== Configuration over OpenRC ==
runit can be used as the system init. To do this, simply add {{f|init{{=}}/sbin/runit-init}} to the bootloader's kernel command-line and reboot. OpenRC (sysvinit) will remain installed and you go back to it at any time by reverting the change.
'''''runit''''' can be used as the system init. To do this, simply add {{f|init{{=}}/sbin/runit-init}} to the bootloader's kernel command-line and reboot. OpenRC will remain installed and you go back to it at any time by reverting the change.


At this point, we are still somewhat relying on OpenRC. Because runit is very minimal, and unless you have all the boot/init scripts required, you should prefer this combo.
At this point, we are still somewhat relying on OpenRC. Because '''''runit''''' is very minimal, and unless you have all the boot/init scripts required, you should prefer this combo.


{{c|runit}} performs the system's booting, running and shutting down in '''3 stages''':
{{c|runit}} performs the system's booting, running and shutting down in '''3 stages''':
* '''Stage 1''':<br />
* '''Stage 1''':<br />
''runit'' starts {{f|/etc/runit/1}} and waits for it to terminate. The system's one time initialization tasks are done here. {{f|/etc/runit/1}} has full control over {{f|/dev/console}} to be able to start an emergency shell in case the one time initialization tasks fail.
'''''runit''''' starts {{f|/etc/runit/1}} and waits for it to terminate. The system's one time initialization tasks are done here. {{f|/etc/runit/1}} has full control over {{f|/dev/console}} to be able to start an emergency shell in case the one time initialization tasks fail.
* '''Stage 2''':<br />
* '''Stage 2''':<br />
''runit'' starts {{f|/etc/runit/2}} which should not return until the system is going to halt or reboot; if it crashes, it will be restarted. Normally, {{f|/etc/runit/2}} runs [http://smarden.org/runit/runsvdir.8.html runsvdir].
'''''runit''''' starts {{f|/etc/runit/2}} which should not return until the system is going to halt or reboot; if it crashes, it will be restarted. Normally, {{f|/etc/runit/2}} runs [http://smarden.org/runit/runsvdir.8.html runsvdir].
* '''Stage 3''':<br />
* '''Stage 3''':<br />
If ''runit'' is told to halt or reboot the system, or '''Stage 2''' returns without errors, it terminates '''Stage 2''' if it is running, and runs {{f|/etc/runit/3}}. The systems tasks to shutdown and halt or reboot are done here.
If '''''runit''''' is told to halt or reboot the system, or '''Stage 2''' returns without errors, it terminates '''Stage 2''' if it is running, and runs {{f|/etc/runit/3}}. The systems tasks to shutdown and halt or reboot are done here.


Now, let's configure '''Stage 1'''
First, configure '''Stage 1'''
{{file|name=/etc/runit/1|desc=Stage 1 script file|body=
{{file|name=/etc/runit/1|desc=Stage 1 script file|body=
#!/bin/sh
#!/bin/sh
Line 38: Line 38:


It might look like OpenRC is running the whole thing during boot time, but once you log in and issue {{c|pstree}} you will see which init has started.
It might look like OpenRC is running the whole thing during boot time, but once you log in and issue {{c|pstree}} you will see which init has started.
Let's check if our ''runit'' works. We will create a new service for it, and then write a script to run something.
{{console|body=
$ ##i## ls /etc/sv
}}
{{tip|Feel free to study /etc/sv directory. This is where runit's services are.}}
By default, you should see only the list of {{f|getty-tty{1..6} }}
If you run {{c|ls}} on either one of those, you will notice three items:
# {{f|run}} script - just a shell script that starts our service
# {{f|finish}} script - another shell script to end our service
# {{f|supervise}} directory - this directory contains access control settings for services
Now, let's make a service of our own...
{{console|body=
###i## mkdir /etc/sv/gpm
###i## touch /etc/sv/gpm/run
}}
{{file|name=/etc/sv/gpm/run |desc=An example of a run script for a gpm service |body=
#!/bin/sh
exec 2>&1
source /etc/conf.d/gpm
exec gpm -D -m ${MOUSEDEV} -t ${MOUSE}
}}
We have written the script, now let's make it +x (executable):
{{console|body=
###i## chmod a+x /etc/sv/gpm/run
}}
And there is one more thing we must do... Even though our {{f|/etc/sv/}} directory now contains an executable script to start a gpm service, '''runit''' will not be aware of this service until it's soft-linked to {{f|/etc/service/}}. Remember, the services can be present in the {{f|/etc/sv}} directory, '''runit''' will not be aware of them until they are linked.
{{console|body=
###i## ln -sv /etc/sv/gpm /etc/service/
}}
{{warning|Please don't confuse {{f|/etc/service/}} (which is a directory) with {{f|/etc/services}} (which is a file)! You need a directory! }}
{{note|If you don't have the {{f|/etc/service/}} directory, create one. This directory should only contain symbolic links to services in {{f|/etc/sv/}}. Since runit installs getty-tty{1..6} services by default, up and running, you probably have this directory. }}
Now let's see if '''runit''' will run our service for us...
{{console|body=
###i## sv up gpm
}}
It would be a good idea to switch to a tty, and check if gpm is running... Another way of doing that is:
{{console|body=
###i## sv status gpm
}}
If the service is running, you should see something like this:
{{console|body=
run: gpm: (pid 11948) 178s
}}
To shut the service down:
{{console|body=
###i## sv down gpm
}}
{{Note|Runit does not accept the traditional shutdown/reboot commands, use the following instead:
{{console|body=
###i## runit-init 0
}}
which will shutdown to halt, and
{{console|body=
###i## runit-init 6
}}
which will reboot.
You can '''''alias''''' these commands or simply put them in an executable bash script.
}}
{{tip|In case you want to find out more, [http://smarden.org/runit/faq.html here]'s runit's '''FAQ''' and [http://smarden.org/runit/runscripts.html here] you can find a collection of various runit scripts.}}

Latest revision as of 11:22, June 12, 2017

runit is a cross-platform Unix init scheme with service supervision, a replacement for sysvinit, and other init schemes. It can be used together with OpenRC as an alternative to sysvinit or replace OpenRC completely.

Installation

root # emerge -av sys-process/runit

Configuration over OpenRC

runit can be used as the system init. To do this, simply add init=/sbin/runit-init to the bootloader's kernel command-line and reboot. OpenRC will remain installed and you go back to it at any time by reverting the change.

At this point, we are still somewhat relying on OpenRC. Because runit is very minimal, and unless you have all the boot/init scripts required, you should prefer this combo.

runit performs the system's booting, running and shutting down in 3 stages:

  • Stage 1:

runit starts /etc/runit/1 and waits for it to terminate. The system's one time initialization tasks are done here. /etc/runit/1 has full control over /dev/console to be able to start an emergency shell in case the one time initialization tasks fail.

  • Stage 2:

runit starts /etc/runit/2 which should not return until the system is going to halt or reboot; if it crashes, it will be restarted. Normally, /etc/runit/2 runs runsvdir.

  • Stage 3:

If runit is told to halt or reboot the system, or Stage 2 returns without errors, it terminates Stage 2 if it is running, and runs /etc/runit/3. The systems tasks to shutdown and halt or reboot are done here.

First, configure Stage 1

   /etc/runit/1 - Stage 1 script file
#!/bin/sh
# system one time tasks

PATH=/sbin:/usr/sbin:/bin:/usr/bin

RUNLEVEL=S /sbin/rc sysinit
RUNLEVEL=S /sbin/rc boot
# We will add default runlevel here as well
RUNLEVEL=S /sbin/rc default

touch /etc/runit/stopit
chmod 0 /etc/runit/stopit

Now it's time to reboot...

It might look like OpenRC is running the whole thing during boot time, but once you log in and issue pstree you will see which init has started.

Let's check if our runit works. We will create a new service for it, and then write a script to run something.

user $  ls /etc/sv
   Tip

Feel free to study /etc/sv directory. This is where runit's services are.

By default, you should see only the list of getty-tty{1..6} If you run ls on either one of those, you will notice three items:

  1. run script - just a shell script that starts our service
  2. finish script - another shell script to end our service
  3. supervise directory - this directory contains access control settings for services

Now, let's make a service of our own...

root # mkdir /etc/sv/gpm
root # touch /etc/sv/gpm/run
   /etc/sv/gpm/run - An example of a run script for a gpm service
#!/bin/sh

exec 2>&1
source /etc/conf.d/gpm
exec gpm -D -m ${MOUSEDEV} -t ${MOUSE}

We have written the script, now let's make it +x (executable):

root # chmod a+x /etc/sv/gpm/run

And there is one more thing we must do... Even though our /etc/sv/ directory now contains an executable script to start a gpm service, runit will not be aware of this service until it's soft-linked to /etc/service/. Remember, the services can be present in the /etc/sv directory, runit will not be aware of them until they are linked.

root # ln -sv /etc/sv/gpm /etc/service/
   Warning

Please don't confuse /etc/service/ (which is a directory) with /etc/services (which is a file)! You need a directory!

   Note

If you don't have the /etc/service/ directory, create one. This directory should only contain symbolic links to services in /etc/sv/. Since runit installs getty-tty{1..6} services by default, up and running, you probably have this directory.

Now let's see if runit will run our service for us...

root # sv up gpm

It would be a good idea to switch to a tty, and check if gpm is running... Another way of doing that is:

root # sv status gpm

If the service is running, you should see something like this:

run: gpm: (pid 11948) 178s

To shut the service down:

root # sv down gpm
   Note

Runit does not accept the traditional shutdown/reboot commands, use the following instead:

root # runit-init 0

which will shutdown to halt, and

root # runit-init 6

which will reboot.

You can alias these commands or simply put them in an executable bash script.

   Tip

In case you want to find out more, here's runit's FAQ and here you can find a collection of various runit scripts.