mirror of https://github.com/k3s-io/k3s
return a more human readable error message if mount an unformatted volume as readonly
parent
84bfc7ada5
commit
2f46df3540
|
@ -125,12 +125,6 @@ type SafeFormatAndMount struct {
|
|||
// 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" {
|
||||
return mounter.Interface.Mount(source, target, fstype, options)
|
||||
}
|
||||
}
|
||||
return mounter.formatAndMount(source, target, fstype, options)
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ limitations under the License.
|
|||
package mount
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
@ -472,9 +473,18 @@ func (mounter *Mounter) ExistsPath(pathname string) bool {
|
|||
|
||||
// formatAndMount uses unix utils to format and mount the given disk
|
||||
func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, fstype string, options []string) error {
|
||||
readOnly := false
|
||||
for _, option := range options {
|
||||
if option == "ro" {
|
||||
readOnly = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
options = append(options, "defaults")
|
||||
|
||||
// Run fsck on the disk to fix repairable issues
|
||||
if !readOnly {
|
||||
// Run fsck on the disk to fix repairable issues, only do this for volumes requested as rw.
|
||||
glog.V(4).Infof("Checking for issues with fsck on disk: %s", source)
|
||||
args := []string{"-a", source}
|
||||
out, err := mounter.Exec.Run("fsck", args...)
|
||||
|
@ -491,6 +501,7 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string,
|
|||
glog.Infof("`fsck` error %s", string(out))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Try to mount the disk
|
||||
glog.V(4).Infof("Attempting to mount disk: %s %s %s", fstype, source, target)
|
||||
|
@ -503,8 +514,13 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string,
|
|||
return err
|
||||
}
|
||||
if existingFormat == "" {
|
||||
if readOnly {
|
||||
// Don't attempt to format if mounting as readonly, return an error to reflect this.
|
||||
return errors.New("failed to mount unformatted volume as read only")
|
||||
}
|
||||
|
||||
// Disk is unformatted so format it.
|
||||
args = []string{source}
|
||||
args := []string{source}
|
||||
// Use 'ext4' as the default
|
||||
if len(fstype) == 0 {
|
||||
fstype = "ext4"
|
||||
|
|
Loading…
Reference in New Issue