| package installer | |
| import ( | |
| "fmt" | |
| "io/fs" | |
| "os" | |
| "path/filepath" | |
| "strings" | |
| log "github.com/golang/glog" | |
| "github.com/pkg/errors" | |
| "cos.googlesource.com/cos/tools.git/src/pkg/utils" | |
| ) | |
| const ( | |
| // CacheFile is the name of the file an installation directory holds to | |
| // describe the driver installation in it. | |
| CacheFile = ".cache" | |
| buildNumberKey = "BUILD_ID" | |
| driverVersionKey = "DRIVER_VERSION" | |
| kernelOpenKey = "KERNEL_OPEN" | |
| kernelOpenValue = "Y" | |
| ) | |
| // Cacher is to cache GPU driver installation info. | |
| type Cacher struct { | |
| gpuInstallDir string | |
| buildNumber string | |
| driverVersion string | |
| } | |
| // NewCacher returns an instance of Cacher. | |
| func NewCacher(gpuInstallDir, buildNumber, driverVersion string) *Cacher { | |
| if gpuInstallDir != "" { | |
| return &Cacher{gpuInstallDir: gpuInstallDir, buildNumber: buildNumber, driverVersion: driverVersion} | |
| } | |
| return &Cacher{gpuInstallDir: gpuInstallDirContainer, buildNumber: buildNumber, driverVersion: driverVersion} | |
| } | |
| // Cache writes to fs about the information that a given GPU driver has been installed. | |
| func (c *Cacher) Cache(kernelOpen bool) error { | |
| cachePath := filepath.Join(c.gpuInstallDir, CacheFile) | |
| f, err := os.Create(cachePath) | |
| defer f.Close() | |
| if err != nil { | |
| return errors.Wrapf(err, "Failed to create file %s", cachePath) | |
| } | |
| cacheMap := map[string]string{ | |
| buildNumberKey: c.buildNumber, | |
| driverVersionKey: c.driverVersion} | |
| if kernelOpen { | |
| cacheMap[kernelOpenKey] = kernelOpenValue | |
| } | |
| var cache string | |
| for k, v := range cacheMap { | |
| cache = cache + fmt.Sprintf("%s=%s\n", k, v) | |
| } | |
| if _, err = f.WriteString(cache); err != nil { | |
| return errors.Wrapf(err, "Failed to write to file %s", cachePath) | |
| } | |
| log.Info("Updated cached version as") | |
| for key, value := range cacheMap { | |
| log.Infof("%s=%s", key, value) | |
| } | |
| return nil | |
| } | |
| // IsCached returns a bool pair indicating whether a given GPU driver has been | |
| // installed and if the installation contains open source kernel modules | |
| func (c *Cacher) IsCached() (bool, bool, error) { | |
| cacheMap, err := utils.LoadEnvFromFile(c.gpuInstallDir, CacheFile) | |
| if err != nil { | |
| if errors.Is(err, fs.ErrNotExist) { | |
| // This is an expected case, so we should not log it as an error. | |
| log.Infof("Did not find existing cache: %v.", err) | |
| } else { | |
| log.Infof("error: %v", err) | |
| } | |
| return false, false, err | |
| } | |
| log.Infof("%v", cacheMap) | |
| return (c.buildNumber == cacheMap[buildNumberKey] && | |
| c.driverVersion == cacheMap[driverVersionKey]), | |
| cacheMap[kernelOpenKey] == kernelOpenValue, | |
| nil | |
| } | |
| // findPreloadedInstall returns the subdirectory of hostInstallDir holding a | |
| // preloaded installation of the given driver version and kernel module flavor, | |
| // or "" if there is none. | |
| // | |
| // Preloaded installations are complete installation directories, so each one | |
| // carries its own cache file describing what it holds. That file, rather than | |
| // the directory name, decides whether an installation matches: the names are | |
| // chosen by the image preloader and are not a contract. | |
| func findPreloadedInstall(hostInstallDir, buildNumber, driverVersion string, kernelOpen bool) string { | |
| entries, err := os.ReadDir(hostInstallDir) | |
| if err != nil { | |
| return "" | |
| } | |
| for _, entry := range entries { | |
| if !entry.IsDir() { | |
| continue | |
| } | |
| dir := filepath.Join(hostInstallDir, entry.Name()) | |
| isCached, isOpen, err := NewCacher(dir, buildNumber, driverVersion).IsCached() | |
| if err != nil || !isCached || isOpen != kernelOpen { | |
| continue | |
| } | |
| return dir | |
| } | |
| return "" | |
| } | |
| // ActivatePreloadedInstall makes the preloaded installation of driverVersion, | |
| // if the image ships one, visible at the install dir, so that the caller finds | |
| // a cached installation and neither downloads nor builds the driver. | |
| // | |
| // Preloaded installations are stored by the image preloader in subdirectories | |
| // of the install dir, one per driver version and kernel module flavor, since an | |
| // installation directory can hold only one of them. Activating one links its | |
| // payload into the install dir rather than copying it, which keeps every | |
| // installation confined to the subdirectory it was built in: the install dir | |
| // can never present a mix of two versions, and writes reaching it, such as an | |
| // updated cache entry, land in the version they describe. | |
| // | |
| // The links of an earlier activation are always removed first, so that a driver | |
| // installed the regular way is written to the install dir itself instead of | |
| // into the subdirectory of an unrelated version. Whatever else the install dir | |
| // holds is left alone, and no installation is activated if activating it would | |
| // have to replace any of it. | |
| func ActivatePreloadedInstall(hostInstallDir, buildNumber, driverVersion string, kernelOpen bool) error { | |
| if err := DeactivatePreloadedInstall(hostInstallDir); err != nil { | |
| return err | |
| } | |
| if driverVersion == "" { | |
| return nil | |
| } | |
| src := findPreloadedInstall(hostInstallDir, buildNumber, driverVersion, kernelOpen) | |
| if src == "" { | |
| // No preloaded installation matches. This is the common case outside of | |
| // test images, so it is not an error. | |
| return nil | |
| } | |
| // What an installation consists of is its directories, holding the driver | |
| // itself, followed by its cache entry, which describes them. The remaining | |
| // files are the archives it was installed from and its log, neither of which | |
| // the install dir needs. | |
| entries, err := os.ReadDir(src) | |
| if err != nil { | |
| return errors.Wrapf(err, "failed to read the preloaded installation %s", src) | |
| } | |
| var names []string | |
| for _, entry := range entries { | |
| if entry.IsDir() { | |
| names = append(names, entry.Name()) | |
| } | |
| } | |
| names = append(names, CacheFile) | |
| for _, name := range names { | |
| if _, err := os.Lstat(filepath.Join(hostInstallDir, name)); err == nil { | |
| log.Infof("Not activating the preloaded GPU driver installation %s: %s already holds %s", src, hostInstallDir, name) | |
| return nil | |
| } | |
| } | |
| log.Infof("Activating the preloaded GPU driver installation %s", src) | |
| for _, name := range names { | |
| // The link target is relative because the install dir is bind mounted | |
| // under other paths, both on the host and into this container, where an | |
| // absolute target would point outside of the installation. | |
| target := filepath.Join(filepath.Base(src), name) | |
| if err := os.Symlink(target, filepath.Join(hostInstallDir, name)); err != nil { | |
| return errors.Wrapf(err, "failed to link %s of the preloaded installation %s", name, src) | |
| } | |
| } | |
| return nil | |
| } | |
| // DeactivatePreloadedInstall removes from hostInstallDir the links left by an | |
| // earlier activation of a preloaded installation, leaving the preloaded | |
| // installations themselves in place. Only links into hostInstallDir are | |
| // removed; anything else it holds belongs to someone else. | |
| func DeactivatePreloadedInstall(hostInstallDir string) error { | |
| entries, err := os.ReadDir(hostInstallDir) | |
| if os.IsNotExist(err) { | |
| return nil | |
| } | |
| if err != nil { | |
| return errors.Wrapf(err, "failed to read the install dir %s", hostInstallDir) | |
| } | |
| for _, entry := range entries { | |
| if entry.Type()&os.ModeSymlink == 0 { | |
| continue | |
| } | |
| path := filepath.Join(hostInstallDir, entry.Name()) | |
| target, err := os.Readlink(path) | |
| if err != nil { | |
| return errors.Wrapf(err, "failed to read the link %s", path) | |
| } | |
| if filepath.IsAbs(target) || strings.HasPrefix(target, "..") { | |
| continue | |
| } | |
| log.Infof("Removing %s, left by an earlier activated GPU driver installation", path) | |
| if err := os.Remove(path); err != nil { | |
| return errors.Wrapf(err, "failed to remove the link %s", path) | |
| } | |
| } | |
| return nil | |
| } |