Merge pull request #112877 from ConnorJC3/format-options

Add ability to pass format options in mount-utils
This commit is contained in:
Kubernetes Prow Robot 2022-11-07 11:20:30 -08:00 committed by GitHub
commit 8c3777aa63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 6 deletions

View File

@ -165,7 +165,15 @@ func (mounter *SafeFormatAndMount) FormatAndMount(source string, target string,
// be used by callers that pass sensitive material (like passwords) as mount // be used by callers that pass sensitive material (like passwords) as mount
// options. // options.
func (mounter *SafeFormatAndMount) FormatAndMountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { func (mounter *SafeFormatAndMount) FormatAndMountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error {
return mounter.formatAndMountSensitive(source, target, fstype, options, sensitiveOptions) return mounter.FormatAndMountSensitiveWithFormatOptions(source, target, fstype, options, sensitiveOptions, nil /* formatOptions */)
}
// FormatAndMountSensitiveWithFormatOptions behaves exactly the same as
// FormatAndMountSensitive, but allows for options to be passed when the disk
// is formatted. These options are NOT validated in any way and should never
// come directly from untrusted user input as that would be an injection risk.
func (mounter *SafeFormatAndMount) FormatAndMountSensitiveWithFormatOptions(source string, target string, fstype string, options []string, sensitiveOptions []string, formatOptions []string) error {
return mounter.formatAndMountSensitive(source, target, fstype, options, sensitiveOptions, formatOptions)
} }
// getMountRefsByDev finds all references to the device provided // getMountRefsByDev finds all references to the device provided

View File

@ -471,7 +471,7 @@ func (mounter *SafeFormatAndMount) checkAndRepairFilesystem(source string) error
} }
// formatAndMount uses unix utils to format and mount the given disk // formatAndMount uses unix utils to format and mount the given disk
func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string, formatOptions []string) error {
readOnly := false readOnly := false
for _, option := range options { for _, option := range options {
if option == "ro" { if option == "ro" {
@ -523,6 +523,7 @@ func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target
source, source,
} }
} }
args = append(formatOptions, args...)
klog.Infof("Disk %q appears to be unformatted, attempting to format as type: %q with options: %v", source, fstype, args) klog.Infof("Disk %q appears to be unformatted, attempting to format as type: %q with options: %v", source, fstype, args)
output, err := mounter.Exec.Command("mkfs."+fstype, args...).CombinedOutput() output, err := mounter.Exec.Command("mkfs."+fstype, args...).CombinedOutput()

View File

@ -90,7 +90,7 @@ func (mounter *Mounter) GetMountRefs(pathname string) ([]string, error) {
return nil, errUnsupported return nil, errUnsupported
} }
func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string, formatOptions []string) error {
return mounter.Interface.Mount(source, target, fstype, options) return mounter.Interface.Mount(source, target, fstype, options)
} }

View File

@ -273,7 +273,7 @@ func (mounter *Mounter) GetMountRefs(pathname string) ([]string, error) {
return []string{pathname}, nil return []string{pathname}, nil
} }
func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string, formatOptions []string) error {
// Try to mount the disk // Try to mount the disk
klog.V(4).Infof("Attempting to formatAndMount disk: %s %s %s", fstype, source, target) klog.V(4).Infof("Attempting to formatAndMount disk: %s %s %s", fstype, source, target)
@ -288,8 +288,12 @@ func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target
} }
// format disk if it is unformatted(raw) // format disk if it is unformatted(raw)
formatOptionsUnwrapped := ""
if len(formatOptions) > 0 {
formatOptionsUnwrapped = " " + strings.Join(formatOptions, " ")
}
cmd := fmt.Sprintf("Get-Disk -Number %s | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle GPT -PassThru"+ cmd := fmt.Sprintf("Get-Disk -Number %s | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle GPT -PassThru"+
" | New-Partition -UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false", source, fstype) " | New-Partition -UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false%s", source, fstype, formatOptionsUnwrapped)
if output, err := mounter.Exec.Command("powershell", "/c", cmd).CombinedOutput(); err != nil { if output, err := mounter.Exec.Command("powershell", "/c", cmd).CombinedOutput(); err != nil {
return fmt.Errorf("diskMount: format disk failed, error: %v, output: %q", err, string(output)) return fmt.Errorf("diskMount: format disk failed, error: %v, output: %q", err, string(output))
} }

View File

@ -68,6 +68,7 @@ func TestSafeFormatAndMount(t *testing.T) {
fstype string fstype string
mountOptions []string mountOptions []string
sensitiveMountOptions []string sensitiveMountOptions []string
formatOptions []string
execScripts []ExecArgs execScripts []ExecArgs
mountErrs []error mountErrs []error
expErrorType MountErrorType expErrorType MountErrorType
@ -213,6 +214,15 @@ func TestSafeFormatAndMount(t *testing.T) {
}, },
expErrorType: FormatFailed, expErrorType: FormatFailed,
}, },
{
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, mount passes (with format options)",
fstype: "ext4",
formatOptions: []string{"-b", "1024"},
execScripts: []ExecArgs{
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
{"mkfs.ext4", []string{"-b", "1024", "-F", "-m0", "/dev/foo"}, "", nil},
},
},
} }
for _, test := range tests { for _, test := range tests {
@ -233,7 +243,9 @@ func TestSafeFormatAndMount(t *testing.T) {
device := "/dev/foo" device := "/dev/foo"
dest := mntDir dest := mntDir
var err error var err error
if len(test.sensitiveMountOptions) == 0 { if len(test.formatOptions) > 0 {
err = mounter.FormatAndMountSensitiveWithFormatOptions(device, dest, test.fstype, test.mountOptions, test.sensitiveMountOptions, test.formatOptions)
} else if len(test.sensitiveMountOptions) == 0 {
err = mounter.FormatAndMount(device, dest, test.fstype, test.mountOptions) err = mounter.FormatAndMount(device, dest, test.fstype, test.mountOptions)
} else { } else {
err = mounter.FormatAndMountSensitive(device, dest, test.fstype, test.mountOptions, test.sensitiveMountOptions) err = mounter.FormatAndMountSensitive(device, dest, test.fstype, test.mountOptions, test.sensitiveMountOptions)