生成swap分区
可以通过磁盘分区和文件两种方式进行操作
1. 通过磁盘分区
- 划出一块4G磁盘fdisk /dev/sdb
sdb磁盘对应的文件
[root@centosgpt ~]# fdisk /dev/sdb
Command (m for help): n
Partition number (1-128, default 1):
First sector (34-41943006, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943006, default 41943006): +4G
Created partition 1
...
Command (m for help): p
...
# Start End Size Type Name
1 2048 8390655 4G Linux filesyste
..
Command (m for help): w
The partition table has been altered!
- 创建分区mkswap /dev/sdb1
sdb1对应分区
[root@centosgpt ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 1020 KiB
no label, UUID=5dabc5b1-d763-4757-9e0c-383f0cacb0ea
2. 通过文件生成
- 生成文件dd if=/dev/zero of=/swap bs=1MB count=1024
bs 以1M为单位, count多少个1MB块
[root@centosgpt ~]# dd if=/dev/zero of=/swap bs=1MB count=1024
1024+0 records in
1024+0 records out
1024000000 bytes (1.0 GB) copied, 1.36373 s, 751 MB/s
- 赋予权限chown root:root /swap
chmod 600 /swap
[root@centosgpt ~]# chown root:root /swap
[root@centosgpt ~]# chmod 600 /swap
- 创建分区
mkswap -L swap /swap
[root@centosgpt ~]# mkswap -L swap /swap
Setting up swapspace version 1, size = 999996 KiB
LABEL=swap, UUID=e382380e-c729-40e6-bf3b-823d5cd9b55f
3. 通过LVM2创建
1. Create the LVM2 logical volume of size 2 GB: # lvcreate VolGroup00 -n LogVol02 -L 2G 2. Format the new swap space: # mkswap /dev/VolGroup00/LogVol02
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/storage_administration_guide/ch-swapspace
开启关闭swap分区
1.开启分区
swapon -s
[root@centosgpt ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 4194300 0 -2
[root@centosgpt ~]# swapon /dev/sdb1
[root@centosgpt ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 4194300 0 -2
/dev/sdb1 partition 1020 0 -3
2 永久生效
/etc/fstab
[root@centosgpt ~]# cat /etc/fstab |grep swap
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/sdb1 none swap sw 0 0
3.关闭分区
swapoff
[root@centosgpt ~]# swapon
NAME TYPE SIZE USED PRIO
/swap file 976.6M 0B -2
/dev/sdb1 partition 1020K 0B -3
/dev/dm-1 partition 4G 0B -4
[root@centosgpt ~]# swapoff /swap
[root@centosgpt ~]# swapon
NAME TYPE SIZE USED PRIO
/dev/sdb1 partition 1020K 0B -2
/dev/dm-1 partition 4G 0B -3
[root@centosgpt ~]# swapoff /dev/sdb1
[root@centosgpt ~]# swapon
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 4G 0B -2
swappiness参数
swappiness参数指导如何使用swap分区, 值越小越偏向使用物理内存, 越大越趋向使用swap分区.
1. 查看
[root@centosgpt ~]# sysctl -n vm.swappiness
30
cat /proc/sys/vm/swappiness
1. 临时调整
sysctl vm.swappiness=40
2. 永久调整
/etc/sysctl.conf
vm.swappiness = 10
sysctl -p
参考引用
Linux交换空间(swap space)的那些优缺点
Linux 创建交换(swap)分区
Be First to Comment