Linux Swap擴容

Rangle發表於2018-05-23

最近在做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擴容。

正常資料庫擴容後需要執行以下命令才能看到擴容後的磁碟容量:

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

###備註:線上新增硬碟並擴充套件邏輯分割槽

以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

 

相關文章