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

查看磁盘分区挂在情况:

Linux Swap扩容
[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
Linux Swap扩容
Linux Swap扩容
[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
Linux Swap扩容

这里看到swap空间为10.7GB,而我们系统内存时16GB,所以至少要扩展6GB才能达到rac检查的要求

二、swap扩容

1、通过添加磁盘扩容

本系统看到是通过逻辑卷做的swap分区,那么我们可以考虑通过逻辑卷扩展的方式扩容swap分区,如果逻辑卷组空间有富余最好,如果没有富余而我们磁盘添加允许的情况下可以通过添加额外的磁盘来扩展逻辑卷组,进而扩展逻辑卷,再到swap分区。本系统是虚拟机,所以扩展磁盘还是比较方便的,具体操作步骤如下:

查看逻辑卷——>查看逻辑卷组——>逻辑卷组扩展(空间不足的情况下)——>卸载逻辑卷——>逻辑卷扩展——>格式化逻辑卷——>重新挂载

1)查看逻辑卷和逻辑卷组

Linux Swap扩容
[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
Linux Swap扩容
Linux Swap扩容
[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
Linux Swap扩容

这里看到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.
Linux Swap扩容
[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
Linux Swap扩容

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分区扩容完毕,但如果我们卷组容量不够的情况下如何扩展呢?首先肯定是扩展卷组,然后再重复上面的步骤。下面将扩展卷组的方法在下面补充

磁盘添加

Linux Swap扩容

Linux 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
Linux Swap扩容

6)创建新的物理卷

[root@rac02 ~]# pvcreate  /dev/sdc 
  Physical volume "/dev/sdc" successfully created

7)扩展逻辑卷组

Linux Swap扩容
[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
Linux Swap扩容

这里逻辑卷组扩容完毕,后面可以重复1——5步骤完成swap扩容。

正常数据库扩容后需要执行以下命令才能看到扩容后的磁盘容量:

e2fsck -f /dev/vgtest/vgtest-lv

resize2fs  /dev/vgtest/vgtest-lv

2、通过本身磁盘剩余空间扩容

1)创建大的swap文件

dd if=/dev/zero of=/home/swap bs=1024 count=20971520
2)格式化swap空间

/sbin/mkswap /home/swap

3)关闭旧swap

swapoff /dev/mapper/vg_swap

4)开启新swap

/sbin/swapon /home/swap

5)开机挂载

/home/swap swap                    swap    defaults        0 0

###备注:在线添加硬盘并扩展逻辑分区

Linux Swap扩容
以root身份运行echo "scsi add-single-device a b c d" > /proc/scsi/scsi

a是硬盘所在SCSI控制器号(一般机器就一个SCSI控制器,所以就是0);
b是硬盘所在SCSI通道的编号(一般单通道的就是0,多通道的要看是哪个通道了);
c是硬盘的SCSI ID号(可以通过具体插入的硬盘插槽来判断);
d是硬盘的lun号(默认情况都是0) 

root@ubuntu:~# cat /proc/scsi/scsi
root@ubuntu:~# echo "scsi add-single-device 2 0 11 0" > /proc/scsi/scsi
root@ubuntu:~# pvcreate  /dev/sdk
root@ubuntu:~# vgextend lhvg /dev/sdk
root@ubuntu:~# lvextend   -l +76790 /dev/mapper/vg-lv_data
root@ubuntu:~# resize2fs -p /dev/vg/lv_data
##查看磁盘和卷对应关系
pvscan
lvscan
vgscam
Linux Swap扩容

Linux Swap扩容

上一篇:Linux 简介、目录结构


下一篇:五、Linux的常用命令 和 使用方式 1