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 <dave.scott@docker.com>
This commit is contained in:
David Scott 2018-07-13 11:43:48 +01:00
parent 5201049f2c
commit 7c243a8e8b
2 changed files with 17 additions and 5 deletions

View File

@ -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

View File

@ -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
}