How to Use the Command 'lvresize' (with examples)
- Linux
- December 17, 2024
The lvresize
command is a powerful utility in Linux used for resizing logical volumes within the Logical Volume Manager (LVM) infrastructure. It provides flexibility in storage management by allowing logical volumes to be extended or reduced in size without significant hassle. This command is particularly useful in dynamic environments where storage needs can change rapidly, offering the ability to adapt to these changes seamlessly.
Use case 1: Change the Size of a Logical Volume to 120 GB
Code:
lvresize --size 120G volume_group/logical_volume
Motivation:
In environments where storage demands can fluctuate, there might be a need to allocate a fixed amount of space to a particular application or service. Suppose a database is initially set with some space but grows over time and then stabilizes, necessitating a resizing to 120 GB to optimize resource allocation. This command can be used efficiently to assign precisely 120 GB to that logical volume, ensuring maximal utilization of storage resources and avoiding wastage.
Explanation:
--size 120G
: This argument specifies the new size to which we want to resize the logical volume. The “120G” indicates 120 gigabytes as the target size.volume_group/logical_volume
: This indicates the logical volume and its associated volume group that we intend to resize. Thevolume_group
represents a pool of storage, whilelogical_volume
is the specific volume within that group.
Example Output:
Logical volume "logical_volume" successfully resized.
Use case 2: Extend the Size of a Logical Volume as well as the Underlying Filesystem by 120 GB
Code:
lvresize --size +120G --resizefs volume_group/logical_volume
Motivation:
When additional storage needs arise due to increased data requirements, it becomes essential to not only increase the logical volume size but also expand the filesystem to accommodate the extra space seamlessly. This use case is frequently encountered in scenarios where applications start consuming more data than initially estimated. The dual functionality of extending both the logical volume and the underlying filesystem ensures that applications can immediately start using the additional space without manual intervention.
Explanation:
--size +120G
: The “+” sign indicates that the logical volume will be extended by an additional 120 gigabytes to its current size.--resizefs
: This option ensures that the filesystem on the logical volume is resized along with the logical volume itself, preventing any mismatch between the logical volume and the filesystem.volume_group/logical_volume
: Specifies the exact logical volume within the volume group targeted for extension.
Example Output:
Extending logical volume logical_volume to (current size + 120.00 GiB)
Logical volume logical_volume successfully resized.
Resizing filesystem on logical_volume to match new size.
Filesystem on logical_volume successfully resized.
Use case 3: Extend the Size of a Logical Volume to 100% of the Free Physical Volume Space
Code:
lvresize --size 100%FREE volume_group/logical_volume
Motivation:
In situations where you want to utilize all the available space within a physical volume, it makes sense to extend a logical volume to occupy the entire available free space. This command is particularly useful in scenarios where there’s an imminent application demand for maximum storage usage, and administrators decide to allocate all available resources to a single logical volume to maximize performance and ensure uninterrupted operation.
Explanation:
--size 100%FREE
: This argument adjusts the logical volume size to consume all free space available in the specified volume group.volume_group/logical_volume
: Identifies the logical volume and its volume group which will be extended to use up all free space.
Example Output:
Extending logical volume logical_volume to the maximum available size.
Logical volume logical_volume successfully resized to 100% of available space.
Use case 4: Reduce the Size of a Logical Volume as well as the Underlying Filesystem by 120 GB
Code:
lvresize --size -120G --resizefs volume_group/logical_volume
Motivation:
When an application or service no longer requires as much storage space due to better data management or efficient data cleaning processes, reducing the logical volume size may become necessary. This situation not only helps in freeing up unused space that can be allotted elsewhere but also optimizes storage usage across the environment. The ability to shrink the filesystem alongside the logical volume ensures data integrity and avoids potential issues during storage operations.
Explanation:
--size -120G
: The “-” sign indicates that the specified amount, 120 gigabytes, will be removed from the current size of the logical volume.--resizefs
: Automatically resizes the filesystem along with the logical volume, ensuring their sizes remain in alignment.volume_group/logical_volume
: Denotes the specific logical volume that will be reduced in size.
Example Output:
Reducing logical volume logical_volume by 120.00 GiB
Logical volume logical_volume successfully resized.
Resizing filesystem on logical_volume to reflect reduced size.
Filesystem on logical_volume successfully resized.
Conclusion:
The lvresize
command is an essential tool for efficient storage management in Linux systems using Logical Volume Manager (LVM). With its ability to extend or reduce logical volumes—and also handle the underlying filesystem—it provides system administrators with substantial flexibility in adapting to varying storage requirements. Whether faced with expanding projects or the need to optimize existing storage, lvresize
offers a straightforward solution to meet those demands effectively.