To say clear Systemd, you have to start with the Linux operating system started speaking. The startup of the Linux operating system starts with the BIOS, then the boot Loader loads into the kernel and initializes the kernel. The final step in kernel initialization is to start the init process. This process is the first process of the system, PID 1, also known as the super process, called the root process. It is responsible for generating all other user processes. All processes will be suspended under this process, and if the process exits, then all processes are killed. If the parent process of a child process is retired, the child process is hung under PID 1. (Note: PID 0 is part of the kernel, mainly for internal feed-in, see: Process identifier)
SysV Init
PID 1 This process is very special, its main task is to bring the entire operating system into an operational state. For example: Start Ui–shell for human-computer interaction, or go to the X graphics window. Traditionally, PID 1 is compatible with the traditional Unix System V, so also calledsysvinit, which is the most used init implementation. Unix System V was released in 1983.
In thesysvintnext, there are several modes of operation, also calledrunlevel. For example: Common level 3 Specifies the command line interface to launch to a multi-user character, level 5 Specifies the start to graphical interface,0 indicates shutdown , and 6 means reboot. It is configured in the/etc/inittabfile.
In addition/etc/init.d//etc/rc[X].d, the former holds various process start-stop scripts (which need to be supported by Specificationstart,stopsubcommands), and the latter X represents the corresponding background process services under different runlevel, such as:/etc/rc3.drunlevel=3. The file is mainly the start-/etc/init.d/stop script in link. There are also certain naming conventions: S or K, followed by a number, and followed by a custom name, such as:S01rsyslog,S02ssh. S means start, k means stop, number indicates the order of execution.
Upstart
Unix and Linux have been operating for manysysvintyears, about 2006 years, when the Linux kernel entered the 2.6 era, Linux has a lot of updates. And, Linux began to enter the desktop system, and desktop systems and server systems are not the same, the desktop system is faced with frequent reboots, and users will be very frequent use of hardware hot-plug technology. As a result, these new scenes have beensysvintchallenged.
For example, the printer needs CUPS and other service processes, but if the user does not play machine printing, start the service is a waste, and if not start, if you want to use a printer, you can not use, becausesysvintthere is no automatic detection mechanism, it can only start all services at once. In addition, there is a problem with the network disk mount. In/etc/fstab, responsible for the hard drive mount, sometimes there is a network hard disk (NFS or ISCSI) in which, but on the desktop, there is probably no network when the boot, so the network hard disk can not access, and cannot mount, which will greatly affect the startup speed.sysvinitnetdevthe way to solve this problem, that is, the user needs/etc/fstabto give the corresponding hard disk configuration on thenetdevproperty, so thesysvintboot will not mount it, only after the network is available, by a dedicatednetfsservice process to mount. This management method is more difficult to manage, it is easy to let people fall out of the pit.
So, after evaluating several optional init systems, the Ubuntu developer decided to redesign the system, so that's what we saw laterupstart. Anupstartevent-driven mechanism that changes the way in which the previous fully-serialized synchronous start-up service has been changed to an event-driven asynchronous approach. For example: If you have a USB stick inserted,udevget notified,upstartaware of the event triggered by the corresponding service program, such as Mount file system and so on. Because of the use of an event-driven gameplay, so, when the operating system starts, many unnecessary services can not start, but wait for notification, lazy boot. And the benefit of event-driven is that the service can be started in parallel, and the dependencies between them are completed by the corresponding event notification.
Upstart has a very good design, the two most important concepts are Job and Event.
Job has a general job, there is a service job, andupstartmanagement of the entire job life cycle, such as: waiting, starting, Pre-start, spawned, Post-start, Runn ING, pre-stop, stopping, killed, post-stop, etc., and maintain the state machine for this life cycle.
The Event is divided into three categories,signalmethodandhooks. Is thesignalasynchronous message, whichmethodis synchronous blocking.hooksis also synchronous, but between the preceding two, the process that issues the hook event must wait for the event to complete, but not check for success.
However,upstartthe events are complex and chaotic, with a variety of events (events not well-categorized) causing a bit of clutter. But because the whole event-driven design is much better than it was before,sysvinitit's also welcome.
Systemd
Until 2010 one day, an engineer working at RedHat Lennart Poettering and Kay Sievers, began introducing a newinitsystemsystemd. This is a very ambitious project that almost changed everything,systemdnot only to replace the existing init system, but also to do more things.
Lennart agreedupstartto do well, code quality is good, event-based design is also very good. But he thinksupstartthere are problems, the biggest problem is not fast enough, although theupstartuse of events can achieve a certain degree of start-up parallelism, but, in essence, these events will let the startup process serially together. such as: The Startup eventNetworkManagerin the waitD-Bus, whileD-Busin thesyslogstartup event.
Lennart that,upstartin fact, is to manage a logical service dependency tree, but the service relies on the tree in the form of a simple, you only need to configure-"Start B OK to start a" or "stop a after a" Stop the "b" rule. But, Lennart says, this simplicity is actually harmful (this simplification is actually detrimental). He believes that
- from a system management point of view, he will initially set up the entire system to start the service dependency tree, but this system administrator to human flesh of this is a very clean service according to the whole tree to translate into a computer to see the event/action form of understanding, and event/ This configuration of Action is run-time, so you need to run it to know what it looks like.
- The event logic is everywhere from head to toe, which expands the complexity of operations rather than the previous sysvint. That is, when the user has configured "start d-bus please start NetworkManager", this upstart can dry, but conversely, if the user launches NetworkManager, we should first start his predecessor dependency D-bus, However, you should also configure the corresponding reverse Event. Originally, I only need to configure a dependency, the result now I want to configure a lot of situations in the event.
- Finally, theupstart Event is not standard, confusing, and not well defined. For example: Existing, process start, run, stop events, USB device plug-in, available, unplugged events, file system devices being mounted, mounted and umounted events, and AC power cord connection and disconnection events. You will find that this process is starting to stop, USB, file system, power cord events, looks very similar, but not by the standardized pumping out, because the vast majority of events are triples: start, condition, stop. This conceptual design model does not appear in the upstart . Because upstart is designed as a single event, ignoring logical dependencies.
Of course, if itsystemdwas just a problem, he would have done itupstartupstart, but Lennart's ambition was not just to do such a thing, he wanted to do more.
First of all,systemdawake to realize that the primary goal of the INIT process is to allow users to quickly enter the environment that can operate the OS, so this speed must be fast, the faster the better, sosystemdthe design concept is two:
- To start less.
- And to start more in parallel.
That is, on-demand startup, can not start without booting, if you want to start, can be started in parallel parallel start, including you have dependencies between, I also started in parallel. Start on Demand OK understand, then, there is dependency on the parallel start, how does it do? Here,systemdusing MacOS'sLaunchdgameplay (on Youtube there is a share--launchd:one program to Rule them all, there are also relevant design documents on Apple's open source site--about daemons and Services)
To address these dependencies, SYSTEMD needs to address three underlying dependencies--socket, D-bus, and file systems.
-
- Socket dependent . If service C relies on the socket of the service s, start s before starting C, because C fails if the socket of S is not found at c startup.systemdcan help you to set up a socket to receive all C requests and data when S is not ready, and to cache it, once S is all started, replace the cached data and the socket descriptor with SYSTEMD.
-
- D-bus dependent . The fullD-Busname of the Desktop Bus is a service used to communicate between processes. In addition to the process used for user-state processes and kernel-state process communication, also used for user-state processes. Now, a lot of the current service processes are usedD-Businstead of sockets to communicate. For example,NetworkManagerbyD-Buscommunicating with other service processes, that is, if a process needs to know the state of the network, it will need toD-Buscommunicate.D-Bussupports the "Bus Activation" feature. In other words, a to theD-Busservice and B communication, but B does not start, thenD-Busyou can put B up, in the process of B startup, toD-Bushelp you cache data.systemdcan help you use this feature to start A and B in parallel.
-
- file system dependent . During system startup, file system-related activity is the most time consuming, such as mounting the file system, disk checking on the file system (FSCK), disk quota checking, and so on are very time-consuming operations. While waiting for these jobs to complete, the system is in an idle state. Services that want to use the file system seem to have to wait for the file system to initialize before it can be started.systemdreference toautofsthe design ideas, so that the dependent file system services and the file system itself to initialize both can work concurrently.autofsit is possible to monitor when a file system mount point is actually accessed to trigger a mount operation, which is achieved through the support of the kernelautomountermodule. For example, when an open () system call acts on "", the/misc/cd/file1/misc/cdmount operation has not yet been performed, at which timeopen ()the call is suspended waiting, the Linux kernel notifiesautofs, and theautofsmount is executed. At this point, control is returned toopen ()the system call, and the file is opened normally.
A page of PPT from Lennart's presentation shows the start-up of different init systems.
In addition, SYSTEMD also managed some of the following when it was launched.
Replace the traditional scripted start-up with the C language. As mentioned earlier,sysvintuse/etc/rcX.dthe various scripts below to start. However, these scripts need to use,,, andawksedgrepfindxargsso on these operating system commands, these commands need to build the process, the cost of the build process is very large, the key is to generate these processes, the process will be a bit of a fart to go back. In other words, my operating system has done so many things for you to pull a process up, the result you turn a string into lowercase to retire, what is my operating system?
In a normalsysvinitscript, there may be hundreds or thousands of such commands. So, slow death. Therefore, thesystemdfull use of C language all replaced. In general,sysvinitafter the operating system starts to complete,echo $$you can see that the PID is assigned to thousands of appearance, andsystemdthe system is just hundreds.
In addition, SYSTEMD is a truly managed service process that can track all the processes fork/exec on the service process.
-
- We know that the best practices of the traditional Unix/linux Daemon service process are essentially this way (see the article "SysV Daemon" for a specific process)--
- When the process starts, close all open file descriptors (in addition to the standard descriptor 0,1,2) and reset all signal processing.
- Called tofork ()Create a child process, in a child processsetsid (), and then the parent process exits (for background execution)
- Again, call againfork (), create grandson process, determine no interactive terminal. The child process then exits.
- In the grandson process, the standard input standard output standard error is connected to the/dev/nullabove, but also to create PID files, log files, processing related signals ...
- Finally, the service is really started.
-
- In this process, the service process, in addition to two timesforkthere will be a lotforkof sub-processes (such as some WEB services process, according to the user's request link to fork sub-process), the process tree is quite difficult to manage, because once the parent process exits, the child process will be suspended to PID 1, so, basically, you can't find all the related processes from a given PID file by the service process (this is too high for developers), so it's quite Buggy to start the service in the traditional way with a script. Because can't do to all the waiter out of the descendants to do monitoring.
-
- In order to solve this problem,upstartthrough the abnormalstraceto track the process offork ()andexec ()orexit ()other related system calls. This method is rather clumsy.systemdusing a very interesting gameplay to tracking all the processes produced by the service process, it is usedcgroup(I have said this in the basic technology "cgroup" of Docker). Cgroup is mainly used to manage the process group resource quotas, so regardless of how the service starts a new subprocess, all of these related processes will belong to onecgroup, sosystemdsimply go through the corresponding file in thecgroupvirtual file system directory, will be able to find all the relevant processes correctly and stop them all.
In addition, thesystemdentire daemon development process is simplified:
- It does not need to be two timesfork (), just to implement the main logic of the service itself.
- Nosetsid (), Isystemd'll do it for you.
- Do not need maintenancepid 文件,systemdwill help to deal with.
- No need to manage log files or usesyslog, or processHUPlog reload signals. Put the logsstderron andsystemdhelp you manage.
- Processing theSIGTERMsignal, the signal is to correctly exit the current service, do not do anything else.
- ......
In addition,systemdyou can--
- Automatic detection of the start-up between the services there is no ring dependency.
- Built-in AutoFS automatic mount management function.
- Log service. Thesystemdproblem of traditional syslog is changed, the log is saved in binary format, and the log index is faster.
- Snapshots and recoveries. A snapshot of the collection of services running on the current system and can be resumed.
- ......
There are many more, he took over a lot of things, so let a lot of people unhappy, because he is doing a lot of things that do not belong to PID 1.
Systemd Controversy and gossip
Sosystemdthis thing is probably the most spit-fighting in the history of an open source software. Thesystemdbiggest controversy is that he has broken the design philosophy of Unix (the related philosophy can read "UNIX Programming Art"), doing a chatty and rather complicated thing. Of course, Lennart does not agree with such a statement, he later wrote a blog "The biggest Myths" to explain thatsystemdis not the case, we can go to the first reading.
What's the big controversy like? In 2014, Debian Linux was replaced assystemda standard init daemon because it wanted to be ready for usesysvinit. The controversy surrounding the matter reached an unprecedented heat, and the debate was filled with hatred, withsystemdsupporters and opponents abusing each other, leading to the separation of the Debian camp. Others sent Lennart a death-threatening message, used Bitcoin to hire a killer, threatened to take his life, posted insulting songs in Youbute, and sent him nasty and insulting messages on IRC and various social channels. This is no longer a dispute, but a kind of outright hatred!
So, Lennart on Google Plus on the post, criticizing the entire Linux open source community and Linus himself. He said to the effect that
The community is so sick, it's all about the holes, and you're constantly insulting and abusing me in different languages and ways in various places. I am still a young man, I have never experienced such a scene, but today I have been very familiar with the scene. I may not be accurate at times, but I will not say that as he did, and I am not affected by these things because I am thick-skinned, so why I can make a success in the face of big objections,systemdbut your Linux community is horrible. There are too many mentally ill people in you. In addition, for Linus Torvalds, you are the role model of this community, but unfortunately you are a bad Role model, you are in the community of the caustic and insulting words and deeds, basically to some extent, encourage others like you, of course, not just you a person's problem, Instead, a group of people like you have gathered around you to do this. Give you a word--a fish rots from the head down! A fish is rotting from the ground down ...
This flung is very long, and the students who like to gossip can go to the first reading. Feel the Lennart at the time (I think it is very smooth).
Linus is also being asked by a mediasystemdabout this (see "Torvalds says he has no strong opinions on Systemd"), Linus said in an interview,
Isystemdhave no strong idea about the post with Lennart. While the traditional Unix design philosophy-"Do one thing and does it well"-is good, and most of us have practiced it for years, it doesn't mean all the real world. In history, it's not the only way to do itsystemd. However, I am personally a old-fashioned person, at least I like the text-based log, not the binary log. But theresystemdis no need to have such a taste. Oh, I said the details ...
Today, thesystemddefault configuration for almost all major Linux distributions is included: Arch Linux, CentOS, CoreOS, Debian, Fedora, Megeia, OpenSUSE, RHEL, SUSE Enterprise, and Ubuntu. And, for CentOS, CoreOS, Fedora, RHEL, SUSE, these distributions cannot besystemd. (Ubuntu also has a nice wiki–systemd for upstart Users How to switch between the two)
Other
Remember in the article "Cache update routines," I said, if you're going to be a good architect, first you have to thoroughly understand the computer architecture and many of the old-fashioned basic technologies . Because there will be a lot of things that can be borrowed and communicated. So, did you see something similar to distributed architecture in this article?
For example: from the to thesysvinitupstartagainsystemd, like not like the service governance? Are these service processes under the Linux system much like microservices in a distributed architecture? And the D-bus, isn't it like an ESB in SOA? and is the init system much like a control system? Even like a service orchestration (Orchestration) system?
There are also a lot of dependencies between services in a distributed system, so when we start a schema, if we can do it in parallel like SYSTEMD, is it just like a micro-service play?
Well, you will find that a lot of technical things are interlinked, but also each other's shadow, so, in fact, the technology is not much. The key is that we learn on the surface or see the essence.
https://news.cnblogs.com/n/573985/
LINUX PID 1 and systemd PID 0 is part of the kernel, mainly used for internal page feed, the last step of kernel initialization is to start the init process. This process is the first process of the system, the PID is 1, also called the super process