Sunday, October 9, 2016

systemd and its "unit"

When the system is started, a unit called default.target is activated at first. The setting file for this unit is at a directory: /etc/systemd/system.

graphical.target is a unit which corresponds to runlevel 5. Although runlevel was used in SysVinit, units called target are used in systemd.

runlevel : target
0 : poweroff.target
1 : rescue.target
2, 3, 4 : multi-user.target
5 : graphical.target
6 : reboot.target

To manage services in systemd, "systemctl" command is used.

# systemctl [subcommand] [unit name] [-t unit type]

The followings are main sub command:
start: this starts a service.
stop: this stops a service.
restart: this restarts a service.
reload: reload (re-check) the setting of a service.
status: this is used to check the status of a service.
is-active: this checks if the service is active.
enable: when this is used to a service, the service is started automatically when the whole system starts.
disable: when this is used to a service, the service is NOT started automatically when the whole system starts.
list-unit-files: displays all units.
reboot: reboot the system.
poweroff: power off the system.


systemd

We have talked about SysVinit on
SysVinit
Run level
Set a default runlevel

But actually SysVinit is less popular than "systemd" recently. Now we will talk about systemd here and go through the features of systemd.

In a system which uses systemd, systemd process manages services instead of init process when the system is started. The following daemon[1] processes work together for the purpose.
systemd  : main process of systemd
systemd-journald  : this manages journals (logs)
systemd-logind  : this is a process to deal with log in
systemd-udevd  : this process detects devices dynamically
In systemd, a unit refers to any resource that the system knows how to operate on and manage. This is the primary object that the systemd tools know how to deal with. These resources are defined using configuration files called unit files.[2]

There are some types of unit like a unit to start some services or a unit to mount a file system. Unit's type can be seen by its extension. For example, "htttpd.service"  can be estimated to be a service to start httpd service (and it is really so).

main types of the unit:
service
device
mount
swap
target.
systemd can understand the system's dependence and order relationship unlike SysVinit[3]. systemd considers "in order to start service A, service B will be needed" and systemd starts services smartly. In the end, necessary time to start the whole system is much shorter than SysVinit.


[1] Daemon processes reside in memory and provice services.
[2] Cited from Understanding Systemd Units and Unit Files.
[3] SysVinit starts services just in order stated beforehand. So if some service doesn't work properly, SysVinit get stuck at the service and it might take a lot of time to start the whole services as a result.

Set a default runlevel

When Linux is started, "init" is started at first. And then the system is started at a runlevel stated at /etc/inittab. If you want to change the default runlevel, open "/etc/inittab".

And look for a sentence saying "Default runlevel. The runlevels used by RHS are." then change the following sentence:

id:3:initdefault:

to the following:

id:5:initdefault:
Then the default runlevel is changed to 5 from 3.


Run level

In Linux system, "Run level" provides necessary service (and don't provide unnecessary service) for each run level. For example, you can decide that GUI should be enabled for "run level 5" and also you can decide what service shouldn't be enable for run level 5.

Run level for Ubuntu Debian GNU/Linux
0: Halt
1: Single user mode
2: Multi user mode
3: Multi user mode
4: Multi user mode
5: Multi user mode
6: Re-start
S or s: Single user mode
Single user mode is a special mode which can be used by root users. Single user mode is used for special cases which normal users should not touch, like system maintenance, because normal users can't log in the single user mode.

To check the current run level, run level command is used.

# runlevel
N means Run level was not decided because it was just after system was started. After N, the run level became 2  and this is the current run level.

To change the run level, "init" command or "telinit" command is used. The following example changes the current run level to 1.

# init 1

While normal users are logged in, if you log in as a single user, those normal users become unable to do anything and get stuck. So normal users should be logged out before you log in as a single user. If you use "wall" command, you can send messages to certain user(s). "wall" displays the contents of file or, by default, its standard input, on the terminals of all currently logged in users.

$ wall << .
Hi guys. This system is going down to maintenance mode. Beware.
.


SysVinit

The flow chart to explain how Linux PC is started

PC gets powered on
BIOS/UEFI
Boot loader
Kernel
init or systemd

SysVinit (System Five Init) has been main stream for Unix OS so far. But recently, "systemd" is getting more popular for Linux to boot its system. Now the certificate LPIC requires both knowledge of systemd and SysVinit.

In Linux system, the process "init", which is firstly executed in the system, starts necessary services serially following the setting file locating at /etc/inittab. The flow will be as follows:

1, init reads /etc/inittab file.
2, init reads /etc/rc.sysinit script.
3, init executes /etc/rc script.
4, This /etc/rc script executes the scripts locating under /etc/rc<runlevel>.d

But in case your system is not using SysVinit, which was a main stream before, there might not be /etc/inittab file. Even if the file exists, if the system is not usnig SysVinit, the file etc/inittab can be blank.

SysVinit uses scripts locating under /etc/rc<runlevel>.d to start services needed for each runlevel. For example, /etc/rc3.d directory has script files used for runlevel 3. These can be seen by using the following command:

$ ls /etc/rc3.d

These scripts stored at /etc/rc3.d is used to start, for example, servers, system services, or to restart the PC. If you want to use httpd service for example, use a command as follows:

# /etc/init.d/httpd start

To stop the httpd, use a command as follows:

# /etc/init.d/httpd stop


shutdown command

Linux PC can be shut down by using "shutdown" command.

# shutdown

By using options, shutdown command is used for a variety of purposes.

  • -h  : shut down PC
  • -r   : Restart PC after shut down
  • -f   : fsck[1] is skipped when PC is re-started
  • -F  : fsck is forced when PC is re-started
  • -k  : Send warning messages as if the shutdown is real but PC isn't shutdown actually.
  • -c  : Cancel a pending shutdown. (This does not apply to "shutdown now", which does not wait before shutting down.)


The following command shutdowns PC in 15 minutes and sends a warning message saying "please prepare for the shutdown in 15 minutes." But you need to be root to perform this command.

# shutdown -h +15 "please prepare for the shutdown in 15 minutes."

If you just want to send warning messages as if the shutdown is real, use -k option. But you need to be root to perform this command also.

# shutdown -k now "please prepare for the shutdown."

The following command shuts down the PC at 22:00.

# shutdown -h 22:00

The following command restarts the PC immediately.

# shutdown -r now

Once shutdown command is executed, users can't log in the system since then until the system is actually shut down and get restarted. The closer the planned time to be shutdown becomes, the more the users get warning messages.

[1] Linux fsck utility is used to check and repair Linux filesystems


References
Computer Hope http://www.computerhope.com/unix/ushutdow.htm Visited on 9th October 2016

Tuesday, October 4, 2016

lspci and device information

Citation from wikipedia "device file"
In Unix-like operating systems, a device file orspecial file is an interface for a device driverthat appears in a file system as if it were an ordinary file. There are also special files inMS-DOSOS/2, and Microsoft Windows. They allow software to interact with a device driver using standard input/output system calls, which simplifies many tasks and unifies user-space I/O mechanisms.
Device files often provide simple interfaces to peripheral devices such as printers and serial ports, but they can also be used to access specific resources on those devices, such asdisk partitions. Finally, device files are useful for accessing system resources that have no connection with any actual device such asdata sinks and random number generators.

And... device information like PCI devices which Linux kernel is detecting can be seem by using lspci command:

$ lspci

The detail of lspci is:

Citation from https://linux.die.net/man/8/lspci
lspci is a utility for displaying information about PCI buses in the system and devices connected to them.

By default, it shows a brief list of devices. Use the options described below to request either a more verbose output or output intended for parsing by other programs.

If you are going to report bugs in PCI device drivers or in lspci itself, please include output of "lspci -vvx" or even better "lspci -vvxxx" (however, see below for possible caveats).

Some parts of the output, especially in the highly verbose modes, are probably intelligible only to experienced PCI hackers. For exact definitions of the fields, please consult either the PCI specifications or the header.h and/usr/include/linux/pci.h include files.

Access to some parts of the PCI configuration space is restricted to root on many operating systems, so the features of lspci available to normal users are limited. However, lspci tries its best to display as much as available and mark all other information with <access denied>text.

Options

Basic display modes

-m

Dump PCI device data in a backward-compatible machine readable form. See below for details.

-mm

Dump PCI device data in a machine readable form for easy parsing by scripts. See below for details.

-t

Show a tree-like diagram containing all buses, bridges, devices and connections between them.

Display options

-v

Be verbose and display detailed information about all devices.

-vv

Be very verbose and display more details. This level includes everything deemed useful.

-vvv

Be even more verbose and display everything we are able to parse, even if it doesn't look interesting at all (e.g., undefined memory regions).