Home  >  Article  >  Computer Tutorials  >  How to expand the default LVM space of Linux

How to expand the default LVM space of Linux

PHPz
PHPzforward
2024-02-19 19:30:031244browse

How to expand the default LVM space of Linux

1. Introduction to Linux LVM

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.

  • Physical Volume (PV): A physical volume is a physical hard disk or partition, which is used by LVM to store data. LVM combines one or more physical volumes into volume groups.
  • Volume Group (VG): A volume group is a logical storage unit composed of one or more physical volumes. Logical volumes are created on volume groups, and they can dynamically allocate and reclaim storage space. A system can contain multiple volume groups.
  • Logical Volume (LV): A logical volume is a logical storage unit created on a volume group. They can be formatted as a file system and mounted on the file system tree like ordinary hard disk partitions. The size and location of logical volumes can be dynamically adjusted at runtime without shutting down or restarting the system.

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:

2.Linux system installation program default settings

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

3. Use the default available space

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.

4.Expand physical disk 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!

Statement:
This article is reproduced at:mryunwei.com. If there is any infringement, please contact admin@php.cn delete