From 7153124f97ea58539a6be36db0bd4783ed9a2dd6 Mon Sep 17 00:00:00 2001 From: Matt Heon Date: Mon, 16 Oct 2023 15:06:23 -0400 Subject: [PATCH] Log gvproxy and server9 to file on log-level=debug Logging to os.Stdout and os.Stderr does not seem to work in Powershell. I am not entirely certain why. Logfiles are the best alternative I can think of. Signed-off-by: Matt Heon --- cmd/podman/machine/client9p.go | 12 ++++-------- pkg/machine/hyperv/machine.go | 29 +++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/cmd/podman/machine/client9p.go b/cmd/podman/machine/client9p.go index 4bf1318230..a4305ca535 100644 --- a/cmd/podman/machine/client9p.go +++ b/cmd/podman/machine/client9p.go @@ -106,16 +106,12 @@ func client9p(portNum uint32, mountPath string) error { cmd := exec.Command("mount", "-t", "9p", "-o", "trans=fd,rfdno=3,wfdno=3,version=9p2000.L", "9p", mountPath) cmd.ExtraFiles = []*os.File{vsock} - err := cmd.Run() - output, outErr := cmd.CombinedOutput() - switch { - case outErr != nil: - logrus.Errorf("Unable to obtain output of mount command: %v", err) - case err == nil: + output, err := cmd.CombinedOutput() + if err != nil { + err = fmt.Errorf("running mount: %w\nOutput: %s", err, string(output)) + } else { logrus.Debugf("Mount output: %s", string(output)) logrus.Infof("Mounted directory %s using 9p", mountPath) - default: - err = fmt.Errorf("running mount: %w\nOutput: %s", err, string(output)) } errChan <- err diff --git a/pkg/machine/hyperv/machine.go b/pkg/machine/hyperv/machine.go index 891e0a9f75..5bdba9bb2a 100644 --- a/pkg/machine/hyperv/machine.go +++ b/pkg/machine/hyperv/machine.go @@ -772,8 +772,9 @@ func (m *HyperVMachine) startHostNetworking() (string, machine.APIForwardingStat c := cmd.Cmd(gvproxyBinary) if logrus.IsLevelEnabled(logrus.DebugLevel) { - c.Stdout = os.Stdout - c.Stderr = os.Stderr + if err := logCommandToFile(c, "gvproxy.log"); err != nil { + return "", 0, err + } } logrus.Debugf("Starting gvproxy with command: %s %v", gvproxyBinary, c.Args) @@ -809,8 +810,9 @@ func (m *HyperVMachine) startHostNetworking() (string, machine.APIForwardingStat fsCmd := exec.Command(executable, args...) if logrus.IsLevelEnabled(logrus.DebugLevel) { - fsCmd.Stdout = os.Stdout - fsCmd.Stderr = os.Stderr + if err := logCommandToFile(fsCmd, "podman-machine-server9.log"); err != nil { + return "", 0, err + } } if err := fsCmd.Start(); err != nil { @@ -822,6 +824,25 @@ func (m *HyperVMachine) startHostNetworking() (string, machine.APIForwardingStat return forwardSock, state, nil } +func logCommandToFile(c *exec.Cmd, filename string) error { + dir, err := machine.GetDataDir(machine.HyperVVirt) + if err != nil { + return fmt.Errorf("obtain machine dir: %w", err) + } + path := filepath.Join(dir, filename) + logrus.Infof("Going to log to %s", path) + log, err := os.Create(path) + if err != nil { + return fmt.Errorf("create log file: %w", err) + } + defer log.Close() + + c.Stdout = log + c.Stderr = log + + return nil +} + func (m *HyperVMachine) setupAPIForwarding(cmd gvproxy.GvproxyCommand) (gvproxy.GvproxyCommand, string, machine.APIForwardingState) { socket, err := m.forwardSocketPath() if err != nil {