Skip to content

Commit 7abf9d4

Browse files
author
satken2
committed
Add umask option for mount module
1 parent d20b629 commit 7abf9d4

File tree

2 files changed

+34
-95
lines changed

2 files changed

+34
-95
lines changed

plugins/modules/mount.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
default: no
108108
umask:
109109
description:
110-
- The permission applied to create new directory(ies) for the mount point.
110+
- The umask to set before creating new directory(ies) for the mount point.
111111
If the mount point already exists, this parameter is not used.
112112
- Note that after running this task and the device being successfully mounted,
113113
the mode of the original directory will be hidden by the target device.
@@ -801,8 +801,18 @@ def main():
801801

802802
changed = True
803803
elif state == 'mounted':
804+
804805
dirs_created = []
805806
if not os.path.exists(name) and not module.check_mode:
807+
if umask is not None:
808+
if not isinstance(umask, int):
809+
try:
810+
umask = int(umask, 8)
811+
except ValueError as e:
812+
module.fail_json(msg="umask must be an octal integer: %s" % (to_native(e)))
813+
old_umask = os.umask(umask)
814+
os.umask(umask)
815+
806816
try:
807817
# Something like mkdir -p but with the possibility to undo.
808818
# Based on some copy-paste from the "file" module.
@@ -827,31 +837,9 @@ def main():
827837
except (OSError, IOError) as e:
828838
module.fail_json(
829839
msg="Error making dir %s: %s" % (name, to_native(e)))
830-
831-
# Set permissions to the newly created mount point.
832-
if umask is not None:
833-
# When umask is integer, calculate logical complement of the value
834-
# otherwise, pass it to set_mode_if_different() as is.
835-
if isinstance(umask, int):
836-
directory_mode = 0o0777 & ~umask
837-
else:
838-
try:
839-
umask = int(umask, 8)
840-
directory_mode = 0o0777 & ~umask
841-
except Exception:
842-
directory_mode = umask
843-
844-
try:
845-
for dirname in dirs_created:
846-
changed = module.set_mode_if_different(dirname, directory_mode, changed)
847-
except Exception as e:
848-
try:
849-
for dirname in dirs_created[::-1]:
850-
os.rmdir(dirname)
851-
except Exception:
852-
pass
853-
module.fail_json(
854-
msg="Error setting permissions %s: %s" % (name, to_native(e)))
840+
finally:
841+
if old_umask is not None:
842+
os.umask(old_umask)
855843

856844
name, backup_lines, changed = _set_mount_save_old(module, args)
857845
res = 0

tests/integration/targets/mount/tasks/main.yml

Lines changed: 20 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -335,106 +335,57 @@
335335

336336
- name: Block to test umask option
337337
block:
338-
- name: Create empty file
339-
community.general.filesize:
340-
path: /tmp/myfs2.img
341-
size: 20M
342-
- name: Format FS
343-
community.general.filesystem:
344-
fstype: ext2
345-
dev: /tmp/myfs2.img
346338
- name: Make sure that mount point does not exist
347339
file:
348-
path: /tmp/myfs2_mountpoint
340+
path: /tmp/mount_dest
349341
state: absent
350-
- name: Mount the FS to non existent directory with raw umask
342+
- name: Create a directory to bind mount
343+
file:
344+
state: directory
345+
path: /tmp/mount_source
346+
- name: Bind mount a filesystem (Linux)
351347
mount:
352-
path: /tmp/myfs2_mountpoint
353-
src: /tmp/myfs2.img
354-
fstype: ext2
348+
src: /tmp/mount_source
349+
name: /tmp/mount_dest
355350
state: mounted
351+
fstype: None
352+
opts: bind
356353
umask: 0777
357354
- name: Unmount FS to access underlying directory
358355
command: |
359-
umount /tmp/myfs2.img
356+
umount /tmp/mount_dest
360357
- name: Stat mount point directory
361358
stat:
362-
path: /tmp/myfs2_mountpoint
359+
path: /tmp/mount_dest
363360
register: mount_point_stat
364361
- name: Assert that the mount point has right permission
365362
assert:
366363
that:
367364
- mount_point_stat['stat']['mode'] == '0000'
368365
- name: Cleanup directory
369366
file:
370-
path: /tmp/myfs2_mountpoint
367+
path: /tmp/mount_dest
371368
state: absent
372369
- name: Mount the FS to non existent directory with string umask
373370
mount:
374-
path: /tmp/myfs2_mountpoint
375-
src: /tmp/myfs2.img
376-
fstype: ext2
371+
src: /tmp/mount_source
372+
name: /tmp/mount_dest
377373
state: mounted
374+
fstype: None
375+
opts: bind
378376
umask: "0777"
379377
- name: Unmount FS to access underlying directory
380378
command: |
381-
umount /tmp/myfs2.img
379+
umount /tmp/mount_dest
382380
- name: Stat mount point directory
383381
stat:
384-
path: /tmp/myfs2_mountpoint
382+
path: /tmp/mount_dest
385383
register: mount_point_stat
386384
- name: Assert that the mount point has right permission
387385
assert:
388386
that:
389387
- mount_point_stat['stat']['mode'] == '0000'
390-
- name: Cleanup directory
391-
file:
392-
path: /tmp/myfs2_mountpoint
393-
state: absent
394-
- name: Remount the FS to non existent directory with symbolic umask expression
395-
mount:
396-
path: /tmp/myfs2_mountpoint
397-
src: /tmp/myfs2.img
398-
fstype: ext2
399-
state: mounted
400-
umask: "u+rw,g-wx,o-rwx"
401-
- name: Unmount FS to access underlying directory
402-
command: |
403-
umount /tmp/myfs2.img
404-
- name: Stat mount point directory
405-
stat:
406-
path: /tmp/myfs2_mountpoint
407-
register: mount_point_stat
408-
- name: Assert that the mount point has right permission
409-
assert:
410-
that:
411-
- mount_point_stat['stat']['mode'] == '0640'
412-
- name: Cleanup directory
413-
file:
414-
path: /tmp/myfs2_mountpoint
415-
state: absent
416-
- name: Remount the FS to non existent directory with symbolic umask expression
417-
mount:
418-
path: /tmp/myfs2_mountpoint
419-
src: /tmp/myfs2.img
420-
fstype: ext2
421-
state: mounted
422-
umask: "u=rw,g=r,o=r"
423-
- name: Unmount FS to access underlying directory
424-
command: |
425-
umount /tmp/myfs2.img
426-
- name: Stat mount point directory
427-
stat:
428-
path: /tmp/myfs2_mountpoint
429-
register: mount_point_stat
430-
- name: Assert that the mount point has right permission
431-
assert:
432-
that:
433-
- mount_point_stat['stat']['mode'] == '0644'
434388
- name: Remove the test FS
435389
file:
436-
path: '{{ item }}'
390+
path: /tmp/mount_dest
437391
state: absent
438-
loop:
439-
- /tmp/myfs2.img
440-
- /tmp/myfs2_mountpoint

0 commit comments

Comments
 (0)