From 7c243a8e8bf610fefac70ec21329a4bc72ba018b Mon Sep 17 00:00:00 2001 From: David Scott Date: Fri, 13 Jul 2018 11:43:48 +0100 Subject: [PATCH] pkg/init: only create /var/log/onboot symlink if not using memlogd When logging directly to files (the not-using-memlogd case) the onboot services must log to /run/log because /var/log might be overmounted by a persistent disk. Therefore we create a symlink at the end of the onboot section. When logging via memlogd, all logs are buffered until a logwrite service starts, so no symlink is needed. Signed-off-by: David Scott --- pkg/init/cmd/service/logging.go | 16 ++++++++++++++++ pkg/init/cmd/service/runc.go | 6 +----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pkg/init/cmd/service/logging.go b/pkg/init/cmd/service/logging.go index 820465bc8..ad6c0bd12 100644 --- a/pkg/init/cmd/service/logging.go +++ b/pkg/init/cmd/service/logging.go @@ -29,6 +29,7 @@ type Log interface { Path(string) string // Path of the log file (may be a FIFO) Open(string) (io.WriteCloser, error) // Opens a log stream Dump(string) // Copies logs to the console + Symlink(string) // Symlinks to the log directory (if there is one) } // GetLog returns the log destination we should use. @@ -82,6 +83,16 @@ func (f *fileLog) Dump(n string) { } } +// Symlinks to the log directory. This is useful if we are logging directly to tmpfs and now need to symlink from a permanent disk. +func (f *fileLog) Symlink(path string) { + parent := filepath.Dir(path) + if err := os.MkdirAll(parent, 0755); err != nil { + log.Printf("Error creating secondary log directory %s: %v", parent, err) + } else if err := os.Symlink(f.dir, path); err != nil && !os.IsExist(err) { + log.Printf("Error creating symlink from %s to %s: %v", path, f.dir, err) + } +} + type remoteLog struct { fifoDir string } @@ -164,6 +175,11 @@ func (r *remoteLog) Dump(n string) { } } +// Symlinks to the log directory. This is a no-op because there is no log directory. +func (r *remoteLog) Symlink(path string) { + return +} + func sendToLogger(name string, fd int) error { var ctlSocket int var err error diff --git a/pkg/init/cmd/service/runc.go b/pkg/init/cmd/service/runc.go index 656912bfd..30e85c38d 100644 --- a/pkg/init/cmd/service/runc.go +++ b/pkg/init/cmd/service/runc.go @@ -160,11 +160,7 @@ func runcInit(rootPath, serviceType string) int { _ = os.RemoveAll(tmpdir) // make sure the link exists from /var/log/onboot -> /run/log/onboot - if err := os.MkdirAll(varLogDir, 0755); err != nil { - log.Printf("Error creating secondary log directory %s: %v", varLogDir, err) - } else if err := os.Symlink(logDir, varLogLink); err != nil && !os.IsExist(err) { - log.Printf("Error creating symlink from %s to %s: %v", varLogLink, logDir, err) - } + logger.Symlink(varLogLink) return status }