lvresize
The lvresize
command is used in Linux to resize logical volumes (LVs) in LVM (Logical Volume Manager). You can use this command to increase or decrease the size of an existing logical volume. LVM allows for flexible disk management, and resizing logical volumes is one of the key features.
Syntax
Where:
<logical-volume>
is the path to the logical volume you want to resize, for example,/dev/vgname/lvname
.Options specify how you want to resize the logical volume, either by increasing or decreasing its size.
Common Options
-L
or--size <size>
: Specifies the new size for the logical volume.You can specify the size in various units, such as
M
for megabytes,G
for gigabytes, orT
for terabytes.Example: Resize the LV to 10 GB:
-l
or--extents <extents>
: Specifies the size of the logical volume in terms of extents. An extent is a unit of space in the volume group. For example,-l +100%FREE
would resize the LV to use all available free space in the volume group.Example: Resize the LV to use all remaining free space in the volume group:
--resizefs
: This option resizes the filesystem on the logical volume after resizing the LV. It is particularly useful when increasing the size of the LV.Example: Resize the logical volume and filesystem:
--noresizefs
: By default,lvresize
resizes the filesystem if it is supported. This option disables that behavior, preventing the filesystem from being resized.Example: Resize the LV without resizing the filesystem:
-f
or--force
: Forces the resizing operation even if some checks fail.Example:
-d
or--debug
: Displays debugging information while running the command.Example:
--name
: Renames the logical volume during the resize.Example:
Resizing a Logical Volume
Increasing the size of a logical volume:
To increase the size of a logical volume, you can use the -L
option. For example, to increase the logical volume lvname
by 10 GB:
You can also resize the filesystem automatically by using the --resizefs
option:
Decreasing the size of a logical volume:
To decrease the size of a logical volume, you can specify the new size using the -L
option. However, decreasing the size of a logical volume may cause data loss if not done carefully. Therefore, always ensure that the filesystem is resized before resizing the LV:
Resize the filesystem first (e.g., using
resize2fs
for ext4).Example: Resize the filesystem to 15 GB before resizing the LV:
Then resize the logical volume:
Resize the logical volume to use all free space:
If you want to resize the logical volume to use all remaining free space in the volume group, use the -l +100%FREE
option:
Example Scenarios
Increase the logical volume size to 50 GB:
Increase the logical volume size by 20% of its current size:
Resize the logical volume and automatically resize the filesystem:
Resize the logical volume to use all free space in the volume group:
Important Notes
Data Backup: Always back up your data before resizing, especially when reducing the size of the logical volume, to avoid potential data loss.
Filesystem Resize: When increasing the size of a logical volume, the filesystem should be resized accordingly to make use of the new space. This can usually be done with tools like
resize2fs
for ext4 orxfs_growfs
for XFS. For decreasing, be sure to reduce the filesystem size before shrinking the logical volume to avoid data loss.Non-Destructive Resize: It’s typically safe to increase the size of a logical volume without any risk to data, but shrinking the volume requires caution, as it can cause data corruption if not performed carefully.
Conclusion
The lvresize
command is a powerful tool for resizing logical volumes in LVM, enabling you to adjust the size of your logical volumes based on your storage needs. Always be cautious when shrinking volumes to avoid data loss and ensure the filesystem is resized appropriately.
Last updated