From 3b0e0ba3e00c92ab82a6db4c01cd77d065721422 Mon Sep 17 00:00:00 2001 From: Jose Carlos Venegas Munoz Date: Mon, 13 Apr 2020 19:15:16 +0000 Subject: [PATCH] virtiofsd: Improve logging Send virtiofsd logs to syslog in the same way that qemu implementation does. This requires not to wait for messages from virtiofsd stdout. This takes the qemu implementation approach. Give the socket fd to virtiofsd. Signed-off-by: Jose Carlos Venegas Munoz --- virtcontainers/virtiofsd.go | 57 ++++++++++++++++++++++++++------ virtcontainers/virtiofsd_test.go | 3 ++ 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/virtcontainers/virtiofsd.go b/virtcontainers/virtiofsd.go index d15b5ca15a..cb1abf4c22 100644 --- a/virtcontainers/virtiofsd.go +++ b/virtcontainers/virtiofsd.go @@ -10,6 +10,7 @@ import ( "context" "fmt" "io" + "net" "os" "os/exec" "strings" @@ -58,6 +59,22 @@ type virtiofsd struct { wait virtiofsdWaitFunc } +// Open socket on behalf of virtiofsd +// return file descriptor to be used by virtiofsd. +func (v *virtiofsd) getSocketFD() (*os.File, error) { + var listener *net.UnixListener + + listener, err := net.ListenUnix("unix", &net.UnixAddr{Name: v.socketPath, Net: "unix"}) + if err != nil { + return nil, err + } + defer listener.Close() + + listener.SetUnlinkOnClose(false) + + return listener.File() +} + // Start the virtiofsd daemon func (v *virtiofsd) Start(ctx context.Context) (int, error) { span, _ := v.trace("Start") @@ -68,20 +85,28 @@ func (v *virtiofsd) Start(ctx context.Context) (int, error) { return pid, err } - args, err := v.args() + cmd := exec.Command(v.path) + + socketFD, err := v.getSocketFD() + if err != nil { + return 0, err + } + + cmd.ExtraFiles = append(cmd.ExtraFiles, socketFD) + + // Extra files start from 2 (0: stdin, 1: stdout, 2: stderr) + // Extra FDs for virtiofsd start from 3 + // Get the FD number for previous added socketFD + socketFdNumber := 2 + uint(len(cmd.ExtraFiles)) + args, err := v.args(socketFdNumber) if err != nil { return pid, err } + cmd.Args = append(cmd.Args, args...) v.Logger().WithField("path", v.path).Info() v.Logger().WithField("args", strings.Join(args, " ")).Info() - cmd := exec.Command(v.path, args...) - stderr, err := cmd.StderrPipe() - if err != nil { - return pid, fmt.Errorf("failed to get stderr from virtiofsd command, error: %s", err) - } - if err = utils.StartCmd(cmd); err != nil { return pid, err } @@ -96,7 +121,7 @@ func (v *virtiofsd) Start(ctx context.Context) (int, error) { v.wait = waitVirtiofsReady } - return cmd.Process.Pid, v.wait(cmd, stderr, v.debug) + return pid, socketFD.Close() } func (v *virtiofsd) Stop() error { @@ -115,7 +140,7 @@ func (v *virtiofsd) Stop() error { return nil } -func (v *virtiofsd) args() ([]string, error) { +func (v *virtiofsd) args(FdSocketNumber uint) ([]string, error) { if v.sourcePath == "" { return []string{}, errors.New("vitiofsd source path is empty") } @@ -125,11 +150,23 @@ func (v *virtiofsd) args() ([]string, error) { } args := []string{ + // Send logs to syslog + "--syslog", + // foreground operation "-f", + // cache mode for virtiofsd "-o", "cache=" + v.cache, + // disable posix locking in daemon: bunch of basic posix locks properties are broken + // apt-get update is broken if enabled "-o", "no_posix_lock", + // shared directory tree "-o", "source=" + v.sourcePath, - "-o", "vhost_user_socket=" + v.socketPath, + // fd number of vhost-user socket + fmt.Sprintf("--fd=%v", FdSocketNumber), + } + + if v.debug { + args = append(args, "-o", "debug") } if len(v.extraArgs) != 0 { diff --git a/virtcontainers/virtiofsd_test.go b/virtcontainers/virtiofsd_test.go index ebac2644b4..65b92f6778 100644 --- a/virtcontainers/virtiofsd_test.go +++ b/virtcontainers/virtiofsd_test.go @@ -11,6 +11,7 @@ import ( "io/ioutil" "os" "os/exec" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -45,6 +46,7 @@ func TestVirtiofsdStart(t *testing.T) { wantErr bool }{ {"empty config", fields{}, true}, + {"Directory socket does not exist", validConfig, false}, {"valid config", validConfig, false}, } for _, tt := range tests { @@ -64,6 +66,7 @@ func TestVirtiofsdStart(t *testing.T) { }, } var ctx context.Context + err = os.MkdirAll(filepath.Dir(v.socketPath), testDirMode) _, err := v.Start(ctx) if (err != nil) != tt.wantErr { t.Errorf("virtiofsd.Start() error = %v, wantErr %v", err, tt.wantErr)