Skip to content

add action to extand LVM based volume with additionall drives

We need ability to extend filesystem on lvm with additional hard drives.

Usecase:

  • Host drives:
    • /dev/sda
    • /dev/sdb
  • LVM Device before run:
    • centos_local/root @ /dev/sda3
  • expected LVM Device after run:
    • centos_local/root @ /dev/sda3,/dev/sdb1

Examples below assume (for testing pourpouses):

  • Partitions used by lvm before run /dev/vdb1
  • Partitions used by lvm after run /dev/vdb1 /dev/vdc1

Bash steps to achive goal (assuming filesystem is xfs)

parted -s /dev/vdc "mklabel gpt unit mib mkpart primary 1% 100% set 1 lvm on" # Create lvm partiton
pvcreate /dev/vdc1                         # init lvm device
vgextend centos_local /dev/vdc1            # add LVM Device to Volume Group
lvextend -l +100%FREE centos_local/root    # Extend LVM volume to maximum available
xfs_growfs /dev/centos_local/root          # Resize filesystem

Example implementation:

- name: "extend LVM backed filesystem"
  hosts: all
  vars: 
    # Action arguemtns
    filesystem_extend_lvm_devices:
      - "vdc"
    filesystem_extend_lvm_vg: "centos_local"
    filesystem_extend_lvm_lv: "root"

    # Action vars
    _filesystem_empty_drives: |-
      [
        {% for device, params in ansible_facts.devices.items() %}
          {% if params.partitions == {} and params.vendor != None %}
            "{{ device }}",
          {% endif %}
        {% endfor %}
      ]
    _filesystem_extend_lvm_partitions: "{{ filesystem_extend_lvm_devices | map('regex_replace', '(.*)', '/dev/\\g<1>1')| list }}"
    _filesystem_lvm_filesystem: |-
      {{ 
        (
          ansible_facts.mounts
          | selectattr('device', 'equalto', '/dev/mapper/' ~ filesystem_extend_lvm_vg ~ '-' ~ filesystem_extend_lvm_lv) 
          | first
        ).fstype
      }}
  tasks:
    - name: "Create partitions"
      become: yes
      parted:
        device: "/dev/{{ item }}"
        number: 1
        flags: 
          - "lvm"
        state: "present"
      loop: "{{ filesystem_extend_lvm_devices | intersect(_filesystem_empty_drives) }}"

    - name: "extend volume group"
      become: yes
      lvg:
        vg: "{{ filesystem_extend_lvm_vg }}"
        pvs: "{{ _filesystem_extend_lvm_partitions | join(',') }}"

    - name: "extend logival volume"
      become: yes
      lvol:
        vg: "{{ filesystem_extend_lvm_vg }}"
        lv: "{{ filesystem_extend_lvm_lv }}"
        size: +100%FREE

    - name: "resize filesystem"
      become: yes
      filesystem:
        fstype: "{{ _filesystem_lvm_filesystem }}"
        dev: "/dev/{{ filesystem_extend_lvm_vg }}/{{ filesystem_extend_lvm_lv }}"
        resizefs: yes

API1:

  • filesystem_extend_lvm_devices - List of devices on which partition should be created and added to LVM
  • filesystem_extend_lvm_vg - name of LVM volume group
  • filesystem_extend_lvm_lv - name of LVM volume
  • filesystem_extend_lvm_size - target size of volume (default: "+100%FREE")

API2:

  • filesystem_extend_lvm_partitions - List of partitions that should be used in volume group
  • filesystem_extend_lvm_vg - name of LVM volume group
  • filesystem_extend_lvm_lv - name of LVM volume
  • filesystem_extend_lvm_size - target size of volume (default: "+100%FREE")

API3:

  • filesystem_lvm_physical_devices - List of partitions that should be used in volume group (partitions need to be created with lvm flag)
  • filesystem_lvm_volume_group - name of LVM volume group
  • filesystem_lvm_volume - name of LVM volume
  • filesystem_lvm_volume_size - target size of volume (default: "+100%FREE")

comments on API1:

  • filesystem_extend_lvm_devices - suggests that list of devices should be calculated before running
  • According to docs module lvg should be given list of all devices that should exist in group
  • There is no nice way to list devices attached to volume group (best found is to use command lvdisplay --maps)

comments on API2:

  • who will create partitions and if this action conversion of partition to device will not be nice

comments on API3:

  • this API is similar to API2 but design assumes that this action will create partitions on LVM not only extend them
Edited by Piotr Korthals
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information