Swap partition in Linux is actually virtual memory in windows, when the server memory is not enough, it will use swap partition memory. to alleviate the situation of not having enough memory.
In the cloud server scenario, the servers provided to us by the cloud vendors are basically without swap partition.
Creating a Swap Partition
The swap partition uses the local hard disk and uses the capacity of the hard disk as memory. When the physical memory is relatively large, there are not enough resources. Then the swap partition also needs to be set larger to easily relieve memory pressure.
View the current swap partition
1
2
3
4
|
[root@ops ~]# free -mh
total used free shared buff/cache available
Mem: 15G 3.4G 10G 16M 1.6G 11G
Swap: 0B 0B 0B
|
Close swap partition
1
2
3
4
5
|
# In some scenarios, turning on swap partitioning will affect the normal operation of the service and can be turned off with the following command
# Temporary closed
swapoff -a
# Permanently closed
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
|
Creating a swap partition
As mentioned before, the swap partition actually uses the local hard disk, so the file size is still based on the local hard disk when we create it.
Everything is a file in linux.
1
2
3
4
5
6
7
|
[root@ops ~]# dd if=/dev/zero of=/opt/swapfile bs=1M count=8096
8096+0 records in
8096+0 records out
8489271296 bytes (8.5 GB) copied, 203.405 s, 41.7 MB/s
# Here we generate 8g swapfile file under /opt/ by dd command
# bs is the file size
# count the number of files
|
Next we modify the /opt/swapfile
format to swap-readable format.
1
2
3
4
5
6
7
|
[root@ops ~]# ls -lh /opt/swapfile
-rw-r--r-- 1 root root 8.0G May 17 18:02 /opt/swapfile
# Formatting files via mkswap
[root@ops ~]# mkswap /opt/swapfile
Setting up swapspace version 1, size = 8290300 KiB
no label, UUID=bdef23e2-0b8f-4bbb-85f7-67487b336525
# The file you just generated can be attached to the back
|
Mount swap partition
1
2
3
4
5
6
7
8
|
# This step is actually mostly the same as the drive initialization and mounting steps
[root@ops ~]# swapon /opt/swapfile
swapon: /opt/swapfile: insecure permissions 0644, 0600 suggested.
[root@ops ~]#
[root@ops ~]# free -mh
total used free shared buff/cache available
Mem: 15G 3.1G 2.6G 16M 9.8G 11G
Swap: 7.9G 0B 7.9G
|
Set boot up
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@ops ~]# echo "/opt/swapfile swap swap default 0 0" >>/etc/fstab
# View appended files
[root@ops ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Dec 16 21:18:04 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=2ca29691-58cc-49a2-81df-2f48c0a6c660 / xfs defaults 0 0
UUID=5745e5f0-aac7-4df1-9939-5e150b0c9421 /boot xfs defaults 0 0
/opt/swapfile swap swap default 0 0
|
Delete SWAP partition
Many times the swap partition may be something we use temporarily for a while and need to stop later in the test project. So how to turn off the mounted swap partition? ?
Stop using the swap partition.
1
2
3
4
5
|
[root@ops ~]# swapoff /opt/swapfile
[root@ops ~]# free -mh
total used free shared buff/cache available
Mem: 15G 3.1G 2.6G 16M 9.8G 11G
Swap: 0B 0B 0B
|
Delete the swap partition file.
1
|
[root@ops ~]# rm -rf /opt/swapfile
|
Remove swap setting from /etc/fstab
.
1
|
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
|