Linux扩展Swap分区详解
最近在做rac,在环境检查的时候发现swap空间检查不通过,所以我们第一想到的是对swap进行扩容,那么swap扩容有哪些方法呢?这里主要介绍两种方法,一通过添加额外磁盘,扩展swap分区,二是通过本地多余空间的方式扩展swap分区。
一、现象描述
Check: Swap space
Node Name Available Required Status
------------ ------------------------ ------------------------ ----------
rac02 10GB (1.0485756E7KB) 15.7037GB (1.6466544E7KB) failed
rac01 10GB (1.0485756E7KB) 15.7032GB (1.6466E7KB) failed
查看内容使用情况:
[root@rac01 ~]# free -g
total used free shared buffers cached
Mem: 15 8 7 0 0 8
-/+ buffers/cache: 0 15
Swap: 9 0 9
查看磁盘分区挂在情况:
[root@rac01 ~]# cat /etc/fstab
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=ea4095fa-f040-1234-b0b5-abcd1532d332 /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/shm /tmp tmpfs defaults 0 0
[root@rac01 ~]# fdisk -l /dev/mapper/VolGroup-lv_swap
Disk /dev/mapper/VolGroup-lv_swap: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
这里看到swap空间为10.7GB,而我们系统内存时16GB,所以至少要扩展6GB才能达到rac检查的要求
二、swap扩容
1、通过添加磁盘扩容
本系统看到是通过逻辑卷做的swap分区,那么我们可以考虑通过逻辑卷扩展的方式扩容swap分区,如果逻辑卷组空间有富余最好,如果没有富余而我们磁盘添加允许的情况下可以通过添加额外的磁盘来扩展逻辑卷组,进而扩展逻辑卷,再到swap分区。本系统是虚拟机,所以扩展磁盘还是比较方便的,具体操作步骤如下:
查看逻辑卷——>查看逻辑卷组——>逻辑卷组扩展(空间不足的情况下)——>卸载逻辑卷——>逻辑卷扩展——>格式化逻辑卷——>重新挂载
1)查看逻辑卷和逻辑卷组
[root@rac01 ~]# lvdisplay /dev/VolGroup/lv_swap
--- Logical volume ---
LV Path /dev/VolGroup/lv_swap
LV Name lv_swap
VG Name VolGroup
LV UUID 4K2GGg-An39-9Vim-RdQW-U0es-BU9R-gwKoBb
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2017-10-11 14:42:02 +0800
LV Status available
# open 1
LV Size 10.00 GiB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
[root@rac01 ~]# vgdisplay VolGroup
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 49.51 GiB
PE Size 4.00 MiB
Total PE 12674
Alloc PE / Size 7680 / 30.00 GiB
Free PE / Size 4994 / 19.51 GiB
VG UUID HYL3bE-tccs-SUil-i75E-qUqG-d6dD-6jzj2u
这里看到swap对应的逻辑卷组还有多余的19G空间,所以我们可以直接扩容swap分区
2)卸载swap分区
[root@rac01 ~]# swapoff /dev/mapper/VolGroup-lv_swap
3)扩展swap逻辑卷
[root@rac01 ~]# lvextend -L +6GB /dev/mapper/VolGroup-lv_swap
Size of logical volume VolGroup/lv_swap changed from 10.00 GiB (2560 extents) to 16.00 GiB (4096 extents).
Logical volume lv_swap successfully resized.
[root@rac01 ~]# fdisk -l /dev/mapper/VolGroup-lv_swap
Disk /dev/mapper/VolGroup-lv_swap: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
4)格式化新swap分区
[root@rac01 ~]# mkswap /dev/mapper/VolGroup-lv_swap
mkswap: /dev/mapper/VolGroup-lv_swap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 16777212 KiB
no label, UUID=5cef886d-5537-4aa0-88d8-f4ce92e01698
5)重新挂在swap分区
[root@rac01 ~]# swapon /dev/mapper/VolGroup-lv_swap
[root@rac01 ~]# free -g
total used free shared buffers cached
Mem: 15 8 7 0 0 8
-/+ buffers/cache: 0 15
Swap: 15 0 15
至此swap分区扩容完毕,但如果我们卷组容量不够的情况下如何扩展呢?首先肯定是扩展卷组,然后再重复上面的步骤。下面将扩展卷组的方法在下面补充
磁盘添加
[root@rac02 ~]# fdisk -l |grep sd
Disk /dev/sda: 53.7 GB, 53687091200 bytes
/dev/sda1 * 1 64 512000 83 Linux
/dev/sda2 64 6528 51915776 8e Linux LVM
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
Disk /dev/sdc: 8589 MB, 8589934592 bytes
Disk /dev/sdd: 5368 MB, 5368709120 bytes
/dev/sdd1 1 652 5237158+ 83 Linux
Disk /dev/sdf: 107.4 GB, 107374182400 bytes
/dev/sdf1 1 13054 104856223+ 83 Linux
Disk /dev/sde: 5368 MB, 5368709120 bytes
/dev/sde1 1 652 5237158+ 83 Linux
Disk /dev/sdg: 10.7 GB, 10737418240 bytes
/dev/sdg1 1 1305 10482381 83 Linux
6)创建新的物理卷
[root@rac02 ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created
7)扩展逻辑卷组
[root@rac02 ~]# vgdisplay
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 49.51 GiB
PE Size 4.00 MiB
Total PE 12674
Alloc PE / Size 7680 / 30.00 GiB
Free PE / Size 4994 / 19.51 GiB
VG UUID HYL3bE-tccs-SUil-i75E-qUqG-d6dD-6jzj2u
[root@rac02 ~]# vgextend VolGroup /dev/sdc
Volume group "VolGroup" successfully extended
[root@rac02 ~]# vgdisplay
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
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 57.50 GiB
PE Size 4.00 MiB
Total PE 14721
Alloc PE / Size 7680 / 30.00 GiB
Free PE / Size 7041 / 27.50 GiB
VG UUID HYL3bE-tccs-SUil-i75E-qUqG-d6dD-6jzj2u
这里逻辑卷组扩容完毕,后面可以重复1——5步骤完成swap扩容。