Linux扩展逻辑卷的方法
lvextend uses the space from the volume group and adds it to the logical volume. After extending the logical volume, it is necessary to increase the file system to match the size.
We will take you through step-by-step guides to extend the logical volume properly in Linux.
Step-1: Check LV and VG size
You can check the size of logical volume using the lvs command. You also have to make sure whether there is free space available in the volume group. To display volume group information, you can use vgs or vgdisplay command.
root@ubuntu-PC:~# lvs vol_grp/lvol1 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lvol1 vol_grp -wi-a----- 60.00m root@ubuntu-PC:~# root@ubuntu-PC:~# vgs VG #PV #LV #SN Attr VSize VFree vol_grp 1 4 0 wz--n- 508.00m 328.00m
The VSize column shows the total size and the VFree shows the free space in the volume group. In the next section, we will increase the logical volume lvol1 to 200MiB. The current size of lvol1 is 60MiB.
Step-2: Extend the logical volume using lvextend command
The following command extends the logical volume lvol1 to 200MiB in the volume group vol_grp.
root@ubuntu-PC:~# lvextend -L 200M /dev/vol_grp/lvol1 Size of logical volume vol_grp/lvol1 changed from 60.00 MiB (15 extents) to 200.00 MiB (50 extents). Logical volume vol_grp/lvol1 successfully resized.
Step-3: Resize the partition using resize2fs/xfs_growfs command
It is necessary to resize the extended partition and update the file system. You can use the resize2fs command to resize the partition for an ext3 or ext4 partition and xfs_growfs for an XFS partition.
Sometimes, you might get an error like this as I have got.
root@ubuntu-PC:~# resize2fs /dev/vol_grp/lvol1
resize2fs 1.45.5 (07-Jan-2020)
Please run 'e2fsck -f /dev/vol_grp/lvol1' first.
In that case, you need to run e2fsck -f /dev/vol_grp/lvol1 command first. It forcefully checks the filesystem for errors, even if the filesystem is clean.
root@ubuntu-PC:~# e2fsck -f /dev/vol_grp/lvol1 e2fsck 1.45.5 (07-Jan-2020) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/vol_grp/lvol1: 11/15360 files (0.0% non-contiguous), 1518/15360 blocks
Then run resize2fs /dev/vol_grp/lvol.
root@ubuntu-PC:~# resize2fs /dev/vol_grp/lvol1
resize2fs 1.45.5 (07-Jan-2020)
Resizing the filesystem on /dev/vol_grp/lvol1 to 51200 (4k) blocks.
The filesystem on /dev/vol_grp/lvol1 is now 51200 (4k) blocks long.
Step-4: Check the logical volume size and mount it back
You can verify the extended size of the logical volume using lvs or lvdisplay command.
root@ubuntu-PC:~# lvs /dev/vol_grp/lvol1
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvol1 vol_grp -wi-a----- 200.00m
As you can see, the current size of lvol1 is 200MiB.
Now, you can mount the LV back using the mount command as shown below.
root@ubuntu-PC:~# mount /dev/vol_grp/lvol1 /test
root@ubuntu-PC:~#
root@ubuntu-PC:~# df -h /test
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vol_grp-lvol1 193M 52K 183M 1% /test