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 }