From 1880c4eedb30eb4173858636e77a6989f05fa7b3 Mon Sep 17 00:00:00 2001 From: Sami Wagiaalla Date: Fri, 6 Nov 2015 15:37:46 -0500 Subject: [PATCH] move formatAndMount and diskLooksUnformatted to mount_linux --- pkg/util/mount/mount.go | 50 ----------------------------- pkg/util/mount/mount_linux.go | 48 +++++++++++++++++++++++++++ pkg/util/mount/mount_unsupported.go | 8 +++++ 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/pkg/util/mount/mount.go b/pkg/util/mount/mount.go index f3829fa197d..74c3b3230d8 100644 --- a/pkg/util/mount/mount.go +++ b/pkg/util/mount/mount.go @@ -19,8 +19,6 @@ limitations under the License. package mount import ( - "strings" - "github.com/golang/glog" "k8s.io/kubernetes/pkg/util/exec" ) @@ -68,54 +66,6 @@ func (mounter *SafeFormatAndMount) Mount(source string, target string, fstype st return mounter.formatAndMount(source, target, fstype, options) } -// formatAndMount uses unix utils to format and mount the given disk -func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, fstype string, options []string) error { - options = append(options, "defaults") - - // Try to mount the disk - 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} - // Disk is unformatted so format it. - // 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} - } - cmd := mounter.Runner.Command("mkfs."+fstype, args...) - _, err := cmd.CombinedOutput() - if err == nil { - // the disk has been formatted sucessfully try to mount it again. - return mounter.Interface.Mount(source, target, fstype, options) - } - return err - } - } - return err -} - -// diskLooksUnformatted uses 'lsblk' to see if the given disk is unformated -func (mounter *SafeFormatAndMount) diskLooksUnformatted(disk string) (bool, error) { - args := []string{"-nd", "-o", "FSTYPE", disk} - cmd := mounter.Runner.Command("lsblk", args...) - dataOut, err := cmd.CombinedOutput() - output := strings.TrimSpace(string(dataOut)) - - // TODO (#13212): check if this disk has partitions and return false, and - // an error if so. - - if err != nil { - return false, err - } - - return output == "", nil -} - // New returns a mount.Interface for the current system. func New() Interface { return &Mounter{} diff --git a/pkg/util/mount/mount_linux.go b/pkg/util/mount/mount_linux.go index 18c16edf2e1..cf1f1fa6d50 100644 --- a/pkg/util/mount/mount_linux.go +++ b/pkg/util/mount/mount_linux.go @@ -235,3 +235,51 @@ func readProcMountsFrom(file io.Reader, out *[]MountPoint) (uint32, error) { } return hash.Sum32(), nil } + +// formatAndMount uses unix utils to format and mount the given disk +func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, fstype string, options []string) error { + options = append(options, "defaults") + + // Try to mount the disk + 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} + // Disk is unformatted so format it. + // 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} + } + cmd := mounter.Runner.Command("mkfs."+fstype, args...) + _, err := cmd.CombinedOutput() + if err == nil { + // the disk has been formatted sucessfully try to mount it again. + return mounter.Interface.Mount(source, target, fstype, options) + } + return err + } + } + return err +} + +// diskLooksUnformatted uses 'lsblk' to see if the given disk is unformated +func (mounter *SafeFormatAndMount) diskLooksUnformatted(disk string) (bool, error) { + args := []string{"-nd", "-o", "FSTYPE", disk} + cmd := mounter.Runner.Command("lsblk", args...) + dataOut, err := cmd.CombinedOutput() + output := strings.TrimSpace(string(dataOut)) + + // TODO (#13212): check if this disk has partitions and return false, and + // an error if so. + + if err != nil { + return false, err + } + + return output == "", nil +} diff --git a/pkg/util/mount/mount_unsupported.go b/pkg/util/mount/mount_unsupported.go index 25ee8a7ee13..8942c003635 100644 --- a/pkg/util/mount/mount_unsupported.go +++ b/pkg/util/mount/mount_unsupported.go @@ -35,3 +35,11 @@ func (mounter *Mounter) List() ([]MountPoint, error) { func (mounter *Mounter) IsLikelyNotMountPoint(file string) (bool, error) { return true, nil } + +func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, fstype string, options []string) error { + return nil +} + +func (mounter *SafeFormatAndMount) diskLooksUnformatted(disk string) (bool, error) { + return true, nil +}