diff --git a/pkg/azuredisk/azure_common_linux.go b/pkg/azuredisk/azure_common_linux.go index e9688d8ceb..0c0d3e4db4 100644 --- a/pkg/azuredisk/azure_common_linux.go +++ b/pkg/azuredisk/azure_common_linux.go @@ -120,6 +120,10 @@ func findDiskByLun(lun int, io azureutils.IOHandler, _ *mount.SafeFormatAndMount } func formatAndMount(source, target, fstype string, options []string, m *mount.SafeFormatAndMount) error { + if newOptions, exists := azureutils.RemoveOptionIfExists(options, "noformat"); exists { + klog.V(2).Infof("formatAndMount - skip format for %s, old options: %v, new options: %v", target, options, newOptions) + return m.Mount(source, target, fstype, newOptions) + } return m.FormatAndMount(source, target, fstype, options) } diff --git a/pkg/azureutils/azure_disk_utils.go b/pkg/azureutils/azure_disk_utils.go index 775274e85e..8051aa2bf6 100644 --- a/pkg/azureutils/azure_disk_utils.go +++ b/pkg/azureutils/azure_disk_utils.go @@ -839,3 +839,14 @@ func RunPowershellCmd(command string, envs ...string) ([]byte, error) { klog.V(6).Infof("Executing command: %q", cmd.String()) return cmd.CombinedOutput() } + +// RemoveOptionIfExists removes the given option from the list of options +// return the new list and a boolean indicating whether the option was found. +func RemoveOptionIfExists(options []string, removeOption string) ([]string, bool) { + for i, option := range options { + if option == removeOption { + return append(options[:i], options[i+1:]...), true + } + } + return options, false +} diff --git a/pkg/azureutils/azure_disk_utils_test.go b/pkg/azureutils/azure_disk_utils_test.go index 2dc4aac71e..0648f3329a 100644 --- a/pkg/azureutils/azure_disk_utils_test.go +++ b/pkg/azureutils/azure_disk_utils_test.go @@ -1964,3 +1964,64 @@ func TestIsThrottlingError(t *testing.T) { } } } + +func TestRemoveOptionIfExists(t *testing.T) { + tests := []struct { + desc string + options []string + removeOption string + expectedOptions []string + expected bool + }{ + { + desc: "nil options", + removeOption: "option", + expected: false, + }, + { + desc: "empty options", + options: []string{}, + removeOption: "option", + expectedOptions: []string{}, + expected: false, + }, + { + desc: "option not found", + options: []string{"option1", "option2"}, + removeOption: "option", + expectedOptions: []string{"option1", "option2"}, + expected: false, + }, + { + desc: "option found in the last element", + options: []string{"option1", "option2", "option"}, + removeOption: "option", + expectedOptions: []string{"option1", "option2"}, + expected: true, + }, + { + desc: "option found in the first element", + options: []string{"option", "option1", "option2"}, + removeOption: "option", + expectedOptions: []string{"option1", "option2"}, + expected: true, + }, + { + desc: "option found in the middle element", + options: []string{"option1", "option", "option2"}, + removeOption: "option", + expectedOptions: []string{"option1", "option2"}, + expected: true, + }, + } + + for _, test := range tests { + result, exists := RemoveOptionIfExists(test.options, test.removeOption) + if !reflect.DeepEqual(result, test.expectedOptions) { + t.Errorf("test[%s]: unexpected output: %v, expected result: %v", test.desc, result, test.expectedOptions) + } + if exists != test.expected { + t.Errorf("test[%s]: unexpected output: %v, expected result: %v", test.desc, exists, test.expected) + } + } +}