Logical Volume Management (LVM) is a flexible and advanced option available to manage hard disks in most Linux distributions. It provides a higher level of storage management than traditional partitioning. One of the main advantages of LVM is the ability to resize logical volumes dynamically as storage needs increase.
In this tutorial, we will walk through the steps to increase the size of a logical volume on Linux. This can be particularly useful when you’re running out of space on a specific volume and have available unallocated space on your disk or a new disk.
Let’s get started.
Step 1: Check Current Disk Usage
Use the command `df -lh` to view the current disk usage.
df -lh
Example:
Filesystem Size Used Avail Use% Mounted on /dev/sda3 5.8G 4.3G 1.2G 79% / /dev/sda1 99M 12M 83M 13% /boot tmpfs 506M 0 506M 0% /dev/shm /dev/mapper/VolGroup00-datavol 13G 855M 12G 7% /data /dev/mapper/VolGroup01-optvol 20G 173M 19G 1% /opt
Step 2: List All Partitions
To list all partitions on your system, use:
fdisk -lu
Example:
Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Device Boot Start End Blocks Id System /dev/sda1 * 63 208844 104391 83 Linux /dev/sda2 208845 2313359 1052257+ 82 Linux swap / Solaris /dev/sda3 2313360 14667344 6176992+ 8e Linux LVM /dev/sda4 14667345 41929649 13631152+ 8e Linux LVM Disk /dev/sdb: 26.8 GB, 26843545600 bytes 255 heads, 63 sectors/track, 3263 cylinders, total 52428800 sectors Units = sectors of 1 * 512 = 512 bytes Device Boot Start End Blocks Id System /dev/sdb1 63 41929649 20964793+ 8e Linux LVM
Step 3: Create a New Partition
If you have unallocated space on `/dev/sdb`, create a new partition:
fdisk /dev/sdb
Follow the on-screen instructions to create a new partition.
Example:
The number of cylinders for this disk is set to 3263. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (2611-3263, default 2611): Using default value 2611 Last cylinder or +size or +sizeM or +sizeK (2611-3263, default 3263): Using default value 3263 Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdb: 26.8 GB, 26843545600 bytes 255 heads, 63 sectors/track, 3263 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 2610 20964793+ 8e Linux LVM /dev/sdb2 2611 3263 5245222+ 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks.
Reboot the server:
reboot
Verify disk usage:
Filesystem Size Used Avail Use% Mounted on /dev/sda3 5.8G 4.3G 1.2G 79% / /dev/sda1 99M 12M 83M 13% /boot tmpfs 506M 0 506M 0% /dev/shm /dev/mapper/VolGroup00-datavol 13G 855M 12G 7% /data /dev/mapper/VolGroup01-optvol 20G 173M 19G 1% /opt
Step 4: Format the New Partition
Once the partition is created, format it:
mke2fs -j /dev/sdb2
Example:
mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 656000 inodes, 1311305 blocks 65565 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1346371584 41 block groups 32768 blocks per group, 32768 fragments per group 16000 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 20 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
Step 5: Create a Physical Volume
Now, create a physical volume on the new partition:
pvcreate /dev/sdb2
Example:
Physical volume "/dev/sdb2" successfully created
Step 6: Scan All Physical Volumes
To ensure the system recognizes the new PV, scan all physical volumes:
pvscan
Example:
PV /dev/sdb1 VG VolGroup01 lvm2 [19.99 GB / 0 free] PV /dev/sda4 VG VolGroup00 lvm2 [13.00 GB / 0 free] PV /dev/sdb2 lvm2 [5.00 GB] Total: 3 [37.99 GB] / in use: 2 [32.99 GB] / in no VG: 1 [5.00 GB]
Step 7: Extend the Volume Group
Extend your volume group (e.g., `VolGroup01`) to include the new PV:
vgextend VolGroup01 /dev/sdb2
Example:
Volume group "VolGroup01" successfully extended
Step 8: Extend the Logical Volume
Now, increase the size of your logical volume:
lvextend -l +100%FREE /dev/VolGroup01/optvol
Example:
Extending logical volume optvol to 24.99 GB Logical volume optvol successfully resized
Step 9: Resize the File System
After extending the logical volume, resize the file system:
resize2fs /dev/mapper/VolGroup01-optvol
Example:
resize2fs 1.39 (29-May-2006) Filesystem at /dev/mapper/VolGroup01-optvol is mounted on /opt; on-line resizing required Performing an on-line resize of /dev/mapper/VolGroup01-optvol to 6551552 (4k) blocks. The filesystem on /dev/mapper/VolGroup01-optvol is now 6551552 blocks long.
Step 10: Verify the Changes
Finally, check the disk usage again to confirm the changes:
df -lh
Example:
Filesystem Size Used Avail Use% Mounted on /dev/sda3 5.8G 4.3G 1.2G 79% / /dev/sda1 99M 12M 83M 13% /boot tmpfs 506M 0 506M 0% /dev/shm /dev/mapper/VolGroup00-datavol 13G 855M 12G 7% /data /dev/mapper/VolGroup01-optvol 25G 173M 24G 1% /opt
Commands Mentioned
- df -lh – Displays disk space usage
- fdisk -lu – Lists all partitions
- fdisk /dev/sdb – Creates a new partition on /dev/sdb
- mke2fs -j /dev/sdb2 – Formats the new partition
- pvcreate /dev/sdb2 – Creates a physical volume
- pvscan – Scans all physical volumes
- vgextend VolGroup01 /dev/sdb2 – Extends the volume group
- lvextend -l +100%FREE /dev/VolGroup01/optvol – Extends the logical volume
- resize2fs /dev/mapper/VolGroup01-optvol – Resizes the file system
FAQ
-
What is LVM in Linux?
LVM stands for Logical Volume Management. It’s a method of allocating space on hard drives into virtual partitions that can be easily resized unlike traditional disk partitioning. This provides greater flexibility and efficiency in disk management.
-
Why would one need to resize a logical volume?
Resizing a logical volume is often necessary when a particular volume is running out of space, and there’s available unallocated space on the disk or another disk. LVM provides the flexibility to resize volumes dynamically based on storage needs.
-
Is it safe to resize logical volumes?
While LVM provides the tools to resize volumes, it’s essential to take precautions. Always backup critical data before making changes, and ensure you follow the correct procedures to avoid data loss or corruption.
-
Can I reduce the size of a logical volume?
Yes, LVM allows both increasing and decreasing the size of logical volumes. However, reducing size requires extra caution to ensure data within the volume isn’t lost.
-
What’s the difference between a physical volume and a logical volume?
In LVM, a physical volume (PV) refers to a storage device or a partition on a storage device, which can be used as part of LVM. It’s the foundational building block in the LVM hierarchy. On the other hand, a logical volume (LV) is a virtual partition created from space allocated from one or more physical volumes. It’s where your file systems can be created. Logical volumes offer the flexibility to be resized, moved, and modified as needed, unlike traditional partitions.
Conclusion
Increasing the size of a logical volume in Linux using LVM is a powerful feature that provides administrators with the flexibility to manage storage dynamically. This adaptability is especially beneficial in environments where storage needs can change, such as in cloud hosting or VPS server setups.
However, while LVM offers many advantages, it’s crucial to approach the process with caution. Always ensure that you have a backup of essential data before making any changes to your storage configurations. Mistakes or unforeseen issues can lead to data loss, so it’s better to be safe than sorry.
Furthermore, while this tutorial focused on increasing the size of a logical volume, LVM also supports other operations like reducing the size of a volume, migrating data between disks, and more. It’s a testament to the versatility and power of LVM in Linux.
For those who manage web servers or host multiple websites, understanding LVM can be a game-changer. It’s not just about adding more space; it’s about optimizing and making the most of the resources you have. Whether you’re on a dedicated server or a shared hosting environment, mastering LVM can significantly enhance your server management capabilities.
In conclusion, LVM is an indispensable tool for Linux administrators, offering a level of flexibility and control over storage that traditional partitioning methods can’t match. By following the steps outlined in this tutorial, you can efficiently manage and optimize your storage needs.
2 Comments
Thanks Dude!
Great Work.
Thanks a lot for this article… you saved the day and i Learned a lot.