Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

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 <[email protected]>
  • Loading branch information
jcvenegas committed May 13, 2020
1 parent 90b4470 commit 882a823
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
62 changes: 52 additions & 10 deletions virtcontainers/virtiofsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"context"
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -58,6 +60,26 @@ 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

if _, err := os.Stat(filepath.Dir(v.socketPath)); err != nil {
return nil, errors.Errorf("Socket directory does not exist %s", filepath.Dir(v.socketPath))
}

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,19 +90,27 @@ 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 pid, err
return 0, err
}

v.Logger().WithField("path", v.path).Info()
v.Logger().WithField("args", strings.Join(args, " ")).Info()
cmd.ExtraFiles = append(cmd.ExtraFiles, socketFD)

cmd := exec.Command(v.path, args...)
stderr, err := cmd.StderrPipe()
// 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, fmt.Errorf("failed to get stderr from virtiofsd command, error: %s", err)
return pid, err
}
cmd.Args = append(cmd.Args, args...)

v.Logger().WithField("path", v.path).Info()
v.Logger().WithField("args", strings.Join(args, " ")).Info()

if err = utils.StartCmd(cmd); err != nil {
return pid, err
Expand All @@ -96,7 +126,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 +145,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 +155,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
13 changes: 11 additions & 2 deletions virtcontainers/virtiofsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,27 @@ func TestVirtiofsdStart(t *testing.T) {
assert.NoError(err)
defer os.RemoveAll(sourcePath)

socketDir, err := ioutil.TempDir("", "")
assert.NoError(err)
defer os.RemoveAll(sourcePath)

socketPath := socketDir + "socket.s"

validConfig := fields{
path: "/tmp/a/path",
socketPath: "/tmp/a/path/to/sock.sock",
path: "/usr/bin/virtiofsd-path",
socketPath: socketPath,
sourcePath: sourcePath,
}
NoDirectorySocket := validConfig
NoDirectorySocket.socketPath = "/tmp/path/to/virtiofsd/socket.sock"

tests := []struct {
name string
fields fields
wantErr bool
}{
{"empty config", fields{}, true},
{"Directory socket does not exist", NoDirectorySocket, true},
{"valid config", validConfig, false},
}
for _, tt := range tests {
Expand Down

0 comments on commit 882a823

Please sign in to comment.