Add a swap partition to RedHat Linux

Environment Redhat 7.2, example:
Add a new swap partition
============Requirements:================================
swap partition capacity is 512 MiB

When your system starts, the swap partition should be able to mount automatically

Do not remove or modify other swap partitions that already exist in your system
================================================
1. First, check the current swap partition used in the system. I have only one. Now I want to add another one with a capacity of 512M...

[root@desktop ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/sda2                               partition   2047996 0   -1

2. Use the partition tool to partition a partition with a capacity of 512M. The main partition of the logical partition is OK. It is recommended to use the logical partition...

[root@desktop ~]# fdisk /dev/sda 
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.


Command (m for help): p  -->Take a look at the current partition status

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bffad

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    20482047    10240000   83  Linux
/dev/sda2        20482048    24578047     2048000   82  Linux swap / Solaris
/dev/sda3        24578048    25602047      512000   83  Linux

Command (m for help): n  -->Use n New partition
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e -->Select expansion zone
Selected partition 4
First sector (25602048-41943039, default 25602048): 
Using default value 25602048
Last sector, +sectors or +size{K,M,G} (25602048-41943039, default 41943039):  --->I'll give the rest of the space to this expansion zone
Using default value 41943039
Partition 4 of type Extended and of size 7.8 GiB is set

Command (m for help): n -->Press n Continue partition, start logical partition
All primary partitions are in use
Adding logical partition 5
First sector (25604096-41943039, default 25604096): 
Using default value 25604096
Last sector, +sectors or +size{K,M,G} (25604096-41943039, default 41943039): +512M  -->Enter the size of the partition I want. Here I am512M,Remember the one in front+Number
Partition 5 of type Linux and of size 512 MiB is set

Command (m for help): p  -->Look again

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bffad

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    20482047    10240000   83  Linux
/dev/sda2        20482048    24578047     2048000   82  Linux swap / Solaris
/dev/sda3        24578048    25602047      512000   83  Linux
/dev/sda4        25602048    41943039     8170496    5  Extended
/dev/sda5        25604096    26652671      524288   83  Linux

Command (m for help): t -->Press t Modify zone type
Partition number (1-5, default 5): 
Hex code (type L to list all codes): 82 -->Modified to swap Type, if you don't know the number, you can press l See
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p -->Reconfirm

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bffad

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    20482047    10240000   83  Linux
/dev/sda2        20482048    24578047     2048000   82  Linux swap / Solaris
/dev/sda3        24578048    25602047      512000   83  Linux
/dev/sda4        25602048    41943039     8170496    5  Extended
/dev/sda5        25604096    26652671      524288   82  Linux swap / Solaris

Command (m for help): w  -->Press w Save exit
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.
[root@desktop ~]#Partprobe > refresh disk information < very important >

3. Format the partition in swap format and enable

[root@desktop ~]# Mkswap / dev / sda5 -- > format
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=8a529678-5180-4c8c-bd2c-18677c573ba3
[root@desktop ~]# Swapon / dev / sda5 -- > Enable
[root@desktop ~]# Swapon - s -- > view the swap partition in use, it already exists
Filename                Type        Size    Used    Priority
/dev/sda2                               partition   2047996 0   -1
/dev/sda5                               partition   524284  0   -2

4. Configure power on auto mount

[root@desktop ~]# Blkid - > view the UUID of the swap partition to be mounted, and then copy
/dev/sda1: UUID="3c6e20fa-3e12-42ca-8dba-b12eee74e43e" TYPE="xfs" 
/dev/sda2: UUID="ff7d2e6d-c2d7-46a0-af09-70c85898ab46" TYPE="swap" 
/dev/sda3: UUID="4Ntfep-th0e-bCXr-Qo3b-EPJ1-SRsu-nfeOru" TYPE="LVM2_member" 
/dev/sda5: UUID="8a529678-5180-4c8c-bd2c-18677c573ba3" TYPE="swap" 
/dev/mapper/vg0-lv0: UUID="b2285e5e-de78-4392-945a-817843fd7f10" TYPE="ext3" 
[root@desktop ~]# VIM /etc/fstab -- > edit the mount file, as shown in the following figure
[root@desktop ~]# Mount - a -- > reload mount table
[root@desktop ~]# 


So far, finish........

Keywords: Linux REST vim

Added by Wintergreen on Sat, 04 Apr 2020 09:18:08 +0300