一、swap分區簡介
Swap分區在系統的實體記憶體不夠用的時候,把硬碟記憶體中的一部分空間釋放出來,以供目前運作的程式使用。那些被釋放的空間可能來自一些很長時間沒有什麼操作的程式,這些被釋放的空間被臨時儲存到Swap分區中,等到那些程式要運作時,再從Swap分區中恢複儲存的資料到記憶體中。衆所周知,現代作業系統都實作了“虛拟記憶體”這一技術,不但在功能上突破了實體記憶體的限制,使程式可以操縱大于實際實體記憶體的空間,更重要的是,“虛拟記憶體”是隔離每個程序的安全保護網,使每個程序都不受其它程式的幹擾。
通常情況下,Swap空間應大于或等于實體記憶體的大小,最小不應小于64M,通常Swap空間的大小應是實體記憶體的2-2.5倍。但根據不同的應用,應有不同的配置:如果是小的桌面系統,則隻需要較小的Swap空間,而大的伺服器系統則視情況不同需要不同大小的Swap空間。特别是資料庫伺服器和Web伺服器,随着通路量的增加,對Swap空間的要求也會增加,具體配置參見各伺服器産品的說明。
另外,Swap分區的數量對性能也有很大的影響。因為Swap交換的操作是磁盤IO的操作,如果有多個Swap交換區,Swap空間的配置設定會以輪流的方式操作于所有的Swap,這樣會大大均衡IO的負載,加快Swap交換的速度。如果隻有一個交換區,所有的交換操作會使交換區變得很忙,使系統大多數時間處于等待狀态,效率很低。用性能監視工具就會發現,此時的CPU并不很忙,而系統卻慢。這說明,瓶頸在IO上,依靠提高CPU的速度是解決不了問題的。
二、擴充SWAP分區的三種方法
一)、通過LVM擴容
通過LVM擴容SWAP分區前天條件為系統使用LVM管理磁盤,且vg卷組有可配置設定空間。
0、檢查swap大小
[[email protected] ~]# lvdisplay
— Logical volume —
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID MESyl1-3HeV-ZchW-dUm6-AMjp-vede-Yvimel
LV Write Access read/write
LV Creation host, time localhost, 2020-12-31 05:12:59 -0500
LV Status available
# open 2
LV Size 1.99 GiB
Current LE 510
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
目前SWAP分區大小2G
1、檢查卷組可用空間
[[email protected] ~]# vgdisplay
— Volume group —
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 12
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 2
Act PV 2
VG Size <27.66 GiB
PE Size 4.00 MiB
Total PE 7080
Alloc PE / Size 6142 / 23.99 GiB
Free PE / Size 938 / 3.66 GiB
VG UUID h7zLOd-BvJI-juZw-8DZt-f2Pe-Zxbz-1I36t7
卷組剩餘空間3.66G
2、取消swap分區挂載
[[email protected] ~]# swapoff /dev/mapper/centos-swap
[[email protected] ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 115M 1.5G 8.8M 140M 1.5G
Swap: 0B 0B 0B
3、檢查swap分區
[[email protected] ~]# e2fsck -f /dev/mapper/centos-swap
e2fsck 1.42.9 (28-Dec-2013)
ext2fs_open2: Bad magic number in super-block
e2fsck: Superblock invalid, trying backup blocks…
e2fsck: Bad magic number in super-block while trying to open /dev/mapper/centos-swap
The superblock could not be read or does not describe a correct ext2
filesystem. If the device is valid and it really contains an ext2
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
e2fsck -b 8193 <device>
4、擴容swap分區
[[email protected] ~]# lvextend -L 4G /dev/mapper/centos-swap
Size of logical volume centos/swap changed from 1.99 GiB (510 extents) to 4.00 GiB (1024 extents).
Logical volume centos/swap successfully resized.
5、格式化swap分區為swap格式
[[email protected] ~]# mkswap /dev/mapper/centos-swap
mkswap: /dev/mapper/centos-swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=7b41169c-bb9d-4f85-9dec-7c11ffb572a2
6、挂載swap分區
[[email protected] ~]# swapon /dev/mapper/centos-swap
7、檢查swap分區大小
[[email protected] ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 118M 1.5G 8.8M 140M 1.5G
Swap: 4.0G 0B 4.0G
二)、使用獨立的分區來實作
1、檢查确認目前swap大小
[[email protected] ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 117M 1.5G 8.8M 140M 1.5G
Swap: 2.0G 0B 2.0G
2、檢查确認空閑磁盤
[[email protected] ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 18.3G 0 part
├─centos-root 253:0 0 10G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 12G 0 lvm /home
sdb 8:16 0 10G 0 disk
sdc 8:32 0 10G 0 disk
└─sdc1 8:33 0 9.3G 0 part
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─centos-home 253:2 0 12G 0 lvm /home
sr0 11:0 1 4.3G 0 rom
空閑可用磁盤sdb
3、建立磁盤分區,磁盤格式為swap
[[email protected] ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +2G
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82
Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
4、通知系統分區表的變化
[[email protected] ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
4、将建立分區格式化為swap
[[email protected] ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=029ddda5-ca64-418a-b083-778341ecc3e5
注意把此處的 UUID=XXX 複制下來,第7步需要使用
5、挂載建立swap分區
[[email protected] ~]# swapon /dev/sdb1
6、檢查确認擴容後的swap大小
[[email protected] ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 118M 1.5G 8.8M 138M 1.5G
Swap: 4.0G 0B 4.0G
7、自動挂載swap分區
#vim /etc/fstab
添加如下内容後儲存,其中XXX為swap分區的UUID
UUID=XXXX swap swap defaults 0 0
本執行個體如下:
UUID=029ddda5-ca64-418a-b083-778341ecc3e5 swap swap defaults 0 0
三)、檔案模拟分區建立swap檔案系統
1、檢查swap分區大小
[[email protected] ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 116M 1.1G 8.8M 615M 1.5G
Swap: 4.0G 0B 4.0G
2、選擇有足夠剩餘空間的磁盤目錄
[[email protected] home]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 9.8G 1.2G 8.1G 13% /
devtmpfs 908M 0 908M 0% /dev
tmpfs 920M 0 920M 0% /dev/shm
tmpfs 920M 8.9M 911M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
/dev/sda1 976M 115M 794M 13% /boot
/dev/mapper/centos-home 12G 6.8G 4.4G 61% /home
tmpfs 184M 0 184M 0% /run/user/0
根分區和home分區有足夠剩餘空間
3、建立swap檔案
建立一個2G大小的swap檔案,檔案大小及為swap擴容大小,按需建立
[[email protected] /]# dd if=/dev/zero of=swapfile bs=1G count=2
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB) copied, 121.791 s, 17.6 MB/s
4、修改檔案權限為600
[[email protected] /]# chmod 600 swapfile
4、格式swapfile格式化為swap分區
[[email protected] /]# mkswap -L swap_file swapfile
Setting up swapspace version 1, size = 2097148 KiB
LABEL=swap_file, UUID=0e2fe387-e8d0-473a-b85f-7dffe2d6d8f9
5、挂載新的swap分區
[[email protected] /]# swapon /swapfile
6、檢查最新swap大小
[[email protected] /]# free -h
total used free shared buff/cache available
Mem: 1.8G 118M 1.1G 8.8M 619M 1.5G
Swap: 6.0G 0B 6.0G
7、自動挂載swap分區
#vim /etc/fstab
添加如下内容後儲存,其中XXX為swap分區的UUID
UUID=XXXX swap swap defaults 0 0
或者
/swapfile swap swap defaults 0 0
三、常用swap指令
#swapon -a 挂載所有配置在/etc/fstab中的swap分區
#swapon -s 檢視swap分區的資訊,順便看看自己剛剛建立的swap分區是否生效
#swapoff /dev/sdb1 取消swap分區sdb1挂載
#swapon /dev/sdb1 挂載swap分區sdb1
#mkswap /dev/sdb1 将磁盤分區sdb1格式化為swap分區
#swapon -V檢視swap指令工具版本