Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

6.6 Add swap partitions


May 23, 2021 That's what Linux should learn



SWAP partitioning is a technique designed to address real-world physical memory shortages by pre-dividing a certain amount of space in the hard disk and then temporarily storing data that is temporarily not commonly used in memory on the hard disk to free up physical memory space for more active program services. However, since the swap partition is after all, the data is read and written through the hard disk device, the speed is certainly slower than the physical memory, so only when the real physical memory is exhausted will the resources of the swap partition be called.

Swap partitions are created very similar to the previous procedure for mounting and using storage devices. B efore partitioning /dev/sdb storage devices, it is necessary to talk about the partitioning recommendations of switching partitions: In a production environment, the size of the swap partition is generally 1.5 to 2 times the real physical memory, in order to let everyone more obviously feel the changes in the swap partition space, here to take out a size of 5GB of the main partition as the switching partition resources. Save and exit once the partition is created:

[root@linuxprobe ~]# 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. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xb3d27ce1. Command (M for Help): N Partition Type: P Primary (1 Primary, 0 Extended, 3 Free) E Extendedselect (Default P): P Partition Number (2-4, Default 2): First Sector (4196352-41943039, default4196352: This hit the Enter Using Default Value 4196352 Last Sector, + Sectors OR + Size {K, M, G} (4196352-41943039): + 5G Partition 2 of Type Linux and of size 5 GIB ISSET Command (M for Help): P Disk / dev / sdb: 21.5 GB, 21474836480 Bytes, 41943040 Sects Units = Sectors of 1 * 512 = 512 BYTES SIZE (Logical / Physical): 512 Bytes / 512 BYtes I / O Size(minimum / optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xb0ced57f Device Boot Start End Blocks Id System / dev / sdb1 2048 4196351 2097152 83 Linux / dev / sdb2 4196352 14682111 5242880 83 Linux Command (m for help) : w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. Use the SWAP partition-specific format command MKSWAP to format the new home partition:

the root@linuxprobe/sdb2 Set up swapspace version 1, size s 5242876 KiB no label, UUID s 2972f9cb-17f0-4113-84c6-c64b97c40c75 uses the swapon command to officially mount the ready SWAP partition system. We can use the free -m command to see how the size of the swap partition has changed (from 2047MB to 7167MB):

free root@linuxprobe -m total free shared buffers cached Mem: 1483 782 701 9 0 254 -/+buffers/cache: 526 957 Swap: 2047 0 2047 (root@linuxprobe s) sdb2 (root@linuxprobe) Free -m total use free shared buffers cached Mem: 1483 785 697 9 0 254 -/buffers/cache: 530 953 Swap: 7167 0 7167 In order for the new swap partition device to remain in effect after reboot, you need to write the relevant information to the profile in the following format and remember to save:

[root@linuxprobe ~]# vim /etc/fstab #

/etc/fstab

  1. # Created by anaconda on Wed May 4 19:26:23 2017
  2. #
  3. # Accessible filesystems, by reference, are maintained under '/dev/disk'
  4. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  5. #
  6. /dev/mapper/rhel-root / xfs defaults 1 1
  7. UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b /boot xfs defaults 1 2
  8. /dev/mapper/rhel-swap swap swap defaults 0 0
  9. /dev/cdrom /media/cdrom iso9660 defaults 0 0
  10. /dev/sdb1 /newFS xfs defaults 0 0
  11. /dev/sdb2 swap swap defaults 0 0