-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdline.go
140 lines (130 loc) · 3.75 KB
/
cmdline.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
package cmdline
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"strings"
"unicode"
"github.com/bensallen/rbd/pkg/krbd"
)
// Mount is filesystem mount specification including Ceph RBD Image mapping
// configuration which was generated via parsed data from the kernel cmdline.
type Mount struct {
Image *krbd.Image
MountOpts []string `json:"mntopts"`
Part string
Overlay bool
Path string
FsType string
}
// Leading prefix for cmdline arguments
const prefix = "rbd"
// Parse attempts to find rbd options from input kernel cmdline and return one
// or more Images. Only JSON formats below is implemented.
//
// rbd.<name>... where <name> is an arbitrary string identifer for the mount
// rbd.root.image=test-image1
// rbd.root.image.pool=rbd
// rbd.root.image.mons=192.168.0.1,192.168.0.2,192.168.0.3:6789
// rbd.root.image.user=admin
// rbd.root.image.secret=<key>
//
// Optional
// rbd.root.image.snap=snap1
// rbd.root.image.opts=rw,share
// rbd.root.part=1
// rbd.root.mntopts=defaults
// rbd.root.fstype=ext4
// rbd.root.overlay=false
// rbd.root.path=/newroot
//
// JSON
// rbd={"root": {"image":{"mons": ["192.168.0.1","192.168.0.2","192.168.0.3:6789"], "opts":{"name": "admin", "secret": "AQAvjX9eabfZAhAAj/g5nXSe/uaemYGCu1w53Q=="}, "pool":"rbd", "image":"test-image1"}, "path":"/", "fstype":"ext4"}}
// rbd.root={"image":{"mons": ["192.168.0.1","192.168.0.2","192.168.0.3:6789"], "opts":{"name": "admin", "secret": "AQAvjX9eabfZAhAAj/g5nXSe/uaemYGCu1w53Q=="}, "pool":"rbd", "image":"test-image1"}, "path":"/", "fstype":"ext4"}
func Parse(cmdline string) map[string]*Mount {
log.Printf("Debug: %s", cmdline)
mounts := map[string]*Mount{}
for _, part := range split(cmdline) {
switch {
case strings.HasPrefix(part, prefix+"."):
splitN := strings.IndexRune(part, '=')
if splitN > 0 {
keySplit := strings.Split(part[len(prefix)+1:splitN], ".")
mount := &Mount{}
// Check if a mount is already in the map
if _, ok := mounts[keySplit[0]]; ok {
mount = mounts[keySplit[0]]
}
switch len(keySplit) {
case 1:
// Image label and no attribute as part of key, eg. rbd.root=
// so assume the value is JSON.
if err := json.Unmarshal([]byte(part[splitN+1:]), mount); err != nil {
//log.Printf("Error parsing json: %v", err)
continue
}
case 2:
// Volume label with attribute, eg. rbd.root.image=
// [TODO]
default:
continue
}
mounts[keySplit[0]] = mount
}
case strings.HasPrefix(part, prefix+"="):
// Bare rbd key, assume value is JSON
if err := json.Unmarshal([]byte(part[len(prefix)+1:]), &mounts); err != nil {
log.Printf("Error parsing json: %v", err)
continue
}
default:
continue
}
}
return mounts
}
//Split strings on spaces except when a space is within a quoted, bracketed, or braced string.
//Supports nesting multiple brackets or braces.
func split(s string) []string {
lastRune := map[rune]int{}
f := func(c rune) bool {
switch {
case lastRune[c] > 0:
lastRune[c]--
return false
case unicode.In(c, unicode.Quotation_Mark):
lastRune[c]++
return false
case c == '[':
lastRune[']']++
return false
case c == '{':
lastRune['}']++
return false
case mapGreaterThan(lastRune, 0):
return false
default:
return c == ' '
}
}
return strings.FieldsFunc(s, f)
}
// mapGreaterThan ranges across the provided map[rune]int looking for any values greater than
func mapGreaterThan(runes map[rune]int, g int) bool {
for _, i := range runes {
if i > g {
return true
}
}
return false
}
// Read open and returns all of the contents of the provided file.
func Read(path string) ([]byte, error) {
r, err := os.OpenFile(path, os.O_RDONLY, 0644)
defer r.Close()
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
}