How to increase Swap in Linux / CentOS / Redhat / Ubuntu

0

Hi here we will go to know about the swap file system, and how to increase?

First thing, what is swap?

Swap is one type of file system (id=82), which is used as the virtual ram for the system, it provide the extra memory resource to system when it required. In windows it’s called pagefile.sys, and this file system will be created manually or automatically during installation of operating system.

Once swap file system created, you may want to increase the swap space. For that you can follow this tutorial.

In two methods we can create the swap space.

  • Using swap partition
  • Using swap file

Here we use the first method.

Let check the available swap space.

root@client ~]# swapon -s

Filename Type Size Used Priority

/dev/sda2 partition 2096472 0 -1

I am going to increase the swap form 2GB to 3GB.by the following command also. As per this command total swap space is 2GB.

[root@client ~]# free -m

total used free shared buffers cached

Mem: 434 427 6 0 17 242

-/+ buffers/cache: 167 266

Swap: 2047 0 2047

As in the above /dev/sda2 is the swap file system. In my machine I don’t have any space in my primary HDD, so I use another HDD for demonstration. But you can use the same HDD for new swap partition, if you have enough space.

Let’s see the HDD’s attached to this machine.

[root@client ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes

255 heads, 63 sectors/track, 3916 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux

/dev/sda2 14 274 2096482+ 82 Linux swap / Solaris

/dev/sda3 275 529 2048287+ 83 Linux

/dev/sda4 530 3916 27206077+ 5 Extended

/dev/sda5 530 1549 8193118+ 83 Linux

/dev/sda6 1550 2569 8193118+ 83 Linux

/dev/sda7 2570 2824 2048256 83 Linux

/dev/sda8 2825 3079 2048256 83 Linux

/dev/sda9 3080 3916 6723171 83 Linux

Disk /dev/sdb: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn’t contain a valid partition table

In the above I have one extra HDD with free space, with the help of the HDD I am going to create the new partition for swap size of 1 GB and assigning the id of 82. The size may be depending on your swap size requirement.

Let create the partition with id of 82 for swap.

[root@client ~]# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. Changes will remain in memory only,

until you decide to write them. After that, of course, the previous

content won’t be recoverable.

The number of cylinders for this disk is set to 1044.

There is nothing wrong with that, but this is larger than 1024,

and could in certain setups cause problems with:

1) software that runs at boot time (e.g., old versions of LILO)

2) booting and partitioning software from other OSs

(e.g., DOS FDISK, OS/2 FDISK)

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): p  —–> To See the Details

Disk /dev/sdb: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

Command (m for help): n —-–> To create the new partition

Command action

e extended

p primary partition (1-4)

p   —–>To create the Primary Partition

Partition number (1-4): 1 —–> Partition Number

First cylinder (1-1044, default 1):

Using default value 1

Last cylinder or +size or +sizeM or +sizeK (1-1044, default 1044): +1000M

Command (m for help):

Command (m for help): p —–> List the Partition

Disk /dev/sdb: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sdb1 1 123 987966 83 Linux

Command (m for help): t —–> Change the partition ID

Selected partition 1

Hex code (type L to list codes): l —–> List the code

0 Empty 1e Hidden W95 FAT1 80 Old Minix bf Solaris

1 FAT12 24 NEC DOS 81 Minix / old Lin c1 DRDOS/sec (FAT-

2 XENIX root 39 Plan 9 82 Linux swap / So c4 DRDOS/sec (FAT-

3 XENIX usr 3c PartitionMagic 83 Linux c6 DRDOS/sec (FAT-

4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: c7 Syrinx

5 Extended 41 PPC PReP Boot 85 Linux extended da Non-FS data

6 FAT16 42 SFS 86 NTFS volume set db CP/M / CTOS / .

7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set de Dell Utility

8 AIX 4e QNX4.x 2nd part 88 Linux plaintext df BootIt

9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM e1 DOS access

a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e3 DOS R/O

b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e4 SpeedStor

c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS eb BeOS fs

e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi ee EFI GPT

f W95 Ext’d (LBA) 54 OnTrackDM6 a5 FreeBSD ef EFI (FAT-12/16/

10 OPUS 55 EZ-Drive a6 OpenBSD f0 Linux/PA-RISC b

11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f1 SpeedStor

12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f4 SpeedStor

14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f2 DOS secondary

16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot fb VMware VMFS

17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE

18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto

1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep

1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT

Hex code (type L to list codes): 82 —–> Id for swap

Changed system type of partition 1 to 82 (Linux swap / Solaris)

Command (m for help): p —–> List the partitions

Disk /dev/sdb: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sdb1 1 123 987966 82 Linux swap / Solaris

Command (m for help): w ——> Write the changes

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

Tell the changes to kernel.

[root@client ~]# partprobe

Confirm the created partition.

[root@client ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes

255 heads, 63 sectors/track, 3916 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux

/dev/sda2 14 274 2096482+ 82 Linux swap / Solaris

/dev/sda3 275 529 2048287+ 83 Linux

/dev/sda4 530 3916 27206077+ 5 Extended

/dev/sda5 530 1549 8193118+ 83 Linux

/dev/sda6 1550 2569 8193118+ 83 Linux

/dev/sda7 2570 2824 2048256 83 Linux

/dev/sda8 2825 3079 2048256 83 Linux

/dev/sda9 3080 3916 6723171 83 Linux

Disk /dev/sdb: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sdb1 1 123 987966 82 Linux swap / Solaris

Make the swap partition using following command.

[root@client ~]# mkswap /dev/sdb1

Setting up swapspace version 1, size = 1011671 kB

Enable it using following command.

[root@client ~]# swapon /dev/sdb1

List the swap space available on the machine.

[root@client ~]# swapon -s

Filename Type Size Used Priority

/dev/sda2 partition 2096472 0 -1

/dev/sdb1 partition 987956 0 -2

In the above you can find /dev/sdb1 has activated and serves the swap resource to the machine with /dev/sda2.

Use the following command to see the memory available on the machine. In the follow you will be able see the actual memory and virtual memory also.

[root@client ~]# free -m

total used free shared buffers cached

Mem: 434 430 3 0 17 246

-/+ buffers/cache: 166 267

Swap: 3012 0 3012

As per the above total size of swap is 3GB, this is as per our requirement.

If would like to off the swap, you can use the following command.

[root@client ~]# swapoff /dev/sda2

List the swap space. Because we swap off the above one, you will be able to see he one swap partition which we created now.

[root@client ~]# swapon -s

/dev/sdb1 partition 987956 0 -2

In normal case, if you restart the server the swap will not get on automatically. So put it on /etc/fstab

[root@client ~]# vi /etc/fstab

LABEL=/ / ext3 defaults 1 1

LABEL=/tmp /tmp ext3 defaults 1 2

LABEL=/home /home ext3 defaults,usrquota,grpquota 1 2

LABEL=/usr /usr ext3 defaults 1 2

LABEL=/var /var ext3 defaults 1 2

LABEL=/boot /boot ext3 defaults 1 2

tmpfs /dev/shm tmpfs defaults 0 0

devpts /dev/pts devpts gid=5,mode=620 0 0

sysfs /sys sysfs defaults 0 0

proc /proc proc defaults 0 0

LABEL=SWAP-sda2 swap swap defaults 0 0

/dev/sda9 /opt ext3 defaults 0 0

###########New Swap Partion###############

/dev/sdb1 swap swap defaults 0 0

Reboot the server.

[root@client ~]# reboot

Verify it.

[root@client ~]# free -m

total used free shared buffers cached

Mem: 434 430 3 0 17 246

-/+ buffers/cache: 166 267

Swap: 3012 0 3012

If you want to make the priority of swap usage by system, use the following command. High priority will be use more by system. High=10 low= -10.

[root@client ~]# swapon -p 10 /var/swapfile

Verify the priority.

[root@client ~]# swapon -s

Filename Type Size Used Priority

/dev/sdb1 partition 1023992 0 10

/dev/sda2 partition 2096472 296 -2

That’s all


You might also like