-
Notifications
You must be signed in to change notification settings - Fork 239
/
installdrivers.go
99 lines (83 loc) · 2.49 KB
/
installdrivers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// +build linux
package main
import (
"context"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/Microsoft/hcsshim/internal/guest/storage/overlay"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
lcowGlobalDriversPrefix = "/run/drivers/%s"
moduleExtension = ".ko"
)
func install(ctx context.Context) error {
args := []string(os.Args[1:])
if len(args) == 0 {
return errors.New("no driver paths provided for install")
}
for _, driver := range args {
modules := []string{}
driverGUID, err := uuid.NewRandom()
if err != nil {
return err
}
// create an overlay mount from the driver's UVM path so we can write to the
// mount path in the UVM despite having mounted in the driver originally as
// readonly
runDriverPath := fmt.Sprintf(lcowGlobalDriversPrefix, driverGUID.String())
upperPath := filepath.Join(runDriverPath, "upper")
workPath := filepath.Join(runDriverPath, "work")
rootPath := filepath.Join(runDriverPath, "content")
if err := overlay.Mount(ctx, []string{driver}, upperPath, workPath, rootPath, false); err != nil {
return err
}
// find all module files, which end with ".ko" extension, and remove extension
// for use when calling `modprobe` below.
if walkErr := filepath.WalkDir(rootPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return errors.Wrap(err, "failed to read directory while walking dir")
}
if !d.IsDir() && filepath.Ext(d.Name()) == moduleExtension {
moduleName := strings.TrimSuffix(d.Name(), moduleExtension)
modules = append(modules, moduleName)
fmt.Fprintln(os.Stderr, moduleName)
}
return nil
}); walkErr != nil {
return walkErr
}
// create a new module dependency map database for the driver
depmodArgs := []string{"-b", rootPath}
cmd := exec.Command("depmod", depmodArgs...)
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "failed to run cmd with message: %s", out)
}
// run modprobe for every module name found
modprobeArgs := append([]string{"-d", rootPath, "-a"}, modules...)
cmd = exec.Command(
"modprobe",
modprobeArgs...,
)
out, err = cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "failed to run cmd with message: %s", out)
}
}
return nil
}
func installDriversMain() {
ctx := context.Background()
if err := install(ctx); err != nil {
logrus.Errorf("error in install drivers: %s", err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}