Skip to content

Commit 06e19a7

Browse files
ImSingeerobbat2
authored andcommitted
Support parse raid type for mdstat
Note: rebased on top of master for reformatting Signed-off-by: Singee <[email protected]> Signed-off-by: Robin H. Johnson <[email protected]>
1 parent 61fe412 commit 06e19a7

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

mdstat.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ var (
3434
type MDStat struct {
3535
// Name of the device.
3636
Name string
37+
// raid type of the device.
38+
Type string
3739
// activity-state of the device.
3840
ActivityState string
3941
// Number of active disks.
@@ -97,6 +99,13 @@ func parseMDStat(mdStatData []byte) ([]MDStat, error) {
9799
mdName := deviceFields[0] // mdx
98100
state := deviceFields[2] // active or inactive
99101

102+
mdType := ""
103+
if len(deviceFields) > 3 && strings.HasPrefix(deviceFields[3], "raid") {
104+
mdType = deviceFields[3] // raid1, raid5, etc.
105+
} else if len(deviceFields) > 4 && strings.HasPrefix(deviceFields[4], "raid") {
106+
mdType = deviceFields[4]
107+
}
108+
100109
if len(lines) <= i+3 {
101110
return nil, fmt.Errorf("%w: Too few lines for md device: %q", ErrFileParse, mdName)
102111
}
@@ -153,6 +162,7 @@ func parseMDStat(mdStatData []byte) ([]MDStat, error) {
153162

154163
mdStats = append(mdStats, MDStat{
155164
Name: mdName,
165+
Type: mdType,
156166
ActivityState: state,
157167
DisksActive: active,
158168
DisksFailed: fail,

mdstat_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestFS_MDStat(t *testing.T) {
3030
refs := map[string]MDStat{
3131
"md127": {
3232
Name: "md127",
33+
Type: "raid1",
3334
ActivityState: "active",
3435
DisksActive: 2,
3536
DisksTotal: 2,
@@ -45,6 +46,7 @@ func TestFS_MDStat(t *testing.T) {
4546
Devices: []string{"sdi2", "sdj2"}},
4647
"md0": {
4748
Name: "md0",
49+
Type: "raid1",
4850
ActivityState: "active",
4951
DisksActive: 2,
5052
DisksTotal: 2,
@@ -60,6 +62,7 @@ func TestFS_MDStat(t *testing.T) {
6062
Devices: []string{"sdi1", "sdj1"}},
6163
"md4": {
6264
Name: "md4",
65+
Type: "raid1",
6366
ActivityState: "inactive",
6467
DisksActive: 0,
6568
DisksTotal: 0,
@@ -75,6 +78,7 @@ func TestFS_MDStat(t *testing.T) {
7578
Devices: []string{"sda3", "sdb3"}},
7679
"md6": {
7780
Name: "md6",
81+
Type: "raid1",
7882
ActivityState: "recovering",
7983
DisksActive: 1,
8084
DisksTotal: 2,
@@ -90,6 +94,7 @@ func TestFS_MDStat(t *testing.T) {
9094
Devices: []string{"sdb2", "sdc", "sda2"}},
9195
"md3": {
9296
Name: "md3",
97+
Type: "raid6",
9398
ActivityState: "active",
9499
DisksActive: 8,
95100
DisksTotal: 8,
@@ -105,6 +110,7 @@ func TestFS_MDStat(t *testing.T) {
105110
Devices: []string{"sda1", "sdh1", "sdg1", "sdf1", "sde1", "sdd1", "sdc1", "sdb1", "sdd1", "sdd2"}},
106111
"md8": {
107112
Name: "md8",
113+
Type: "raid1",
108114
ActivityState: "resyncing",
109115
DisksActive: 2,
110116
DisksTotal: 2,
@@ -120,6 +126,7 @@ func TestFS_MDStat(t *testing.T) {
120126
Devices: []string{"sdb1", "sda1", "sdc", "sde"}},
121127
"md7": {
122128
Name: "md7",
129+
Type: "raid6",
123130
ActivityState: "active",
124131
DisksActive: 3,
125132
DisksTotal: 4,
@@ -135,6 +142,7 @@ func TestFS_MDStat(t *testing.T) {
135142
Devices: []string{"sdb1", "sde1", "sdd1", "sdc1"}},
136143
"md9": {
137144
Name: "md9",
145+
Type: "raid1",
138146
ActivityState: "resyncing",
139147
DisksActive: 4,
140148
DisksTotal: 4,
@@ -150,6 +158,7 @@ func TestFS_MDStat(t *testing.T) {
150158
Devices: []string{"sdc2", "sdd2", "sdb2", "sda2", "sde", "sdf", "sdg"}},
151159
"md10": {
152160
Name: "md10",
161+
Type: "raid0",
153162
ActivityState: "active",
154163
DisksActive: 2,
155164
DisksTotal: 2,
@@ -165,6 +174,7 @@ func TestFS_MDStat(t *testing.T) {
165174
Devices: []string{"sda1", "sdb1"}},
166175
"md11": {
167176
Name: "md11",
177+
Type: "raid1",
168178
ActivityState: "resyncing",
169179
DisksActive: 2,
170180
DisksTotal: 2,
@@ -180,6 +190,7 @@ func TestFS_MDStat(t *testing.T) {
180190
Devices: []string{"sdb2", "sdc2", "sdc3", "hda", "ssdc2"}},
181191
"md12": {
182192
Name: "md12",
193+
Type: "raid0",
183194
ActivityState: "active",
184195
DisksActive: 2,
185196
DisksTotal: 2,
@@ -195,6 +206,7 @@ func TestFS_MDStat(t *testing.T) {
195206
Devices: []string{"sdc2", "sdd2"}},
196207
"md120": {
197208
Name: "md120",
209+
Type: "",
198210
ActivityState: "active",
199211
DisksActive: 2,
200212
DisksTotal: 2,
@@ -210,6 +222,7 @@ func TestFS_MDStat(t *testing.T) {
210222
Devices: []string{"sda1", "sdb1"}},
211223
"md126": {
212224
Name: "md126",
225+
Type: "raid0",
213226
ActivityState: "active",
214227
DisksActive: 2,
215228
DisksTotal: 2,
@@ -225,6 +238,7 @@ func TestFS_MDStat(t *testing.T) {
225238
Devices: []string{"sdb", "sdc"}},
226239
"md219": {
227240
Name: "md219",
241+
Type: "",
228242
ActivityState: "inactive",
229243
DisksTotal: 0,
230244
DisksFailed: 0,
@@ -240,6 +254,7 @@ func TestFS_MDStat(t *testing.T) {
240254
Devices: []string{"sdc", "sda"}},
241255
"md00": {
242256
Name: "md00",
257+
Type: "raid0",
243258
ActivityState: "active",
244259
DisksActive: 1,
245260
DisksTotal: 1,
@@ -255,6 +270,7 @@ func TestFS_MDStat(t *testing.T) {
255270
Devices: []string{"xvdb"}},
256271
"md101": {
257272
Name: "md101",
273+
Type: "raid0",
258274
ActivityState: "active",
259275
DisksActive: 3,
260276
DisksTotal: 3,
@@ -270,6 +286,7 @@ func TestFS_MDStat(t *testing.T) {
270286
Devices: []string{"sdb", "sdd", "sdc"}},
271287
"md201": {
272288
Name: "md201",
289+
Type: "raid1",
273290
ActivityState: "checking",
274291
DisksActive: 2,
275292
DisksTotal: 2,

0 commit comments

Comments
 (0)