-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblkinfo.go
More file actions
326 lines (262 loc) · 8.19 KB
/
Copy pathblkinfo.go
File metadata and controls
326 lines (262 loc) · 8.19 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Package blkinfo implements methods for block device information.
package blkinfo
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// BlkInfo shows block device information.
type BlkInfo struct {
Path string `json:"path" `
ResolvedPath string `json:"resolved_path" `
ParentPath string `json:"parent_path" `
ChildPaths []string `json:"child_paths" `
SysPath string `json:"sys_path" `
ResolevedSysPath string `json:"resolved_sys_path"`
Sys *Sys `json:"sys" `
MajorMinor string `json:"major_minor" `
UdevDataPath string `json:"udev_data_path" `
UdevData []string `json:"udev_data" `
MountInfoPath string `json:"mount_info_path" `
MountInfo *MountInfo `json:"mount_info" `
OSReleasePath string `json:"os_release_path" `
OSRelease map[string]string `json:"os_release" `
}
// Sys shows sys information.
type Sys struct {
// See https://github.com/torvalds/linux/blob/d13937116f1e82bf508a6325111b322c30c85eb9/fs/block_dev.c#L1229-L1242
// /sys/block/dm-0/slaves/sda --> /sys/block/sda
// /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
Uevent []string `json:"uevent" `
Slaves []string `json:"slaves" `
Holders []string `json:"holders"`
}
// MountInfo shows mount information.
type MountInfo struct {
// See https://github.com/torvalds/linux/blob/d8372ba8ce288acdfce67cb873b2a741785c2e88/Documentation/filesystems/proc.txt#L1711
MountID string `json:"mount_id" `
ParentID string `json:"parent_id" `
MajorMinor string `json:"major_minor" `
Root string `json:"root" `
MountPoint string `json:"mount_point" `
MountOptions []string `json:"mount_options" `
OptionalFields []string `json:"optional_fields"`
FilesystemType string `json:"filesystem_type"`
MountSource string `json:"mount_source" `
SuperOptions []string `json:"super_options" `
}
// New initializes *BlkInfo.
func New(path string) (*BlkInfo, error) { // nolint: funlen
var err error
if path == "" {
return nil, fmt.Errorf("a path is not given")
}
bi := &BlkInfo{
Sys: &Sys{},
}
bi.Path = path
bi.ResolvedPath, err = filepath.EvalSymlinks(bi.Path)
if err != nil {
return nil, err
}
bi.SysPath, bi.ResolevedSysPath, bi.ParentPath, bi.ChildPaths, err = relatedPaths(bi.ResolvedPath)
if err != nil {
return nil, err
}
bi.Sys.Uevent, err = lines(filepath.Join(bi.SysPath, "uevent"))
if err != nil {
return nil, err
}
bi.Sys.Slaves, err = ls(filepath.Join(bi.SysPath, "slaves"))
if err != nil {
return nil, err
}
bi.Sys.Holders, err = ls(filepath.Join(bi.SysPath, "holders"))
if err != nil {
return nil, err
}
bi.MajorMinor, err = majorMinor(bi.SysPath)
if err != nil {
return nil, err
}
bi.UdevDataPath = filepath.Join("/", "run", "udev", "data", "b"+bi.MajorMinor)
bi.UdevData, err = lines(bi.UdevDataPath)
if err != nil {
return nil, err
}
bi.MountInfoPath = filepath.Join("/", "proc", "self", "mountinfo")
bi.MountInfo, err = newMountInfo(bi.MountInfoPath, bi.ResolvedPath)
if err != nil {
return nil, err
}
bi.OSReleasePath = osReleasePath(bi.MountInfo.MountPoint)
bi.OSRelease, err = newOSRelease(bi.OSReleasePath)
if err != nil {
return nil, err
}
return bi, nil
}
func readFile(path string) (string, error) {
b, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
return "", err
}
return strings.TrimSpace(string(b)), nil
}
func lines(path string) ([]string, error) {
text, err := readFile(path)
if err != nil {
return []string{}, err
}
return strings.Split(text, "\n"), nil
}
func trimQuotationMarks(s string) string {
for _, q := range []string{`"`, `'`} {
if strings.HasPrefix(s, q) && strings.HasSuffix(s, q) {
s = strings.TrimPrefix(s, q)
s = strings.TrimSuffix(s, q)
break
}
}
return s
}
func ls(path string) ([]string, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return []string{}, nil
}
fileInfoList, err := ioutil.ReadDir(path)
if err != nil {
return []string{}, err
}
names := []string{}
for _, fileInfo := range fileInfoList {
names = append(names, fileInfo.Name())
}
return names, nil
}
func newMountInfo(mountInfoPath string, path string) (*MountInfo, error) {
resolvedPath, err := filepath.EvalSymlinks(path)
if err != nil {
return nil, err
}
lines, err := readFile(mountInfoPath)
if err != nil {
return nil, err
}
mountInfo := &MountInfo{
MountOptions: []string{},
OptionalFields: []string{},
SuperOptions: []string{},
}
for _, line := range strings.Split(lines, "\n") {
separated := strings.SplitN(line, " - ", 2)
separatedFirst := strings.Fields(separated[0])
separatedLast := strings.Fields(separated[1])
mountSource := separatedLast[1]
if !strings.HasPrefix(mountSource, "/dev") {
continue
}
realMountSource, err := filepath.EvalSymlinks(mountSource)
if err != nil {
return nil, err
}
if resolvedPath == realMountSource {
mountInfo.MountID = separatedFirst[0]
mountInfo.ParentID = separatedFirst[1]
mountInfo.MajorMinor = separatedFirst[2]
mountInfo.Root = separatedFirst[3]
mountInfo.MountPoint = separatedFirst[4]
mountInfo.MountOptions = strings.Split(separatedFirst[5], ",")
mountInfo.OptionalFields = separatedFirst[6:]
mountInfo.FilesystemType = separatedLast[0]
mountInfo.MountSource = separatedLast[1]
mountInfo.SuperOptions = strings.Split(separatedLast[2], ",")
return mountInfo, nil
}
}
return mountInfo, nil
}
func relatedPaths(path string) (sysPath string, resolvedSysPath string, parentPath string, childPaths []string, err error) {
resolvedPath, err := filepath.EvalSymlinks(path)
if err != nil {
return "", "", "", []string{}, err
}
devName := filepath.Base(resolvedPath)
blockPath := filepath.Join("/", "sys", "block")
fileInfoList, err := ioutil.ReadDir(blockPath)
if err != nil {
return "", "", "", []string{}, err
}
for _, fileInfo := range fileInfoList {
fileInfoName := fileInfo.Name()
if strings.HasPrefix(devName, fileInfoName) {
switch devName {
case fileInfoName:
// for example /sys/block/sda
sysPath = filepath.Join(blockPath, fileInfoName)
fileInfoList, err = ioutil.ReadDir(sysPath)
if err != nil {
return "", "", "", []string{}, err
}
childPaths = []string{}
for _, fileInfo := range fileInfoList {
fileInfoName = fileInfo.Name()
if strings.HasPrefix(fileInfoName, devName) {
childPaths = append(childPaths, filepath.Join("/", "dev", fileInfoName))
}
}
default:
// for example /sys/block/sda/sda1
sysPath = filepath.Join(blockPath, fileInfoName, devName)
parentPath = filepath.Join("/", "dev", fileInfoName)
childPaths = []string{}
}
resolvedSysPath, err := filepath.EvalSymlinks(sysPath)
if err != nil {
return "", "", "", []string{}, err
}
return sysPath, resolvedSysPath, parentPath, childPaths, nil
}
}
return "", "", "", []string{}, fmt.Errorf("sysPath, parentPath, and childPaths are not found")
}
func majorMinor(sysPath string) (string, error) {
majorMinor, err := readFile(filepath.Join(sysPath, "dev"))
if err != nil {
return "", err
}
return majorMinor, nil
}
func osReleasePath(mountPoint string) (path string) {
if mountPoint != "" {
path = filepath.Join(mountPoint, "etc", "os-release")
}
return path
}
func newOSRelease(osReleasePath string) (osRelease map[string]string, err error) {
osRelease = map[string]string{}
if osReleasePath != "" {
osReleaseLines, err := lines(osReleasePath)
if err != nil {
return map[string]string{}, err
}
for _, osReleaseLine := range osReleaseLines {
line := strings.TrimSpace(osReleaseLine)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
kv := strings.SplitN(osReleaseLine, "=", 2)
expectedKVSize := 2
if len(kv) != expectedKVSize {
return map[string]string{}, fmt.Errorf(`unexpected osReleaseLine, "%s"`, osReleaseLine)
}
key := kv[0]
value := kv[1]
osRelease[key] = trimQuotationMarks(value)
}
}
return osRelease, nil
}