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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM