|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package drivers |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net" |
| 11 | + |
| 12 | + "github.com/Microsoft/go-winio/pkg/guid" |
| 13 | + |
| 14 | + "github.com/Microsoft/hcsshim/internal/cmd" |
| 15 | + "github.com/Microsoft/hcsshim/internal/guestpath" |
| 16 | +) |
| 17 | + |
| 18 | +var noExecOutputErr = errors.New("failed to get any pipe output") |
| 19 | + |
| 20 | +// guest is the UVM instance in which the driver will be installed. |
| 21 | +type guest interface { |
| 22 | + ExecInUVM(ctx context.Context, request *cmd.CmdProcessRequest) (int, error) |
| 23 | +} |
| 24 | + |
| 25 | +// ExecGCSInstallDriver installs a driver into the UVM by running 'install-drivers' |
| 26 | +// inside the guest. hostPath is the host VHD path and guestPath is the |
| 27 | +// SCSI-mounted location inside the UVM. Returns an error if installation fails, |
| 28 | +// along with any stderr output from the guest process. |
| 29 | +func ExecGCSInstallDriver(ctx context.Context, guest guest, hostPath string, guestPath string) error { |
| 30 | + driverReadWriteDir, err := getDriverWorkDir(hostPath) |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("failed to create a guid path for driver %+v: %w", hostPath, err) |
| 33 | + } |
| 34 | + |
| 35 | + p, l, err := cmd.CreateNamedPipeListener() |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + defer l.Close() |
| 40 | + |
| 41 | + var stderrOutput string |
| 42 | + errChan := make(chan error) |
| 43 | + |
| 44 | + go readAllPipeOutput(l, errChan, &stderrOutput) |
| 45 | + |
| 46 | + args := []string{ |
| 47 | + "/bin/install-drivers", |
| 48 | + driverReadWriteDir, |
| 49 | + guestPath, |
| 50 | + } |
| 51 | + req := &cmd.CmdProcessRequest{ |
| 52 | + Args: args, |
| 53 | + Stderr: p, |
| 54 | + } |
| 55 | + |
| 56 | + // A call to `ExecInUvm` may fail in the following ways: |
| 57 | + // - The process runs and exits with a non-zero exit code. In this case we need to wait on the output |
| 58 | + // from stderr so we can log it for debugging. |
| 59 | + // - There's an error trying to run the process. No need to wait for stderr logs. |
| 60 | + // - There's an error copying IO. No need to wait for stderr logs. |
| 61 | + // |
| 62 | + // Since we cannot distinguish between the cases above, we should always wait to read the stderr output. |
| 63 | + exitCode, execErr := guest.ExecInUVM(ctx, req) |
| 64 | + |
| 65 | + // wait to finish parsing stdout results |
| 66 | + select { |
| 67 | + case err := <-errChan: |
| 68 | + if err != nil && !errors.Is(err, noExecOutputErr) { |
| 69 | + return fmt.Errorf("failed to get stderr output from command %s: %w", guestPath, err) |
| 70 | + } |
| 71 | + case <-ctx.Done(): |
| 72 | + return fmt.Errorf("timed out waiting for the console output from installing driver %s: %w", guestPath, ctx.Err()) |
| 73 | + } |
| 74 | + |
| 75 | + if execErr != nil { |
| 76 | + return fmt.Errorf("%w: failed to install driver %s in uvm with exit code %d: %v", execErr, guestPath, exitCode, stderrOutput) |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// getDriverWorkDir returns the deterministic guest path used as the overlayfs |
| 82 | +// root for a driver installation. 'install-drivers' uses the read-only SCSI VHD |
| 83 | +// as the lower layer and uses this directory for the upper, work, and content |
| 84 | +// directories, giving depmod/modprobe a writable view. |
| 85 | +// |
| 86 | +// If the directory already exists, 'install-drivers' skips reinstallation. |
| 87 | +// The path is derived from a v5 UUID seeded with the host VHD path, |
| 88 | +// ensuring a stable mapping across reboots. |
| 89 | +func getDriverWorkDir(hostPath string) (string, error) { |
| 90 | + // 914aadc8-f700-4365-8016-ddad0a9d406d. Random GUID chosen for namespace. |
| 91 | + ns := guid.GUID{ |
| 92 | + Data1: 0x914aadc8, |
| 93 | + Data2: 0xf700, |
| 94 | + Data3: 0x4365, |
| 95 | + Data4: [8]byte{0x80, 0x16, 0xdd, 0xad, 0x0a, 0x9d, 0x40, 0x6d}, |
| 96 | + } |
| 97 | + |
| 98 | + driverGUID, err := guid.NewV5(ns, []byte(hostPath)) |
| 99 | + if err != nil { |
| 100 | + return "", err |
| 101 | + } |
| 102 | + |
| 103 | + return fmt.Sprintf(guestpath.LCOWGlobalDriverPrefixFmt, driverGUID.String()), nil |
| 104 | +} |
| 105 | + |
| 106 | +// readAllPipeOutput is a helper function that connects to a listener and attempts to |
| 107 | +// read the connection's entire output. Resulting output is returned as a string |
| 108 | +// in the `result` param. The `errChan` param is used to propagate an errors to |
| 109 | +// the calling function. |
| 110 | +func readAllPipeOutput(l net.Listener, errChan chan<- error, result *string) { |
| 111 | + defer close(errChan) |
| 112 | + c, err := l.Accept() |
| 113 | + if err != nil { |
| 114 | + errChan <- fmt.Errorf("failed to accept named pipe: %w", err) |
| 115 | + return |
| 116 | + } |
| 117 | + bytes, err := io.ReadAll(c) |
| 118 | + if err != nil { |
| 119 | + errChan <- err |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + *result = string(bytes) |
| 124 | + |
| 125 | + if len(*result) == 0 { |
| 126 | + errChan <- noExecOutputErr |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + errChan <- nil |
| 131 | +} |
0 commit comments