SWAP交换分区扩容

一、 标准指导操作

第一章 扩容当前swap交换分区

注:这种需要停止当前业务,否则会产生影响

1、先查看当前swap大小

[17:09:31 root@libin3 ~]# free -h

              total        used        free      shared  buff/cache   available

Mem:           3.7G        945M        1.9G         69M        885M        2.5G

Swap:          2.0G          0B        2.0G

 

2、查看磁盘分区挂载

[17:09:35 root@libin3 ~]# cat /etc/fstab

 

#

# /etc/fstab

# Created by anaconda on Thu Sep 30 01:20:00 2021

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

/dev/mapper/cl-root     /                       xfs     defaults        0 0

UUID=95ea2cb4-3558-4cf6-b0d3-68afc5ee1f28 /boot                   xfs     defaults        0 0

/dev/mapper/cl-swap     swap                    swap    defaults        0 0

#/dev/datevg/date_lv /date/ xfs defaults 0 0

#UUID=52402663-ca47-4de3-8088-ed175cfc3687 /RAID-LIBIN/ xfs defaults 0 0

/dev/vg_libin/lv_libin /mnt/rhce ext4 defaults 0 0

 

[17:10:27 root@libin3 ~]# fdisk -l /dev/mapper/cl-swap

 

磁盘 /dev/mapper/cl-swap2147 MB, 2147483648 字节,4194304 个扇区

Units = 扇区 of 1 * 512 = 512 bytes

扇区大小(逻辑/物理)512 字节 / 512 字节

I/O 大小(最小/最佳)512 字节 / 512 字节

 

 

3、开始扩容,将swap分区扩容到3G

(1)查看逻辑卷与卷组

[17:16:16 root@libin3 ~]# lvdisplay /dev/mapper/cl-swap

  connect() failed on local socket: 没有那个文件或目录

  Internal cluster locking initialisation failed.

  WARNING: Falling back to local file-based locking.

  Volume Groups with the clustered attribute will be inaccessible.

  --- Logical volume ---

  LV Path                /dev/cl/swap

  LV Name                swap

  VG Name                cl

  LV UUID                Fap9Ef-mj4e-HnDa-gFF9-CUcO-amGQ-ahg8l9

  LV Write Access        read/write

  LV Creation host, time libin3.com, 2021-09-30 01:19:59 +0800

  LV Status              available

  # open                 2

  LV Size                2.00 GiB

  Current LE             512

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:1

 

 

[17:16:18 root@libin3 ~]# vgdisplay cl

  connect() failed on local socket: 没有那个文件或目录

  Internal cluster locking initialisation failed.

  WARNING: Falling back to local file-based locking.

  Volume Groups with the clustered attribute will be inaccessible.

  --- Volume group ---

  VG Name               cl

  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               12.00 GiB

  PE Size               4.00 MiB

  Total PE              3073

  Alloc PE / Size       3072 / 12.00 GiB

  Free  PE / Size       1 / 4.00 MiB

  VG UUID               Qa4iMJ-dW4g-2h0R-ckhW-OM7f-6wJm-QftlK2

 

 

(2)查看磁盘使用情况

[17:16:53 root@libin3 ~]# lsblk

NAME                  MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                     8:0    0   40G  0 disk

├─sda1                  8:1    0  200M  0 part /boot

├─sda2                  8:2    0   12G  0 part

│ ├─cl-root           253:0    0   10G  0 lvm  /

│ └─cl-swap           253:1    0    2G  0 lvm  [SWAP]

└─sda3                  8:3    0    1G  0 part

sdb                     8:16   0    8G  0 disk

├─sdb1                  8:17   0    4G  0 part

└─sdb2                  8:18   0    2G  0 part

  └─vg_libin-lv_libin 253:2    0    2G  0 lvm  /mnt/rhce

sr0                    11:0    1  4.2G  0 rom  /run/media/root/CentOS 7 x86_64

 

 

(3)扩容逻辑卷

[17:22:54 root@libin3 ~]# fdisk /dev/sda3

欢迎使用 fdisk (util-linux 2.23.2)

 

更改将停留在内存中,直到您决定将更改写入磁盘。

使用写入命令前请三思。

 

Device does not contain a recognized partition table

使用磁盘标识符 0x3b66a00d 创建新的 DOS 磁盘标签。

 

命令(输入 m 获取帮助)n

Partition type:

   p   primary (0 primary, 0 extended, 4 free)

   e   extended

Select (default p): p

分区号 (1-4,默认 1)

起始 扇区 (2048-2097151,默认为 2048)

将使用默认值 2048

Last 扇区, +扇区 or +size{K,M,G} (2048-2097151,默认为 2097151)

将使用默认值 2097151

分区 1 已设置为 Linux 类型,大小设为 1023 MiB

 

命令(输入 m 获取帮助)t

已选择分区 1

Hex 代码(输入 L 列出所有代码)8e

已将分区“Linux”的类型更改为“Linux LVM”

 

命令(输入 m 获取帮助)w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

 

WARNING: Re-reading the partition table failed with error 22: 无效的参数.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

正在同步磁盘。

[17:30:55 root@libin3 ~]# partprobe

 

[17:33:28 root@libin3 ~]# pvcreate /dev/sda3              创建物理卷

 

[17:34:45 root@libin3 ~]# vgextend cl /dev/sda3            扩容卷组

 

 

[17:35:51 root@libin3 ~]# lvextend  -L +1G /dev/cl/swap    扩容swap逻辑卷

connect() failed on local socket: 没有那个文件或目录

  Internal cluster locking initialisation failed.

  WARNING: Falling back to local file-based locking.

  Volume Groups with the clustered attribute will be inaccessible.

  Size of logical volume cl/swap changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents).

  Logical volume cl/swap successfully resized.

 

[17:37:36 root@libin3 ~]# fdisk -l /dev/mapper/cl-swap       

 

磁盘 /dev/mapper/cl-swap3221 MB, 3221225472 字节,6291456 个扇区

Units = 扇区 of 1 * 512 = 512 bytes

扇区大小(逻辑/物理)512 字节 / 512 字节

I/O 大小(最小/最佳)512 字节 / 512 字节

(4)卸载交换分区

[17:18:06 root@libin3 ~]# swapoff /dev/mapper/cl-swap

 

注:我们从(1)可以看到已经没有多余的逻辑卷组空间了,否则可以直接用下面这条命令

  Alloc PE / Size       3072 / 12.00 GiB

  Free  PE / Size       1 / 4.00 MiB

[17:18:06 root@libin3 ~]# lvextend  -L +1GB  /dev/mapper/cl-swap  这条命令无法使用

 

(5)格式化新swap分区

[17:41:10 root@libin3 ~]# mkswap /dev/mapper/cl-swap

mkswap: /dev/mapper/cl-swap: warning: wiping old swap signature.

正在设置交换空间版本 1,大小 = 3145724 KiB

无标签,UUID=0c5c95af-0922-43d8-a02a-5bca3174d0bb

 

(6)重新挂载swap分区

[17:42:13 root@libin3 ~]# swapon /dev/mapper/cl-swap

(7)检验,扩容成功

[17:43:14 root@libin3 ~]# free -h

              total        used        free      shared  buff/cache   available

Mem:           3.7G        1.0G        1.8G         69M        938M        2.4G

Swap:          3.0G          0B        3.0G

 

[17:43:54 root@libin3 ~]# lsblk

NAME                  MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                     8:0    0   40G  0 disk

├─sda1                  8:1    0  200M  0 part /boot

├─sda2                  8:2    0   12G  0 part

│ ├─cl-root           253:0    0   10G  0 lvm  /

│ └─cl-swap           253:1    0    3G  0 lvm  [SWAP]

└─sda3                  8:3    0    1G  0 part

  └─cl-swap           253:1    0    3G  0 lvm  [SWAP]

sdb                     8:16   0    8G  0 disk

├─sdb1                  8:17   0    4G  0 part

└─sdb2                  8:18   0    2G  0 part

  └─vg_libin-lv_libin 253:2    0    2G  0 lvm  /mnt/rhce

sr0                    11:0    1  4.2G  0 rom  /run/media/root/CentOS 7 x86_64

 

4、回退

[17:44:48 root@libin3 ~]# swapoff /dev/mapper/cl-swap     卸载交换分区

[18:02:15 root@libin3 ~]# lvreduce  -L -1G /dev/cl/swap  缩减逻辑卷

[18:05:08 root@libin3 ~]# vgreduce cl /dev/sda3           缩小卷组

[18:05:30 root@libin3 ~]# pvremove /dev/sda3            移除物理卷

[18:03:32 root@libin3 ~]# mkswap /dev/mapper/cl-swap    重新格式化交换分区

[18:05:56 root@libin3 ~]# swapon /dev/mapper/cl-swap     重新挂载交换分区

检验

[18:05:56 root@libin3 ~]# swapon /dev/mapper/cl-swap

[18:06:06 root@libin3 ~]# free -h

              total        used        free      shared  buff/cache   available

Mem:           3.7G        1.0G        1.8G         69M        940M        2.4G

Swap:          2.0G          0B        2.0G

[18:08:02 root@libin3 ~]# lsblk

NAME                  MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                     8:0    0   40G  0 disk

├─sda1                  8:1    0  200M  0 part /boot

├─sda2                  8:2    0   12G  0 part

│ ├─cl-root           253:0    0   10G  0 lvm  /

│ └─cl-swap           253:1    0    2G  0 lvm  [SWAP]

└─sda3                  8:3    0    1G  0 part

sdb                     8:16   0    8G  0 disk

├─sdb1                  8:17   0    4G  0 part

└─sdb2                  8:18   0    2G  0 part

  └─vg_libin-lv_libin 253:2    0    2G  0 lvm  /mnt/rhce

sr0                    11:0    1  4.2G  0 rom  /run/media/root/CentOS 7 x86_64


第二章 DD扩容swap交换分区

可以在线扩容

1、使用新分区,查看当前分区

[18:08:02 root@libin3 ~]# lsblk

NAME                  MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                     8:0    0   40G  0 disk

├─sda1                  8:1    0  200M  0 part /boot

├─sda2                  8:2    0   12G  0 part

│ ├─cl-root           253:0    0   10G  0 lvm  /

│ └─cl-swap           253:1    0    2G  0 lvm  [SWAP]

└─sda3                  8:3    0    1G  0 part

sdb                     8:16   0    8G  0 disk

├─sdb1                  8:17   0    4G  0 part

└─sdb2                  8:18   0    2G  0 part

  └─vg_libin-lv_libin 253:2    0    2G  0 lvm  /mnt/rhce

sr0                    11:0    1  4.2G  0 rom  /run/media/root/CentOS 7 x86_64

2、使用dd创建一个文件

[18:12:37 root@libin3 ~]# dd if=/dev/sda3 of=/opt/swap_tmp.swap bs=1M count=1024

记录了1024+0 的读入

记录了1024+0 的写出

1073741824字节(1.1 GB)已复制,11.2764 秒,95.2 MB/

3、格式化swap文件

[18:12:56 root@libin3 ~]# mkswap /opt/swap_tmp.swap

正在设置交换空间版本 1,大小 = 1048572 KiB

无标签,UUID=5e25b023-0139-4fb3-b562-15f64b08d266

4、修改文件权限0600

[18:19:46 root@libin3 ~]# chmod 0600 /opt/swap_tmp.swap

5、挂载swap扩容的新swap分区

[18:19:52 root@libin3 ~]# swapon /opt/swap_tmp.swap

6、检验,成功

[18:20:21 root@libin3 ~]# free -h

              total        used        free      shared  buff/cache   available

Mem:           3.7G        1.0G        1.1G         69M        1.6G        2.4G

Swap:          3.0G          0B        3.0G

7、回退

[18:21:03 root@libin3 ~]# swapoff /opt/swap_tmp.swap

[18:22:00 root@libin3 ~]# rm -fr /opt/swap_tmp.swap

 

posted @ 2022-09-11 18:28  LB_运维技术  阅读(862)  评论(0编辑  收藏  举报