[systemd-devel] Why systemd-journald is creating '/var/log/journal/machine_id" directory when Storage=auto

Michael Chapman mike at very.puzzling.org
Sun Aug 29 12:12:42 UTC 2021


On Sun, 29 Aug 2021, Nishant Nayan wrote:
> I was looking into the code of systemd-journald and found this (in
> 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");
> 
> Here, system_storage.path is set to "strjoin("/var/log/journal/",
> SERVER_MACHINE_ID(s));" in previous function call.
> 
> As far as I understood its saying to create '/var/log/journal' directory
> when storage is set to 'persistent'.
> 
> But in either of the cases (persistent or auto) why is it creating
> '/var/log/journal/machine_id' directory ( (void)
> mkdir(s->system_storage.path, 0755); ) ??
> 
> 'auto' will store logs persistently if '/var/log/journal' is created
> beforehand or else logs will be written in '/run/log/journal' .
> 
> For 'auto' it should not create '/var/log/journal/machine_id' directory
> right?

All of this is guarded with:

    flush_requested || flushed_flag_is_set()

In other words, this code path is used if a request to flush the journal 
data from /run into /var had been received, or the runtime journal had 
already been flushed in the past.

> Also after reading the comment, how is it possible to create
> '/var/log/journal/machine_id' without creating the prefix? I am assuming
> '/var/log/journal' is the prefix .

mkdir_p() creates parent directories, like `mkdir -p` in your shell.


More information about the systemd-devel mailing list