近期,在机房服务器上安装数据库系统时,提示 swap分区太小,为了符合安装要求,就对swap分区进行了扩容,但在操作过程中,出现了值得思考的问题。
[root@or19c ~]# free
total used free shared buff/cache available
Mem: 8008932 225728 7646872 9128 136332 7573516
Swap: 2097148 0 2097148
[root@or19c ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <39.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 9983
Free PE 0
Allocated PE 9983
PV UUID l0vUVd-cfba-EXlb-Il51-VU4v-31Ey-JGvH2O
--- Physical volume ---
PV Name /dev/sda3
VG Name centos
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 5119
Free PE 0
Allocated PE 5119
PV UUID gC9wKc-cTtk-xwp6-AyNE-Wm3S-4egM-S6rrQT
[root@or19c ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 58.99 GiB
PE Size 4.00 MiB
Total PE 15102
Alloc PE / Size 15102 / 58.99 GiB
Free PE / Size 0 / 0
VG UUID NrPbkL-TWeQ-0bXH-AsX0-FfPT-GaYV-yYIP6J
--summary about used swap devices
[root@orc19c ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 15626236 31488 -2
[root@or19c ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00015f38
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 83886079 40893440 8e Linux LVM
/dev/sda3 83886080 125829119 20971520 83 Linux
Disk /dev/mapper/centos-root: 61.2 GB, 61194895360 bytes, 119521280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
-- 关闭swap分区
[root@or19c ~]# swapoff /dev/dm-1
[root@or19c ~]# lvextend -L 10GB /dev/mapper/centos-swap
Insufficient free space: 2048 extents needed, but only 0 available
root@or19c mapper]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 9.0M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 57G 15G 43G 25% /
/dev/sda1 1014M 199M 816M 20% /boot
tmpfs 783M 0 783M 0% /run/user/0
显示没有空间,确实所有的PE都分配完了,这时自然考虑到,何不把根分区释放部分空间给swap用。
[root@or19c mapper]# lvresize /dev/centos/root -L -8G
WARNING: Reducing active and open logical volume to 48.99 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/root? [y/n]: y
Size of logical volume centos/root changed from 56.99 GiB (14590 extents) to 48.99 GiB (12542 extents).
Logical volume centos/root successfully resized.
--扩容交换分区
[root@or19c mapper]# lvresize /dev/centos/swap -L +8G
Size of logical volume centos/swap changed from 2.00 GiB (512 extents) to 10.00 GiB (2560 extents).
Logical volume centos/swap successfully resized.
--按新的容量创建交换分区
[root@or19c mapper]# mkswap /dev/centos/swap
mkswap: /dev/centos/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 10485756 KiB
no label, UUID=04aa91ef-b16e-4003-943b-d067dedfd05d
--挂载交换分区
[root@or19c mapper]# swapon -va
[root@or19c mapper]# lvdisplay
— Logical volume —
LV Path /dev/centos/swap
LV Size 10.00 GiB
— Logical volume —
LV Path /dev/centos/root
LV Size 48.99 GiB
看来,一切符合要求,随后重启了一下机器,悲剧了,系统无法启动。
centos7 默认的文件系统是xfs,其LVM不能直接在线缩减空间,所以一定注意,如果你强行用以前ext4文件系统的LVM扩容方法去缩减空间,会导致出现superblock错误无法挂载。
通常 xfs文件系统的LVM分区,尽量往小了分,一旦上线就只扩不减!
要实现 xfs文件系统LVM分区的缩减,需要借助 xfsdump 和 xfsrestore工具。
2023-04-05