扩展swap分区,有三种方法:创建新的交换分区,创建新的交换文件或扩展现有LVM2逻辑卷上的swap分区。建议您扩展现有LVM2逻辑卷上的swap分区。

扩展现有LVM2逻辑卷上的swap分区,需要您注意的是:
(1)需要确保swap使用的卷组有剩余的空间。如果没有剩余的空间,则需要将额外存储添加到swap的卷组后,然后再进行扩展。
(2)确保交换分区使用率为0后,再停掉swap,以免交换分区里面的内容没有交换回去造成数据丢失。

以下是扩展现有LVM2逻辑卷上的swap分区的测试步骤,测试如下:

测试环境
# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.2 (Maipo)
Linux RHEL7.2 3.10.0-327.el7.x86_64 #1 SMP Thu Oct 29 17:29:29 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux

1、vg剩余空间40M  free,swap(lv)大小为820.00 MiB
# free -m
              total        used        free      shared  buff/cache   available
Mem:            993          89         614           6         289         749
Swap:           819           0         819

# vgdisplay
  --- Volume group ---
  VG Name               rhel
  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               7.51 GiB
  PE Size               4.00 MiB
  Total PE              1922
  Alloc PE / Size       1912 / 7.47 GiB
  Free  PE / Size       10 / 40.00 MiB                              ---.>40M  free
  VG UUID               wH5cs8-KryD-pH0y-G1JI-MPdZ-rwzn-SesVdi
# lvdisplay
  --- Logical volume ---
  LV Path                /dev/rhel/swap     ---->swap
  LV Name                swap
  VG Name                rhel
  LV UUID                vAWe01-lbQN-8Tt9-C8CX-Zkit-2RMe-sgeTCM
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-08-07 04:03:50 -0400
  LV Status              available
  # open                 2
  LV Size                820.00 MiB
  Current LE             205
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

2、扩大/dev/vg_rhel69/lv_swap,创建文件系统
#swapoff /dev/rhel/swap                                注意:确保交换分区使用率为0后,再停掉swap,以免交换分区里面的内容没有交换回去造成数据丢失。
# lvextend -l +5 /dev/rhel/swap
  Size of logical volume rhel/swap changed from 820.00 MiB (205 extents) to 840.00 MiB (210 extents).
  Logical volume swap successfully resized.
# mkswap -f /dev/rhel/swap
mkswap: /dev/rhel/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 860156 KiB
no label, UUID=111f95aa-56fe-413d-9211-e5ff248dfa64
# swapon /dev/rhel/swap

3、验证swap大小,变成了840M
# lvdisplay
  --- Logical volume ---
  LV Path                /dev/rhel/swap
  LV Name                swap
  VG Name                rhel
  LV UUID                vAWe01-lbQN-8Tt9-C8CX-Zkit-2RMe-sgeTCM
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-08-07 04:03:50 -0400
  LV Status              available
  # open                 2
  LV Size                840.00 MiB                 ---->840M
  Current LE             210
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
# free -m
              total        used        free      shared  buff/cache   available
Mem:            993          89         614           6         288         749
Swap:           839           0         839