Skip to content

Commit

Permalink
virtiofsd: Improve logging
Browse files Browse the repository at this point in the history
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 <jose.carlos.venegas.munoz@intel.com>
  • Loading branch information
jcvenegas committed Apr 28, 2020
1 parent 85d0d2d commit 3b0e0ba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
57 changes: 47 additions & 10 deletions virtcontainers/virtiofsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"context"
"fmt"
"io"
"net"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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")
}
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions virtcontainers/virtiofsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 3b0e0ba

Please sign in to comment.