diff --git a/tests/core/persistent-journal-namespace/task.yaml b/tests/core/persistent-journal-namespace/task.yaml new file mode 100644 index 00000000000..9acf4fc903a --- /dev/null +++ b/tests/core/persistent-journal-namespace/task.yaml @@ -0,0 +1,60 @@ +summary: Test journal.persistent core config option is honored by journal namespaces + +# these systems do not support journal quota groups due to their old systemd versions. +# requires systemd v245+ +systems: + - -ubuntu-core-16-* + - -ubuntu-core-18-* + +prepare: | + "$TESTSTOOLS"/snaps-state install-local test-snapd-journal-quota + snap set system experimental.quota-groups=true + tests.cleanup defer snap unset system experimental.quota-groups + +restore: | + # Stop test service and cleanup + snap stop test-snapd-journal-quota.logger + snap remove-quota group-one + + # disable persistent journal and reload the journal + rm -rf /var/log/journal + systemctl kill --signal=SIGUSR1 systemd-journald + +execute: | + echo "Wait for first boot to be done" + snap wait system seed.loaded + + echo "Precondition check, persistent journal is not available by default" + not test -e /var/log/journal + + echo "Check that persistent journal can be enabled" + snap set core journal.persistent=true + test -e /var/log/journal/ + test -e /var/log/journal/.snapd-created + + # Create a journal quota group and set arbitrary limits + echo "Create a journal namespace with the logger snap in it" + snap set-quota group-one --journal-size=16MB --journal-rate-limit=100/10ms test-snapd-journal-quota + + # this check relies on *anything* getting logged; enabling persistent + # journal writes an entry about journal size, which should be sufficient. + MACHINE_ID=$(cat /etc/machine-id) + retry -n 30 --wait 1 test -e "/var/log/journal/$MACHINE_ID" + + echo "Starting service to fill namespace with logs" + snap start test-snapd-journal-quota.logger + + # Verify the presence of the namespace log, and do this with retries as + # the logger writes onces every second + retry -n 5 sh -c "test -e /var/log/journal/$MACHINE_ID.snap-group-one" + + echo "Check that persistent journal can be disabled" + snap set core journal.persistent=false + not test -e /var/log/journal + + # Verify that, over a few seconds, nothing is created + # to make sure the namespace is not recreating the path + if retry -n 2 sh -c "test -e /var/log/journal"; then + echo "Journal path was recreated, it shouldn't have happened" + exit 1 + fi diff --git a/wrappers/services.go b/wrappers/services.go index c3c8c1685a5..55582dcfd38 100644 --- a/wrappers/services.go +++ b/wrappers/services.go @@ -184,8 +184,15 @@ func generateJournaldConfFile(grp *quota.Group) []byte { sizeOptions := formatJournalSizeConf(grp) rateOptions := formatJournalRateConf(grp) + // Set Storage=auto for all journal namespaces we create. This is + // the setting for the default namespace, and 'persistent' is the default + // setting for all namespaces. However we want namespaces to honor the + // journal.persistent setting, and this only works if Storage is set + // to 'auto'. + // See https://www.freedesktop.org/software/systemd/man/journald.conf.html#Storage= template := `# Journald configuration for snap quota group %[1]s [Journal] +Storage=auto ` buf := bytes.Buffer{} fmt.Fprintf(&buf, template, grp.Name) diff --git a/wrappers/services_test.go b/wrappers/services_test.go index 7e9cf3c0df7..6f9d9f2c244 100644 --- a/wrappers/services_test.go +++ b/wrappers/services_test.go @@ -560,6 +560,7 @@ WantedBy=multi-user.target ) jconfTempl := `# Journald configuration for snap quota group %s [Journal] +Storage=auto ` sliceTempl := `[Unit] @@ -663,6 +664,7 @@ WantedBy=multi-user.target ) jconfTempl := `# Journald configuration for snap quota group %s [Journal] +Storage=auto SystemMaxUse=10485760 RuntimeMaxUse=10485760 RateLimitIntervalSec=5000000us @@ -769,6 +771,7 @@ WantedBy=multi-user.target ) jconfTempl := `# Journald configuration for snap quota group %s [Journal] +Storage=auto RateLimitIntervalSec=0us RateLimitBurst=0 `