From ca59d53a604fbaeb69b1c25ab8cc09a29766ecff Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Fri, 21 Aug 2020 19:00:06 +0200 Subject: [PATCH] rbd: introduce appendDeviceTypeAndOptions() Factor out --device-type and --options formatting. Signed-off-by: Ilya Dryomov (cherry picked from commit c2493686b77861dbb501a5ee8701bfa2504e9369) --- internal/rbd/rbd_attach.go | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/internal/rbd/rbd_attach.go b/internal/rbd/rbd_attach.go index 22938be99..650d7431e 100644 --- a/internal/rbd/rbd_attach.go +++ b/internal/rbd/rbd_attach.go @@ -208,6 +208,20 @@ func attachRBDImage(ctx context.Context, volOptions *rbdVolume, cr *util.Credent return devicePath, err } +func appendDeviceTypeAndOptions(cmdArgs []string, isNbd bool, userOptions string) []string { + accessType := accessTypeKRbd + if isNbd { + accessType = accessTypeNbd + } + + cmdArgs = append(cmdArgs, "--device-type", accessType) + if userOptions != "" { + cmdArgs = append(cmdArgs, "--options", userOptions) + } + + return cmdArgs +} + func createPath(ctx context.Context, volOpt *rbdVolume, cr *util.Credentials) (string, error) { isNbd := false imagePath := volOpt.String() @@ -222,22 +236,15 @@ func createPath(ctx context.Context, volOpt *rbdVolume, cr *util.Credentials) (s } // Choose access protocol - accessType := accessTypeKRbd if volOpt.Mounter == rbdTonbd && hasNBD { isNbd = true - accessType = accessTypeNbd } - // Update options with device type selection - mapArgs = append(mapArgs, "--device-type", accessType) - + mapArgs = appendDeviceTypeAndOptions(mapArgs, isNbd, volOpt.MapOptions) if volOpt.readOnly { mapArgs = append(mapArgs, "--read-only") } - if volOpt.MapOptions != "" { - mapArgs = append(mapArgs, "--options", volOpt.MapOptions) - } // Execute map stdout, stderr, err := util.ExecCommand(ctx, rbd, mapArgs...) if err != nil { @@ -310,14 +317,8 @@ func detachRBDImageOrDeviceSpec(ctx context.Context, imageOrDeviceSpec string, i } } - accessType := accessTypeKRbd - if isNbd { - accessType = accessTypeNbd - } - unmapArgs := []string{"unmap", "--device-type", accessType, imageOrDeviceSpec} - if unmapOptions != "" { - unmapArgs = append(unmapArgs, "--options", unmapOptions) - } + unmapArgs := []string{"unmap", imageOrDeviceSpec} + unmapArgs = appendDeviceTypeAndOptions(unmapArgs, isNbd, unmapOptions) _, stderr, err := util.ExecCommand(ctx, rbd, unmapArgs...) if err != nil {