How to manage systemd on centos 7

CentOS boot process:
POST –> Boot Sequence –> Bootloader –> kernel + initramfs(initrd) –> rootfs –> /sbin/init
Innit program:
CentOS 5:SysV init
CetnOS 6: Upstart
CentOS 7 : Systemd
Systemd new features:
System Sys v init and LSB init scripts compatible
When the system boots to achieve parallel starting of services; with socket/D-Bus activation techniques start the service in order to reduce startup time, systemd goal is: start a process less as possible; more process started in parallel as possible;
Ondemand activation process Systemd provides ability to start on demand, only when a service is requested by real time when starting it. When the service ended, systemd can shut it down, wait for the next time you need to start it again.
To the system snapshot and recovery;
Start the mount point, and automatic mount point management:
Systemd self management system on the mount point to be able to mount them automatically when the system starts. And compatible with/etc/fstab file;
Implementation dependency management:
Systemd maintains a transactional consistency concept, ensure that all relevant services can start normally without interdependence, that deadlock situations.
Based on the dependencies defined within the service control logic;
System using the features of the Linux kernel which CGroup to complete process tracking tasks. When you stop the service, by querying the CGroup,systemd can be sure to find all the relevant processes, so as to clean out of service;
Log service: with systemd log service journald, the log service is designed to overcome the shortcomings of the existing syslog service.
The basic concepts of System
System initialization needs to be done very much. Need to start the spooler service, start the SSHD service configuration needs to be done, such as mounting the file system. Systemd each step in this process is abstracted into a hive, the unit. A service can be considered to be a hive; a mount point is a hive; a swap partition configuration is a configuration element, and so on. Systemd hive into a number of different types. However, systemd is growing rapidly, increasing new features. Configuring element types may continue to increase in the near future.
Service: represents a background server process, such as mysqld. This is a common one;
Socket: this type of hive package system and a socket in the Internet. Now, systemd supports streaming, packets and continuous for AF_INET, AF_INET6, AF_UNIX socket. Each socket configuration profiles with the unit has a corresponding unit. The corresponding service in the first connected into the socket, it will start (for example: nscd.Socket new connection will start after nscd.service).
Device: this class encapsulates a exists in a hive Linux device in the device tree. Each device using udev rules mark all systemd will appear as an equipment unit.
Mount: this type of hive package a mount point of the file system hierarchy. Systemd will be monitored and managed on this mount point. For example, can be mounted automatically at boot time; can be unloaded automatically under certain conditions. Systemd will convert the entries in the/etc/fstab mount points, and when powered on.
Automount: this type of hive packaging systems in the hierarchy of a mount point. Each unit corresponds to a mounting configurations mounts hives when the automatic mount point is accessed, systemd mount behaviors that are defined in the implementation of the mount point.
Swap: mount the hive and is similar to Exchange configuration unit to manage the swap partition. Users can use swap partition Exchange configuration elements to define the system, enough to make a swap partition is activated at boot time.
Target: this type of hive is configured for other logical grouping unit. Themselves without actually doing anything, just refer to other hives. This allows a unified control of the hive. This can be achieved you are already very familiar with the concept of run level. For example, want to bring the system into graphical mode, you need to run many of the services and configuration commands, which are represented by the hive, combines all these hives into a target (target), it means all these hives need to be implementation again to enter the State represented by the target system is running. (For example, is equivalent to the traditional SysV system run level 3)
Timer: timer hive used timed userdefined action is triggered, this type of hive is replaced the timing of ATD, crond and other traditional services.
Snapshot: target similar to hives, a snapshot is a set of configuration elements. It saves the current operational state of the system.
Systemd will start the work a lot of lifting to rely on, so that they can start. Yet there are some tasks, among them there is naturally dependent, you can’t use socket to activate (socket activation), D-Bus activation and autofs, the three methods to relieve dependence (three methods see subsequent descriptions for details). For example: mount must wait for the mount point is created in the file system; also must wait for the corresponding physical device is mounted in place. In order to solve this kind of dependency, systemd hive you can define dependencies between.
Systemd hive keyword in the definition file is used to describe dependencies between hives. For example: unit a dependent unit b, unit b, used in the definition of require a to mean. Systemd will ensure to start a startup b.
Systemd services:
Systemd guarantee transactional integrity. Systemd concepts and data in the database is different, mainly in order to ensure that no circular references between multiple dependent hive. There are circular dependencies, then systemd will not start any services. Systemd will try to resolve the issue at this time, because the dependencies between the hive there are two: required is a strong reliance on want is a weak dependence on systemd will remove wants keyword Specifies that see if you can break the cycle of dependence. If you cannot repair, systemd will report an error.
Systemd can automatically detect and repair this type of configuration errors, greatly reducing the burden on administrators troubleshoot.
Target and run level:
Systemd target (target) replaces the runlevel concept, provide greater flexibility, as you can inherit an existing target and add other services, create your own goal. Systemd goals are listed in the following table and correspond to the common runlevel:
Systemd startup principle
As mentioned earlier, Systemd, and all services were started, including Avahi, D-Bus, livirtd, X11, HAL can be started simultaneously. At first glance, this seems to have a problem, such as Avahi need syslog service, Avahi, and syslog to start, assuming that Avahi starts faster, syslog is not ready, Avahi, however, need to log, this would mean that problems can arise?
Systemd development very carefully studied the nature of interdependence between services, found the so-called dependency can be divided into three types, each type is actually all dependencies can be prevented through appropriate technology.
Start one of principle: addressing socket dependence
Most of the services rely on the socket is on. Services through a socket port S1 to provide their services, other service if you need service, you need to connect to S1. If the service is not started, S1 does not exist, other services will get boot error. Traditionally, people need to start the service, waiting for it to enter the ready state, then start the other services that need it. Systemd believes that as long as we establish S1, then all the other services can start at the same time without waiting for the service to create a S1. If the service is not started, then other process service requests will be sent to S1 Linux operating system cache and other processes in place waiting for this request. Once the service is a starting place, cached requests can be dealt with immediately, and everything is up and running.
Service how to use the socket created by the init process it?
Linux has a feature of the operating system, when the process calls fork or exec creates a child process, all opened in the parent process file handles (file descriptor) inherits the quilt process. Socket is a file handle, process a can create a socket, since when a process when a child process calls exec launches a new, just make sure the socket‘s close_on_exec flag is cleared, then the new child processes can inherit the socket. Child process see the socket and the parent process creates the socket the socket is the same system, it was as if the socket is created as the child process, there is no difference.
This feature was previously exploited by a system called the inetd service. Inetd process will be responsible for monitoring the common socket port, such as Telnet, connection requests when the port, inetd start telnetd processes and Sockets connected telnetd passed to the new process for handling. In this way, when the system when there is no Telnet client connections, you do not need to start telnetd process. Inetd can represent a lot of network services, so that you can load and save a lot of system memory resources only when there is a real connection request starts only when the appropriate service, and passed to the appropriate sockets of service process.
And like inetd, systemd is the parent of all other processes, which can set up all the required socket, then call EXEC will pass the socket to the new service process, and the new process directly using the socket services.
Start the second principle: resolving D-Bus dependence
D-Bus is short for desktop-bus, is a low latency, low overhead, high availability of inter-process communication mechanism. It is increasingly being used for communication between applications, and is also used for communication between the application and the operating system kernel. Many modern service processes use D-Bus instead of Sockets as interprocess communication mechanisms, provides services. NetworkManager simplifies Linux network configuration services, for example, use D-Bus and interact with other applications or services: software evolution mail client available through D-Bus from the NetworkManager service network state changes, so as to ensure appropriate treatment.
D-Bus supports the so-called busactivation function. If the service is a required service b D-Bus services, while service b does not run, you can D-Bus in the service start the service automatically when a request service b D-Bus b. Service a request is issued by D-Bus cache, a will b waiting for service to start in place of service. With this feature, rely on d-bus service can be started in parallel.
Start principle three: resolving file system depends on
During system startup, file system related activities is the most time consuming, such as mounting file systems, disk for file system check (fsck), disk quota checking is a very time-consuming operation. While waiting for the completion of these works, the system is idle. The service seems to have to wait for those who want to use file system file system initialization to complete before they can start. But systemd found this dependence could be avoided.
Systemd autofs reference design, make the dependent file system services and initializing both the file system itself can work concurrently. Autofs can monitor the real access to a file system mount point, to trigger a mount operation, which is realized through the kernel automounter module support. For example, an open () system call function /Misc/CD/File1 when/Misc/CD not perform a mount operation, open () call is suspended waiting for, Linux kernel notifies the autofs,autofs to perform the mount. At this time, control is returned to the open () system call and opens the files properly.
Systemd autofsintegrated implementation for mount points in the system, such as/home, when the system starts when systemd automatically to create a temporary mount point. /Home at this time the real mount a device not started well, true mount operation is not implemented file system detection has not yet completed. But those who rely on the contents of process can start, they open () operation is built into the autofs capture systemd, the open () calls pending (interruptible sleep state). Then wait for the real mount operation is complete, file system testing is also completed, systemd replaces the automatic mount point with the real mount points, and open () call returns. Thus, realize that depend on file system services and file system itself at the start.
Of course, for / root dependence must actually be executed serially, because systemd themselves are stored under/, mount the check must wait for the system root directory.
But for similar/home, hanging contains points, this concurrent can improve system of started speed, especially Dang/home is remote of NFS node, or is encryption disc,, need spent more long of time only can prepared ready of situation Xia, because concurrent started, this paragraph time within, system is not completely no thing can do, but can using this paragraph spare time do more of started process of things, overall on shortened has system started time.
Following different roles for technicians to simply introduce systemd use. Only intended to give a brief description, allowing you to have a general understanding of the use of systemd. Too many specific details, which cannot be written in a short essay. Documents that require the reader to further check out systemd.
Unit writing
Developer has developed a new service program, such as httpd, you need to write a configuration file so that the service can be systemd management, similar to the work of the UpStart configuration file. Service startup command line syntax defined in that file, and other services and dependencies, and so on.
In addition we have learned before, systemd featurebloat, not only for management services, you can also manage mount points, define a scheduled task. All this work is done by editing the appropriate hive files. I am given several hive files
Below is the SSH service configuration file, service hive file to. service for file name suffix.
[root@kalaguiyin system]# cat/usr/lib/systemd/system/sshd.service
Description=OpenSSH server daemon sshd-keygen.service
#[Unit] Part description
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
#[Service] definition defined ExecStartPre commands should be run before starting the service;
#ExecStart defines the specific command-line syntax for starting a service.
#[Install] part: WangtedBy shows that the service is required in multi-user mode.
Then we look at
[root@kalaguiyin system]#
Description=Multi-User System
# Definition Requires that started must be started; also stop #, and must be stopped. If you then view the file, you will find it # other specified unit must be started. Also will also contain other elements. Using layerbylayer link structure, end # the components that need to support multiple usermode service is initializing start good.
# Alias definition, which defines an alias in this unit, so when running systemctl can use this name to refer to this unit.
In addition in the/etc/systemd/system directory, you can see the directory, such as *.wants, put it in the directory of a hive file is equivalent to [Unit] wants keywords section, which starts in this unit, you also need to start the unit. For example, you can easily write your own foo.service file in the directory, so always starts by default.
[root@kalaguiyin system]# pwd
[root@kalaguiyin system]# ls display-manager.service
Let us look at the sys-kernel-debug.MOUT file, this file defines a file mount point:
[root@kalaguiyin system]# cat
Description=Debug File System
The hive file defines a mount point. Mount the hive file has a [Mount] configuration section, configure the What,Where and Type three data items. This is necessary for the mount command, example configuration is equivalent to the following mount command:
mount –t debugfs /sys/kernel/debug debugfs
Systemd system management:
Systemd systemctl is the main command line tool.
Most administrators are already familiar with the system management services and init system, such as using service and chkconfig as well as telinit command. Systemd completed the same administrative task, just different syntax for command tool systemctl.
Start the service
Systemctl start httpd.service figure 1:
Stop the service
Systemctl stop httpd.service figure 2:
Restart services
Systemctl restarthttpd.service as in Figure 3:
Heavy duty service
systemctl reloadhttpd.service
Conditional restart
systemctl condrestarthttpd.service
Status view
systemctl statushttpd.service
List of lists you can start or stop services.
systemctl list-unit-files –type=service
Setting service startup
chkconfig httpd on
systemctl enablehttpd.service
Cancel the service startup;
systemctl disablehttpd.service
Check for a service in the current environment is configured to enable or disable.
systemctl is-enabledhttpd.service;echo $?
Output under the various run levels of service enabling and disabling
systemctl list-unit-files –type=service
List the services in which the run level is enabled and disabled.
ls /etc/lib/systemd/system/*.wants/httpd.service
Changing the user level:
systemctl == the 3rd run level == 5th run level symbolic link pointing to symbolic link pointing to
Change the default runlevel:
[root@kalaguiyinsystem]# systemctl set-default
ln -s’/usr/lib/systemd/system/”/etc/systemd/system/’
The essence of this is to remove/usr/lib/systemd/system/, then links to the target levels of target file/etc/systemd/system/ file;
Systemd is not only one of the original system:
Systemd is also responsible for other administrative configuration of the system, such as configuring the network, Locale management, management system kernel module is loaded.
Systemd ably has replaced sysvinit all feature, but it was not to be complacent. Because the init process is the parent of all processes of the system processes the particularity of systemd is well suited to provide once the functionality provided by other services, such as scheduled tasks (previously done by crond), session management (formerly managed by ConsoleKit/PolKit). From just coats as introduced in this paper, Systemd is tube a lot, but it is still in development. It will gradually become a multifunctional system, able to handle so many system management tasks, or even be thought of as an operating system. This is very help to standardize management of Linux!