Linux Runlevels

Posted by

Key Points

This article will explain following points

    • Stages of Linux bootup
    • “Init” stage of Linux Operating system bootup.
    • How to add/modify run level script

Following is a high level view  of Linux booting mechanism :

After the kernel boots up , it will be in  init/runlevel  state.   In this state, kernel reads the run level config file from “/etc/inittab” and run the default runlevel folder scripts/executable. In the absence of any file,  kernel booting is configured with default run level.

Unless the user specifies another value via kernel boot parameter, the system will attempt to enter (start) the default “runlevel”.

Linux boot up init / runlevel stage

Few services are required to be launched for providing access to various features at user level. There will be few  init/startup scripts which loads required services.

These scripts are loaded at “runlevel”. Where “runlevel” is one of mode oin Unix based operating system  to load all mandatory services .

There are many runlevels.  each runlevel has a certain number of services stopped or started. Conventionally, seven runlevels exist from 0 to 6.

Run Level Action

0 Halt Shuts down system
1 Single-User Mode Does not configure network interfaces, daemons.
2 Multi-User Mode Does not configure network interfaces or start daemons.
3 Multi-User Mode with Networking Starts the system normally.
4 Undefined Not used/User-definable
5 X11 As runlevel 3 + display manager(X)
6 Reboot Reboots the system

As  there are multiple runlevels , We can utilize these runlevels to  boot up with a
different set of programs. There are runlevels to  halt and reboot too.

So system can move into halt state  by executing  runlevel 0 and can reboot with runlevel 6.
Sometimes you may find few runlevels are same.  For ex  ubuntu runlevel 3 and 5 are copied from 2.

If user wants to run an application after booting at init stage then he needs to add user script in root file system, make the entry in “runlevel” and update the “runlevel” file system .  In the  next boot  time that service/script will be considered and loaded in “init” mode.

How Linux boots in Raspberry PI ?

Here are some snapshots kernel booting log : 

[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Linux version 4.19.97-v7+ (dom@buildbot) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1294 SMP Thu Jan 30 13:15:58 GMT 2020
[    0.000000] CPU: ARMv7 Processor [410fd034] revision 4 (ARMv7), cr=10c5383d
[    0.000000] CPU: div instructions available: patching division code
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[    0.000000] OF: fdt: Machine model: Raspberry Pi 3 Model B Plus Rev 1.3

::::::::::::::::::::::::::::

Kernel boot parameters

[    0.000000] Kernel command line: coherent_pool=1M 8250.nr_uarts=0 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2708_fb.fbswap=1 smsc95xx.macaddr=B8:27:EB:AD:1E:BB vc_mem.mem_base=0x3ec00000 vc_mem.mem_size=0x40000000  console=ttyS0,115200 console=tty1 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

:::::::::::::::::::::::::::::::

UART driver

0.031776] Serial: AMBA PL011 UART driver
[    0.034140] bcm2835-mbox 3f00b880.mailbox: mailbox enabled
[    0.050038] raspberrypi-firmware soc:firmware: Attached to firmware from 2020-02-12 12:38, variant start
[    0.060051] raspberrypi-firmware soc:firmware: Firmware hash is 53a54c770c493957d99bf49762dfabc4eee00e45
[    0.103152] bcm2835-dma 3f007000.dma: DMA legacy API manager at (ptrval), dmachans=0x1
[    0.104793] SCSI subsystem initialized


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Root File system mounting 

rypi kernel: [    0.826301] Waiting for root device /dev/mmcblk0p7...
Feb 23 00:11:24 raspberrypi kernel: [    0.893531] random: fast init done
Feb 23 00:11:24 raspberrypi kernel: [    0.902351] mmc1: new high speed SDIO card at address 0001
Feb 23 00:11:24 raspberrypi kernel: [    0.912804] mmc0: host does not support reading read-only switch, assuming write-enable
Feb 23 00:11:24 raspberrypi kernel: [    0.916403] mmc0: new high speed SDHC card at address aaaa
Feb 23 00:11:24 raspberrypi kernel: [    0.917530] mmcblk0: mmc0:aaaa JULIE 14.8 GiB
Feb 23 00:11:24 raspberrypi kernel: [    0.922654]  mmcblk0: p1 p2 < p5 p6 p7 >
Feb 23 00:11:24 raspberrypi kernel: [    0.946925] Indeed it is in host mode hprt0 = 00021501
Feb 23 00:11:24 raspberrypi kernel: [    0.951321] EXT4-fs (mmcblk0p7): mounted filesystem with ordered data mode. Opts: (null)
Feb 23 00:11:24 raspberrypi kernel: [    0.951374] VFS: Mounted root (ext4 filesystem) readonly on device 179:7.
Feb 23 00:11:24 raspberrypi kernel: [    0.958902] devtmpfs: mounted
Feb 23 00:11:24 raspberrypi kernel: [    1.008777] Freeing unused kernel memory: 1024K
Feb 23 00:11:24 raspberrypi kernel: [    1.037208] Run /sbin/init as init process


::::::::::::::::::::::::::::::::::::::::::::::::::::::
System Level script 

rypi systemd[1]: Starting /etc/rc.local Compatibility...
Feb 23 00:11:25 raspberrypi systemd[1]: Starting Permit User Sessions...
Feb 23 00:11:25 raspberrypi systemd[1]: Started /etc/rc.local Compatibility.


:::::::::::::::::::::::::::::::::::::::::::
Feb 24 21:49:20 raspberrypi systemd[1]: Starting Daily man-db regeneration...
Feb 24 21:49:20 raspberrypi systemd[1]: Starting Daily apt download activities...
Feb 24 21:49:20 raspberrypi systemd[1]: Starting Rotate log files...

Startup script or init script experiment is performed below :

Experiment

Aim:  Add a user dummy script in “runlevel” so at the time of init, script will loaded and some
dummy service should start.

1.  User script has to be placed in  kernel root file system  As it is “init” section part we are placing  our script at “/etc/init.d/hello.sh” as shown below. Following script  will load a “w1” named dummy process.

2. Check the default “runlevel” of system by typing command “runlevel”.  runlevel command prints the previous and current run level . If there is no previous runlevel, It prints N.

3. Create a softlink of user script in rc5.d folder as default runlevel is 5. As shown in the picture below S01hello soft link will be created Here ‘S01’ Indicate as start time process.

4. After Creating soft link Update the rc directory file system by giving below command.

Observations

Linux “top”  command  displays information about running processes

  •  “top” Command output before modified startup script activation

  • “top” command output after modified startup script activation

As you can see “W1” service is running when system reboots and again starts up

Conclusion:

After all this we can reboot the system and you will notice  “w1” process  loaded due to  intialization script .

Embedded Software Engineer
Skills: C Programming, Embedded System Designing
Qualification: Electrical Engineering, PGd- Embedded Systems designing

Thanks for reading till end. If you liked it, Please share in your network.

You may also like GitHub Video 

Have a look at Embedkari Low Cost Products 

You may  find interesting topics at HowTo

Also subscribe  Embedkari for other interesting topics.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.