@@ -30,25 +30,35 @@ const RBDFormatPrefix = "rbd"
30
30
// RBDFormatSeparator is the field separate used in disk paths for RBD devices.
31
31
const RBDFormatSeparator = " "
32
32
33
- // DiskParseRBDFormat parses an rbd formatted string, and returns the pool name, volume name, and list of options.
34
- func DiskParseRBDFormat (rbd string ) (string , string , []string , error ) {
35
- if ! strings .HasPrefix (rbd , fmt .Sprintf ("%s%s" , RBDFormatPrefix , RBDFormatSeparator )) {
36
- return "" , "" , nil , fmt .Errorf ("Invalid rbd format, missing prefix" )
33
+ // DiskParseRBDFormat parses an rbd formatted string, and returns the pool name, volume name, and map of options.
34
+ func DiskParseRBDFormat (rbd string ) (string , string , map [string ]string , error ) {
35
+ // Remove and check the prefix.
36
+ prefix , rbd , _ := strings .Cut (rbd , RBDFormatSeparator )
37
+ if prefix != RBDFormatPrefix {
38
+ return "" , "" , nil , fmt .Errorf ("Invalid rbd format, wrong prefix: %q" , prefix )
37
39
}
38
40
39
- fields := strings .SplitN (rbd , RBDFormatSeparator , 3 )
40
- if len (fields ) != 3 {
41
- return "" , "" , nil , fmt .Errorf ("Invalid rbd format, invalid number of fields" )
41
+ // Split the path and options.
42
+ path , rawOpts , _ := strings .Cut (rbd , RBDFormatSeparator )
43
+
44
+ // Check for valid RBD path.
45
+ pool , volume , validPath := strings .Cut (path , "/" )
46
+ if ! validPath {
47
+ return "" , "" , nil , fmt .Errorf ("Invalid rbd format, missing pool and/or volume: %q" , path )
42
48
}
43
49
44
- opts := fields [2 ]
50
+ // Parse options.
51
+ opts := make (map [string ]string )
52
+ for _ , o := range strings .Split (rawOpts , ":" ) {
53
+ k , v , isValid := strings .Cut (o , "=" )
54
+ if ! isValid {
55
+ return "" , "" , nil , fmt .Errorf ("Invalid rbd format, bad option: %q" , o )
56
+ }
45
57
46
- fields = strings .SplitN (fields [1 ], "/" , 2 )
47
- if len (fields ) != 2 {
48
- return "" , "" , nil , fmt .Errorf ("Invalid rbd format, invalid pool or volume" )
58
+ opts [k ] = v
49
59
}
50
60
51
- return fields [ 0 ], fields [ 1 ], strings . Split ( opts , ":" ) , nil
61
+ return pool , volume , opts , nil
52
62
}
53
63
54
64
// DiskGetRBDFormat returns a rbd formatted string with the given values.
@@ -59,7 +69,6 @@ func DiskGetRBDFormat(clusterName string, userName string, poolName string, volu
59
69
opts := []string {
60
70
fmt .Sprintf ("id=%s" , optEscaper .Replace (userName )),
61
71
fmt .Sprintf ("pool=%s" , optEscaper .Replace (poolName )),
62
- fmt .Sprintf ("conf=/etc/ceph/%s.conf" , optEscaper .Replace (clusterName )),
63
72
}
64
73
65
74
return fmt .Sprintf ("%s%s%s/%s%s%s" , RBDFormatPrefix , RBDFormatSeparator , optEscaper .Replace (poolName ), optEscaper .Replace (volumeName ), RBDFormatSeparator , strings .Join (opts , ":" ))
@@ -77,7 +86,7 @@ func BlockFsDetect(dev string) (string, error) {
77
86
78
87
// IsBlockdev returns boolean indicating whether device is block type.
79
88
func IsBlockdev (path string ) bool {
80
- // Get a stat struct from the provided path
89
+ // Get a stat struct from the provided path.
81
90
stat := unix.Stat_t {}
82
91
err := unix .Stat (path , & stat )
83
92
if err != nil {
@@ -240,7 +249,7 @@ again:
240
249
241
250
// diskCephfsOptions returns the mntSrcPath and fsOptions to use for mounting a cephfs share.
242
251
func diskCephfsOptions (clusterName string , userName string , fsName string , fsPath string ) (string , []string , error ) {
243
- // Get the FSID
252
+ // Get the FSID.
244
253
fsid , err := storageDrivers .CephFsid (clusterName )
245
254
if err != nil {
246
255
return "" , nil , err
0 commit comments