Skip to content

Commit 256c68f

Browse files
committed
added two parameters to the eligible block device check
whether to accept unmounted partitions (default false) and minimum acceptable device size (default 0)
1 parent 17bff4b commit 256c68f

File tree

3 files changed

+117
-12
lines changed

3 files changed

+117
-12
lines changed

pkg/analyze/host_block_devices.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (a *AnalyzeHostBlockDevices) Analyze(getCollectedFileContents func(string)
5151
return &result, nil
5252
}
5353

54-
isMatch, err := compareHostBlockDevicesConditionalToActual(outcome.Fail.When, devices)
54+
isMatch, err := compareHostBlockDevicesConditionalToActual(outcome.Fail.When, hostAnalyzer.MinimumAcceptableSize, hostAnalyzer.IncludeUnmountedPartitions, devices)
5555
if err != nil {
5656
return nil, errors.Wrapf(err, "failed to compare %s", outcome.Fail.When)
5757
}
@@ -72,7 +72,7 @@ func (a *AnalyzeHostBlockDevices) Analyze(getCollectedFileContents func(string)
7272
return &result, nil
7373
}
7474

75-
isMatch, err := compareHostBlockDevicesConditionalToActual(outcome.Warn.When, devices)
75+
isMatch, err := compareHostBlockDevicesConditionalToActual(outcome.Warn.When, hostAnalyzer.MinimumAcceptableSize, hostAnalyzer.IncludeUnmountedPartitions, devices)
7676
if err != nil {
7777
return nil, errors.Wrapf(err, "failed to compare %s", outcome.Warn.When)
7878
}
@@ -93,7 +93,7 @@ func (a *AnalyzeHostBlockDevices) Analyze(getCollectedFileContents func(string)
9393
return &result, nil
9494
}
9595

96-
isMatch, err := compareHostBlockDevicesConditionalToActual(outcome.Pass.When, devices)
96+
isMatch, err := compareHostBlockDevicesConditionalToActual(outcome.Pass.When, hostAnalyzer.MinimumAcceptableSize, hostAnalyzer.IncludeUnmountedPartitions, devices)
9797
if err != nil {
9898
return nil, errors.Wrapf(err, "failed to compare %s", outcome.Pass.When)
9999
}
@@ -113,7 +113,7 @@ func (a *AnalyzeHostBlockDevices) Analyze(getCollectedFileContents func(string)
113113

114114
// <regexp> <op> <count>
115115
// example: sdb > 0
116-
func compareHostBlockDevicesConditionalToActual(conditional string, devices []collect.BlockDeviceInfo) (res bool, err error) {
116+
func compareHostBlockDevicesConditionalToActual(conditional string, minimumAcceptableSize uint64, includeUnmountedPartitions bool, devices []collect.BlockDeviceInfo) (res bool, err error) {
117117
parts := strings.Split(conditional, " ")
118118
if len(parts) != 3 {
119119
return false, fmt.Errorf("Expected exactly 3 parts, got %d", len(parts))
@@ -123,7 +123,7 @@ func compareHostBlockDevicesConditionalToActual(conditional string, devices []co
123123
if err != nil {
124124
return false, errors.Wrapf(err, "failed to compile regex %q", parts[0])
125125
}
126-
count := countEligibleBlockDevices(rx, devices)
126+
count := countEligibleBlockDevices(rx, minimumAcceptableSize, includeUnmountedPartitions, devices)
127127

128128
desiredInt, err := strconv.Atoi(parts[2])
129129
if err != nil {
@@ -146,25 +146,37 @@ func compareHostBlockDevicesConditionalToActual(conditional string, devices []co
146146
return false, fmt.Errorf("Unexpected operator %q", parts[1])
147147
}
148148

149-
func countEligibleBlockDevices(rx *regexp.Regexp, devices []collect.BlockDeviceInfo) int {
149+
func countEligibleBlockDevices(rx *regexp.Regexp, minimumAcceptableSize uint64, includeUnmountedPartitions bool, devices []collect.BlockDeviceInfo) int {
150150
count := 0
151151

152152
for _, device := range devices {
153-
if isEligibleBlockDevice(rx, device, devices) {
153+
if isEligibleBlockDevice(rx, minimumAcceptableSize, includeUnmountedPartitions, device, devices) {
154154
count++
155155
}
156156
}
157157

158158
return count
159159
}
160160

161-
func isEligibleBlockDevice(rx *regexp.Regexp, device collect.BlockDeviceInfo, devices []collect.BlockDeviceInfo) bool {
161+
func isEligibleBlockDevice(rx *regexp.Regexp, minimumAcceptableSize uint64, includeUnmountedPartitions bool, device collect.BlockDeviceInfo, devices []collect.BlockDeviceInfo) bool {
162162
if !rx.MatchString(device.Name) {
163163
return false
164164
}
165165

166-
if device.Type != "disk" {
167-
return false
166+
if includeUnmountedPartitions {
167+
if device.Type != "disk" && device.Type != "part" {
168+
return false
169+
}
170+
} else {
171+
if device.Type != "disk" {
172+
return false
173+
}
174+
}
175+
176+
if minimumAcceptableSize != 0 {
177+
if device.Size < minimumAcceptableSize {
178+
return false
179+
}
168180
}
169181

170182
if device.Mountpoint != "" {

pkg/analyze/host_block_devices_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,97 @@ func TestAnalyzeBlockDevices(t *testing.T) {
149149
Message: "No block device available",
150150
},
151151
},
152+
{
153+
name: ".* > 1, pass with unmounted partition",
154+
devices: []collect.BlockDeviceInfo{
155+
{
156+
Name: "sdb",
157+
KernelName: "sdb",
158+
Type: "disk",
159+
Major: 8,
160+
Serial: "disk1",
161+
},
162+
{
163+
Name: "sdb1",
164+
KernelName: "sdb1",
165+
ParentKernelName: "sdb",
166+
Type: "part",
167+
Major: 8,
168+
Minor: 1,
169+
},
170+
},
171+
hostAnalyzer: &troubleshootv1beta2.BlockDevicesAnalyze{
172+
IncludeUnmountedPartitions: true,
173+
Outcomes: []*troubleshootv1beta2.Outcome{
174+
{
175+
Pass: &troubleshootv1beta2.SingleOutcome{
176+
When: ".* >= 1",
177+
Message: "Block device or partition available",
178+
},
179+
},
180+
},
181+
},
182+
result: &AnalyzeResult{
183+
Title: "Block Devices",
184+
IsPass: true,
185+
Message: "Block device or partition available",
186+
},
187+
},
188+
{
189+
name: ".* = 2, pass with two unmounted partitions/devices of at least 10gb in size",
190+
devices: []collect.BlockDeviceInfo{
191+
{
192+
Name: "sdb",
193+
KernelName: "sdb",
194+
Type: "disk",
195+
Major: 8,
196+
Serial: "disk1",
197+
Size: 1024 * 1024 * 1024 * 128,
198+
},
199+
{
200+
Name: "sdb1",
201+
KernelName: "sdb1",
202+
ParentKernelName: "sdb",
203+
Type: "part",
204+
Major: 8,
205+
Minor: 1,
206+
Size: 1024 * 1024 * 1024 * 128,
207+
},
208+
{
209+
Name: "sdc",
210+
KernelName: "sdc",
211+
Type: "disk",
212+
Major: 8,
213+
Serial: "disk2",
214+
Size: 1024 * 1024 * 1024 * 16,
215+
},
216+
{
217+
Name: "sdd",
218+
KernelName: "sdd",
219+
Type: "disk",
220+
Major: 8,
221+
Serial: "disk3",
222+
Size: 1024 * 1024 * 1024 * 8,
223+
},
224+
},
225+
hostAnalyzer: &troubleshootv1beta2.BlockDevicesAnalyze{
226+
IncludeUnmountedPartitions: true,
227+
MinimumAcceptableSize: 1024 * 1024 * 1024 * 10,
228+
Outcomes: []*troubleshootv1beta2.Outcome{
229+
{
230+
Pass: &troubleshootv1beta2.SingleOutcome{
231+
When: ".* = 2",
232+
Message: "Two block devices or partitions of >10gb size available",
233+
},
234+
},
235+
},
236+
},
237+
result: &AnalyzeResult{
238+
Title: "Block Devices",
239+
IsPass: true,
240+
Message: "Two block devices or partitions of >10gb size available",
241+
},
242+
},
152243
}
153244
for _, test := range tests {
154245
t.Run(test.name, func(t *testing.T) {

pkg/apis/troubleshoot/v1beta2/hostanalyzer_shared.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ type TimeAnalyze struct {
4646
}
4747

4848
type BlockDevicesAnalyze struct {
49-
AnalyzeMeta `json:",inline" yaml:",inline"`
50-
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
49+
AnalyzeMeta `json:",inline" yaml:",inline"`
50+
MinimumAcceptableSize uint64 `json:"minimumAcceptableSize" yaml:"minimumAcceptableSize"`
51+
IncludeUnmountedPartitions bool `json:"includeUnmountedPartitions" yaml:"includeUnmountedPartitions"`
52+
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
5153
}
5254

5355
type TCPConnectAnalyze struct {

0 commit comments

Comments
 (0)