Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: is it possible to resize ext4 filesystem?

Tags:

linux

ansible

How I can resize ext4 filesystem via Ansible? I know about the filesystem module, but I didn't see a "resize" parameter, or something else about it ...

like image 636
zombi_man Avatar asked Jan 22 '15 12:01

zombi_man


People also ask

Can ext4 be resized?

Use the appropriate resizing methods for the affected block device. When resizing an ext4 file system, the resize2fs utility reads the size in units of file system block size, unless a suffix indicating a specific unit is used. The following suffixes indicate specific units: s — 512 byte sectors.

Which command is use to resize the ext4 file system?

In that case, you can use the resize2fs utility to increase and decrease a filesystem size. The resize2fs is a command-line utility that allows you to resize ext2, ext3, or ext4 file systems.

Which command helps to resize any Ext file system?

The size of an XFS file system can be increased by using the xfs_growfs command when the file system is mounted. Reducing the size of an XFS file system is not possible.


1 Answers

In order to make the task idempotent, add another task to first check for any unexpanded partitions. E.g., if you want the root partition to be at least 10 GB:

  - name: Assert root partition is expanded
    assert: { that: item.mount != '/' or item.size_total > 10737418240 } # 10 GB
    with_items: '{{ ansible_mounts }}'
    ignore_errors: yes
    register: expanded

NOTE: This task fails if the partition / is less than 10 GB.

Next, make the expansion task conditional on expanded|failed:

  - name: Expand partition
    command: parted /dev/mmcblk0 resizepart 2 15.5GB # NOTE: Assumes 16GB card
    when: expanded|failed
    notify: Expand filesystem

In my case, I'm expanding partition 2 on the block device /dev/mmcblk0 (for the Raspberry Pi). You should of course replace with the device names on your system.

Finally, notify triggers filesystem expansion:

  handlers:
  - name: Expand filesystem
    command: resize2fs /dev/mmcblk0p2
like image 168
crishoj Avatar answered Sep 26 '22 05:09

crishoj