-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapviz.go
197 lines (175 loc) · 4.55 KB
/
mapviz.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
package main
import (
"bufio"
"fmt"
"os"
"os/user"
"path"
"strings"
"github.com/lxc/lxd/shared/idmap"
"github.com/olekukonko/tablewriter"
)
func help(status int) {
me := path.Base(os.Args[0])
fmt.Printf("Usage:\n%s\n", me)
fmt.Printf(" show default id mapping\n")
fmt.Printf("%s cfile\n", me)
fmt.Printf(" show id mappings for containers in cfile\n")
os.Exit(status)
}
func isHelp(s string) bool {
switch s {
case "-h":
return true
case "--help":
return true
case "help":
return true
default:
return false
}
}
func showDefaultMap() {
u, err := user.Current()
if err != nil {
fmt.Printf("Error resolving current user")
help(1)
}
set, err := idmap.DefaultIdmapSet("", u.Username)
if err != nil {
fmt.Printf("Error reading default mapset: %q\n", err)
help(1)
}
fmt.Printf("Your current default allocation is:\n\n")
for _, m := range set.Idmap {
t := "uid"
if !m.Isuid {
t = "gid"
}
hmin := m.Hostid
hmax := m.Hostid + m.Maprange - 1
cmin := m.Nsid
cmax := m.Nsid + m.Maprange - 1
fmt.Printf("host %s %d - %d mapping to %d - %d in container\n",
t, hmin, hmax, cmin, cmax)
}
}
func main() {
if len(os.Args) > 2 {
fmt.Printf("Too many arguments")
help(1)
}
if len(os.Args) == 2 && isHelp(os.Args[1]) {
help(0)
}
if len(os.Args) == 1 {
showDefaultMap()
return
}
// to do - parse file. Let's just use bogus input for now
containers, err := ParseFile(os.Args[1])
if err != nil {
fmt.Printf("Error opening file %s: %q\n", os.Args[1], err)
os.Exit(1)
}
data, err := Process(containers)
if err != nil {
fmt.Printf("Error processing file %s: %q\n", os.Args[1], err)
os.Exit(1)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"Container",
"parent start",
"parent end",
"container start",
"container end",
"host start",
"host end"})
table.AppendBulk(data)
table.Render()
return
}
type container struct {
idmap *idmap.IdmapSet
// for nested containers, the true host min/max
hostmin int64
hostmax int64
}
type containers map[string]container
func ParseFile(fName string) (containers, error) {
set := containers{}
file, err := os.Open(fName)
if err != nil {
return set, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// c1/c2 0:100000:65536
s := strings.Fields(scanner.Text())
if len(s) > 2 {
return set, fmt.Errorf("Too many fields")
}
mapstr := fmt.Sprintf("b:%s", s[1])
m, err := idmap.IdmapSet{}.Append(mapstr)
if err != nil {
return set, err
}
name := s[0]
hostmin, hostmax, err := verifyRange(name, m.Idmap[0], set)
if err != nil {
return set, err
}
c := container{idmap: &m, hostmin: hostmin, hostmax: hostmax}
set[name] = c
}
return set, nil
}
func Process(containers containers) ([][]string, error) {
result := [][]string{}
for name, c := range containers {
// note - we only do cases where uid+gid are the same, so just
// take the first idmap
fmt.Printf("Looking at %s\n", name)
idMap := c.idmap
r := idMap.Idmap[0].Maprange
pstart := fmt.Sprintf("%d", idMap.Idmap[0].Hostid)
pend := fmt.Sprintf("%d", idMap.Idmap[0].Hostid+r)
cstart := fmt.Sprintf("%d", idMap.Idmap[0].Nsid)
cend := fmt.Sprintf("%d", idMap.Idmap[0].Nsid+r)
hstart := fmt.Sprintf("%d", c.hostmin)
hend := fmt.Sprintf("%d", c.hostmax)
newstr := []string{name, pstart, pend, cstart, cend, hstart, hend}
result = append(result, newstr)
}
return result, nil
}
func verifyRange(name string, idMap idmap.IdmapEntry, c containers) (int64, int64, error) {
lineage := strings.Split(name, "/")
if len(lineage) == 1 {
return idMap.Hostid, idMap.Hostid + idMap.Maprange, nil
}
last := len(lineage) - 1
pname := strings.Join(lineage[0:last], "/")
parent, ok := c[pname]
if !ok || parent.hostmin == -1 {
return 0, 0, fmt.Errorf("Parent for %s (%s) is undefined", name, pname)
}
pidmap := parent.idmap.Idmap[0]
if idMap.Nsid+idMap.Maprange >= pidmap.Nsid+pidmap.Maprange || idMap.Hostid < pidmap.Nsid {
return 0, 0, fmt.Errorf("Mapping for %s exceeds its parent's, parentids should be between %d - %d",
name, pidmap.Nsid, pidmap.Nsid+pidmap.Maprange-1)
}
// make an idmap shifting the parent's mapping straight onto the host
absstr := fmt.Sprintf("b:%d:%d:%d", pidmap.Nsid, parent.hostmin, pidmap.Maprange)
m := idmap.IdmapSet{}
m, err := m.Append(absstr)
if err != nil {
return 0, 0, err
}
// map the desired 'hostid' (which is really the parent-ns-id) onto the host
hoststart, _ := m.ShiftIntoNs(idMap.Hostid, idMap.Hostid)
hostend := hoststart + idMap.Maprange
return hoststart, hostend, nil
}