Pages

Tuesday, August 2, 2011

SWAP

Share it Please

Systems now are currently having large number of hard disk sizes and memory. Linux divides the Physical RAM into parts called pages. Swapping is a process of moving a page into the space which is allocated on hard disk (this space is called swap) to free up space in ram and to allocate memory for the newly started programs.

The main advantage of using of swap is that, when ever an application needs a part of the memoy, the Operating system takes out a few pages that are less used and send them to the swap space and allocate the memory to the new application. When the old pages are needs it just takes them back from swap.

The main disadvantage of using swap is that disk speed is slow when compared with memory which sometimes may slow down the swap access. 

There are 2 ways of creating swap in Linux

Swap Partition
In this case we create a new partition and allocate swap from that partition. By using a partition as a swap, we can make multiple operating system on the same box use this swap partition.

Swap File
In this case, we create a separate file on the file system and allocate that as a swap. The advantage would be that we can resize the file at any time where as in case of swap partition it would be a little difficult.

In this article, we will see how we can configure Swap using a partition. In the next article we will see how we can create swap using file.

Create Swap Using Partition

First we need to make sure how much amount of swap is already allocated to the system (may be created during the installation of operating System).for this we can use “free” command in Linux,

[root@localhost ~]# free
            total       used       free     shared    buffers     cached
Mem:            2042720     478248    1564472          0      31516     302536
-/+ buffers/cache:     144196    1898524
Swap:      2048248          0    2048248
 
In this command, we can see that the allocated swap size is around 2.5GB.This does not give any information whether the swap created was a partition or a file.

We can get the better information using “swapon –s” command like,

[root@localhost ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda7                               partition       2048248 0       -1

From this command, we are sure that the swap was created from a partition /dev/sda7.
Now let’s see how we can create a swap by using fdisk(for more information on how to work with fdisk check here),

[root@localhost ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 60801.
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)

Let’s create a new partition with 150mb size like,

Command (m for help): n
First cylinder (6346-60801, default 6346):
Using default value 6346
Last cylinder or +size or +sizeM or +sizeK (6346-60801, default 60801): +150M

Once created check the partition table like,

Command (m for help): p

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks         Id  System
/dev/sda1   *           1         382     3068383+    83  Linux
/dev/sda2             383        2294    15358140     83  Linux
/dev/sda3            2295        3569    10241437+ 83  Linux
/dev/sda4            3570       60801   459716040  5  Extended
/dev/sda5            3570        4589     8193118+  83  Linux
/dev/sda6            4590        5481     7164958+  83  Linux
/dev/sda7            5482        5736     2048256     82  Linux swap / Solaris
/dev/sda8            5737        6345     4891761     83  Linux
/dev/sda9            6346        6364      152586     83  Linux

See a new partition /dev/sda9 is created with partition type as Linux (ext3).

Now here comes the main part, once the partition is created we need to modify the partition type to swap. For this we do like this,

Command (m for help): t
Partition number (1-9): 9
Hex code (type L to list codes): 82
Changed system type of partition 9 to 82 (Linux swap / Solaris)

Now check the partition table,

Command (m for help): p    

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         382     3068383+  83  Linux
/dev/sda2             383        2294    15358140   83  Linux
/dev/sda3            2295        3569    10241437+  83  Linux
/dev/sda4            3570       60801   459716040    5  Extended
/dev/sda5            3570        4589     8193118+  83  Linux
/dev/sda6            4590        5481     7164958+  83  Linux
/dev/sda7            5482        5736     2048256   82  Linux swap / Solaris
/dev/sda8            5737        6345     4891761   83  Linux
/dev/sda9            6346        6364      152586   82  Linux swap / Solaris

See the /dev/sda9 now says that it is a swap partition.

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.
Syncing disks.

Once saved, execute the partprobe command.

So now the partition is created, we need to set the new partition as a swap patition. For this we can use “mkswap” like,

[root@localhost ~]# mkswap /dev/sda9
Setting up swapspace version 1, size = 1028120 kB

Once we set the new partition as swap,we need to start that using “swapon” like,

[root@localhost ~]# swapon /dev/sda9

Once we started the swap, execute the “free” command to see the new swap size,

[root@localhost ~]# free
             total       used       free     shared    buffers     cached
Mem:       2042720     497324    1545396          0      33140     308624
-/+ buffers/cache:     155560    1887160
Swap:      3052268          0    3052268

You can see that the swap size was increased.

NOTE: we need to make changes to the /etc/fstab file in order to make sure that the newly created swap is mounted automatically when the system starts. Just add the line in /etc/fstab
/dev/sda9      swap            swap            defaults         0 0

DELETE SWAP PARTITION

If we need to delete the swap partition,we need to first stop the swap using “swapoff” like,
[root@localhost /]# swapoff /dev/sda9


Now execute the free command.
[root@localhost /]# free
                    total       used       free     shared    buffers     cached
Mem:       2042720     494520    1548200          0      32440     307940
-/+ buffers/cache:     154140    1888580
Swap:      2048248          0    2048248

We can see that the swap size was reduced. Now delete the /dev/sda9 partition using fdisk command and also entry in the /etc/fstab file.

More articles to come .Happy CodingJ

No comments :

Post a Comment