Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move formatAndMount and diskLooksUnformatted to mount_linux #16948

Merged
merged 1 commit into from Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 0 additions & 50 deletions pkg/util/mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ limitations under the License.
package mount

import (
"strings"

"github.com/golang/glog"
"k8s.io/kubernetes/pkg/util/exec"
)
Expand Down Expand Up @@ -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{}
Expand Down
48 changes: 48 additions & 0 deletions pkg/util/mount/mount_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions pkg/util/mount/mount_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}