From c8df30973b62f5d6566152a2082e3de8305004c4 Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Fri, 26 Aug 2016 12:49:26 +0200 Subject: [PATCH] Show specific error when a volume is formatted by unexpected filesystem. kubelet now detects that e.g. xfs volume is being mounted as ext3 because of wrong volume.Spec. Mount error is left in the error message to diagnose issues with mounting e.g. 'ext3' volume as 'ext4' - they are different filesystems, however kernel should mount ext3 as ext4 without errors. --- pkg/util/mount/mount_linux.go | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/pkg/util/mount/mount_linux.go b/pkg/util/mount/mount_linux.go index 2c94f32d76a..992c26128c5 100644 --- a/pkg/util/mount/mount_linux.go +++ b/pkg/util/mount/mount_linux.go @@ -323,17 +323,22 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, // Try to mount the disk glog.V(4).Infof("Attempting to mount disk: %s %s %s", fstype, source, target) - err = mounter.Interface.Mount(source, target, fstype, options) - if err != nil { - // It is possible that this disk is not formatted. Double check using diskLooksUnformatted - notFormatted, err := mounter.diskLooksUnformatted(source) - if err == nil && notFormatted { - args = []string{source} + mountErr := mounter.Interface.Mount(source, target, fstype, options) + if mountErr != nil { + // Mount failed. This indicates either that the disk is unformatted or + // it contains an unexpected filesystem. + existingFormat, err := mounter.getDiskFormat(source) + if err != nil { + return err + } + if existingFormat == "" { // Disk is unformatted so format it. + args = []string{source} // Use 'ext4' as the default if len(fstype) == 0 { fstype = "ext4" } + if fstype == "ext4" || fstype == "ext3" { args = []string{"-E", "lazy_itable_init=0,lazy_journal_init=0", "-F", source} } @@ -347,13 +352,22 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, } glog.Errorf("format of disk %q failed: type:(%q) target:(%q) options:(%q)error:(%v)", source, fstype, target, options, err) return err + } else { + // Disk is already formatted and failed to mount + if len(fstype) == 0 || fstype == existingFormat { + // This is mount error + return mountErr + } else { + // Block device is formatted with unexpected filesystem, let the user know + return fmt.Errorf("failed to mount the volume as %q, it's already formatted with %q. Mount error: %v", fstype, existingFormat, mountErr) + } } } - return err + return mountErr } // diskLooksUnformatted uses 'lsblk' to see if the given disk is unformated -func (mounter *SafeFormatAndMount) diskLooksUnformatted(disk string) (bool, error) { +func (mounter *SafeFormatAndMount) getDiskFormat(disk string) (string, error) { args := []string{"-nd", "-o", "FSTYPE", disk} cmd := mounter.Runner.Command("lsblk", args...) glog.V(4).Infof("Attempting to determine if disk %q is formatted using lsblk with args: (%v)", disk, args) @@ -365,8 +379,8 @@ func (mounter *SafeFormatAndMount) diskLooksUnformatted(disk string) (bool, erro if err != nil { glog.Errorf("Could not determine if disk %q is formatted (%v)", disk, err) - return false, err + return "", err } - return output == "", nil + return strings.TrimSpace(output), nil }