From 1d9c41510ff298a3d74a7022a294f57b5d99d2e3 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Sun, 22 Dec 2024 09:04:58 +0000 Subject: [PATCH] feat: add noformat option to fix fsck stuck issue on Linux node --- pkg/azuredisk/azure_common_linux.go | 4 ++ pkg/azureutils/azure_disk_utils.go | 11 +++++ pkg/azureutils/azure_disk_utils_test.go | 61 +++++++++++++++++++++++++ 3 files changed, 76 insertions(+) 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 abf931311c..df2b29e529 100644 --- a/pkg/azureutils/azure_disk_utils.go +++ b/pkg/azureutils/azure_disk_utils.go @@ -862,3 +862,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 bbaf28bb25..af26cf6d20 100644 --- a/pkg/azureutils/azure_disk_utils_test.go +++ b/pkg/azureutils/azure_disk_utils_test.go @@ -1987,3 +1987,64 @@ func TestGetRetryAfterSeconds(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) + } + } +}