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

fix(fs/NfsHandler): race conds on sync() #3460

Merged
merged 5 commits into from Sep 28, 2018
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
78 changes: 32 additions & 46 deletions @xen-orchestra/fs/src/nfs.js
@@ -1,6 +1,5 @@
import execa from 'execa'
import fs from 'fs-extra'
import { forEach } from 'lodash'

import LocalHandler from './local'

Expand All @@ -18,54 +17,33 @@ export default class NfsHandler extends LocalHandler {
async _mount () {
await fs.ensureDir(this._getRealPath())
const { host, path, port, options } = this._remote
return execa('mount', [
'-t',
'nfs',
'-o',
DEFAULT_NFS_OPTIONS + (options !== undefined ? `,${options}` : ''),
`${host}${port !== undefined ? ':' + port : ''}:${path}`,
this._getRealPath(),
])
}

async _sync () {
const mounts = {}
try {
const stdout = await execa.stdout('findmnt', [
'-P',
return execa(
'mount',
[
'-t',
'nfs,nfs4',
'--output',
'SOURCE,TARGET',
'--noheadings',
])
const regex = /^SOURCE="([^:]*):(.*)" TARGET="(.*)"$/
forEach(stdout.split('\n'), m => {
if (m) {
const match = regex.exec(m)
mounts[match[3]] = {
host: match[1],
share: match[2],
}
}
})
} catch (exc) {
// When no mounts are found, the call pretends to fail...
if (exc.stderr !== '') {
throw exc
'nfs',
'-o',
DEFAULT_NFS_OPTIONS + (options !== undefined ? `,${options}` : ''),
`${host}${port !== undefined ? ':' + port : ''}:${path}`,
this._getRealPath(),
],
{
env: {
LANG: 'C',
},
}
}

const isMounted = this._getRealPath() in mounts
const shouldBeMounted = this._remote.enabled
if (isMounted) {
if (!shouldBeMounted) {
await this._umount()
).catch(error => {
if (!error.stderr.includes('already mounted')) {
throw error
}
})
}

async _sync () {
if (this._remote.enabled) {
await this._mount()
} else {
if (shouldBeMounted) {
await this._mount()
}
await this._umount()
}

return this._remote
Expand All @@ -80,6 +58,14 @@ export default class NfsHandler extends LocalHandler {
}

async _umount () {
await execa('umount', ['--force', this._getRealPath()])
await execa('umount', ['--force', this._getRealPath()], {
env: {
LANG: 'C',
},
}).catch(error => {
if (!error.stderr.includes('not mounted')) {
throw error
}
})
}
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,9 +10,11 @@
[File restore] Fix a path issue when going back to the parent folder (PR [#3446](https://github.com/vatesfr/xen-orchestra/pull/3446))
[File restore] Fix a minor issue when showing which selected files are redundant (PR [#3447](https://github.com/vatesfr/xen-orchestra/pull/3447))
[Memory] Fix a major leak [#2580](https://github.com/vatesfr/xen-orchestra/issues/2580) [#2820](https://github.com/vatesfr/xen-orchestra/issues/2820) (PR [#3453](https://github.com/vatesfr/xen-orchestra/pull/3453))
[NFS Remotes] Fix `already mounted` race condition [#3380](https://github.com/vatesfr/xen-orchestra/issues/3380) (PR [#3460](https://github.com/vatesfr/xen-orchestra/pull/3460))

### Released packages

- @xen-orchestra/fs v0.3.1
- vhd-lib v0.3.1
- xo-vmdk-to-vhd v0.1.4
- xo-server v5.28.0
Expand Down