salt icon indicating copy to clipboard operation
salt copied to clipboard

Mount state does not support multiple tmpfs entries in fstab.

Open dumol opened this issue 12 years ago • 16 comments

Suppose I want to set up two tmpfs entries in /etc/fstab through Salt. I have tried the following:

/tmp:
  mount.mounted:
    - device: tmpfs
    - fstype: nodev,nosuid
    - mount: False

/srv/buildslave/.cache:
  mount.mounted:
    - device: tmpfs
    - fstype: nodev,nosuid
    - mkmnt: True
    - mount: True

But this added in /etc/fstab on the minion only the following line:

tmpfs       /srv/buildslave/.cache  nodev,nosuid    defaults    0 0

Obviously, if I put /tmp after /srv/buildslave/.cache in my state, I get:

tmpfs       /tmp    nodev,nosuid    defaults    0 0

There doesn't seem to be room for two entries with the same device field as it would be the case for multiple tmpfs entries in /etc/fstab.

In case it helps, here's the debug log for the second scenario:

2014-11-05 20:54:54,637 [salt.state       ][INFO    ] Executing state mount.mounted for /srv/buildslave/.cache
2014-11-05 20:54:54,639 [salt.loaded.int.module.cmdmod][INFO    ] Executing command 'mount -l' in directory '/root'
2014-11-05 20:54:54,653 [salt.loaded.int.module.cmdmod][INFO    ] stdout: /dev/sda1 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
/dev/sda7 on /srv type ext4 (rw)
tmpfs on /srv/buildslave/.cache type tmpfs (rw,noexec,nosuid,nodev)
rpc_pipefs on /run/rpc_pipefs type rpc_pipefs (rw)
nfs.chevah.com:/srv/nfs on /nfs type nfs (rw,vers=4,addr=172.20.1.13,clientaddr=172.20.0.20)
gvfs-fuse-daemon on /srv/buildslave/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=buildslave)
2014-11-05 20:54:54,656 [salt.state       ][INFO    ] {'persist': 'new'}
2014-11-05 20:54:54,656 [salt.state       ][INFO    ] Completed state [/srv/buildslave/.cache] at time 20:54:54.656139
2014-11-05 20:54:54,657 [salt.state       ][INFO    ] Running state [/tmp] at time 20:54:54.656479
2014-11-05 20:54:54,657 [salt.state       ][INFO    ] Executing state mount.mounted for /tmp
2014-11-05 20:54:54,658 [salt.loaded.int.module.cmdmod][INFO    ] Executing command 'mount -l' in directory '/root'
2014-11-05 20:54:54,673 [salt.loaded.int.module.cmdmod][INFO    ] stdout: /dev/sda1 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
/dev/sda7 on /srv type ext4 (rw)
tmpfs on /srv/buildslave/.cache type tmpfs (rw,noexec,nosuid,nodev)
rpc_pipefs on /run/rpc_pipefs type rpc_pipefs (rw)
nfs.chevah.com:/srv/nfs on /nfs type nfs (rw,vers=4,addr=172.20.1.13,clientaddr=172.20.0.20)
gvfs-fuse-daemon on /srv/buildslave/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=buildslave)
2014-11-05 20:54:54,675 [salt.loaded.int.module.mount][DEBUG   ] fstab entry for mount point /tmp needs to be updated
2014-11-05 20:54:54,676 [salt.state       ][INFO    ] {'persist': 'update'}
2014-11-05 20:54:54,676 [salt.state       ][INFO    ] Completed state [/tmp] at time 20:54:54.676053

On both master and minion we use Salt 2014.1.13 from the official PPA for Ubuntu 12.04:

# salt --versions-report
           Salt: 2014.1.13
         Python: 2.7.3 (default, Feb 27 2014, 20:00:17)
         Jinja2: 2.6
       M2Crypto: 0.21.1
 msgpack-python: 0.1.10
   msgpack-pure: Not Installed
       pycrypto: 2.4.1
         PyYAML: 3.10
          PyZMQ: 13.0.0
            ZMQ: 3.2.2

Thanks!

dumol avatar Nov 05 '14 19:11 dumol

Thanks for the report @dumol! We'll take a look at this.

rallytime avatar Nov 06 '14 17:11 rallytime

This is confirmed on 2014.1, 2014.7, and develop, but your state looks wrong to me. I believe that you need to specify tmpfs for fstype (in addition to device) and nodev,nosuid for opts. Here's my test case:

centos-7-main ~ master # cat /srv/salt/tmpfs.sls
/mnt/a:
  mount.mounted:
    - device: tmpfs
    - fstype: tmpfs
    - opts: nodev,nosuid
    - mount: False
/mnt/b:
  mount.mounted:
    - device: tmpfs
    - fstype: tmpfs
    - opts: nodev,nosuid
    - mkmnt: True
    - mount: True
centos-7-main ~ master # mkdir /mnt/a /mnt/b
centos-7-main ~ master # salt '*' state.sls tmpfs
centos-7-main:
----------
          ID: /mnt/a
    Function: mount.mounted
      Result: True
     Comment: Target was successfully mounted. Added new entry to the fstab.
     Changes:   
              ----------
              mount:
                  True
              persist:
                  new
----------
          ID: /mnt/b
    Function: mount.mounted
      Result: True
     Comment: Target was successfully mounted. Updated the entry in the fstab.
     Changes:   
              ----------
              mount:
                  True
              persist:
                  update

Summary
------------
Succeeded: 2
Failed:    0
------------
Total:     2
centos-7-main ~ master # cat /etc/fstab 
/dev/mapper/centos-root /                       xfs     defaults        1 1
UUID=5afd94bb-ea31-451b-8a51-20fcedc175e7 /boot                   xfs     defaults        1 2
/dev/mapper/centos-swap swap                    swap    defaults        0 0
tmpfs           /mnt/b  tmpfs   nodev,nosuid    0 0
centos-7-main ~ master # 

jfindlay avatar Nov 10 '14 22:11 jfindlay

Looking at the code, states/mount.mounted uses modules/mount.set_fstab to modify fstab. How does set_fstab match fs_tab entries? Let's look:

   with salt.utils.fopen(config, 'r') as ifile:
        for line in ifile:
            ....
            comps = line.split()
            ....
            if comps[0] == device:
                # check to see if there are changes
                # and fix them if there are any
                ....

Yes...that just happened:( To be fair, device is a decent choice for a primary key for traditional devices, as most devices don't support multi-mounting. Perhaps we should allow users to specify primary key(s) and also make some intelligent default choices for non-block-device-backed file systems?

    def __match_fstab_line( line, criteria ):
            '''Compare line components against a list of (index, value) match criteria'''
            for (index, value) in criteria:
                     if line[ index ] != value:
                            return False
            return True

    def set_fstab(
            name,
            device,
            fstype,
            opts='defaults',
            dump=0,
            pass_num=0,
            config='/etc/fstab',
            test=False,
            match_on = 'auto'
            **kwargs):

            # Build match criteria list
            fstab_indices = {
                    'device': 0,
                    'name': 1,
                    'fstype': 2,
                    'opts': 3,
                    'dump': 4,
                    'pass_num': 5
            }

            # Detect if match_on is a list of criteria, a known keyword, or an error
            if isinstance( match_on, list ):
                      pass
            elif isinstance( match_on, string_type ) and match_on == 'auto':
                     if fstype in [ 'tmpfs', 'procfs', 'sysfs' ]:
                            match_on = ['name']
                     else
                            match_on = [ 'device' ]
            elif isinstance( match_on, string_type ) and match_on in fstab_indices:
                    match_on = [ match_on ]
            else:
                    #FIXME! raise error for invalid argument ( not list or recognized keyword )

            # Build match criteria as list of (index,value) tuples
            try:
                    match_criteria = map( lambda key: ( fstab_indices[ key ], kwargs[ key ] ), match_on )
            except KeyError:
                    #FIXME! raise error for invalid key in match_on list
            ....
            with salt.utils.fopen(config, 'r') as ifile:
                for line in ifile:
                    ....
                    comps = line.split()
                    ....
                    if __match_fstab_line( comps, match_criteria ):
                        # check to see if there are changes
                        # and fix them if there are any
                        ....

Naturally, we'd have to propagate match_on to most state/mount and module/mount methods, but it'd allow the user to be as selective as they want:

      /mnt/a:
        mount.mounted:
          - device: tmpfs
          - fstype: tmpfs
          - opts: nodev,nosuid
          - mount: False
          - match_on: 
             - 'name'
             - 'pass_num'

      /mnt/b:
        mount.mounted:
          - device: tmpfs
          - fstype: tmpfs
          - opts: nodev,nosuid
          - mount: False
          - match_on: 'name'

Everybody wins?

AlexanderHaase avatar Nov 21 '14 17:11 AlexanderHaase

@AlexanderHaase, your proposed solution looks good to me. Please feel free to submit a pull request to implement it. If not, whoever fixes this bug should address the great points you've brought up. Thanks.

jfindlay avatar Nov 21 '14 19:11 jfindlay

That would be fine by me too… Thanks!

dumol avatar Nov 21 '14 20:11 dumol

@AlexanderHaase @jfindlay

Hi, what is the status of this problem? Is there already an implementation of the suggested fix? I have the same problem in my environment with multiple tmpfs devices and would like to get it fixed.

I could also implement the suggested the solution and create a merge request if required/wanted.

Thx

RobertFach avatar Jan 07 '15 17:01 RobertFach

Hey! I have an implementation in testing, may be about a week before I submit a pull request (day job woes). In the interim, I've worked-around via creative device names: "ignored-tmp", "ignored-opt-cache", etc. Not for the Strunk and White type when it comes Unix best practices, but works well enough as far as salt and tmpfs care.

AlexanderHaase avatar Jan 07 '15 17:01 AlexanderHaase

Finally got around to testing my code from November. Tested against states and regular salt and fixed all the warts I found. Pull request submitted: https://github.com/saltstack/salt/pull/19829

AlexanderHaase avatar Jan 18 '15 04:01 AlexanderHaase

Is that fix part of 2014.7.1 ?

RobertFach avatar Jan 29 '15 18:01 RobertFach

@RobertFach That fix was made against the develop branch, so no, it was not included in 2014.7.1. I can look at backporting it though so it may be included in 2014.7.2.

rallytime avatar Jan 29 '15 19:01 rallytime

@RobertFach This did not backport cleanly at all to 2014.7, so I backported it to 2015.2 instead. Therefore, this fix won't make it into the 2014.7.2 release, but will be in the 2051.2.0 release which should come out in February.

rallytime avatar Jan 29 '15 21:01 rallytime

@rallytime Okay, thx for the clarification. Btw. Is there an easy way to find out which bugfixes have been included and which not? Last time when I was looking at the changelog for the 2014.7.1 it was not complete...

RobertFach avatar Jan 29 '15 22:01 RobertFach

@RobertFach Good question! Unfortunately I don't have a very good answer to that quickly/easily with a full list. Usually what I do is if I am looking for a particular fix, I check what branch the pull request was made against. Then, if it's against the same branch, you can compare the merge date on the pull request against the date of the release tag.

If they are on separate branches, then I check to see if there are any Bugfix - * labels indicating if the fix has been or needs to be backported. If not, then that fix was just made to that branch. If so, then there should be information linked in the pull request to where the backporting pull request was made.

I know that isn't a stellar solution, especially if you're looking for multiple fixes. We're working on getting a better way of communicating fixes via release notes, but for now, that might help a bit.

rallytime avatar Jan 29 '15 22:01 rallytime

Since this bug is fixed in the upcoming 2015.2.0 release, I am going to close this issue. If it remains a problem on the new release, please comment and we will be happy to re-open this issue.

rallytime avatar Feb 24 '15 03:02 rallytime

With 2015.5.0 I have replaced our current workaround with:

/tmp:
  mount.mounted:
    - device: tmpfs
    - fstype: nodev,nosuid
    - mount: False

/srv/buildslave/.cache:
  mount.mounted:
    - device: tmpfs
    - fstype: nodev,nosuid
    - mkmnt: True
    - mount: True

and I'm happy to report things work just fine this time. Thank you all!

dumol avatar Jun 23 '15 08:06 dumol

Nice, thanks, @dumol.

jfindlay avatar Jun 24 '15 16:06 jfindlay