-
Notifications
You must be signed in to change notification settings - Fork 1
/
blkid.go
151 lines (138 loc) · 4.07 KB
/
blkid.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
// Package blkid extracts useful info from that command on linux
//
// Csv output is particularly supported, so that a csvfile-based enterprise's ETL tools can also monitor its servers and desktops
package blkid
import (
"fmt"
"github.com/LDCS/genutil"
"sort"
"strings"
)
// Blkiddata is a type
type Blkiddata struct {
Devname_ string
Uuid_ string
Uuidsub_ string
Type_ string
Label_ string
Parttype_ string
Partuuid_ string
Partlabel_ string
}
const (
names = "Devname,Uuid,Uuidsub,Type,Label,Parttype,Partuuid,Partlabel"
hdrprefix = ",bi."
semi = ";"
)
var (
headerString string
commaString string
pctString string
namePctString string
)
func init() {
headerString = (hdrprefix + strings.Join(strings.Split(names, ","), hdrprefix))[1:]
commaString = strings.Repeat(",", strings.Count(headerString, ","))
pctString = strings.Repeat(",%s", 1+strings.Count(headerString, ","))[1:]
namePctString = strings.Replace(names, ",", "=%s ", -1) + "=%s\n"
}
// SortedKeys_String2PtrBlkiddata is generic
func SortedKeys_String2PtrBlkiddata(_mp *map[string]*Blkiddata) []string {
keys := make([]string, len(*_mp))
ii := 0
for kk := range *_mp {
keys[ii] = kk
ii++
}
sort.Strings(keys)
return keys
}
// Keys_String2PtrBlkiddata is generic
func Keys_String2PtrBlkiddata(_mp *map[string]*Blkiddata) []string {
keys := make([]string, len(*_mp))
ii := 0
for kk := range *_mp {
keys[ii] = kk
ii++
}
return keys
}
// Header is generic
func Header() string { return headerString }
// Csv is generic
func (self *Blkiddata) Csv() string {
if self == nil {
return commaString
}
return fmt.Sprintf(pctString, self.Devname_, self.Uuid_, self.Uuidsub_, self.Type_, self.Label_, self.Parttype_, self.Partuuid_, self.Partlabel_)
}
// Sprint is generic
func (self *Blkiddata) Sprint() string {
if self == nil {
return ""
}
return fmt.Sprintf(namePctString, self.Devname_, self.Uuid_, self.Uuidsub_, self.Type_, self.Label_, self.Parttype_, self.Partuuid_, self.Partlabel_)
}
// Print is generic
func (self *Blkiddata) Print() {
if self == nil {
return
}
fmt.Printf(fmt.Sprint())
}
// New is generic
func New() *Blkiddata { return new(Blkiddata) }
// Blkid obtains information from blkid
func Blkid(_verbose bool) (smap map[string]*Blkiddata) {
smap = make(map[string]*Blkiddata)
out := genutil.BashExecOrDie(_verbose, "/usr/bin/timeout 10 /sbin/blkid | sed -e 's/\" /\"|/g' | sed -s 's/ /|/' | sed -e 's/ /_/g' | sed -e 's/|/ /g'", ".") // turn sep spaces into pipe, then embedded spaces into underscore, then revert sep pipe to sep space
if _verbose {
fmt.Println(out)
}
lines := genutil.CleanAndSplitOnSpaces(out, ",")
var lastblkidd *Blkiddata
for ii, line := range lines {
items := strings.Split(line, ",")
if len(items) < 1 {
continue
}
switch {
case len(items) > 1:
lastblkidd = new(Blkiddata)
lastblkidd.Devname_ = genutil.ChompStr(items[0], ":")
for _, item := range items[1:] {
kk, vv := genutil.EqualsSplit2Trimmed(item)
switch kk {
case "UUID":
lastblkidd.Uuid_ = genutil.ChompQuotes(vv, true)
case "UUID_SUB":
lastblkidd.Uuidsub_ = genutil.ChompQuotes(vv, true)
case "TYPE":
lastblkidd.Type_ = genutil.ChompQuotes(vv, true)
case "SEC_TYPE": // secondary type is not interesting
case "LABEL":
lastblkidd.Label_ = genutil.ChompQuotes(vv, true)
case "PTTYPE":
lastblkidd.Parttype_ = genutil.ChompQuotes(vv, true)
case "PARTUUID":
lastblkidd.Partuuid_ = genutil.ChompQuotes(vv, true)
case "PARTLABEL":
lastblkidd.Partlabel_ = genutil.ChompQuotes(vv, true)
default:
fmt.Printf("line%d: unknown key: kk=%s vv=%s\n", kk, vv)
}
}
if len(lastblkidd.Devname_) < 1 {
fmt.Printf("Blkid: Skipping empty Devname: line%d: %s\n", ii, strings.Join(items, "#"))
continue
}
smap[lastblkidd.Devname_] = lastblkidd
if _verbose {
fmt.Printf("line%d: item0(%s) %s\n", ii, items[0], strings.Join(items, "#"))
}
default:
fmt.Printf("line%d: lenitems=%d item0(%s) %s\n", ii, len(items), items[0], strings.Join(items, "#"))
}
}
return smap
}