forked from canonical/lxd
/
config.go
222 lines (184 loc) · 3.88 KB
/
config.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/lxc/lxd/shared"
)
var checkedKeys = []string{
"lxc.aa_allow_incomplete",
"lxc.aa_profile",
"lxc.apparmor.allow_incomplete",
"lxc.apparmor.profile",
"lxc.arch",
"lxc.autodev",
"lxc.cap.drop",
"lxc.environment",
"lxc.haltsignal",
"lxc.id_map",
"lxc.idmap",
"lxc.include",
"lxc.loglevel",
"lxc.mount",
"lxc.mount.auto",
"lxc.mount.entry",
"lxc.pts",
"lxc.pty.max",
"lxc.rebootsignal",
"lxc.rootfs",
"lxc.rootfs.backend",
"lxc.rootfs.mount",
"lxc.rootfs.path",
"lxc.seccomp",
"lxc.signal.halt",
"lxc.signal.reboot",
"lxc.signal.stop",
"lxc.start.auto",
"lxc.start.delay",
"lxc.start.order",
"lxc.stopsignal",
"lxc.tty",
"lxc.tty.max",
"lxc.uts.name",
"lxc.utsname",
"lxd.migrated",
}
func getUnsupportedKeys(config []string) []string {
var out []string
for _, a := range config {
supported := false
for _, b := range checkedKeys {
if a == b {
supported = true
break
}
}
if !supported {
out = append(out, a)
}
}
return out
}
func getConfig(config []string, key string) []string {
// Return an array since keys can be specified more than once
var out []string
for _, c := range config {
text := strings.TrimSpace(c)
// Ignore empty lines and comments
if len(text) == 0 || strings.HasPrefix(text, "#") {
continue
}
line := strings.Split(text, "=")
if len(line) != 2 {
continue
}
k := strings.TrimSpace(line[0])
v := strings.Trim(strings.TrimSpace(line[1]), "\"")
if k == key && len(v) > 0 {
out = append(out, v)
}
}
if len(out) == 0 {
return nil
}
return out
}
func getConfigKeys(config []string) []string {
// Make sure we don't have duplicate keys
m := make(map[string]bool, 0)
for _, c := range config {
text := strings.TrimSpace(c)
// Ignore empty lines and comments
if len(text) == 0 || strings.HasPrefix(text, "#") {
continue
}
line := strings.Split(text, "=")
key := strings.TrimSpace(line[0])
if strings.HasPrefix(key, "lxc.") {
m[key] = true
}
}
var out []string
for k := range m {
out = append(out, k)
}
return out
}
func parseConfig(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var config []string
// Parse config
sc := bufio.NewScanner(file)
for sc.Scan() {
text := strings.TrimSpace(sc.Text())
// Ignore empty lines and comments
if len(text) == 0 || strings.HasPrefix(text, "#") {
continue
}
line := strings.Split(text, "=")
if len(line) != 2 {
continue
}
key := strings.TrimSpace(line[0])
value := strings.TrimSpace(line[1])
switch key {
// Parse user-added includes
case "lxc.include":
// Ignore our own default configs
if strings.HasPrefix(value, "/usr/share/lxc/config/") {
continue
}
if shared.PathExists(value) {
if shared.IsDir(value) {
files, err := ioutil.ReadDir(value)
if err != nil {
return nil, err
}
for _, file := range files {
path := filepath.Join(value, file.Name())
if !strings.HasSuffix(path, ".conf") {
continue
}
config = append(config, path)
}
} else {
c, err := parseConfig(value)
if err != nil {
return nil, err
}
config = append(config, c...)
}
continue
}
// Expand any fstab
case "lxc.mount":
if !shared.PathExists(value) {
fmt.Println("Container fstab file doesn't exist, skipping...")
continue
}
file, err := os.Open(value)
if err != nil {
return nil, err
}
defer file.Close()
sc := bufio.NewScanner(file)
for sc.Scan() {
text := strings.TrimSpace(sc.Text())
if len(text) > 0 && !strings.HasPrefix(text, "#") {
config = append(config, fmt.Sprintf("lxc.mount.entry = %s", text))
}
}
continue
default:
config = append(config, text)
}
}
return config, nil
}