Linux系统KVM虚拟机实战LVM逻辑卷之扩展Swap交换分区
Posted 江湖有缘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux系统KVM虚拟机实战LVM逻辑卷之扩展Swap交换分区相关的知识,希望对你有一定的参考价值。
【Linux系统KVM虚拟机实战】LVM逻辑卷之扩展Swap交换分区
一、Swap分区介绍
Linux中Swap(即:交换分区),类似于Windows的虚拟内存,就是当内存不足的时候,把一部分硬盘空间虚拟成内存使用,从而解决内存容量不足的情况。
二、检查本地系统环境
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,要保证剩余内存>=swap的used,自我感觉swap会释放到内存中。
否则会报错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
以上是关于Linux系统KVM虚拟机实战LVM逻辑卷之扩展Swap交换分区的主要内容,如果未能解决你的问题,请参考以下文章