From 66c905d63e146f9478e678fbc8fabc017b6c8bd0 Mon Sep 17 00:00:00 2001 From: Sami Wagiaalla Date: Thu, 5 Nov 2015 16:49:40 -0500 Subject: [PATCH] Distinguish normal mount from format and mount in SafeFormatAndMount --- pkg/util/mount/mount.go | 14 ++++++++----- pkg/util/mount/safe_format_and_mount_test.go | 2 +- pkg/volume/aws_ebs/aws_ebs.go | 2 +- pkg/volume/aws_ebs/aws_util.go | 2 +- pkg/volume/fc/fc.go | 21 +++++++++++--------- pkg/volume/fc/fc_util.go | 2 +- pkg/volume/gce_pd/gce_pd.go | 2 +- pkg/volume/gce_pd/gce_util.go | 2 +- pkg/volume/iscsi/iscsi.go | 19 ++++++++++-------- pkg/volume/iscsi/iscsi_util.go | 2 +- pkg/volume/rbd/rbd.go | 4 ++-- pkg/volume/rbd/rbd_util.go | 2 +- 12 files changed, 42 insertions(+), 32 deletions(-) diff --git a/pkg/util/mount/mount.go b/pkg/util/mount/mount.go index f3829fa197..24b4c8af0c 100644 --- a/pkg/util/mount/mount.go +++ b/pkg/util/mount/mount.go @@ -49,16 +49,20 @@ type MountPoint struct { Pass int } -// SafeFormatAndMount probes a device to see if it is formatted. If -// so it mounts it otherwise it formats it and mounts it +// SafeFormatAndMount probes a device to see if it is formatted. +// Namely it checks to see if a file system is present. If so it +// mounts it otherwise the device is formatted first then mounted. type SafeFormatAndMount struct { Interface Runner exec.Interface } -// Mount mounts the given disk. If the disk is not formatted and the disk is not being mounted as read only -// it will format the disk first then mount it. -func (mounter *SafeFormatAndMount) Mount(source string, target string, fstype string, options []string) error { +// FormatAndMount formats the given disk, if needed, and mounts it. +// That is if the disk is not formatted and it is not being mounted as +// read-only it will format it first then mount it. Otherwise, if the +// disk is already formatted or it is being mounted as read-only, it +// will be mounted without formatting. +func (mounter *SafeFormatAndMount) FormatAndMount(source string, target string, fstype string, options []string) error { // Don't attempt to format if mounting as readonly. Go straight to mounting. for _, option := range options { if option == "ro" { diff --git a/pkg/util/mount/safe_format_and_mount_test.go b/pkg/util/mount/safe_format_and_mount_test.go index 7a96219fda..d859b1e6b9 100644 --- a/pkg/util/mount/safe_format_and_mount_test.go +++ b/pkg/util/mount/safe_format_and_mount_test.go @@ -156,7 +156,7 @@ func TestSafeFormatAndMount(t *testing.T) { device := "/dev/foo" dest := "/mnt/bar" - err := mounter.Mount(device, dest, test.fstype, test.mountOptions) + err := mounter.FormatAndMount(device, dest, test.fstype, test.mountOptions) if test.expectedError == nil { if err != nil { t.Errorf("unexpected non-error: %v", err) diff --git a/pkg/volume/aws_ebs/aws_ebs.go b/pkg/volume/aws_ebs/aws_ebs.go index c0f5a61185..ec3a7f02f7 100644 --- a/pkg/volume/aws_ebs/aws_ebs.go +++ b/pkg/volume/aws_ebs/aws_ebs.go @@ -172,7 +172,7 @@ type awsElasticBlockStoreBuilder struct { // Specifies whether the disk will be attached as read-only. readOnly bool // diskMounter provides the interface that is used to mount the actual block device. - diskMounter mount.Interface + diskMounter *mount.SafeFormatAndMount } var _ volume.Builder = &awsElasticBlockStoreBuilder{} diff --git a/pkg/volume/aws_ebs/aws_util.go b/pkg/volume/aws_ebs/aws_util.go index 6af88d1a1d..aa2468194c 100644 --- a/pkg/volume/aws_ebs/aws_util.go +++ b/pkg/volume/aws_ebs/aws_util.go @@ -74,7 +74,7 @@ func (util *AWSDiskUtil) AttachAndMountDisk(b *awsElasticBlockStoreBuilder, glob options = append(options, "ro") } if notMnt { - err = b.diskMounter.Mount(devicePath, globalPDPath, b.fsType, options) + err = b.diskMounter.FormatAndMount(devicePath, globalPDPath, b.fsType, options) if err != nil { os.Remove(globalPDPath) return err diff --git a/pkg/volume/fc/fc.go b/pkg/volume/fc/fc.go index f783418260..76b69fe5f8 100644 --- a/pkg/volume/fc/fc.go +++ b/pkg/volume/fc/fc.go @@ -107,11 +107,11 @@ func (plugin *fcPlugin) newBuilderInternal(spec *volume.Spec, podUID types.UID, wwns: fc.TargetWWNs, lun: lun, manager: manager, - mounter: &mount.SafeFormatAndMount{mounter, exec.New()}, io: &osIOHandler{}, plugin: plugin}, fsType: fc.FSType, readOnly: readOnly, + mounter: &mount.SafeFormatAndMount{mounter, exec.New()}, }, nil } @@ -121,14 +121,16 @@ func (plugin *fcPlugin) NewCleaner(volName string, podUID types.UID) (volume.Cle } func (plugin *fcPlugin) newCleanerInternal(volName string, podUID types.UID, manager diskManager, mounter mount.Interface) (volume.Cleaner, error) { - return &fcDiskCleaner{&fcDisk{ - podUID: podUID, - volName: volName, - manager: manager, + return &fcDiskCleaner{ + fcDisk: &fcDisk{ + podUID: podUID, + volName: volName, + manager: manager, + plugin: plugin, + io: &osIOHandler{}, + }, mounter: mounter, - plugin: plugin, - io: &osIOHandler{}, - }}, nil + }, nil } func (plugin *fcPlugin) execCommand(command string, args []string) ([]byte, error) { @@ -143,7 +145,6 @@ type fcDisk struct { wwns []string lun string plugin *fcPlugin - mounter mount.Interface // Utility interface that provides API calls to the provider to attach/detach disks. manager diskManager // io handler interface @@ -160,6 +161,7 @@ type fcDiskBuilder struct { *fcDisk readOnly bool fsType string + mounter *mount.SafeFormatAndMount } var _ volume.Builder = &fcDiskBuilder{} @@ -187,6 +189,7 @@ func (b *fcDiskBuilder) SetUpAt(dir string) error { type fcDiskCleaner struct { *fcDisk + mounter mount.Interface } var _ volume.Cleaner = &fcDiskCleaner{} diff --git a/pkg/volume/fc/fc_util.go b/pkg/volume/fc/fc_util.go index b0dbc82b36..afa1ffb2b4 100644 --- a/pkg/volume/fc/fc_util.go +++ b/pkg/volume/fc/fc_util.go @@ -184,7 +184,7 @@ func (util *FCUtil) AttachDisk(b fcDiskBuilder) error { return fmt.Errorf("fc: failed to mkdir %s, error", globalPDPath) } - err = b.mounter.Mount(devicePath, globalPDPath, b.fsType, nil) + err = b.mounter.FormatAndMount(devicePath, globalPDPath, b.fsType, nil) if err != nil { return fmt.Errorf("fc: failed to mount fc volume %s [%s] to %s, error %v", devicePath, b.fsType, globalPDPath, err) } diff --git a/pkg/volume/gce_pd/gce_pd.go b/pkg/volume/gce_pd/gce_pd.go index 3913ba63c1..5624835fe2 100644 --- a/pkg/volume/gce_pd/gce_pd.go +++ b/pkg/volume/gce_pd/gce_pd.go @@ -160,7 +160,7 @@ type gcePersistentDiskBuilder struct { // Specifies whether the disk will be attached as read-only. readOnly bool // diskMounter provides the interface that is used to mount the actual block device. - diskMounter mount.Interface + diskMounter *mount.SafeFormatAndMount } var _ volume.Builder = &gcePersistentDiskBuilder{} diff --git a/pkg/volume/gce_pd/gce_util.go b/pkg/volume/gce_pd/gce_util.go index c43a610bd3..218789fa4c 100644 --- a/pkg/volume/gce_pd/gce_util.go +++ b/pkg/volume/gce_pd/gce_util.go @@ -90,7 +90,7 @@ func (diskUtil *GCEDiskUtil) AttachAndMountDisk(b *gcePersistentDiskBuilder, glo options = append(options, "ro") } if notMnt { - err = b.diskMounter.Mount(devicePath, globalPDPath, b.fsType, options) + err = b.diskMounter.FormatAndMount(devicePath, globalPDPath, b.fsType, options) if err != nil { os.Remove(globalPDPath) return err diff --git a/pkg/volume/iscsi/iscsi.go b/pkg/volume/iscsi/iscsi.go index a4b204309f..e1339e4bae 100644 --- a/pkg/volume/iscsi/iscsi.go +++ b/pkg/volume/iscsi/iscsi.go @@ -105,10 +105,10 @@ func (plugin *iscsiPlugin) newBuilderInternal(spec *volume.Spec, podUID types.UI iqn: iscsi.IQN, lun: lun, manager: manager, - mounter: &mount.SafeFormatAndMount{mounter, exec.New()}, plugin: plugin}, fsType: iscsi.FSType, readOnly: readOnly, + mounter: &mount.SafeFormatAndMount{mounter, exec.New()}, }, nil } @@ -118,13 +118,15 @@ func (plugin *iscsiPlugin) NewCleaner(volName string, podUID types.UID) (volume. } func (plugin *iscsiPlugin) newCleanerInternal(volName string, podUID types.UID, manager diskManager, mounter mount.Interface) (volume.Cleaner, error) { - return &iscsiDiskCleaner{&iscsiDisk{ - podUID: podUID, - volName: volName, - manager: manager, + return &iscsiDiskCleaner{ + iscsiDisk: &iscsiDisk{ + podUID: podUID, + volName: volName, + manager: manager, + plugin: plugin, + }, mounter: mounter, - plugin: plugin, - }}, nil + }, nil } func (plugin *iscsiPlugin) execCommand(command string, args []string) ([]byte, error) { @@ -139,7 +141,6 @@ type iscsiDisk struct { iqn string lun string plugin *iscsiPlugin - mounter mount.Interface // Utility interface that provides API calls to the provider to attach/detach disks. manager diskManager } @@ -154,6 +155,7 @@ type iscsiDiskBuilder struct { *iscsiDisk readOnly bool fsType string + mounter *mount.SafeFormatAndMount } var _ volume.Builder = &iscsiDiskBuilder{} @@ -182,6 +184,7 @@ func (b *iscsiDiskBuilder) SetUpAt(dir string) error { type iscsiDiskCleaner struct { *iscsiDisk + mounter mount.Interface } var _ volume.Cleaner = &iscsiDiskCleaner{} diff --git a/pkg/volume/iscsi/iscsi_util.go b/pkg/volume/iscsi/iscsi_util.go index d9db4fd8e2..819b6bf4a0 100644 --- a/pkg/volume/iscsi/iscsi_util.go +++ b/pkg/volume/iscsi/iscsi_util.go @@ -113,7 +113,7 @@ func (util *ISCSIUtil) AttachDisk(b iscsiDiskBuilder) error { return err } - err = b.mounter.Mount(devicePath, globalPDPath, b.fsType, nil) + err = b.mounter.FormatAndMount(devicePath, globalPDPath, b.fsType, nil) if err != nil { glog.Errorf("iscsi: failed to mount iscsi volume %s [%s] to %s, error %v", devicePath, b.fsType, globalPDPath, err) } diff --git a/pkg/volume/rbd/rbd.go b/pkg/volume/rbd/rbd.go index 9435da4b90..78d628b291 100644 --- a/pkg/volume/rbd/rbd.go +++ b/pkg/volume/rbd/rbd.go @@ -154,7 +154,7 @@ func (plugin *rbdPlugin) newCleanerInternal(volName string, podUID types.UID, ma podUID: podUID, volName: volName, manager: manager, - mounter: mounter, + mounter: &mount.SafeFormatAndMount{mounter, exec.New()}, plugin: plugin, }, Mon: make([]string, 0), @@ -169,7 +169,7 @@ type rbd struct { Image string ReadOnly bool plugin *rbdPlugin - mounter mount.Interface + mounter *mount.SafeFormatAndMount // Utility interface that provides API calls to the provider to attach/detach disks. manager diskManager } diff --git a/pkg/volume/rbd/rbd_util.go b/pkg/volume/rbd/rbd_util.go index bd315d065d..28169b7fc7 100644 --- a/pkg/volume/rbd/rbd_util.go +++ b/pkg/volume/rbd/rbd_util.go @@ -273,7 +273,7 @@ func (util *RBDUtil) AttachDisk(b rbdBuilder) error { // the json file remains invisible during rbd mount and thus won't be removed accidentally. util.persistRBD(b, globalPDPath) - if err = b.mounter.Mount(devicePath, globalPDPath, b.fsType, nil); err != nil { + if err = b.mounter.FormatAndMount(devicePath, globalPDPath, b.fsType, nil); err != nil { err = fmt.Errorf("rbd: failed to mount rbd volume %s [%s] to %s, error %v", devicePath, b.fsType, globalPDPath, err) } return err