Escape spaces, backslashes when create new entry in fstab (#3193)

Supplement to PR #1454 and issue #530.

Fixes #3192.
Fixes #1861 (introduced by PR #1454).

Signed-off-by: Konstantin Gribov <grossws@gmail.com>
reviewable/pr18780/r1
Konstantin Gribov 9 years ago committed by Brian Coca
parent 37a837cd91
commit ab53adad4b

@ -98,6 +98,9 @@ def write_fstab(lines, dest):
def _escape_fstab(v):
""" escape space (040), ampersand (046) and backslash (134) which are invalid in fstab fields """
if isinstance(v, int):
return v
else:
return v.replace('\\', '\\134').replace(' ', '\\040').replace('&', '\\046')
def set_mount(module, **kwargs):
@ -112,11 +115,6 @@ def set_mount(module, **kwargs):
)
args.update(kwargs)
# save the mount name before space replacement
origname = args['name']
# replace any space in mount name with '\040' to make it fstab compatible (man fstab)
args['name'] = args['name'].replace(' ', r'\040')
new_line = '%(src)s %(name)s %(fstype)s %(opts)s %(dump)s %(passno)s\n'
to_write = []
@ -156,14 +154,13 @@ def set_mount(module, **kwargs):
to_write.append(line)
if not exists:
to_write.append(new_line % args)
to_write.append(new_line % escaped_args)
changed = True
if changed and not module.check_mode:
write_fstab(to_write, args['fstab'])
# mount function needs origname
return (origname, changed)
return (args['name'], changed)
def unset_mount(module, **kwargs):
@ -178,11 +175,6 @@ def unset_mount(module, **kwargs):
)
args.update(kwargs)
# save the mount name before space replacement
origname = args['name']
# replace any space in mount name with '\040' to make it fstab compatible (man fstab)
args['name'] = args['name'].replace(' ', r'\040')
to_write = []
changed = False
escaped_name = _escape_fstab(args['name'])
@ -212,8 +204,7 @@ def unset_mount(module, **kwargs):
if changed and not module.check_mode:
write_fstab(to_write, args['fstab'])
# umount needs origname
return (origname, changed)
return (args['name'], changed)
def mount(module, **kwargs):

Loading…
Cancel
Save