Skip to content

Commit de45035

Browse files
committed
pkg/storage/disk: conditionally log disk device information during bootstrap
Epic: none Release note: None
1 parent d54d8a7 commit de45035

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

pkg/storage/disk/platform_linux.go

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"os/exec"
1919
"path/filepath"
2020
"regexp"
21-
"strconv"
2221
"strings"
2322
"time"
2423

@@ -83,27 +82,46 @@ func deviceIDFromFileInfo(finfo fs.FileInfo, path string) DeviceID {
8382
// Per /usr/include/linux/major.h and Documentation/admin-guide/devices.rst:
8483
switch major {
8584
case 0: // UNNAMED_MAJOR
85+
8686
// Perform additional lookups for unknown device types
8787
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,
99106
}
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)
100114
}
101-
case 259: //BLOCK_EXT_MAJOR=259
102-
// noop
103-
}
104115

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)
107125
}
108126

109127
id := DeviceID{
@@ -177,7 +195,7 @@ func getZPoolDevice(poolName _ZPoolName) (string, error) {
177195
if devPart == "" {
178196
devPart = stripDevicePartition(fields[0])
179197
} 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))
181199
}
182200
}
183201
}
@@ -188,17 +206,20 @@ func getZPoolDevice(poolName _ZPoolName) (string, error) {
188206
return "", fmt.Errorf("no device found for zpool %q", poolName)
189207
}
190208

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+
191214
// stripDevicePartition removes partition suffix from a device path.
192215
func stripDevicePartition(devicePath string) string {
193216
base := filepath.Base(devicePath)
194217

195-
var nvmePartitionRegex = regexp.MustCompile(`^(nvme\d+n\d+)(p\d+)?$`)
196218
nvmeMatches := nvmePartitionRegex.FindStringSubmatch(base)
197219
if len(nvmeMatches) == 3 {
198220
return nvmeMatches[1]
199221
}
200222

201-
var scsiPartitionRegex = regexp.MustCompile(`^(/dev/sd.*?)(\d+)$`)
202223
scsiMatches := scsiPartitionRegex.FindStringSubmatch(base)
203224
if len(scsiMatches) == 3 {
204225
return scsiMatches[1]
@@ -231,3 +252,19 @@ func getDeviceID(devPath string) (uint32, uint32, error) {
231252

232253
return maj, min, nil
233254
}
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+
}

pkg/testutils/lint/passes/fmtsafe/functions.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ var requireConstFmt = map[string]bool{
8383

8484
"(*github.com/cockroachdb/cockroach/pkg/internal/rsg/yacc.Tree).errorf": true,
8585

86+
"github.com/cockroachdb/cockroach/pkg/storage/disk.maybeInfof": true,
87+
"github.com/cockroachdb/cockroach/pkg/storage/disk.maybeWarnf": true,
88+
8689
"(github.com/cockroachdb/cockroach/pkg/storage.pebbleLogger).Infof": true,
8790
"(github.com/cockroachdb/cockroach/pkg/storage.pebbleLogger).Fatalf": true,
8891
"(github.com/cockroachdb/cockroach/pkg/storage.pebbleLogger).Errorf": true,

0 commit comments

Comments
 (0)