volume.sls

Salt State for formatting and mounting a volume on Ubuntu
{% import_yaml "server/config.yaml" as config %}
{% set block_device_name = '/dev/nvme3n1' %}

disk_label_{{ config.label_name }}:
  module.run:
    - name: partition.mklabel
    - device: {{ block_device_name }}
    - label_type: gpt
    - unless: "parted {{ block_device_name }} print | grep -i '^Partition Table: gpt'"

disk_partition_{{ config.label_name }}:
  module.run:
    - partition.mkpart:
      - device: {{ block_device_name }}
      - fs_type: ext4
      - part_type: primary
      - start: 0%
      - end: 100%
    - unless: parted {{ block_device_name }} print 1
    - require:
      - module: disk_label_{{ config.label_name }}

disk_name_{{ config.label_name }}:
  module.run:
    - partition.name:
      - device: {{ block_device_name }}
      - partition: 1
      - name: {{ config.label_name }}
    - unless: parted {{ block_device_name }} print | grep {{ config.label_name }}
    - require:
      - module: disk_partition_{{ config.label_name }}

disk_format_{{ config.label_name }}:
  module.run:
    - extfs.mkfs:
      - device: {{ block_device_name }}p1
      - fs_type: ext4
      - label: {{ config.label_name }}
    - unless: blkid --label {{ config.label_name }}
    - require:
      - disk_name_{{ config.label_name }}

disk_mount_{{ config.label_name }}:
  mount.mounted:
    - name: {{ config.mount_point }}
    - device: LABEL={{ config.label_name }}
    - fstype: ext4
    - mkmnt: True
    - require:
      - module: disk_format_{{ config.label_name }}