Linux

為什麼 systemd-journald 在 Storage=auto 時創建“/var/log/journal/machine_id”目錄

  • September 7, 2021

我正在研究 systemd-journald 的程式碼,發現了這個(在system_journal_open()):

if (!s->system_journal &&
   IN_SET(s->storage, STORAGE_PERSISTENT, STORAGE_AUTO) &&
   (flush_requested || flushed_flag_is_set())) {

       /* If in auto mode: first try to create the machine
        * path, but not the prefix.
        *
        * If in persistent mode: create /var/log/journal and
        * the machine path */

       if (s->storage == STORAGE_PERSISTENT)
               (void) mkdir_p("/var/log/journal/", 0755);

       (void) mkdir(s->system_storage.path, 0755);

       fn = strjoina(s->system_storage.path, "/system.journal");

在這裡,在之前的函式呼叫system_storage.path中設置為。strjoin("/var/log/journal/", SERVER_MACHINE_ID(s));

據我了解,/var/log/journal當儲存設置為persistent. 但是在任何一種情況下(persistentauto)為什麼要創建/var/log/journal/machine_id目錄((void) mkdir(s->system_storage.path, 0755);)? auto如果事先創建,將持久儲存日誌/var/log/journal,否則日誌將寫入/run/log/journal. 因為auto它不應該創建/var/log/journal/machine_id目錄嗎?

同樣在閱讀評論後,如何在/var/log/journal/machine_id不創建前綴的情況下創建?我假設/var/log/journal是前綴。

我已經想通了,這只是一個混亂,所以如果設置為persistent程式碼將創建/var/log/journal前綴然後是machine_id目錄,如果設置為auto則不/var/log/journal創建(前綴)因此嘗試通過創建完整路徑(void) mkdir(s->system_storage.path, 0755); 將失敗,因為journal目錄失去(假設大多數係統已經/var/log/mkdir``mkdir

引用自:https://unix.stackexchange.com/questions/666711