百科问答小站 logo
百科问答小站 font logo



如果把systemd视作内核的一部分,Linux是混合内核吗? 第1页

  

user avatar   shi-tou-wu-fa-gai-bian 网友的相关建议: 
      

写在前面

  • 嗯,准备RHCA,学习整理这部分知识
  • 博文内容涉及:
    • systemd简述
    • 对于unit的信息的介绍
    • 通过systemctl命令控制Service unitDemo
    • Service unit配置文件内容,权值的一些介绍

傍晚时分,你坐在屋檐下,看着天慢慢地黑下去,心里寂寞而凄凉,感到自己的生命被剥夺了。当时我是个年轻人,但我害怕这样生活下去,衰老下去。在我看来,这是比死亡更可怕的事。--------王小波


一、Systemd 简述

systemd是Linux操作系统的系统和服务管理器。在系统启动时或者正在运行的系统上的一个守护进程,systemd负责激活系统资源、服务器守护程序和其他进程systemdRHEL 7之后出现的。

守护进程(daemon)是在后台执行各种任务的进程。通常,守护程序在引导时自动启动并继续运行直到系统关闭或手动停止。许多守护程序的名称的约定以字母d结尾

关于RHEL启动进程管理:

  • RHEL 5的时候,使用 Sys init,启动速度最慢的,无论进程相互之间有无依赖关系,都是串行启动过程,
  • RHEL 6的时候,使用 Upstart init相对启动速度快一点有所改进。有依赖的进程之间依次启动而其他与之没有依赖关系的则并行同步启动
  • RHEL 7 Systemd与以上都不同。所有进程无论有无依赖关系则都是并行启动(很多时候进程没有真正启动而是只有一个信号或者说是标记而已,在真正利用的时候才会真正启动)。

Systemd为了解决上文的问题而诞生。它的目标是,为系统的启动和管理提供一套完整的解决方案,

RHEL启动的第一个进程(PID 1)就是systemd,提供的功能包括:

  • 并行化功能(同时启动多个服务),可提高系统的启动速度。
  • 按需启动后台程序而无需单独的服务。
  • 自动管理服务依赖关系,可以防止长时间超时。例如,网络相关服务在网络可用之前不会尝试启动。
  • 使用Linux控制组(Cgroup)一起跟踪相关进程的方法。
Systemd 架构图

systemd Targets 启动级别

二、systemctl 命令管理系统

systemctl Systemd 的主命令,systemctl命令用于管理不同类型的系统对象,这些对象称之为units

  • Service unit:用于定义系统服务,文件扩展名为.service
  • Target unit:用于模拟实现“运行级别”,文件扩展名为.target,可以理解Target 就是一个 Unit 组,包含许多相关的 Unit
  • Device unit:用于定义内核识别的设备,文件扩展名为.device
  • Mount unit:用于定义文件系统挂载点,文件扩展名为.mount
  • Socket unit:用于标识进程间通信用的socket文件,文件扩展名为.socket
       ┌──[root@liruilongs.github.io]-[~] └─$systemctl status cockpit.socket #CocKpit驾驶舱 和SSH类似,用于远程控制,类似于阿里云的控制台。为初级管理员提供基本操作,通过web端的控制台,访问地址:服务器IP:9090 ● cockpit.socket - Cockpit Web Service Socket    Loaded: loaded (/usr/lib/systemd/system/cockpit.socket; disabled; vendor preset: disabled)    Active: inactive (dead)      Docs: man:cockpit-ws(8)    Listen: [::]:9090 (Stream)      
  • Snapshot unit:管理系统快照,文件扩展名为.snapshot
  • Swap unit:用于标识swap设备,文件扩展名为.swap
  • Automount unit:文件系统的自动挂载点,文件扩展名为.automount
  • Path unit:用于根据文件系统上特定对象的变化来启动其他服务,文件扩展名为.path
  • Timer unit:用于管理计划任务,文件扩展名为.timer
       ┌──[root@liruilongs.github.io]-[~] └─$systemctl status systemd-tmpfiles-clean.timer #用于定时清理 `/tmp`,`/var/tmp`之类的临时文件 ● systemd-tmpfiles-clean.timer - Daily Cleanup of Temporary Directories    Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.timer; static; vendor preset: disabl>    Active: active (waiting) since Sun 2022-04-03 11:24:00 CST; 1h 48min ago   Trigger: Mon 2022-04-04 11:39:07 CST; 22h left      Docs: man:tmpfiles.d(5)            man:systemd-tmpfiles(8)  Apr 03 11:24:00 liruilongs.github.io systemd[1]: Started Daily Cleanup of Temporary Directories. ┌──[root@liruilongs.github.io]-[~] └─$      
  • Slice unit:用于资源管理,文件扩展名为.slice

1.查看units列表信息

列出loaded(配置文件以加载)的units

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-units   UNIT                                               LOAD   ACTIVE SUB       DESCRIPTION   proc-sys-fs-binfmt_misc.automount                  loaded active waiting   Arbitrary Executable File>  .............      

命令输出说明:

  • UNIT:服务单元名称。
  • LOAD: systemd是否正确解析了单元的配置并将该单元加载到内存中
  • ACTIVE:单元的高级别激活状态。此信息表明单元是否已成功启动
  • SUB:单元的低级别激活状态。此信息指示有关该单完的更多详细信息。信息视单元类型、状态以及单元的执行方式而异。
  • DESCRIPTION:单元的简短描述
在这里插入图片描述

列出全部的units

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-unit-files UNIT FILE                                  STATE proc-sys-fs-binfmt_misc.automount          static -.mount                                    generated boot.mount                                 generated dev-hugepages.mount                        static dev-mqueue.mount                           static proc-sys-fs-binfmt_misc.mount              static sys-fs-fuse-connections.mount              static sys-kernel-config.mount                    static sys-kernel-debug.mount                     static tmp.mount                                  disabled systemd-ask-password-console.path          static .....      

查看指定的系统对象

查看timer,系统定时任务

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-units -i timer 0 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'. ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-unit-files -t timer UNIT FILE                    STATE dnf-makecache.timer          enabled  #yaml仓库定时更新 fstrim.timer                 disabled   systemd-tmpfiles-clean.timer static  # 临时文件清理 unbound-anchor.timer         enabled  4 unit files listed. ┌──[root@liruilongs.github.io]-[~] └─$      

查看socket套接字

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-unit-files -t socket UNIT FILE                               STATE cockpit-wsinstance-http-redirect.socket static cockpit-wsinstance-http.socket          static cockpit-wsinstance-https-factory.socket static cockpit-wsinstance-https@.socket        static cockpit.socket                          enabled dbus.socket                             static      

列出类型为service,状态为loadedactive的 unit

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-units -t service   UNIT                                                 LOAD   ACTIVE SUB     DESCRIPTION   auditd.service                                       loaded active running Security Auditing Service      

列出类型为service,状态为activeinactive 的unit

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-units --type service --all      

查看失败的服务

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl --failed -t service   UNIT          LOAD   ACTIVE SUB    DESCRIPTION ● kdump.service loaded failed failed Crash recovery kernel arming .....      

三、systemctl操作Service unit

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl status sshd  ● sshd.service - OpenSSH server daemon  #单元的描述信息    Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) #子状态,加载的配置文件,默认开机自启    Active: active (running) since Sun 2022-04-03 11:24:03 CST; 2h 16min ago #运行状态,时间      Docs: man:sshd(8)  #帮助文档            man:sshd_config(5)  Main PID: 880 (sshd) #主进程     Tasks: 1 (limit: 6044) # 任务量     Memory: 7.2M   #消耗的内存    CGroup: /system.slice/sshd.service  # 用的什么slice            └─880 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes2> Apr 03 11:24:02 liruilongs.github.io systemd[1]: Starting OpenSSH server daemon...  #启动日志,什么进程引导激活的 Apr 03 11:24:03 liruilongs.github.io sshd[880]: Server listening on 0.0.0.0 port 22. #监听的端口 Apr 03 11:24:03 liruilongs.github.io sshd[880]: Server listening on :: port 22. Apr 03 11:24:03 liruilongs.github.io systemd[1]: Started OpenSSH server daemon.  #启动服务 Apr 03 11:25:30 liruilongs.github.io sshd[1112]: Accepted publickey for root from 172.25.254.254 port > #连接日志 Apr 03 11:25:30 liruilongs.github.io sshd[1112]: pam_unix(sshd:session): session opened for user root > lines 1-17/17 (END)      

查看Service单元状态信息

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl is-active sshd active ┌──[root@liruilongs.github.io]-[~] └─$systemctl is-enabled sshd enabled ┌──[root@liruilongs.github.io]-[~] └─$systemctl is- is-active          is-enabled         is-failed          is-system-running      

配置文件加载

优先级从高到低

  • 本地配置的系统单元: /etc/systemd/system
  • 运行时配置的系统单元: /run/systemd/system
  • 软件包安装的系统单元: /usr/lib/systemd/system

默认的服务配置文件位置:/usr/lib/systemd/system

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl status sshd | head  -2 ● sshd.service - OpenSSH server daemon    Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)      

添加高优先级配置文件:/run/systemd/system

       ┌──[root@liruilongs.github.io]-[~] └─$cp /usr/lib/systemd/system/sshd.service /run/systemd/system/sshd.service ┌──[root@liruilongs.github.io]-[~] └─$systemctl daemon-reload ┌──[root@liruilongs.github.io]-[~] └─$systemctl status sshd | head  -2 ● sshd.service - OpenSSH server daemon    Loaded: loaded (/run/systemd/system/sshd.service; enabled; vendor preset: enabled)      

添加高优先级配置文件,/etc/systemd/system

       ┌──[root@liruilongs.github.io]-[~] └─$cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/ ┌──[root@liruilongs.github.io]-[~] └─$systemctl daemon-reload ┌──[root@liruilongs.github.io]-[~] └─$systemctl status sshd | head  -2 ● sshd.service - OpenSSH server daemon    Loaded: loaded (/etc/systemd/system/sshd.service; enabled; vendor preset: enabled) ┌──[root@liruilongs.github.io]-[~] └─$      

配置文件内容

       ┌──[root@liruilongs.github.io]-[~] └─$cat /usr/lib/systemd/system/sshd.service [Unit] Description=OpenSSH server daemon #描述信息 Documentation=man:sshd(8) man:sshd_config(5) #帮助文档 After=network.target sshd-keygen.target  #依赖关系,在...之后执行 Wants=sshd-keygen.target  # 强依赖  [Service]  Type=notify  #notify类型 EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config  #环境变量读取 EnvironmentFile=-/etc/sysconfig/sshd   # - 号为文件可以不存在 ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY  # 启动命令 ExecReload=/bin/kill -HUP $MAINPID  # reload从新初始化 KillMode=process  #设置在单元停止时,杀死进程的方法 process 表示仅杀死主进程 Restart=on-failure  #仅在服务进程异常退出时重启 RestartSec=42s # 42秒后重启  [Install] WantedBy=multi-user.target #属于那个target ┌──[root@liruilongs.github.io]-[~] └─$      

手册查看,不错的翻译 systemd.service 中文手册

       ┌──[root@liruilongs.github.io]-[~] └─$man -k systemd      

控制Service unit

启动停止服务

       systemctl start sshd.service  systemctl stop sshd.service      

重新加载配置文件

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl reload sshd #还是原来的进程,只是从新加载配置文件        

重启服务

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl restart  sshd #一个新的进程      

查看服务状态

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl status httpd ● httpd.service - The Apache HTTP Server    Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)    Active: active (running) since Sun 2022-04-03 11:46:36 CST; 3h 5min ago      Docs: man:httpd.service(8)  Main PID: 1616 (httpd)    Status: "Running, listening on: port 80"     Tasks: 213 (limit: 6044)    Memory: 33.7M    CGroup: /system.slice/httpd.service            ├─1616 /usr/sbin/httpd -DFOREGROUND            ├─1617 /usr/sbin/httpd -DFOREGROUND            ├─1618 /usr/sbin/httpd -DFOREGROUND            ├─1619 /usr/sbin/httpd -DFOREGROUND            └─1620 /usr/sbin/httpd -DFOREGROUND  Apr 03 11:46:35 liruilongs.github.io systemd[1]: Starting The Apache HTTP Server... Apr 03 11:46:36 liruilongs.github.io systemd[1]: Started The Apache HTTP Server. Apr 03 11:46:36 liruilongs.github.io httpd[1616]: Server configured, listening on: port 80      

设置开机自启,添加一个软链接

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl enable httpd --now #立刻激活 Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. ┌──[root@liruilongs.github.io]-[~] └─$systemctl is-enabled httpd enabled ┌──[root@liruilongs.github.io]-[~] └─$systemctl is-active  httpd active ┌──[root@liruilongs.github.io]-[~] └─$      

设置服务开机不启动

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl disable  httpd Removed /etc/systemd/system/multi-user.target.wants/httpd.service.      

服务禁用

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl mask httpd Created symlink /etc/systemd/system/httpd.service → /dev/null. ┌──[root@liruilongs.github.io]-[~] └─$systemctl start httpd Failed to start httpd.service: Unit httpd.service is masked. ┌──[root@liruilongs.github.io]-[~] └─$systemctl status  httpd | head -2 ● httpd.service    Loaded: masked (Reason: Unit httpd.service is masked.)      

可以删除软链接取消禁用

       ┌──[root@liruilongs.github.io]-[~] └─$ll  /etc/systemd/system/httpd.service lrwxrwxrwx. 1 root root 9 Apr  3 16:08 /etc/systemd/system/httpd.service -> /dev/null ┌──[root@liruilongs.github.io]-[~] └─$rm -r   /etc/systemd/system/httpd.service rm: remove symbolic link '/etc/systemd/system/httpd.service'? y ┌──[root@liruilongs.github.io]-[~] └─$systemctl daemon-reload ┌──[root@liruilongs.github.io]-[~] └─$systemctl start httpd      

可以通过unmask来取消禁用

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl mask httpd Created symlink /etc/systemd/system/httpd.service → /dev/null. ┌──[root@liruilongs.github.io]-[~] └─$systemctl unmask httpd Removed /etc/systemd/system/httpd.service. ┌──[root@liruilongs.github.io]-[~] └─$      

unit依赖

可以通过如下命令查看unit的启动依赖关系

       systemctl list-dependencies cups.service  systemctl list-dependencies cups.service --reverse      

查看httpd Service 正向依赖,在httpd之前启动

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-dependencies httpd httpd.service ● ├─-.mount ● ├─httpd-init.service ● ├─system.slice ● └─sysinit.target ●   ├─dev-hugepages.mount ●   ├─dev-mqueue.mount ●   ├─dracut-shutdown.service ●   ├─import-state.service ●   ├─iscsi-onboot.service ●   ├─kmod-static-nodes.service ...... ...... ┌──[root@liruilongs.github.io]-[~] └─$      

查看httpd Service 反向依赖,在httpd之后启动

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-dependencies httpd --reverse httpd.service ● └─multi-user.target ●   └─graphical.target      

查看multi-user.target正向依赖是否有httpd

       ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-dependencies multi-user.target | grep httpd ● ├─httpd.service      

嗯,关于systemd和小伙伴们分享到这里,这块涉及的内容很多,这里简单介绍,在Linux生态系统中,systemd被部署到了大多数的标准Linux发行版中,只有为数不多的几个发行版尚未部署。关于systemd启动过程、对其他unit的控制和管理、对系统运行级别等的控制,感兴趣小伙伴可以了解下。


user avatar   sinaean-dean-91 网友的相关建议: 
      

Android的binder,gnome的gvfs等,的确是在用户态提供一些系统级的服务。我不知道你说的systemd的哪个的特性。整体上讲,linux上的确越来越多的系统服务通过用户态进程提供了。通常这些用户态进程不视为内核的一部分。硬把他们视为内核的一部分然后获得一个混合内核的名头没啥必要。

有人说linux本来就是混合内核。我更倾向于认为linux是宏内核,但是它对微内核化的改造是开放的。kbdus,binder,fuse, dpdk, uio,selinux,sysfs,kernal module等,使你可以在用户态提供和管理几乎所有的系统级服务。我觉得linux的开放性使得微内核和宏内核的争论没什么意义。linux是一个宏内核,但是你只要愿意在用户态投入足够的开发,你就可以像微内核一样使用它。基于这个原因,我觉得任何在linux之外另起炉灶的做法意义都不大,比如说zircon。当然我有可能是错的,个人看法。


linux不能胜任的主要是单片机等cpu内存受限的场景,这个我不认为是微内核和宏内核的问题,这个纯粹通用os和专用os的问题。




  

相关话题

  作为一个 Emacs、终端、浏览器的重度用户,在考虑价格因素的情况下,你是否认为 Mac OS 比 Linux 发行版更值得使用? 
  Linux 发行版中哪个包管理器更强? 
  linux在系统调用进入内核时,为什么要将参数从用户空间拷贝到内核空间?不能直接访问,或是使用memcpy吗?非要使用copy_from_user才行吗? 
  要想完全放弃Windows操作系统而使用 Linux需要多少勇气? 
  IO多路复用到底是不是异步的? 
  腾讯以及各大厂的 C++ 开发环境是什么样的? 
  为什么感觉用Ubuntu写代码麻烦呢? 
  Linux C++ 服务器端这条线怎么走?一年半能做出什么? 
  大型c++项目在linux下如何调试? 
  需要用到 Linux/Unix,是买 MacBook Pro还是 Windows 装Linux vm? 

前一个讨论
各地驰援上海的物资能免费分配到隔离的群众手中吗?是按什么规则进行分配?
下一个讨论
4 月 6 日起杭州乘坐地铁需出示健康码及行程卡,目前当地防控情况如何?





© 2024-05-17 - tinynew.org. All Rights Reserved.
© 2024-05-17 - tinynew.org. 保留所有权利