How to use the command lvreduce (with examples)

How to use the command lvreduce (with examples)

The lvreduce command is used to reduce the size of a logical volume (LV) in Linux. It is part of the LVM (Logical Volume Manager) system and allows administrators to shrink existing LVs to free up space or adjust the storage capacity as needed. By reducing the size of an LV, you can reclaim unused space or prepare it for other purposes.

Use case 1: Reduce a volume’s size to 120 GB

Code:

lvreduce --size 120G logical_volume

Motivation:

One common use case for the lvreduce command is to reduce the size of a logical volume to a specific value, such as 120 GB. This can be useful when you have allocated more storage to a logical volume than necessary and want to reclaim some of that space for other purposes.

Explanation:

  • --size 120G: Specifies the new size for the logical volume, in this case, 120 GB. The G is used to indicate gigabytes.
  • logical_volume: Specifies the name of the logical volume to be reduced.

Example output:

WARNING: Reducing active logical volume to 120.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce syslv? [y/n]: y
Logical volume "syslv" successfully resized.

In this example, the lvreduce command prompts for confirmation before reducing the size of the logical volume named “syslv” to 120 GB. After confirming, the command executes, and the volume is successfully resized.

Use case 2: Reduce a volume’s size by 40 GB as well as the underlying filesystem

Code:

lvreduce --size -40G -r logical_volume

Motivation:

Sometimes, reducing just the size of the logical volume might not be enough, especially when the logical volume is associated with a filesystem. In such cases, reducing the LV’s size and the underlying filesystem simultaneously can be beneficial.

Explanation:

  • --size -40G: Specifies the amount to reduce the logical volume’s size by, in this case, 40 GB. The - indicates a reduction in size.
  • -r: Initiates a resize of both the logical volume and the underlying filesystem.
  • logical_volume: Specifies the name of the logical volume to be reduced.

Example output:

WARNING: Reducing active logical volume to 80.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce syslv? [y/n]: y
fsck from util-linux 2.35.1
/dev/mapper/syslv: 278976/5242880 files (0.3% non-contiguous), 6309822/20971520 blocks
resize2fs 1.45.6 (20-Mar-2020)
resize2fs: This filesystem will be checked and resized on the next mount.

In this example, the lvreduce command prompts for confirmation before reducing the size of the logical volume named “syslv” by 40 GB and also resizes the underlying filesystem. The command executes successfully, and the output shows that both the logical volume and the filesystem have been resized.

Conclusion:

The lvreduce command is a powerful tool for reducing the size of logical volumes in Linux. It provides flexibility in reclaiming unused space or adjusting storage capacity according to specific needs. Whether you need to simply shrink an LV or resize both the LV and the underlying filesystem, the lvreduce command offers the necessary functionality.

Related Posts

DVC Init (with examples)

DVC Init (with examples)

Initialize a new local repository dvc init Motivation: This use case is used when you want to create a new DVC (Data Version Control) repository locally.

Read More
How to use the command 'getprop' (with examples)

How to use the command 'getprop' (with examples)

The ‘getprop’ command is used to display information about Android system properties.

Read More
Vue CLI: Introduction to the Multi-purpose CLI for Vue.js (with examples)

Vue CLI: Introduction to the Multi-purpose CLI for Vue.js (with examples)

Vue CLI is a command-line interface tool that provides a set of helpful commands for developing Vue.

Read More