The swap partition is used when the memory is not enough, and it can be expanded when the swap partition is not enough
Method 1: Implemented using separate partitions
1. lsblk to see if there are available disk partitions, use fdsik to divide the disk partition
2. The partition created by fdisk /dev/sdc is /dev/sdc2, pay attention to use the t option to modify the partition id to 82
3. Create swap file system
mkswap /dev/sdc2 -L swap_sdc2
4. vim /etc/fstab
:r!blkid /dev/sdc2 #Read the command output to a file and edit it again
UUID="6f50983b-1452-41fc-8efd-2151e4613959" swap swap defaults 0 0
5. swapon -a makes the swap partition in the edited /etc/fstab effective
Divergent thinking: In fact, whether the execution of mount -a takes effect or not depends on whether the option has auto, found in man 5 fstab (learn to use the man 5 configuration file to view configuration information, you deserve it!)
defaults
use default options: rw, suid, dev, exec, auto, nouser, and async.
noauto do not mount when "mount -a" is given (e.g., at boot time)
Extract key information: defaults: rw, suid, dev, exec, auto, nouser, and async.
noauto: do not mount when "mount -a" is given (eg, at boot time) You will know what it means by negating this sentence!
6. After rereading the mount information, use swapon -s to view the information of the swap file system
[root @ centos7 ~] #swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2047996 2800 -1
/dev/sdc2 partition 10484732 0 -2
Note that there is also a priority, the larger the value, the higher the priority
Divergent thinking: When using a swap partition, you should use the outer track (that is, the track number is smaller, and the outermost track is track 0), because the data access efficiency will be higher than the inner track. The specific reason is here No longer.
7. You can also use lsblk to view the status of the swap partition
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1000M 0 part /boot
├─sda2 8:2 0 9.8G 0 part /
├─sda3 8:3 0 2G 0 part [SWAP] #Original swap partition size
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 48.8G 0 part /data
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 2G 0 part
├─sdb2 8:18 0 2G 0 part
├─sdb3 8:19 0 3G 0 part
├─sdb4 8:20 0 1K 0 part
└─sdb5 8:21 0 2G 0 part
sdc 8:32 0 20G 0 disk
├─sdc1 8:33 0 10G 0 part
└─sdc2 8:34 0 10G 0 part [SWAP] #The size of the newly added swap partition
sr0 11: 0 1 3.7G 0 rom /run/media/root/CentOS_6.9_Final
sr1 11:1 1 8.1G 0 rom /run/media/root/CentOS 7 x86_64
8. Disable new swap points
# swapoff /dev/sdc1
Summary memory: swapon -a takes effect /etc/fstab to add swap partition
When fdisk partitions, pay attention to use t to modify the id of the disk to 82
swpaon -s View swap partition information
swapoff /dev/sdc1 disable swap partition
Method 2: File simulation partition to create swap file system
1. dd if=dev/zero of=/data/swap_file bs=1G count=5 Create a file with a size of 5G space
Divergent thinking: dd if=/dev/zero of=/dev/sdc1 bs=512 count=1 can kill MBR information, you can delete partition information like this, or you can use fdisk /dev/sdc to use d option to delete
Of course, it is also possible to directly fill the 64bytes partition table information with zeros without deleting the bootloader information (446bytes), such as:
dd if=/dev/zero of=/dev/sdc1 bs=1 count=64 seek=446 Fill 64bytes directly to the partition table, skip the first 446bytes of the target /dev/sdc1
Note that you cannot use dd if=/dev/zero of=/dev/sdc1 bs=64 count=1 seek=446
hexdump -C /dev/sda -n 512 -v displays the first 512 bytes of information on the sda partition
The routine of Sunflower Collection: Remember to back up the MBR before playing! Hahaha, dd if=/dev/sdc1 count=/data/mbr.bak bs=512 count=1 (default unit is bytes)
When restoring, you can directly dd if=/dev/mbr.bak of=/dev/sdc1 without giving bs and count, because there is only 512 bytes of backup information, right?
You can also use dd if=/dev/zero of=/dev/null bs=1G count=2 This method can test the read and write speed of the disk, if you are interested, you can try it
2. mkswap -L swap_file /data/swap_file to create a swap file system
3. In order to take effect permanently, editing the configuration file /etc/fstab, and modifying the configuration file for permanent effect configuration seems to be the iron law of linux, what do you think? Don't use UUIDs because you are using file-emulated partitions!
vim /etc/fstab
/data/file2 swap swap defaults 0 0
4. swapon -a reads the swap partition information in /etc/fstab
5.swapon -s View the information of the swap partition, and by the way, check whether the swap partition you just created takes effect.
6.swapoff /data/swap_file If you don’t want to play, you can disable it directly