Check if block device is empty with salt.states.blockdev

255 views
Skip to first unread message

Geek 1

unread,
Jun 1, 2017, 1:37:33 PM6/1/17
to Salt-users
Hello,

is there a way to check if a given block device is not yet formatted ?
We'd like to build the following state but need some sort of validation that no existing data get lost.

ev-lvm-pv-sdb:
  lvm.pv_present:
    - name: /dev/sdb

ev-lvm-vg-application:
  lvm.vg_present:
    - name: application
    - devices: /dev/sdb

ev-lvm-lv-application-data:
  lvm.lv_present:
    - name: data
    - vgname: application
    - extents: 100%FREE

ev-lvm-format-application-data:
  blockdev.formatted:
    - name: /dev/mapper/application-data
    - fs_type: {{ salt['pillar.get']('ev-lvm:fstype', 'ext4') }}

ev-lvm-mount-application-data:
  mount.mounted:
    - name: /mnt/application
    - device: /dev/mapper/application-data
    - fstype: {{ salt['pillar.get']('ev-lvm:fstype', 'ext4') }}
    - mkmnt: True
    - persist: True
    - mount: True

If target fstype is "xfs" (and previous one "ext4") everything is fine:

          ID: ev-lvm-format-application-data
    Function: blockdev.formatted
        Name: /dev/mapper/application-data
      Result: False
     Comment: Failed to format /dev/mapper/application-data
     Started: 18:13:21.925853
    Duration: 67.421 ms
     Changes:   

But if target fstype is "ext4" (and previous one "xfs") it just overwrites it:

          ID: ev-lvm-format-application-data
    Function: blockdev.formatted
        Name: /dev/mapper/application-data
      Result: True
     Comment: /dev/mapper/application-data has been formatted with ext4
     Started: 18:12:27.759521
    Duration: 326.308 ms
     Changes:   
              ----------
              new:
                  ext4
              old:
                  ext4

What seems odd to me ist, that the ext4 change pretend that the old FS was also ext4 which is wrong. In the example above it was xfs.
If I do it manually on the command line mkfs.ext4 as well as mkfs.xfs detect that there is another filesystem on this blockdevice but xfs aborts the command and ext4 just asks if it should overwrite it.

Has anyone an idea how to check the filesystem and only apply the state ev-lvm-format-application-data if there is no other filesystem on this blockdevice ?

Master:
Salt Version:
           Salt: 2016.11.5
 
Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 1.5
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.7.2
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.6
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.6 (default, Oct 26 2016, 20:30:19)
   python-gnupg: Not Installed
         PyYAML: 3.10
          PyZMQ: 14.0.1
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.0.5
 
System Versions:
           dist: Ubuntu 14.04 trusty
        machine: x86_64
        release: 3.13.0-95-generic
         system: Linux
        version: Ubuntu 14.04 trusty

Minion:
Salt Version:
           Salt: 2016.11.5
 
Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.4.2
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
         Jinja2: 2.8
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.6
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.12 (default, Nov 19 2016, 06:48:10)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.2.0
           RAET: Not Installed
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4
 
System Versions:
           dist: Ubuntu 16.04 xenial
        machine: x86_64
        release: 4.4.0-78-generic
         system: Linux
        version: Ubuntu 16.04 xenial

Thanks,
Michael

Kees Bos

unread,
Jun 2, 2017, 1:27:03 AM6/2/17
to salt-...@googlegroups.com

Probably with something like  - unless: blkid | grep '^/dev/sdb'

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/d0677828-7cde-4e7c-9d75-d26863322c6c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thomas Phipps

unread,
Jun 2, 2017, 1:37:38 AM6/2/17
to salt-...@googlegroups.com
this needs to be checked against a filesystem that doesn't have formatting to be sure but try. 

- unless: 'file -sL /dev/mapper/application-data | grep filesystem'

--

Geek 1

unread,
Jun 2, 2017, 4:32:45 AM6/2/17
to Salt-users
Thanks, this one did the job.
Reply all
Reply to author
Forward
0 new messages