How to create a swap partition in Linux / How to remove a swap partition

Swap Partation

In this article we will create swap space using fdisk command. In previous article you have learnt how to create partitions with fdisk, just one more additional step is required to set up that partition for swap space. Before you start making a swap partition make sure that you have enough free and unparted space to create new partition.
Swap memory is required when system requires more memory
than it is physically available, the kernel swaps out less used pages and gives
memory to the current process that needs the memory immediately. So a page of
memory is copied to the pre-configured space on the hard disk. Disk speed is
much slower compared to memory speed. Swapping pages give more space for
current application in the memory (RAM) and make application run faster.

Creating a swap partition

 
Create a normal partition using a fdsik command and
change hex code to make it swap partition.
  • The hex code for SWAP is 82.
  • Note: To change the hex code use t in fdisk and list all the hex code
    use l

 

[root@linuxelearn
~]# fdisk /dev/sda
WARNING:
DOS-compatible mode is deprecated. It’s strongly recommended to
         switch off the mode (command ‘c’)
and change display units to
         sectors (command ‘u’).
Command (m for help): n
First
cylinder (1757-2088, default 1757):
Using
default value 1757
Last
cylinder, +cylinders or +size{K,M,G} (1757-2088, default 2088): +500M
Command (m for help): t
Partition
number (1-7): 7
Hex
code (type L to list codes): 82
Changed
system type of partition 8 to 82 (Linux swap / Solaris)
Command (m for help): p
Disk
/dev/sda: 17.2 GB, 17179869184 bytes
255
heads, 63 sectors/track, 2088 cylinders
Units
= cylinders of 16065 * 512 = 8225280 bytes
Sector
size (logical/physical): 512 bytes / 512 bytes
I/O
size (minimum/optimal): 512 bytes / 512 bytes
Disk
identifier: 0x000efa3d
   Device Boot      Start         End      Blocks
Id  System
/dev/sda1   *
1          26      204800
83  Linux
Partition
1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000
83  Linux
/dev/sda3            1301        1562     2097152
82  Linux swap / Solaris
/dev/sda4            1562        2088     4228884    5
Extended
/dev/sda5            1562        1626      517837+
82  Linux swap / Solaris
/dev/sda6            1627        1691      522081
8e  Linux LVM
/dev/sda7            1692        1756      522081
82  Linux swap / Solaris
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.
[root@linuxelearn
~]#
Format the newly created swap partition with swap file system
 
#mkswap /dev/sda7
[root@linuxelearn
~]# mkswap /dev/sda7
Setting
up swapspace version 1, size = 522076 KiB
no
label, UUID=ca233655-0666-4653-9768-f98fe7e9cb45
[root@linuxelearn
~]#
Start
/ turn on the newly created swap space and verify it.
 
To turn on the swap space the syntax is
#swapon /dev/sda7
[root@linuxelearn
~]# swapon /dev/sda7
To
Verify the newly added swap space use the below commands:
 
    #cat /proc/swaps
    or
    #swapon –s
 
[root@linuxelearn
~]# swapon -s
Filename                                Type            Size    Used
Priority
/dev/sda3                               partition       2097144 0       -1
/dev/sda5                               partition       517828
0       -2
/dev/sda8                               partition       522072
0       -3
[root@linuxelearn
~]# free -m
             total       used       free     shared
buffers     cached
Mem:           997        512        484          0         75        159
-/+
buffers/cache:        277        720
Swap:         3063          0
3063
[root@linuxelearn
~]#
Making
the newly created Swap Partition to mount after reboot
 
In order to make the swap partition mount automatic
after reboot, we need to make an entry in /etc/fstab
file.
#vim
/etc/fstab
 
#
#
/etc/fstab
#
Created by anaconda on Sat Aug 13 17:36:51 2016
#
#
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=4afa457a-d662-4e89-b4e2-09303c44c3eb
/                       ext4    defaults        1 1
UUID=752c2f33-fe60-4269-9480-b27260fc3777
/boot                   ext4    defaults        1 2
UUID=496b4058-1c64-47e0-97f8-8760f41aaef8
swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
oproc                    /proc                   proc    defaults        0 0
/dev/sda5               swap                    swap    defaults        0 0
/dev/sda7               swap                    swap    defaults       0
0
How
to remove the swap partition in Linux?
 
To remove  the
swap partition use the following steps.
Deactivate the swap partition using a following
command.
#swapoff
<device name>
#swapoff
/dev/sda7
 
[root@linuxelearn
~]# swapoff /dev/sda7
[root@linuxelearn
~]#
  • Remove the entery from the /etc/fstab
  • Delete the partition through fdisk.

 

How
to increase swap into LVM Partition ?
 
Use the following steps to increase the Swap for LVM
  •     # swapoff
    -v /dev/kernalvg/swappart
  •     # lvm
    lvresize /dev/kernalvg/swappart -L +5G (increase from 5 GB to 10 GB)
  •     # mkswap
    /dev/kernalvg/swapvol
  •     # swapon
    -va

 

How
to remove or delete a Swap Partition for LVM
 
Use the following steps to delete/ remove the Swap
partition for LVM
  • Step 1

 

    swapoff -v
/dev/VG01/LV01
  • Step 2

 

Then you need to delete the swap partition entirely.
    #lvremove
/dev/VG01/LV01
  • Step 3

 

Remove the following entry from your /etc/fstab
file.
sysfs                   /sys                    sysfs   defaults        0 0
oproc                    /proc                   proc    defaults        0 0
/dev/sda5               swap                    swap    defaults        0 0
/dev/VG01/LV01               swap                    swap    defaults       0
0

 

You Like to read this Also…..

 

Leave a Reply

Your email address will not be published. Required fields are marked *