【Linux系统KVM虚拟机实战】LVM逻辑卷之扩展Swap交换分区
一、Swap介绍
Swap是Linux系统中的一种虚拟内存技术,它允许将一部分硬盘空间用作内存扩展,用于暂时存储不常用的数据。当系统的物理内存(RAM)不足时,操作系统会将一部分内存中的数据移动到Swap分区中,以释放RAM供其他进程使用。Swap分区通常是一个独立的分区,也可以是一个文件。它的大小可以根据系统需要进行调整,但一般建议设置为物理内存的两倍。Swap分区的使用是透明的,对应用程序和用户来说是无感知的。
二、本次实践介绍
1. 环境规划
hostname | IP地址 | 操作系统版本 | 角色 |
---|---|---|---|
server | 192.168.3.158 | centos7.6 | 宿主机 |
server001 | 192.168.3.157 | centos7.6 | KVM虚拟机 |
2.实践介绍
1.本次实践为个人测试环境,生产环境请谨慎;
2.本次实践为KVM虚拟机扩展Swap交换分区大小。
三、检查本地系统环境
1.检查系统版本
[root@server001 ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
2.检查系统内核
[root@server001 ~]# uname -r
3.10.0-957.el7.x86_64
四、检查本地磁盘状态
1.检查swap大小
[root@server001 ~]# free -m
total used free shared buff/cache available
Mem: 1837 1340 97 2 399 291
Swap: 2047 1046 1001
2.检查物理磁盘分区
使用lsblk命令检查磁盘分区情况
[root@server001 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 4.3G 0 rom
vda 252:0 0 150G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 149G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 97G 0 lvm /home
3.查看PV状态
[root@server001 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/vda2
VG Name centos
PV Size <149.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 38143
Free PE 1
Allocated PE 38142
PV UUID kZK4n1-OQbf-7uSQ-f0QI-PwfF-W8bh-zDQPNY
4.查看VG卷组状态
[root@server001 ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size <149.00 GiB
PE Size 4.00 MiB
Total PE 38143
Alloc PE / Size 38142 / 148.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID nxeB2C-4fW1-HijW-n2Bi-P1pN-p2Cv-rvjYZ4
5.查看LVM逻辑卷状态
[root@server001 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID 71m8Iu-dxW6-dGiV-jnkC-ZCQn-BEAA-Yqnf3O
LV Write Access read/write
LV Creation host, time kvm01, 2022-11-15 00:58:26 +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
--- Logical volume ---
LV Path /dev/centos/home
LV Name home
VG Name centos
LV UUID Of3kcY-tc4y-tyx5-VuBh-d7F7-10Zk-qZFHOE
LV Write Access read/write
LV Creation host, time kvm01, 2022-11-15 00:58:26 +0800
LV Status available
# open 1
LV Size 96.99 GiB
Current LE 24830
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID hawN56-mPFc-5dPA-NB72-sHYR-hwyF-okC1DL
LV Write Access read/write
LV Creation host, time kvm01, 2022-11-15 00:58:27 +0800
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12800
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
五、给KVM虚拟机新增硬盘
1.查看虚拟机列表
给KVM虚拟机新增硬盘操作,都在宿主机上进行操作。
[root@server ~]# virsh list --all
Id Name State
----------------------------------------------------
7 kvm01 running
- ikuai shut off
- KVM-qunhui shut off
2.关闭KVM虚拟机
[root@server ~]# virsh shutdown kvm01
Domain kvm01 is being shutdown
3.查看KVM虚拟机磁盘位置
[root@server ~]# virsh dumpxml kvm01 |grep file
<disk type='file' device='disk'>
<source file='/nas/kvm/centos7.img'/>
<disk type='file' device='cdrom'>
<source file='/storage/kvm/CentOS-7-x86_64-DVD-1810.iso'/>
4.创建磁盘文件
[root@server ~]# qemu-img create -f qcow2 /nas/kvm/swap.qcow2 20G
Formatting '/nas/kvm/swap.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off
5.给虚拟机新增硬盘
给虚拟机新增硬盘,配置永久生效。
[root@server ~]# virsh attach-disk kvm01 /nas/kvm/swap.qcow2 vdb --subdriver=qcow2 --config
Disk attached successfully
6.再次查看虚拟机磁盘信息
[root@server ~]# virsh dumpxml kvm01 |grep file
<disk type='file' device='disk'>
<source file='/nas/kvm/centos7.img'/>
<disk type='file' device='disk'>
<source file='/nas/kvm/swap.qcow2'/>
<disk type='file' device='cdrom'>
<source file='/storage/kvm/CentOS-7-x86_64-DVD-1810.iso'/>
7.KVM虚拟机开机
[root@server ~]# virsh start kvm01
Domain kvm01 started
六、关闭当前swap分区
1.清理系统内存占用程序
关闭交换分区(swapoff)前,请确保剩余内存空间大于等于已使用的交换分区空间。关闭交换分区后,交换分区的内容会释放到内存中。如果内存剩余空间不足,可能会导致报错:swapoff failed: Cannot allocate memory。确保物理内存剩余充足后,我们可以查看内存状态。
[root@server001 ~]# free -m
total used free shared buff/cache available
Mem: 1837 156 1113 1 567 1493
Swap: 2047 46 2001
2.关闭swap
[root@server001 ~]# swapoff /dev/mapper/centos-swap
[root@server001 ~]# swapon -s
3.再次查看内存状态
[root@server001 ~]# free -m
total used free shared buff/cache available
Mem: 1837 177 1081 8 578 1465
Swap: 0 0 0
七、扩展swap分区大小
1.查看系统硬盘情况
[root@server001 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 4.3G 0 rom
vda 252:0 0 150G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 149G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm
└─centos-home 253:2 0 97G 0 lvm /home
vdb 252:16 0 20G 0 disk
2.扩展卷组
[root@server001 ~]# pvcreate /dev/vdb
Physical volume "/dev/vdb" successfully created.
[root@server001 ~]# vgextend centos /dev/vdb
Volume group "centos" successfully extended
3.扩展swap逻辑卷
扩展逻辑卷,增加6G大小空间。
[root@server001 ~]# lvextend -L +6G /dev/centos/swap
Size of logical volume centos/swap changed from 2.00 GiB (512 extents) to 8.00 GiB (2048 extents).
Logical volume centos/swap successfully resized.
4.格式化分区
[root@server001 ~]# mkswap /dev/centos/swap
mkswap: /dev/centos/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 8388604 KiB
no label, UUID=82c55c8a-0b9c-4b57-82dc-570354fb90d3
5.启动swap分区
swapon /dev/mapper/centos-swap
6.查看swap状态
[root@server001 ~]# free -m
total used free shared buff/cache available
Mem: 1837 1509 62 10 265 122
Swap: 8191 1 8190