Skip to content

Latest commit

 

History

History
116 lines (92 loc) · 7.66 KB

ex29.md

File metadata and controls

116 lines (92 loc) · 7.66 KB

练习 29:内核:内核消息,dmesg

原文:Exercise 29. Kernel: kernel messages, dmesg

译者:飞龙

协议:CC BY-NC-SA 4.0

自豪地采用谷歌翻译

那么,如果你到达了这里,现在是谈谈内核的时候了。我们将使用维基百科的操作系统内核定义,开始这个讨论:

在计算机中,内核(来自德语 Kern)是大多数计算机操作系统的主要组成部分;它是应用程序和硬件级别上进行的实际数据处理之间的桥梁。内核的职责包括管理系统的资源(硬件和软件组件之间的通信)。通常,作为操作系统的基本组件,内核可以为资源(特别是处理器和 I/O 设备)提供最底层的抽象,应用软件必须控制它来执行其功能。它通常通过进程间通信机制和系统调用,使这些设施可用于应用程序进程。

这是维基百科告诉我们的,Linux 内核的具体内容:

Linux 内核是 Linux 系列类 Unix 操作系统使用的操作系统内核。它是自由和开源软件最突出的例子之一。它支持真正的抢占式多任务(在用户模式和内核模式下),虚拟内存,共享库,按需加载,共享的写时复制(COW)可执行文件,内存管理,互联网协议组和线程。

现在是访问相应的维基百科文章的好时机,并花费一些时间疯狂点击所有可怕的术语,它们描述 Linux 内核的技术特性。这样做之后,让我们谈谈更多的单调的主题,这是内核告诉我们的一种方式。例如,如果 USB 记忆棒连接到计算机,或者网络链接断开或挂载了文件系统,则会发生这种情况。为了能够告诉你所有这些东西,内核使用一种称为显示消息 或驱动消息的机制,其名称缩写为dmesg

该机制由固定大小的缓冲区表示,内核向它写入消息。在 Debian Linux 上,系统日志守护进程启动后,从缓冲区发布的信息也会被复制到/var/log/dmesg。这样做是为了保留这些消息,否则将被新的消息覆盖。

dmesg也是工具的名称,它允许你查看当前在内核缓冲区中的那些消息,并更改此缓冲区大小。

让我总结一下dmesg相关的文件和程序:

  • dmesg - 打印或控制内核环缓冲区
  • /var/log/dmseg - Debian 发行版中的日志文件,仅包含系统引导期间的dmesg消息副本,而不包含时间戳。
  • /var/log/kern.log - Debian 发行版中的日志文件,包含所有dmesg消息的副本,包括时间戳请注意,rsyslog 日志守护进程启动后,这个时间戳开始变化,这意味着rsyslog启动前,所有引导时的消息将具有相同的时间戳。此文件本身包含/var/log/dmseg
  • /var/log/messages - Debian 发行版中的日志文件,记录所有非调试和非关键消息。它本身包含/var/log/dmesg
  • /var/log/syslog - Debian 发行版中的日志文件,记录了所有信息,但权限相关的信息除外。它包含/var/log/messages/var/log/kern.log中的所有消息。

这样做

1: date
2: sudo umount /tmp ; sudo mount /tmp
3: sudo tail -f /var/log/dmesg /var/log/messages /var/log/syslog /var/log/kern.log

你会看到什么

user1@vm1:~$ date
Tue Jul 24 06:55:33 EDT 2012
user1@vm1:~$ sudo umount /tmp ; sudo mount /tmp
user1@vm1:~$ dmesg | tail
[    7.166240] tun: Universal TUN/TAP device driver, 1.6
[    7.166242] tun: (C) 1999-2004 Max Krasnyansky <[email protected]>
[    7.432019] ADDRCONF(NETDEV_UP): eth0: link is not ready
[    7.435270] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
[    7.435927] ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[   17.472049] tap0: no IPv6 routers present
[   17.592044] eth0: no IPv6 routers present
[  217.497357] kjournald starting.  Commit interval 5 seconds
[  217.497561] EXT3 FS on sda8, internal journal
[  217.497564] EXT3-fs: mounted filesystem with ordered data mode.
user1@vm1:~$ sudo tail /var/log/dmesg /var/log/messages /var/log/syslog /var/log/kern.log
==> /var/log/dmesg <==
[    6.762569] EXT3 FS on sda5, internal journal
[    6.762572] EXT3-fs: mounted filesystem with ordered data mode.
[    6.767237] kjournald starting.  Commit interval 5 seconds
[    6.767407] EXT3 FS on sda6, internal journal
[    6.767410] EXT3-fs: mounted filesystem with ordered data mode.
[    7.166240] tun: Universal TUN/TAP device driver, 1.6
[    7.166242] tun: (C) 1999-2004 Max Krasnyansky <[email protected]>
[    7.432019] ADDRCONF(NETDEV_UP): eth0: link is not ready
[    7.435270] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
[    7.435927] ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
 
==> /var/log/messages <==
Jul 24 06:52:07 vm1 kernel: [    6.767407] EXT3 FS on sda6, internal journal
Jul 24 06:52:07 vm1 kernel: [    6.767410] EXT3-fs: mounted filesystem with ordered data mode.
Jul 24 06:52:07 vm1 kernel: [    7.166240] tun: Universal TUN/TAP device driver, 1.6
Jul 24 06:52:07 vm1 kernel: [    7.166242] tun: (C) 1999-2004 Max Krasnyansky <[email protected]>
Jul 24 06:52:07 vm1 kernel: [    7.432019] ADDRCONF(NETDEV_UP): eth0: link is not ready
Jul 24 06:52:07 vm1 kernel: [    7.435270] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
Jul 24 06:52:07 vm1 kernel: [    7.435927] ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
Jul 24 06:55:36 vm1 kernel: [  217.497357] kjournald starting.  Commit interval 5 seconds
Jul 24 06:55:36 vm1 kernel: [  217.497561] EXT3 FS on sda8, internal journal
Jul 24 06:55:36 vm1 kernel: [  217.497564] EXT3-fs: mounted filesystem with ordered data mode.
 
==> /var/log/syslog <==
Jul 24 06:52:08 vm1 acpid: 1 rule loaded
Jul 24 06:52:08 vm1 acpid: waiting for events: event logging is off
Jul 24 06:52:08 vm1 /usr/sbin/cron[882]: (CRON) INFO (pidfile fd = 3)
Jul 24 06:52:08 vm1 /usr/sbin/cron[883]: (CRON) STARTUP (fork ok)
Jul 24 06:52:08 vm1 /usr/sbin/cron[883]: (CRON) INFO (Running @reboot jobs)
Jul 24 06:52:16 vm1 kernel: [   17.472049] tap0: no IPv6 routers present
Jul 24 06:52:16 vm1 kernel: [   17.592044] eth0: no IPv6 routers present
Jul 24 06:55:36 vm1 kernel: [  217.497357] kjournald starting.  Commit interval 5 seconds
Jul 24 06:55:36 vm1 kernel: [  217.497561] EXT3 FS on sda8, internal journal
Jul 24 06:55:36 vm1 kernel: [  217.497564] EXT3-fs: mounted filesystem with ordered data mode.
 
==> /var/log/kern.log <==
Jul 24 06:52:07 vm1 kernel: [    7.166240] tun: Universal TUN/TAP device driver, 1.6
Jul 24 06:52:07 vm1 kernel: [    7.166242] tun: (C) 1999-2004 Max Krasnyansky <[email protected]>
Jul 24 06:52:07 vm1 kernel: [    7.432019] ADDRCONF(NETDEV_UP): eth0: link is not ready
Jul 24 06:52:07 vm1 kernel: [    7.435270] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
Jul 24 06:52:07 vm1 kernel: [    7.435927] ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
Jul 24 06:52:16 vm1 kernel: [   17.472049] tap0: no IPv6 routers present
Jul 24 06:52:16 vm1 kernel: [   17.592044] eth0: no IPv6 routers present
Jul 24 06:55:36 vm1 kernel: [  217.497357] kjournald starting.  Commit interval 5 seconds
Jul 24 06:55:36 vm1 kernel: [  217.497561] EXT3 FS on sda8, internal journal
Jul 24 06:55:36 vm1 kernel: [  217.497564] EXT3-fs: mounted filesystem with ordered data mode.

解释

  1. 打印出当前日期和时间。
  2. 从内核消息缓冲区打印最后 10 条消息。
  3. /var/log/dmesg, /var/log/messages, /var/log/syslog/var/log/kern.log打印最后 10 条消息。

附加题

这就完了,没有附加题,哇哦!