fix a bunch of small bugs in mount module - test with bind and local mounts

This commit is contained in:
Seth Vidal 2012-07-12 01:16:00 -04:00
parent fcfd73de71
commit 634c11748e

View file

@ -56,7 +56,7 @@ def write_fstab(lines, dest):
fs_w.flush()
fs_w.close()
def set_mount(kwargs):
def set_mount(**kwargs):
"set/change a mount point location in fstab"
# kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
args = { 'opts':'defaults',
@ -65,7 +65,7 @@ def set_mount(kwargs):
'fstab':'/etc/fstab' }
args.update(kwargs)
new_line = '%{src}s %{name}s %{fstype}s %{opts}s %{dump}s %{passno}s\n'
new_line = '%(src)s %(name)s %(fstype)s %(opts)s %(dump)s %(passno)s\n'
to_write = []
exists = False
@ -95,7 +95,7 @@ def set_mount(kwargs):
for t in ('src', 'fstype','opts', 'dump', 'passno'):
if ld[t] != args[t]:
changed = True
ld[t] == args[t]
ld[t] = args[t]
if changed:
to_write.append(new_line % ld)
@ -113,8 +113,8 @@ def set_mount(kwargs):
return (args['name'], changed)
def unset_mount(kwargs):
"remount a mount point in fstab"
def unset_mount(**kwargs):
"remove a mount point from fstab"
# kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
args = { 'opts':'default',
'dump':'0',
@ -153,7 +153,7 @@ def unset_mount(kwargs):
return (args['name'], changed)
def mount(kwargs):
def mount(**kwargs):
"mount up a path or remount if needed"
name = kwargs['name']
if os.path.ismount(name):
@ -163,18 +163,18 @@ def mount(kwargs):
call = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = call.communicate()
if call.rc == 0:
if call.returncode == 0:
return 0, ''
else:
return call.rc, out+err
def umount(kwargs):
def umount(**kwargs):
"unmount a path"
cmd = ['/bin/umount', name]
call = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = call.communicate()
if call.rc == 0:
if call.returncode == 0:
return 0, ''
else:
return call.rc, out+err
@ -191,7 +191,7 @@ if not len(items):
params = {}
for x in items:
(k, v) = x.split("=")
(k, v) = x.split("=",1)
params[k] = v
state = params.get('state',None)
@ -201,7 +201,7 @@ passno = params.get('passno', None)
dump = params.get('dump', None)
src = params.get('src', None)
fstype = params.get('fstype', None)
fstab = params.get('fstab', None)
if state not in [ 'present', 'absent', 'mounted', 'unmounted' ]:
@ -228,6 +228,8 @@ if opts is not None:
args['opts'] = opts
if dump is not None:
args['dump'] = dump
if fstab is not None:
args['fstab'] = fstab
# absent == remove from fstab and unmounted
@ -240,7 +242,7 @@ if state == 'absent':
name, changed = unset_mount(**args)
if changed:
if os.path.ismount(name):
res,msg = umount(name)
res,msg = umount(**args)
if res:
fail_json(msg="Error unmounting %s: %s" % (name, msg))
@ -255,7 +257,7 @@ if state == 'absent':
if state == 'unmounted':
if os.path.ismount(name):
res,msg = umount(name)
res,msg = umount(**args)
if res:
fail_json(msg="Error unmounting %s: %s" % (name, msg))
changed = True
@ -276,10 +278,10 @@ if state in ['mounted', 'present']:
res = 0
if os.path.ismount(name):
if changed:
res,msg = mount(name)
res,msg = mount(**args)
else:
changed = True
res,msg = mount(name)
res,msg = mount(**args)
if res:
fail_json(msg="Error mounting %s: %s" % (name, msg))