diff --git a/chapi/chapidriver_linux.go b/chapi/chapidriver_linux.go index fbcc13c..96491c7 100644 --- a/chapi/chapidriver_linux.go +++ b/chapi/chapidriver_linux.go @@ -267,6 +267,9 @@ func (driver *LinuxDriver) MountDevice(device *model.Device, mountPoint string, log.Tracef("Filesystem %+v setup successful,", fsOpts) } + // Check filesystem consistency + err := linux.CheckFileSystem(device.AltFullPathName) + // Setup mountpoint (Create mountpoint and apply mount options) mount, err := linux.SetupMount(device, mountPoint, mountOptions) if err != nil { diff --git a/linux/mount.go b/linux/mount.go index c08441e..f5394cd 100644 --- a/linux/mount.go +++ b/linux/mount.go @@ -23,6 +23,7 @@ var ( updateDbConf = "/etc/updatedb.conf" prunePathsPattern = "^PRUNEPATHS\\s*=\\s*\"(?P.*)\"" defaultFSCreateTimeout = 300 /* 5 minutes */ + defaultFSCheckTimeout = 600 /* 10 minutes */ lsof = "lsof" blkid = "blkid" errCurrentlyMounted = "is currently mounted" @@ -76,6 +77,8 @@ const ( fsext4command = "mkfs.ext4" fsbtrfscommand = "mkfs.btrfs" defaultNFSType = "nfs4" + fsckcommand = "fsck" + fsckoptions = "-a" ) // HashMountID : get hash of the string @@ -325,6 +328,25 @@ func RetryCreateFileSystemWithOptions(devPath string, fsType string, options []s } } +// CheckFileSystem: checks file system on the device for errors +func CheckFileSystem(devPath string) (err error) { + log.Tracef("checkFileSystem called with %s", devPath) + return checkFileSystem(devPath) +} + +func checkFileSystem(devPath string) (err error) { + var output string + options := []string{fsckoptions, devPath} + output, _, err = util.ExecCommandOutputWithTimeout(fsckcommand, options, defaultFSCheckTimeout) + if err != nil { + return fmt.Errorf("unable to check filesystem using %s %s. Error: %s", fsckcommand, options, err.Error()) + } + if output == "" { + return fmt.Errorf("filesystem not checked using %s %s", fsckcommand, options) + } + return nil +} + // CreateFileSystem : creates file system on the device func CreateFileSystem(devPath string, fsType string) (err error) { log.Tracef("createFileSystem called with %s %s", fsType, devPath) diff --git a/util/cmd.go b/util/cmd.go index 9a64eac..8b968b3 100644 --- a/util/cmd.go +++ b/util/cmd.go @@ -82,7 +82,10 @@ func execCommandOutputWithTimeout(cmd string, args []string, stdinArgs []string, // ExecCommandOutputWithTimeout executes ExecCommandOutput with the specified timeout func ExecCommandOutputWithTimeout(cmd string, args []string, timeout int) (string, int, error) { - return execCommandOutputWithTimeout(cmd, args, []string{}, defaultTimeout) + if timeout == 0 { + timeout = defaultTimeout + } + return execCommandOutputWithTimeout(cmd, args, []string{}, timeout) } // ExecCommandOutput returns stdout and stderr in a single string, the return code, and error. // If the return code is not zero, error will not be nil.