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,7 +98,10 @@ def write_fstab(lines, dest):
def _escape_fstab(v): def _escape_fstab(v):
""" escape space (040), ampersand (046) and backslash (134) which are invalid in fstab fields """ """ escape space (040), ampersand (046) and backslash (134) which are invalid in fstab fields """
return v.replace('\\', '\\134').replace(' ', '\\040').replace('&', '\\046') if isinstance(v, int):
return v
else:
return v.replace('\\', '\\134').replace(' ', '\\040').replace('&', '\\046')
def set_mount(module, **kwargs): def set_mount(module, **kwargs):
""" set/change a mount point location in fstab """ """ set/change a mount point location in fstab """
@ -112,11 +115,6 @@ def set_mount(module, **kwargs):
) )
args.update(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' new_line = '%(src)s %(name)s %(fstype)s %(opts)s %(dump)s %(passno)s\n'
to_write = [] to_write = []
@ -156,14 +154,13 @@ def set_mount(module, **kwargs):
to_write.append(line) to_write.append(line)
if not exists: if not exists:
to_write.append(new_line % args) to_write.append(new_line % escaped_args)
changed = True changed = True
if changed and not module.check_mode: if changed and not module.check_mode:
write_fstab(to_write, args['fstab']) write_fstab(to_write, args['fstab'])
# mount function needs origname return (args['name'], changed)
return (origname, changed)
def unset_mount(module, **kwargs): def unset_mount(module, **kwargs):
@ -178,11 +175,6 @@ def unset_mount(module, **kwargs):
) )
args.update(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 = [] to_write = []
changed = False changed = False
escaped_name = _escape_fstab(args['name']) escaped_name = _escape_fstab(args['name'])
@ -212,8 +204,7 @@ def unset_mount(module, **kwargs):
if changed and not module.check_mode: if changed and not module.check_mode:
write_fstab(to_write, args['fstab']) write_fstab(to_write, args['fstab'])
# umount needs origname return (args['name'], changed)
return (origname, changed)
def mount(module, **kwargs): def mount(module, **kwargs):

Loading…
Cancel
Save