@@ -18,7 +18,6 @@ import (
18
18
"os/exec"
19
19
"path/filepath"
20
20
"regexp"
21
- "strconv"
22
21
"strings"
23
22
"time"
24
23
@@ -83,27 +82,46 @@ func deviceIDFromFileInfo(finfo fs.FileInfo, path string) DeviceID {
83
82
// Per /usr/include/linux/major.h and Documentation/admin-guide/devices.rst:
84
83
switch major {
85
84
case 0 : // UNNAMED_MAJOR
85
+
86
86
// Perform additional lookups for unknown device types
87
87
var statfs sysutil.StatfsT
88
- if err := sysutil .Statfs (path , & statfs ); err != nil {
89
- log .Warningf (ctx , "unable statfs(2) path %q: %v" , path , err )
90
- } else {
91
- fsType := statfs .Type
92
- switch strconv .FormatInt (fsType , 16 ) {
93
- case "2fc12fc1" : // ZFS_SUPER_MAGIC from include/sys/fs/zfs.h
94
- if major , minor , err = deviceIDForZFS (path ); err != nil {
95
- log .Warningf (ctx , "unable to find device ID for %q: %v" , path , err )
96
- }
97
- default :
98
- log .Warningf (ctx , "unsupported file system type %q for path %q" , fsType , path )
88
+ err := sysutil .Statfs (path , & statfs )
89
+ if err != nil {
90
+ maybeWarnf (ctx , "unable to statfs(2) path %q (%d:%d): %v" , path , major , minor , err )
91
+ return DeviceID {major , minor }
92
+ }
93
+
94
+ switch statfs .Type {
95
+ case 0x2fc12fc1 : // ZFS_SUPER_MAGIC from include/sys/fs/zfs.h
96
+ major , minor , err = deviceIDForZFS (path )
97
+ if err != nil {
98
+ maybeWarnf (ctx , "zfs: unable to find device ID for %q: %v" , path , err )
99
+ } else {
100
+ maybeInfof (ctx , "zfs: mapping %q to diskstats device %d:%d" , path , major , minor )
101
+ }
102
+
103
+ id := DeviceID {
104
+ major : major ,
105
+ minor : minor ,
99
106
}
107
+ return id
108
+
109
+ case 0x58465342 : // XFS_SUPER_MAGIC from linux/magic.h "XFSB"
110
+ maybeWarnf (ctx , "xfs: unable to find device ID for %q: %v" , path , err )
111
+
112
+ default :
113
+ maybeWarnf (ctx , "unsupported file system type %x for path (%d:%d) %q" , statfs .Type , major , minor , path )
100
114
}
101
- case 259 : //BLOCK_EXT_MAJOR=259
102
- // noop
103
- }
104
115
105
- if major == 0 {
106
- log .Warningf (ctx , "unsupported device type %q" , path )
116
+ case 259 : // BLOCK_EXT_MAJOR=259
117
+
118
+ // NOTE: Major device 259 is the happy path for ext4 and xfs filesystems: no
119
+ // additional handling is required.
120
+
121
+ maybeInfof (ctx , "mapping %q to diskstats device %d:%d" , path , major , minor )
122
+
123
+ default :
124
+ maybeWarnf (ctx , "unsupported device type %d:%d for store at %q" , major , minor , path )
107
125
}
108
126
109
127
id := DeviceID {
@@ -177,7 +195,7 @@ func getZPoolDevice(poolName _ZPoolName) (string, error) {
177
195
if devPart == "" {
178
196
devPart = stripDevicePartition (fields [0 ])
179
197
} else {
180
- log . Warningf (ctx , "unsupported configuration: multiple devices (i.e. %q, %q) detected for zpool %q" , devPart , fields [0 ], string (poolName ))
198
+ maybeWarnf (ctx , "unsupported configuration: multiple devices (i.e. %q, %q) detected for zpool %q" , devPart , fields [0 ], string (poolName ))
181
199
}
182
200
}
183
201
}
@@ -188,17 +206,20 @@ func getZPoolDevice(poolName _ZPoolName) (string, error) {
188
206
return "" , fmt .Errorf ("no device found for zpool %q" , poolName )
189
207
}
190
208
209
+ var (
210
+ nvmePartitionRegex = regexp .MustCompile (`^(nvme\d+n\d+)(p\d+)?$` )
211
+ scsiPartitionRegex = regexp .MustCompile (`^(ram|loop|fd|(h|s|v|xv)d[a-z])(\d+)?$` )
212
+ )
213
+
191
214
// stripDevicePartition removes partition suffix from a device path.
192
215
func stripDevicePartition (devicePath string ) string {
193
216
base := filepath .Base (devicePath )
194
217
195
- var nvmePartitionRegex = regexp .MustCompile (`^(nvme\d+n\d+)(p\d+)?$` )
196
218
nvmeMatches := nvmePartitionRegex .FindStringSubmatch (base )
197
219
if len (nvmeMatches ) == 3 {
198
220
return nvmeMatches [1 ]
199
221
}
200
222
201
- var scsiPartitionRegex = regexp .MustCompile (`^(/dev/sd.*?)(\d+)$` )
202
223
scsiMatches := scsiPartitionRegex .FindStringSubmatch (base )
203
224
if len (scsiMatches ) == 3 {
204
225
return scsiMatches [1 ]
@@ -231,3 +252,19 @@ func getDeviceID(devPath string) (uint32, uint32, error) {
231
252
232
253
return maj , min , nil
233
254
}
255
+
256
+ // maybeWarnf is a convenience function to prevent panicing during bootstrap
257
+ // from using logging before it is setup.
258
+ func maybeWarnf (ctx context.Context , format string , args ... interface {}) {
259
+ if active , _ := log .IsActive (); active {
260
+ log .Ops .WarningfDepth (ctx , 1 , format , args ... )
261
+ }
262
+ }
263
+
264
+ // maybeInfof is a convenience function to prevent panicing during bootstrap
265
+ // from using logging before it is setup.
266
+ func maybeInfof (ctx context.Context , format string , args ... interface {}) {
267
+ if active , _ := log .IsActive (); active {
268
+ log .Ops .InfofDepth (ctx , 1 , format , args ... )
269
+ }
270
+ }
0 commit comments