Linux Tutorial on resizingEs1EN Logical Volume partitions (for xfs and ext4 different file systems)
- 2020-12-05 17:31:36
- OfStack
preface
When we were installing the system, due to the lack of reasonable allocation of partition space, in the follow-up maintenance process, we found that some partition space was not enough to use, while some partition space had a lot of spare space. If these partitions are installed using lvm (provided they are lvm logical volume partitions), then you can easily scale or shrink them! Different file system types correspond to different commands for creating, checking and adjusting. The following is a record of the operations for the expansion and contraction of lvm partition space of xfs and ext2/3/4 file systems:
------------------------------------------------------------------------------------------
Special attention is paid to:
------------------------------------------------------------------------------------------
1) ext2/ext3/ext4 file system adjustment command is resize2fs (support for both increase and decrease)
lvextend -L 120G /dev/mapper/centos-home // Increase to 120G
lvextend -L +20G /dev/mapper/centos-home // increase 20G
lvreduce -L 50G /dev/mapper/centos-home // Reduce to 50G
lvreduce -L -8G /dev/mapper/centos-home // reduce 8G
resize2fs /dev/mapper/centos-home // Perform adjustment
2) xfs file system adjustment command is xfs_growfs (support for increments only)
lvextend -L 120G /dev/mapper/centos-home // Increase to 120G
lvextend -L +20G /dev/mapper/centos-home // increase 20G
xfs_growfs /dev/mapper/centos-home // Perform adjustment
xfs file system only supports increased partition space, not decreased partition space (remember!!!!!). .
If you want to reduce it, you can only mount it by reformatting the logical partition via the mkfs.xfs command after reducing it, so that the original data on the logical partition is lost. If you have important documents, take a break
Example 1 (when there is free space on the system)
1) View the partition space. It is known as the xfs file system (the -ES56en parameter of df shows the file format).
[root@localhost ~]# df -hT
The file system type capacity Has been used available Has been used % The mount point
/dev/mapper/centos-root xfs 200G 2.2G 198G 2% /
devtmpfs devtmpfs 32G 0 32G 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 32G 49M 32G 1% /run
tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda1 xfs 197M 139M 59M 71% /boot
tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0
/dev/mapper/centos-home xfs 628G 33M 718G 1% /home
2) Use the vgdisplay command to view the free space on the system
root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 930.80 GiB
PE Size 4.00 MiB
Total PE 238285
Alloc PE / Size 212736 / 831.00 GiB
Free PE / Size 25549 / 99.80 GiB // this 1 Xiang said the system still exists 99.80G Free space ( 25549 )
VG UUID a5hiAh-LB8M-9lRv-Ps1a-z35L-J4fk-sP3KrF
3) Add or subtract 90G from the free space found above to /home partition
[root@localhost ~]# lvextend -L +90G /dev/mapper/centos-home // Or use - l Parameters (with PE Quantity), i.e lvextend -l +25500 /dev/mapper/centos-home
Size of logical volume centos/home changed from 628.00 GiB (160768 extents) to 718.00 GiB (183808 extents).
Logical volume centos/home successfully resized.
[root@localhost ~]# xfs_growfs /dev/mapper/centos-home
meta-data=/dev/mapper/centos-home isize=512 agcount=4, agsize=41156608 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=164626432, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=80384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 164626432 to 188219392
4) Looking at the system partition again, I found that THE home partition has been increased by 90G (after increasing in this way, the data before home partition will not be lost)
[root@localhost ~]# df -h
The file system capacity Has been used available Has been used % The mount point
/dev/mapper/centos-root 200G 2.2G 198G 2% /
devtmpfs 32G 0 32G 0% /dev
tmpfs 32G 0 32G 0% /dev/shm
tmpfs 32G 49M 32G 1% /run
tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda1 197M 139M 59M 71% /boot
tmpfs 6.3G 0 6.3G 0% /run/user/0
/dev/mapper/centos-home 718G 33M 718G 1% /home
Although the xfs file system only supports increase, not decrease. This does not mean that it cannot be reduced under the xfs system file, only that it needs to be reformatted before it can be mounted. So the original data is lost!
Example 2: This only applies when the system is newly installed and there is little or no data in the logical partition that is not important enough to be deleted or copied
After the system is installed, it is found that the home partition is too large, and I want to take 100G from the home partition to/partition
[root@localhost ~]# df -hT
The file system type capacity Has been used available Has been used % The mount point
/dev/mapper/centos-root xfs 205G 2.2G 203G 2% /
devtmpfs devtmpfs 32G 0 32G 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 32G 49M 32G 1% /run
tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda1 xfs 197M 139M 59M 71% /boot
tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0
/dev/mapper/centos-home xfs 718G 33M 718G 1% /home
[root@localhost ~]# umount /home/
[root@localhost ~]# lvreduce -L -100G /dev/mapper/centos-home
WARNING: Reducing active logical volume to 618.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: y
Size of logical volume centos/home changed from 718.00 GiB (183808 extents) to 618.00 GiB (158208 extents).
Logical volume centos/home successfully resized.
As follows, it is clear that the xfs file system cannot perform partition reduction adjustments!
[root@localhost ~]# xfs_growfs /dev/mapper/centos-home
xfs_growfs: /dev/mapper/centos-home is not a mounted XFS filesystem
[root@localhost ~]# mount /dev/mapper/centos-home /home/
mount: /dev/mapper/centos-home : Cannot read super block
This way, you can only mount it again under home by reformatting the partition
[root@localhost ~]# mkfs.xfs /dev/mapper/centos-home -f
meta-data=/dev/mapper/centos-home isize=512 agcount=4, agsize=41156608 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=164626432, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=80384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mount /dev/mapper/centos-home /home/
Looking at the partition again, I find that the home partition has been reduced by 100G, except that the previous data in this partition is gone.
[root@localhost ~]# df -hT
The file system type capacity Has been used available Has been used % The mount point
/dev/mapper/centos-root xfs 205G 2.2G 203G 2% /
devtmpfs devtmpfs 32G 0 32G 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 32G 49M 32G 1% /run
tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/sda1 xfs 197M 139M 59M 71% /boot
tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0
/dev/mapper/centos-home xfs 618G 73M 578G 1% /home
---------------------------------------------------------------------------
You can also format this as ext4 when reformatting the above.
lvextend -L 120G /dev/mapper/centos-home // Increase to 120G
lvextend -L +20G /dev/mapper/centos-home // increase 20G
xfs_growfs /dev/mapper/centos-home // Perform adjustment
0
---------------------------------------------------------------------------
Then place the above 100G taken from the home partition under /
lvextend -L 120G /dev/mapper/centos-home // Increase to 120G
lvextend -L +20G /dev/mapper/centos-home // increase 20G
xfs_growfs /dev/mapper/centos-home // Perform adjustment
1
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- reminder -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
If the partition space is reduced, the partition must be unloaded before reduction. If there is a problem with unloading, the solution is as follows:
lvextend -L 120G /dev/mapper/centos-home // Increase to 120G
lvextend -L +20G /dev/mapper/centos-home // increase 20G
xfs_growfs /dev/mapper/centos-home // Perform adjustment
2
If the process is occupied /home, use the following command to terminate the occupied process:
[root@localhost ~]# fuser -m -k /home
/home: 1409 1519ce 1531e 1532e 1533e 1534e 1535e 1536e 1537e 1538e 1539e 1541e 1543e 1544e 1545e 1546e 1547e 1548e 1549e 1550e 1601m
Uninstalling the home partition again was successful.
lvextend -L 120G /dev/mapper/centos-home // Increase to 120G
lvextend -L +20G /dev/mapper/centos-home // increase 20G
xfs_growfs /dev/mapper/centos-home // Perform adjustment
4
-ES143en means automatically drop kill from the process occupying the home partition!
If you are not sure if you want to kill all the programs on the commandeering device, you can also add a -ES149en parameter, so that every time you kill a program, you will ask! (i.e. fuser-m-v-i-k /home)
conclusion