cos_gpu_installer: install all available modules

nvidia-peermem.ko is provided by Nvidia, but is not being installed in
cos_gpu_installer. Instead of explicitly installing specific modules,
we should install everything that is present.

BUG=b/243437542
TEST=Ran the following command:

  docker run --rm --name="cos-gpu-installer" --privileged \
    --net=host --pid=host --volume /dev:/dev --volume /:/root \
    gcr.io/$PROJECT/cos-gpu-installer:latest install \
    -host-dir=/var/lib/gpu -debug=true -version=470.141.03 \
    -allow-unsigned-driver=true

on cos-beta-101-17162-0-10. Checked that
/var/lib/gpu/drivers/nvidia-peermem.ko exists.

Change-Id: Idc5a276cc03448027b5b4191e36e874c85b8a281
Reviewed-on: https://cos-review.googlesource.com/c/cos/tools/+/36188
Tested-by: Robert Kolchmeyer <rkolchmeyer@google.com>
Reviewed-by: Arnav Kansal <rnv@google.com>
Reviewed-by: He Gao <hegao@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 3d0d3cd..d02b959 100644
--- a/src/cmd/cos_gpu_installer/internal/installer/installer.go
+++ b/src/cmd/cos_gpu_installer/internal/installer/installer.go
@@ -4,6 +4,7 @@
 import (
 	stderrors "errors"
 	"fmt"
+	"io/fs"
 	"io/ioutil"
 	"os"
 	"os/exec"
@@ -187,7 +188,7 @@
 		filepath.Join(nvidiaKernelDir, "precompiled", "nv-linux.o"),
 		filepath.Join(nvidiaKernelDir, "nvidia", "nv-kernel.o_binary"),
 	}
-	args := append([]string{"-T", linkerScript, "-r", "-o", filepath.Join(nvidiaKernelDir, "nvidia.ko")}, nvidiaObjs...)
+	args := append([]string{"-T", linkerScript, "-r", "-o", filepath.Join(nvidiaKernelDir, "precompiled", "nvidia.ko")}, nvidiaObjs...)
 	cmd := exec.Command(linker, args...)
 	log.Infof("Running link command: %v", cmd.Args)
 	if err := utils.RunCommandAndLogOutput(cmd, false); err != nil {
@@ -198,23 +199,30 @@
 		filepath.Join(nvidiaKernelDir, "precompiled", "nv-modeset-linux.o"),
 		filepath.Join(nvidiaKernelDir, "nvidia-modeset", "nv-modeset-kernel.o_binary"),
 	}
-	args = append([]string{"-T", linkerScript, "-r", "-o", filepath.Join(nvidiaKernelDir, "nvidia-modeset.ko")}, modesetObjs...)
+	args = append([]string{"-T", linkerScript, "-r", "-o", filepath.Join(nvidiaKernelDir, "precompiled", "nvidia-modeset.ko")}, modesetObjs...)
 	cmd = exec.Command(linker, args...)
 	log.Infof("Running link command: %v", cmd.Args)
 	if err := utils.RunCommandAndLogOutput(cmd, false); err != nil {
 		return fmt.Errorf("failed to link nvidia-modeset.ko: %v", err)
 	}
-	// nvidia-uvm.ko is pre-linked; move to kernel dir
-	oldPath := filepath.Join(nvidiaKernelDir, "precompiled", "nvidia-uvm.ko")
-	newPath := filepath.Join(nvidiaKernelDir, "nvidia-uvm.ko")
-	if err := unix.Rename(oldPath, newPath); err != nil {
-		return fmt.Errorf("failed to move %q to %q: %v", oldPath, newPath, err)
-	}
-	// nvidia-drm.ko is pre-linked; move to kernel dir
-	oldPath = filepath.Join(nvidiaKernelDir, "precompiled", "nvidia-drm.ko")
-	newPath = filepath.Join(nvidiaKernelDir, "nvidia-drm.ko")
-	if err := unix.Rename(oldPath, newPath); err != nil {
-		return fmt.Errorf("failed to move %q to %q: %v", oldPath, newPath, err)
+	// Move all modules to kernel dir (includes some pre-linked modules, in
+	// addition to the above linked ones)
+	if err := filepath.WalkDir(filepath.Join(nvidiaKernelDir, "precompiled"), func(path string, d fs.DirEntry, err error) error {
+		if err != nil {
+			return err
+		}
+		if d.IsDir() {
+			return nil
+		}
+		if filepath.Ext(path) == ".ko" {
+			newPath := filepath.Join(nvidiaKernelDir, filepath.Base(path))
+			if err := unix.Rename(path, newPath); err != nil {
+				return fmt.Errorf("failed to move %q to %q: %v", path, newPath, err)
+			}
+		}
+		return nil
+	}); err != nil {
+		return fmt.Errorf("failed to copy kernel modules: %v", err)
 	}
 	log.Info("Done linking drivers")
 	return nil