Home > Server Security Guides > Swap ( Creating , Deleting & adding more swap space in live server )

Swap ( Creating , Deleting & adding more swap space in live server )

October 7th, 2009

To create  a swap partations

1. Create a partations using fdisk /dev/hd(a,b) 1,2

2.Now that you have the swap partition, use the command mkswap to setup the swap partition. At a shell prompt as root, type the following:
mkswap /dev/hdb2

3.To enable the swap partition immediately, type the following command:

swapon /dev/hdb2

4.To enable it at boot time, edit /etc/fstab to include:

/dev/hdb2               swap                    swap    defaults        0 0

The next time the system boots, it will enable the new swap partition.

5. After adding the new swap partition and enabling it, make sure it is enabled by viewing the output of the command cat /proc/swaps or free.
===========================================================================
How to create a swap file
To add a swap file:

1.Determine the size of the new swap file and multiple by 1024 to determine the block size. For example, the block size of a 64 MB swap file is 65536.

2.At a shell prompt as root, type the following command with count being equal to the desired block size:

dd if=/dev/zero of=/swapfile bs=1024 count=65536

3.Setup the swap file with the command:

mkswap /swapfile

4.To enable the swap file immediately but not automatically at boot time:

swapon /swapfile

5.To enable it at boot time, edit /etc/fstab to include:

/swapfile               swap                    swap    defaults        0 0

The next time the system boots, it will enable the new swap file.
6.After adding the new swap file and enabling it, make sure it is enabled by viewing the output of the command cat /proc/swaps or free.

==============================================================================================

removing swap partations

To remove a swap partition:

1.The hard drive can not be in use (partitions can not be mounted, and swap space can not be enabled). The easiest way to achieve this it to boot your system in rescue mode. Refer to Chapter 9 Basic System Recovery for instructions on booting into rescue mode. When prompted to mount the file system, select Skip.

Alternately, if the drive does not contain any partitions in use, you can unmount them and turn off all the swap space on the hard drive with the swapoff command.

2. At a shell prompt as root, execute the following command to make sure the swap partition is disabled (where /dev/hdb2 is the swap partition):

swapoff /dev/hdb2

3. Remove its entry from /etc/fstab.

4. Remove the partition using parted or fdisk. Only parted will be discussed. To remove the partition with parted:

5. At a shell prompt as root, type the command parted /dev/hdb, where /dev/hdb is the device name for the hard drive with the swap space to be removed.

6. At the (parted) prompt, type print to view the existing partitions and determine the minor number of the swap partition you wish to delete.

At the (parted) prompt, type rm MINOR, where MINOR is the minor number of the partition you want to remove.

Warning    Warning

Changes take effect immediately; you must type the correct minor number.

Type quit to exit parted.

To remove a swap file:

1.At a shell prompt as root, execute the following command to disable the swap file (where /swapfile is the swap file):

swapoff /swapfile

2. Remove its entry from /etc/fstab.
3. Remove the actual file:

rm /swapfile

=====================================================================================

How to increase the swap space in live server ?

1) [root@svr92 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda7                               partition       2040212 135292  -1

If you want to make 2 Gb file it should be calculated as 2000 * 1024

2) [root@svr92 ~]# dd if=/dev/zero of=/swapfile01 bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 189.598 seconds, 11.1 MB/s

3) [root@svr92 ~]# ls -l /swapfile01
-rw-r–r– 1 root root 2097152000 Apr 15 01:21 /swapfile01
[root@svr92 ~]# mkswap /swapfile01
Setting up swapspace version 1, size = 2097147 kB

4) [root@svr92 ~]# free
total       used       free     shared    buffers     cached
Mem:       3369592    3241120     128472          0      16232     528564
-/+ buffers/cache:    2696324     673268
Swap:      2040212     737076    1303136
5) [root@svr92 ~]# swapon /swapfile01

6) [root@svr92 ~]# free
total       used       free     shared    buffers     cached
Mem:       3369592    3245052     124540          0      24204     508808
-/+ buffers/cache:    2712040     657552
Swap:      4088204     861776    3226428

Add the line to /etc/fstab

/swapfile01             none                    swap    sw              0 0

So that swap will be up  once rebooting the server

===============================================================================

Server Security Guides

  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.