Red Hat Training

A Red Hat training course is available for RHEL 8

Chapter 6. Modifying the size of a logical volume

After you have created a logical volume, you can modify the size of the volume.

6.1. Extending a logical volume and file system

You can extend a logical volume (LV) using the lvextend command. You can specify by how much you want to extend the LV, or how large you want the LV to be after you extend it. Use the -r option of the lvextend command to grow the underlying file system along with the LV.

Warning

You can also extend logical volumes using the lvresize command, but this command does not guarantee against accidental shrinkage.

Prerequisites

  • You have an existing logical volume (LV) with a file system on it. Determine the file system type and size using the df -Th command. For more information about creating a logical volume and a file system, see Creating LVM logical volume.
  • You have sufficient space in the volume group to grow your LV and file system. Use the vgs -o name,vgfree command to determine the available space. For more information about creating volume groups, see Creating LVM volume group.

Procedure

  1. Optional: If the volume group has insufficient space to grow your LV, add a new physical volume to the volume group:

    # vgextend myvg /dev/vdb3
    Physical volume "/dev/vdb3" successfully created.
    Volume group "myvg" successfully extended.
  2. Extend the LV and the file system:

    Note

    Using the lvextend command without the -r argument extends the LV only. To extend an underlying XFS file system, see Increasing the size of an XFS file system, for a GFS2 file system, see Growing a GFS2 filesystem and for an ext4 file system, see Resizing an ext4 file system.

    Note

    Use the -L option to extend the LV to a new size and the -l option to specify the number of extents depending on the size of the logical volume that you want to increase.

    # lvextend -r -L 3G /dev/myvg/mylv
    fsck from util-linux 2.32.1
    /dev/mapper/myvg-mylv: clean, 11/131072 files, 26156/524288 blocks
      Size of logical volume myvg/mylv changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents).
      Logical volume myvg/mylv successfully resized.
    resize2fs 1.45.6 (20-Mar-2020)
    Resizing the filesystem on /dev/mapper/myvg-mylv to 786432 (4k) blocks.
    The filesystem on /dev/mapper/myvg-mylv is now 786432 (4k) blocks long.

    You can also extend the mylv logical volume to fill all of the unallocated space in the myvg volume group:

    # lvextend -l +100%FREE /dev/myvg/mylv
     Size of logical volume myvg/mylv changed from 10.00 GiB (2560 extents) to 6.35 TiB (1665465 extents).
     Logical volume myvg/mylv successfully resized.

Verification

  • Verify that the file system and the LV has grown:

    # df -Th
    Filesystem            Type      Size  Used Avail Use% Mounted on
    devtmpfs              devtmpfs  1.9G     0  1.9G   0% /dev
    tmpfs                 tmpfs     1.9G     0  1.9G   0% /dev/shm
    tmpfs                 tmpfs     1.9G  8.6M  1.9G   1% /run
    tmpfs                 tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
    /dev/mapper/rhel-root xfs        45G  3.7G   42G   9% /
    /dev/vda1             xfs      1014M  369M  646M  37% /boot
    tmpfs                 tmpfs     374M     0  374M   0% /run/user/0
    /dev/mapper/myvg-mylv xfs       2.0G   47M  2.0G   3% /mnt/mnt1

Additional resources

  • vgextend(8), lvextend(8), and xfs_growfs(8) man pages

6.2. Reducing a logical volume and file system

You can reduce a logical volume and its file system by using the lvreduce command and the resizefs option.

If the logical volume you are reducing contains a file system, to prevent data loss you must ensure that the file system is not using the space in the logical volume that is being reduced. For this reason, use the --resizefs option of the lvreduce command when the logical volume contains a file system.

When you use --resizefs, lvreduce attempts to reduce the file system before shrinking the logical volume. If shrinking the file system fails because it is full or does not support shrinking, then the lvreduce command fails and does not attempt to reduce the logical volume.

Warning

In most cases, the lvreduce command warns about possible data loss and asks for confirmation. However, you should not rely on these confirmation prompts to prevent data loss because in some cases you will not see these prompts, such as when the logical volume is inactive or the --resizefs option is not used.

Note that using the --test option of the lvreduce command does not indicate if the operation is safe because this option does not check the file system or test the file system resize.

Prerequisites

  • File system of the logical volume supports shrinking. Determine the file system type and size using the df -Th command.

    Note

    For example, the GFS2 and XFS filesystems do not support shrinking.

  • Underlying file system is not using the space in the LV that is being reduced.

Procedure

  1. Shrink the mylv logical volume and its filesystem in the myvg volume group using one of the following options:

    • Reduce the LV and its file system to a desired value:

      # lvreduce --resizefs -L 500M myvg/mylv
      File system ext4 found on myvg/mylv.
      File system size (2.00 GiB) is larger than the requested size (500.00 MiB).
      File system reduce is required using resize2fs.
      ...
      Logical volume myvg/mylv successfully resized.
    • Reduce 64 megabytes from the logical volume and filesystem:

      # lvreduce --resizefs -L -64M myvg/mylv
      File system ext4 found on myvg/mylv.
      File system size (500.00 MiB) is larger than the requested size (436.00 MiB).
      File system reduce is required using resize2fs.
      ...
      Logical volume myvg/mylv successfully resized

Additional resources

  • lvreduce(8) man page

6.3. Extending a striped logical volume

You can extend a striped logical volume (LV) by using the lvextend command with the required size.

Prerequisites

  1. You have enough free space on the underlying physical volumes (PVs) that make up the volume group (VG) to support the stripe.

Procedure

  1. Optional: Display your volume group:

    # vgs
      VG      #PV #LV #SN Attr   VSize   VFree
      myvg     2   1   0 wz--n- 271.31G 271.31G
  2. Optional: Create a stripe using the entire amount of space in the volume group:

    # lvcreate -n stripe1 -L 271.31G -i 2 myvg
      Using default stripesize 64.00 KB
      Rounding up size to full physical extent 271.31 GiB
  3. Optional: Extend the myvg volume group by adding new physical volumes:

    # vgextend myvg /dev/sdc1
      Volume group "myvg" successfully extended

    Repeat this step to add sufficient physical volumes depending on your stripe type and the amount of space used. For example, for a two-way stripe that uses up the entire volume group, you need to add at least two physical volumes.

  4. Extend the striped logical volume stripe1 that is a part of the myvg VG:

    # lvextend myvg/stripe1 -L 542G
      Using stripesize of last segment 64.00 KB
      Extending logical volume stripe1 to 542.00 GB
      Logical volume stripe1 successfully resized

    You can also extend the stripe1 logical volume to fill all of the unallocated space in the myvg volume group:

    # lvextend -l+100%FREE myvg/stripe1
      Size of logical volume myvg/stripe1 changed from 1020.00 MiB (255 extents) to <2.00 GiB (511 extents).
      Logical volume myvg/stripe1 successfully resized.

Verification

  • Verify the new size of the extended striped LV:

    # lvs
      LV          VG       Attr     LSize      Pool       Origin Data%  Move Log Copy%  Convert
      stripe1    myvg     wi-ao---- 542.00 GB