-
Notifications
You must be signed in to change notification settings - Fork 43
/
modprobe_stage.go
165 lines (139 loc) · 4.43 KB
/
modprobe_stage.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package osbuild
import (
"encoding/json"
"fmt"
"regexp"
)
const modprobeCfgFilenameRegex = "^[\\w.-]{1,250}\\.conf$"
type ModprobeStageOptions struct {
Filename string `json:"filename"`
Commands ModprobeConfigCmdList `json:"commands"`
}
func (ModprobeStageOptions) isStageOptions() {}
func (o ModprobeStageOptions) validate() error {
if len(o.Commands) == 0 {
return fmt.Errorf("at least one command is required")
}
nameRegex := regexp.MustCompile(modprobeCfgFilenameRegex)
if !nameRegex.MatchString(o.Filename) {
return fmt.Errorf("modprobe configuration filename %q doesn't conform to schema (%s)", o.Filename, nameRegex.String())
}
return nil
}
func NewModprobeStage(options *ModprobeStageOptions) *Stage {
if err := options.validate(); err != nil {
panic(err)
}
return &Stage{
Type: "org.osbuild.modprobe",
Options: options,
}
}
type ModprobeConfigCmd interface {
isModprobeConfigCmd()
}
// ModprobeConfigCmdList represents a modprobe configuration file, which contains
// a list of commands.
type ModprobeConfigCmdList []ModprobeConfigCmd
func (configFile *ModprobeConfigCmdList) UnmarshalJSON(data []byte) error {
var rawConfigFile []interface{}
if err := json.Unmarshal(data, &rawConfigFile); err != nil {
return err
}
for _, rawConfigCmd := range rawConfigFile {
var modprobeCmd ModprobeConfigCmd
// The command object structure depends on the value of "command"
// item, therefore make no assumptions on the structure.
configCmdMap, ok := rawConfigCmd.(map[string]interface{})
if !ok {
return fmt.Errorf("unexpected modprobe configuration file format")
}
command, ok := configCmdMap["command"].(string)
if !ok {
return fmt.Errorf("'command' item should be string, not %T", configCmdMap["command"])
}
switch command {
case "blacklist":
modulename, ok := configCmdMap["modulename"].(string)
if !ok {
return fmt.Errorf("'modulename' item should be string, not %T", configCmdMap["modulename"])
}
modprobeCmd = NewModprobeConfigCmdBlacklist(modulename)
case "install":
modulename, ok := configCmdMap["modulename"].(string)
if !ok {
return fmt.Errorf("'modulename' item should be string, not %T", configCmdMap["modulename"])
}
cmdline, ok := configCmdMap["cmdline"].(string)
if !ok {
return fmt.Errorf("'cmdline' item should be string, not %T", configCmdMap["cmdline"])
}
modprobeCmd = NewModprobeConfigCmdInstall(modulename, cmdline)
default:
return fmt.Errorf("unexpected modprobe command: %s", command)
}
*configFile = append(*configFile, modprobeCmd)
}
return nil
}
// ModprobeConfigCmdBlacklist represents the 'blacklist' command in the
// modprobe configuration.
type ModprobeConfigCmdBlacklist struct {
Command string `json:"command"`
Modulename string `json:"modulename"`
}
func (ModprobeConfigCmdBlacklist) isModprobeConfigCmd() {}
func (c ModprobeConfigCmdBlacklist) validate() error {
if c.Command != "blacklist" {
return fmt.Errorf("'command' must have 'blacklist' value set")
}
if c.Modulename == "" {
return fmt.Errorf("'modulename' must not be empty")
}
return nil
}
// NewModprobeConfigCmdBlacklist creates a new instance of ModprobeConfigCmdBlacklist
// for the provided modulename.
func NewModprobeConfigCmdBlacklist(modulename string) *ModprobeConfigCmdBlacklist {
cmd := &ModprobeConfigCmdBlacklist{
Command: "blacklist",
Modulename: modulename,
}
if err := cmd.validate(); err != nil {
panic(err)
}
return cmd
}
// ModprobeConfigCmdInstall represents the 'install' command in the
// modprobe configuration.
type ModprobeConfigCmdInstall struct {
Command string `json:"command"`
Modulename string `json:"modulename"`
Cmdline string `json:"cmdline"`
}
func (ModprobeConfigCmdInstall) isModprobeConfigCmd() {}
func (c ModprobeConfigCmdInstall) validate() error {
if c.Command != "install" {
return fmt.Errorf("'command' must have 'install' value set")
}
if c.Modulename == "" {
return fmt.Errorf("'modulename' must not be empty")
}
if c.Cmdline == "" {
return fmt.Errorf("'cmdline' must not be empty")
}
return nil
}
// NewModprobeConfigCmdInstall creates a new instance of ModprobeConfigCmdInstall
// for the provided modulename.
func NewModprobeConfigCmdInstall(modulename, cmdline string) *ModprobeConfigCmdInstall {
cmd := &ModprobeConfigCmdInstall{
Command: "install",
Modulename: modulename,
Cmdline: cmdline,
}
if err := cmd.validate(); err != nil {
panic(err)
}
return cmd
}