Skip to content

Commit

Permalink
Merge pull request #196 from saito-hideki/issue/28
Browse files Browse the repository at this point in the history
Modify boot option handling on Linux systems

Reviewed-by: https://github.com/apps/ansible-zuul
  • Loading branch information
ansible-zuul[bot] authored Jun 4, 2021
2 parents 1793cd7 + cfff8a3 commit 9d4ae8b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
4 changes: 4 additions & 0 deletions changelogs/fragments/196_boot_opt_for_linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
minor_changes:
- mount - Change behavior of ``boot`` option to set ``noauto`` on Linux nodes
(https://github.com/ansible-collections/ansible.posix/issues/28).
42 changes: 35 additions & 7 deletions plugins/modules/mount.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@
boot:
description:
- Determines if the filesystem should be mounted on boot.
- Only applies to Solaris systems.
- Only applies to Solaris and Linux systems.
- For Solaris systems, C(true) will set C(yes) as the value of mount at boot
in I(/etc/vfstab).
- For Linux systems, C(true) will add C(noauto) to mount options in I(/etc/fstab).
type: bool
default: yes
backup:
Expand Down Expand Up @@ -169,6 +172,15 @@
opts: rw,sync,hard,intr
state: mounted
fstype: nfs
- name: Mount NFS volumes with noauto according to boot option
ansible.posix.mount:
src: 192.168.1.100:/nfs/ssd/shared_data
path: /mnt/shared_data
opts: rw,sync,hard,intr
boot: no
state: mounted
fstype: nfs
'''


Expand Down Expand Up @@ -227,7 +239,7 @@ def _set_mount_save_old(module, args):
old_lines = []
exists = False
changed = False
escaped_args = dict([(k, _escape_fstab(v)) for k, v in iteritems(args)])
escaped_args = dict([(k, _escape_fstab(v)) for k, v in iteritems(args) if k != 'warnings'])
new_line = '%(src)s %(name)s %(fstype)s %(opts)s %(dump)s %(passno)s\n'

if platform.system() == 'SunOS':
Expand Down Expand Up @@ -673,7 +685,8 @@ def main():
opts='-',
passno='-',
fstab=module.params['fstab'],
boot='yes' if module.params['boot'] else 'no'
boot='yes' if module.params['boot'] else 'no',
warnings=[]
)
if args['fstab'] is None:
args['fstab'] = '/etc/vfstab'
Expand All @@ -683,7 +696,9 @@ def main():
opts='defaults',
dump='0',
passno='0',
fstab=module.params['fstab']
fstab=module.params['fstab'],
boot='yes',
warnings=[]
)
if args['fstab'] is None:
args['fstab'] = '/etc/fstab'
Expand All @@ -700,14 +715,27 @@ def main():
linux_mounts = get_linux_mounts(module)

if linux_mounts is None:
args['warnings'] = (
'Cannot open file /proc/self/mountinfo. '
'Bind mounts might be misinterpreted.')
args['warnings'].append('Cannot open file /proc/self/mountinfo.'
' Bind mounts might be misinterpreted.')

# Override defaults with user specified params
for key in ('src', 'fstype', 'passno', 'opts', 'dump', 'fstab'):
if module.params[key] is not None:
args[key] = module.params[key]
if platform.system().lower() == 'linux':
# Linux has 'noauto' as mount opts to handle mount on boot
# So boot option should manage 'noauto' in opts
# TODO: We need to support other system like *BSD that 'noauto' option available
opts = args['opts'].split(',')
if 'noauto' in opts:
args['warnings'].append("Ignore the 'boot' due to 'opts' contains 'noauto'.")
elif not module.params['boot']:
args['boot'] = 'no'
if 'defaults' in opts:
args['warnings'].append("Ignore the 'boot' due to 'opts' contains 'defaults'.")
else:
opts.append('noauto')
args['opts'] = ','.join(opts)

# If fstab file does not exist, we first need to create it. This mainly
# happens when fstab option is passed to the module.
Expand Down
42 changes: 40 additions & 2 deletions tests/integration/targets/mount/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,13 @@
- name: Block to test remounted option
block:
- name: Create empty file
command: dd if=/dev/zero of=/tmp/myfs.img bs=1048576 count=20
community.general.filesize:
path: /tmp/myfs.img
size: 20M
when: ansible_system in ('Linux')
- name: Format FS
when: ansible_system in ('Linux')
community.general.system.filesystem:
community.general.filesystem:
fstype: ext3
dev: /tmp/myfs.img
- name: Mount the FS for the first time
Expand Down Expand Up @@ -294,3 +296,39 @@
- /tmp/myfs.img
- /tmp/myfs
when: ansible_system in ('Linux')

- name: Block to test boot option for Linux
block:
- name: Create empty file
community.general.filesize:
path: /tmp/myfs.img
size: 20M
- name: Format FS
community.general.filesystem:
fstype: ext3
dev: /tmp/myfs.img
- name: Mount the FS with noauto option
mount:
path: /tmp/myfs
src: /tmp/myfs.img
fstype: ext3
state: mounted
boot: no
opts: rw,user,async
register: mount_info
- name: assert the mount without noauto was successful
assert:
that:
- mount_info['opts'] == 'rw,user,async,noauto'
- name: Unmount FS
mount:
path: /tmp/myfs
state: absent
- name: Remove the test FS
file:
path: '{{ item }}'
state: absent
loop:
- /tmp/myfs.img
- /tmp/myfs
when: ansible_system in ('Linux')

0 comments on commit 9d4ae8b

Please sign in to comment.