Linux下保存dmesg日志

1. 什么是dmesg

​ Linux内核是操作系统的核心,它控制对系统资源(例如:CPU、I/O设备、物理内存和文件系统)的访问。在引导过程中以及系统运行时,内核会将各种消息写入内核环形缓冲区,这些消息包括有关系统操作的各种信息。

​ 内核环形缓冲区是物理内存的一部分(意味着重新开机会重新生成,并不会累加保存),用于保存内核的日志消息,它具有固定的大小,这意味着一旦缓冲区已满,较旧的日志记录将被覆盖。考虑到开机信息非常重要,系统将开机信息也会另外保存到/var/log目录中dmesg文件中。

​ dmesg是一种程序,用于检测和控制内核环缓冲,程序用来帮助用户了解系统的启动和运行信息。kernel会将开机信息存储在ring buffer中,若是开机时来不及查看信息,可利用dmesg来查看

2. 怎么保存dmesg信息

dmesg的使用方法如下所示:

dmesg [OPTIONS]
详细信息可以通过输入 dmesg -h

在不带任何选项的情况下调用时,dmesg将所有消息从内核环形缓冲区写入标准输出
  • dmesg -T,打印人类可读的时间戳

3. QA

  1. 为了方便调查问题,怎么将dmesg信息都保存到系统的日志中去了?

​ 鉴于系统日志并不会持续性保存,应用程序为了方便调查问题,有时候又需要系统日志进行排除问题。这个时候就需要将系统日志保存到emmc等非丢失性设备中,比如可以通过下面的指令来保存系统日志。


[ -e $kotei_dmesg_cur_log ] && cat $kotei_dmesg_cur_log > $kotei_dmesg_last_log
cat /dev/kmsg > $kotei_dmesg_cur_log &
上述脚本的意思是:交替保存两份dmesg信息
  1. dmesg和kmsg有什么区别?

    dmesg is a program that dumps /proc/kmsg. In addition, it provides some filtering capabilities to weed out logs that the user isn't interested in.circular buffer is implemented by kernel itself, dmesg is just a util to read this circular via kernel interface /proc/kmsg

  2. /proc/kmsg和/dev/kmsg有什么区别吗?

The kernel mechanisms have changed.

The kernel generates output to an in-memory buffer. Application softwares can access this in two ways. The logging subsystem usually accesses it as a pseudo-FIFO named /proc/kmsg. This source of log information cannot usefully be shared amongst log readers, because it is read-once. If multiple processes share it, they each get only a part of the kernel log data stream. It is also read-only.

The other way to access it is the newer /dev/kmsg character device. This is a read-write interface that is shareable amongst multiple client processes. If multiple processes share it, they all read the same complete data stream, unaffected by one another. If they open it for write access, they can also inject messages into the kernel's log stream, as if they were generated by the kernel.

/proc/kmsg and /dev/kmsg provide log data in a non-RFC-5424 form.

简单的说,程序中推荐优先使用/dev/kmsg

posted @ 2020-12-31 17:51  yuzhenjin  阅读(8858)  评论(0编辑  收藏  举报