Managing storage space on Linux servers is a vital aspect of system administration, and the logical volume manager (LVM) is one of the most popular methods of achieving this. The LVM provides an efficient way to manage and organize the storage on a Linux system, allowing administrators to create and manage volumes flexibly. This article aims to explain some essential commands that are commonly used in managing LVM volumes.
1. lvcreate
The lvcreate command creates a new logical volume with a specified size and name. The syntax of the command is:
lvcreate -L size -n name volume_group;
The “size” parameter specifies the size of the logical volume in units like GB, MB, or TB, and the “name” parameter specifies the name of the logical volume. The “volume_group” parameter specifies the name of the volume group in which the logical volume will be created.
For example, to create a new logical volume named “partitionName” with a size of 372.3GB in a volume group named “boxname,” the following command can be used:
lvcreate -L 372.3GB -n partitionName /dev/boxname;
2. mkfs.ext4
The mkfs.ext4 command is used to create a new file system on a specified partition or logical volume. The syntax of the command is:
The “device” parameter specifies the name of the device on which the file system is to be created.
For example, to create an ext4 file system on a logical volume named “partitionName” in the “boxname” volume group, the following command can be used:
mkfs.ext4 /dev/boxname/partitionName
3. mount
The mount command is used to mount a file system to a specific directory in the file system hierarchy. The syntax of the command is:
mount -t filesystem device mountpoint;
The “filesystem” parameter specifies the type of file system to be mounted, the “device” parameter specifies the name of the device containing the file system, and the “mountpoint” parameter specifies the directory to which the file system should be mounted.
For example, to mount the ext4 file system created in the previous step on a directory named “someName” under the “media” directory, the following command can be used:
mount -t ext4 /dev/mapper/boxname-partitionName /media/someName/;
4. lvextend
The lvextend command is used to extend the size of an existing logical volume. The syntax of the command is:
lvextend -L +size device;
The “size” parameter specifies the size by which the logical volume is to be extended, and the “device” parameter specifies the name of the logical volume.
For example, to increase the size of the “partitionName” logical volume by 122.39GB, the following command can be used:
lvextend -L +122.39G /dev/boxname/partitionName;
5. resize2fs
The resize2fs command is used to resize the file system on a specified partition or logical volume. The syntax of the command is:
The “device” parameter specifies the name of the device containing the file system.
For example, to resize the ext4 file system on the “partitionName” logical volume after extending its size, the following command can be used:
resize2fs /dev/boxname/partitionName;
6. lvremove
The lvremove command is used to remove an existing logical volume. The syntax of the command is:
The “device” parameter specifies the name of the logical volume to be removed.
For example, to remove the ext4 file system on the “partitionName” logical volume, the following command can be used:
lvremove /dev/boxname/partitionName;
Other
Following you can find the commands I used to create a new partition on an LVM (Logical Volume Manager) system where during installation there was some hard disk space left empty.
lvcreate -L 372.3GB -n partitionName /dev/boxname
mkfs.ext4 /dev/boxname/partitionName
mount -t ext4 /dev/mapper/boxname-partitionName /media/someName/
#lvextend -L+122.39G /dev/boxname/partitionName
#resize2fs /dev/boxname/partitionName
##lvremove /dev/boxname/partitionName
After creating the partition, we set it’s type – here we used ext4.
Later we mount the partition to a folder to gain access to it.
Following in comments (#) we show how to resize/extend an already existing partition and how to remove it (##) — make sure to remove all brackets before performing these operations.