Home > Article > Computer Tutorials > How to expand the default LVM space of Linux
Linux Logical Volume Manager (LVM) is a tool for managing disks and storage space, providing flexible storage management through volume groups and logical volumes. The core concepts of LVM include physical volumes, volume groups and logical volumes.
The following is a visual diagram of the working principle of LVM. Assume there are 5 different disks, each with a partition mapped to a physical volume (PV), and these disks are combined into a volume group (VG), which is split into two different logical volumes (LV), Each LV is used for one file system.
picture
Assume that a disk with 100GB space is divided on the ubuntu system. According to the LVM principle, the division level is as follows:
When installing the Ubuntu system, a screen prompt will appear requiring approval of the storage layout. By default, the storage layout will include several small boot partitions and a third partition that LVM will use to create the root file system.
picture
picture
By default in Ubuntu, the root file system uses less than half of the total disk space. You can use the df -h command to check the free space of the root file system.
As shown in the figure, only about 14% of the 49GB storage space is currently used. In actual use, this part of the space may be filled up quickly. So now the task is to expand this 49GB volume.
To check the existing free space on the volume group (the space left by the installation program's default settings), you can run: vgdisplay command to check the available space, as shown in the figure:
As you can see from the picture above, there is approximately 49.25GB of space available. Of course, the available space here may not be enough, as will be discussed below.
To use the available space on the volume group (VG) of the root logical volume (LV), you can first run: lvdisplay command to check the logical volume size, and then run:
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Expand the LV to the maximum available size, and then run lvdisplay again to ensure that the change is successful, as shown in the figure:
At this time, the size of the block volume where the root file system is located has been increased, but the file system still needs to be expanded on top of it. Run df -h to check the root file system, and then run the command:
resize2fs /dev/mapper/ubuntu — vg-ubuntu — lv
Expand the file system, and run df -h again to confirm, as shown in the figure:
picture
You can see that the space left by the system by default has been allocated successfully. If the space is still not enough, you need to expand the basic disk to allocate more space.
Assume that the expansion space plan is to expand the current 100GB to 200GB. In actual applications, the expanded space may be a virtual machine or a RAID controller or other storage system. First, execute the: cfdisk command to check and see whether the available space is listed. space, use q to exit after completion, as shown in the figure:
If you don’t see the available space listed, you can use:
echo 1>/sys/class/block/sda/device/rescan
Start the rescan of /dev/sda. After completion, re-run cfdisk. You should be able to see the new available space, as shown in the figure:
Select the /dev/sda3 partition from the list and select "Resize" from the bottom menu. Press the Enter key and it will prompt to confirm the new size. Press the Enter key again and you will see that the /dev/sda3 partition has the new space size.
Select "Write" from the bottom menu, enter yes to confirm, then press Enter, enter q to exit the program.
Now that the LVM partition of the /dev/sda3 physical volume (PV) has been extended, you need to extend the PV and run the command:
pvresize /dev/sda3
Perform this operation, and then use pvdisplay to check the new size, as shown in the figure:
picture
Now you can see that the PV has increased from 98.5GB to 198.5GB. Use vgdisplay again to check the available space of the volume group (VG), as shown in the figure:
picture
You can see that VG has 100GB of available space, continue to enter the command:
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Expand LV to use up all available space of VG, and then execute the lvdisplay command to ensure the change is successful, as shown in the figure:
picture
At this time, the block volume of the root file system has been expanded, but the file system itself has not yet been resized to adapt to the new volume. First, check the current size of the file system with df -h, and then run the command:
resize2fs /dev/mapper/ubuntu — vg-ubuntu — lv
Adjust the size, and run df -h again to check the available space of the new file system, as shown in the figure:
picture
You can see that the physical disk has been successfully expanded and the available space has been extended up to the LVM abstraction layer. The root file system already has enough space.
The above is the detailed content of How to expand the default LVM space of Linux. For more information, please follow other related articles on the PHP Chinese website!