MCPcopy
hub / github.com/google/gvisor / MaybeInstall

Method MaybeInstall

tools/gpu/drivers/install_driver.go:93–144  ·  view source on GitHub ↗

MaybeInstall installs a driver if 1) no driver is present on the system already or 2) the driver currently installed does not match the requested version.

(ctx context.Context)

Source from the content-addressed store, hash-verified

91// MaybeInstall installs a driver if 1) no driver is present on the system already or 2) the
92// driver currently installed does not match the requested version.
93func (i *Installer) MaybeInstall(ctx context.Context) error {
94 // If we don't support the driver, don't attempt to install it.
95 if _, ok := i.expectedChecksumFunc(i.requestedVersion); !ok {
96 return fmt.Errorf("requested driver %q is not supported", i.requestedVersion)
97 }
98
99 existingDriver, err := i.getCurrentDriverFunc()
100 if err != nil {
101 log.Warningf("failed to get current driver: %v", err)
102 }
103 if existingDriver.Equals(i.requestedVersion) {
104 log.Infof("Driver already installed: %s", i.requestedVersion)
105 return nil
106 }
107
108 if !existingDriver.Equals(nvconf.DriverVersion{}) {
109 log.Infof("Uninstalling driver: %s", existingDriver)
110 if err := i.uninstallDriver(ctx, existingDriver.String()); err != nil {
111 return fmt.Errorf("failed to uninstall driver: %w", err)
112 }
113 log.Infof("Driver uninstalled: %s", i.requestedVersion)
114 }
115
116 log.Infof("Downloading driver: %s", i.requestedVersion)
117 reader, err := i.downloadFunc(ctx, i.requestedVersion.String(), runtime.GOARCH)
118 if err != nil {
119 return fmt.Errorf("failed to download driver: %w", err)
120 }
121
122 f, err := os.CreateTemp("", "")
123 if err != nil {
124 return fmt.Errorf("failed to open driver file: %w", err)
125 }
126 defer os.Remove(f.Name())
127 if err := i.writeAndCheck(f, reader); err != nil {
128 f.Close()
129 return fmt.Errorf("writeAndCheck: %w", err)
130 }
131 if err := f.Chmod(0755); err != nil {
132 return fmt.Errorf("failed to chmod: %w", err)
133 }
134 if err := f.Close(); err != nil {
135 return fmt.Errorf("failed to close driver file: %w", err)
136 }
137 log.Infof("Driver downloaded: %s", i.requestedVersion)
138 log.Infof("Installing driver: %s", i.requestedVersion)
139 if err := i.installFunc(f.Name()); err != nil {
140 return fmt.Errorf("failed to install driver: %w", err)
141 }
142 log.Infof("Installation Complete!")
143 return nil
144}
145
146func (i *Installer) uninstallDriver(ctx context.Context, driverVersion string) error {
147 exec.Command(nvidiaUninstallPath, "-s", driverVersion)

Callers 5

mainFunction · 0.95
TestVersionInstalledFunction · 0.95
TestVersionNotSupportedFunction · 0.95
TestShaMismatchFunction · 0.95
TestDriverInstallsFunction · 0.95

Calls 10

uninstallDriverMethod · 0.95
writeAndCheckMethod · 0.95
WarningfFunction · 0.92
InfofFunction · 0.92
ErrorfMethod · 0.65
EqualsMethod · 0.65
StringMethod · 0.65
NameMethod · 0.65
CloseMethod · 0.65
RemoveMethod · 0.45

Tested by 4

TestVersionInstalledFunction · 0.76
TestVersionNotSupportedFunction · 0.76
TestShaMismatchFunction · 0.76
TestDriverInstallsFunction · 0.76