swap分区的作用:swap分区是在系统的物理内存不够用时,把硬盘空间中的一部分空间释放出来,以供当前运行的程序使用的。
[root@bogon ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-41943006, default = 10487808) or {+-}size{KMGTP}:
Last sector (10487808-41943006, default = 41943006) or {+-}size{KMGTP}: +5G
Current type is ‘Linux filesystem‘
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to ‘Linux filesystem‘
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
加载新分区
[root@bogon ~]# partx -a /dev/sdb
partx: /dev/sdb: error adding partition 1
格式化swap分区
[root@bogon ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 5242876 KiB
no label, UUID=8d003c6d-43d1-4e51-ab4a-bf60df5e4c9b
查看原有swap大小
[root@bogon ~]# free -m
total used free shared buff/cache available
Mem: 7803 445 6964 16 393 7057
Swap: 5119 0 5119
将磁盘空间加入swap
[root@bogon ~]# swapon /dev/sdb2
查看现有swap大小
[root@bogon ~]# free -m
total used free shared buff/cache available
Mem: 7803 449 6959 16 393 7052
Swap: 10239 0 10239
查看swap分区用到的磁盘
[root@bogon ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 5242876 0 -2
/dev/sdb2 partition 5242876 0 -3
[root@bogon ~]#
删除空间
[root@bogon ~]# free -m
total used free shared buff/cache available
Mem: 7803 449 6960 16 393 7053
Swap: 10239 0 10239
[root@bogon ~]# swapoff /dev/sdb2
[root@bogon ~]# free -m
total used free shared buff/cache available
Mem: 7803 445 6963 16 393 7056
Swap: 5119 0 5119
[root@bogon ~]#
[root@bogon ~]# dd if=/dev/zero of=swap_file bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 0.467293 s, 1.1 GB/s
[root@bogon ~]# ll /root/swap_file
-rw-r--r-- 1 root root 524288000 Dec 6 18:07 /root/swap_file
[root@bogon ~]# chmod 0600 /root/swap_file
[root@bogon ~]# mkswap -f /root/swap_file
Setting up swapspace version 1, size = 511996 KiB
no label, UUID=78e51afa-9ec8-483b-9be8-af83c212daf8
[root@bogon ~]# swapon /root/swap_file
[root@bogon ~]# free -m
total used free shared buff/cache available
Mem: 7803 445 6449 16 907 7049
Swap: 5619 0 5619
[root@bogon ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 5242876 0 -2
/root/swap_file file 511996 0 -3
原文:https://www.cnblogs.com/ai-fei-ai/p/14216014.html