SysVinit

SysVinit, also known as System V init or simply "init," is one of the oldest and most traditional init systems used in Unix and early Linux distributions. It is responsible for initializing the system, starting and stopping services, and changing runlevels.

Overview of SysVinit

Key Concepts

  • Initialization Scripts: Located in /etc/init.d/, these scripts are used to start, stop, and manage services.

  • Runlevels: Define different states of the system, each of which is associated with a specific set of services and processes.

  • Inittab File: The main configuration file (/etc/inittab) that defines how the system should be initialized and which services should be started at each runlevel.

Runlevels

  • 0: Halt the system

  • 1: Single-user mode (maintenance or emergency mode)

  • 2: Multi-user mode without networking

  • 3: Multi-user mode with networking

  • 4: Unused (can be user-defined)

  • 5: Multi-user mode with networking and graphical interface

  • 6: Reboot the system

Initialization Process

  1. Kernel Boot: The kernel is loaded into memory by the bootloader and begins initialization.

  2. Run Init: The kernel starts the init process, which reads the /etc/inittab file.

  3. Set Runlevel: The init process sets the default runlevel specified in the /etc/inittab file.

  4. Execute Scripts: The init process executes the appropriate scripts for the current runlevel, starting and stopping services as needed.

/etc/inittab Configuration

The /etc/inittab file contains configuration directives for the init process. Each line in the file defines an action to be taken when the system enters a particular runlevel.

Example /etc/inittab

# Default runlevel
id:3:initdefault:

# System initialization
si::sysinit:/etc/rc.d/rc.sysinit

# Runlevel 0: halt the system
l0:0:wait:/etc/rc.d/rc 0

# Runlevel 1: single-user mode
l1:1:wait:/etc/rc.d/rc 1

# Runlevel 2: multi-user mode without networking
l2:2:wait:/etc/rc.d/rc 2

# Runlevel 3: multi-user mode with networking
l3:3:wait:/etc/rc.d/rc 3

# Runlevel 4: unused
l4:4:wait:/etc/rc.d/rc 4

# Runlevel 5: multi-user mode with graphical interface
l5:5:wait:/etc/rc.d/rc 5

# Runlevel 6: reboot the system
l6:6:wait:/etc/rc.d/rc 6

# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t1 -a -r now

# Respawn getty processes
1:2345:respawn:/sbin/getty 38400 tty1
2:2345:respawn:/sbin/getty 38400 tty2
3:2345:respawn:/sbin/getty 38400 tty3
4:2345:respawn:/sbin/getty 38400 tty4
5:2345:respawn:/sbin/getty 38400 tty5
6:2345:respawn:/sbin/getty 38400 tty6

Service Management

Services in SysVinit are managed using scripts located in /etc/init.d/. These scripts accept commands such as start, stop, restart, and status.

Example: Managing a Service

  • Starting a Service:

    sudo /etc/init.d/apache2 start
  • Stopping a Service:

    sudo /etc/init.d/apache2 stop
  • Restarting a Service:

    sudo /etc/init.d/apache2 restart
  • Checking Status of a Service:

    sudo /etc/init.d/apache2 status

Runlevel Directories

Runlevel directories (/etc/rc.d/ or /etc/rc?.d/) contain symbolic links to the initialization scripts in /etc/init.d/. These directories are named according to their corresponding runlevel (e.g., rc0.d, rc1.d, etc.).

Example: Runlevel 3 Directory (/etc/rc3.d/)

/etc/rc3.d/
├── K01service1 -> ../init.d/service1
├── S01service2 -> ../init.d/service2
├── S02service3 -> ../init.d/service3
  • K: Stands for "kill" and is used to stop services.

  • S: Stands for "start" and is used to start services.

The numbers following K and S determine the order in which services are started or stopped.

Conclusion

SysVinit is a foundational init system that uses scripts and runlevels to manage the initialization and shutdown of services on Unix and early Linux systems. Understanding how to configure and manage SysVinit is crucial for maintaining and troubleshooting systems that use this traditional init system.

Last updated