cos_gpu_installer: pass driver version to firmware installation

Nvidia module automatically loads firmware from a very specific path
inside the firmware search paths.

Specifically it will query the paths + "nvidia/<driver-version>/" dir.

For the driver installation that takes the route of
--nvidia-installer-url flag the driver-version flag is missing.

This becomes an issue because now the firmware is installed in the dir
"nvidia/" instead of the "nvidia/<driver-version>" directory.

The .manifest file of all installers contain the driver version as part
of the identification text. This change tries to find a driver version
on the manifest file and use that if the driver version is not supplied.

TEST plan:
Ran gpu_e2e_test with this installer to check -nvidia-installer-url path
and also ran gpu driver installation on cos-lts builds to check
-driver-version path.

Change-Id: I092dcef119fcbc291452a8e74d4b42e6348f4153
Reviewed-on: https://cos-review.googlesource.com/c/cos/tools/+/41927
Reviewed-by: Meena Shanmugam <meenashanmugam@google.com>
Tested-by: Arnav Kansal <rnv@google.com>
Cloud-Build: GCB Service account <228075978874@cloudbuild.gserviceaccount.com>
diff --git a/src/cmd/cos_gpu_installer/internal/installer/installer.go b/src/cmd/cos_gpu_installer/internal/installer/installer.go
index a7eb12c..d55ac6c 100644
--- a/src/cmd/cos_gpu_installer/internal/installer/installer.go
+++ b/src/cmd/cos_gpu_installer/internal/installer/installer.go
@@ -397,6 +397,15 @@
 		if err := installUserLibs(extractDir); err != nil {
 			return fmt.Errorf("failed to install userspace libraries: %v", err)
 		}
+
+		// Driver version may be empty if custom nvidia-installer-url is used
+		// read from manifest file
+		if driverVersion == "" {
+
+			driverVersion = findDriverVersionManifestFile(filepath.Join(extractDir, ".manifest"))
+			log.Info("found driver version from nvidia-installer pkg ", driverVersion)
+		}
+
 		if err := prepareGSPFirmware(extractDir, driverVersion, needSigned); err != nil {
 			return fmt.Errorf("failed to prepare GSP firmware, err: %v", err)
 		}
@@ -612,6 +621,21 @@
 	return nil
 }
 
+// tries to read .manifest file to find driverVersion present in the manifest
+func findDriverVersionManifestFile(manifestFilePath string) string {
+	manifestFileRawBytes, err := os.ReadFile(manifestFilePath)
+	if err != nil {
+		return ""
+	}
+	lines := strings.Split(string(manifestFileRawBytes), "\n")
+	if len(lines) < 2 {
+		return ""
+	}
+	// driver version present in the second line of the file
+	driverVersion := strings.TrimSpace(lines[1])
+	return driverVersion
+}
+
 func RunDriverInstallerPrebuiltModules(downloader *cos.GCSDownloader, installerFilename, driverVersion string) error {
 	// fetch the prebuilt modules
 	if err := downloader.DownloadArtifact(gpuInstallDirContainer, fmt.Sprintf(prebuiltModuleTemplate, driverVersion)); err != nil {