Linux | Cloud | DevOps | Scripting

Breaking

Saturday 30 August 2014

Swap

SWAP

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. 

For Example; If your RAM can process 10 services and there are number of services to perform, then an error occurs "Virtual memory is too low". To avoid this we make swap in linux. Swap partitions are used to support virtual memory. In other words, data is written to a swap partition when there is not enough RAM to store the data your system is processing. 

How to create a 100 MB swap partition?
  1. Create a 100 MB partition.
  2. Give system ID 82 to this partition
  3. Restart your system
  4. Create your swap
  5. On your swap
  6. Now, mount it permanently & change its priority.
  7. Restart your system
PRACTICAL:

~] # fdisk  /dev/sda
Command (m for help) :  n
Comand action  p
Partition number  3First cylinder (1301-2610, default 1301) :  1302
Last cylinder, +cylinders or +size{K,M,G}  (1302-2610, default 2610) : +100M

Command (m for help) : p

Device      Boot     Start     End     Blocks     Id     Syatem
/dev/sda1  .............................................................................
/dev/sda2  .............................................................................
/dev/sda3               1302     1315    112455     83    Linux 

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

Command (m for help) :  p 

Device      Boot     Start     End     Blocks     Id     Syatem
/dev/sda1  .............................................................................
/dev/sda2  .............................................................................
/dev/sda3               1302     1315    112455     83    Linux swap / Solaris

Command (m for help) :  w

~] # init 6

~] #  swapon  -s          //to show currently on swaps in your system
~] #  mkswap  /dev/sda3          //to create a new swap for partition /dev/sda3

Note: By using command "mkswap" you can create a new swap but by default it is in off mode. To use this in actual we have to on this swap

~] #  swapon  /dev/sda          //to on this swap

~] #  vim  /etc/fstab
         /dev/sda3     swap     swap     defaults     0  0
         [save and exit]
~] #  mount -a

(As swap has no mounting point and no fstype, this will not be listed in "df  -hT" OR "mount" command.)

~] #  swapon  -s          //this will show your swap which is in active mode.

Filename          Type          Size          Used          Priority
/dev/dm-1          partition      524280     0                -1
/dev/sda3          partition      1048568   0                 -2


Note: If there are number of swaps then which swap will use first. Here, we set the priority. Which priority will be first that will be used first.

To set priority of swap
We can set swap priority temporary and permanently both ways. For this our swap should be in off mode, otherwise it will prompt "Device or Resource is busy".

Temporary Way:
~] #  swapoff  /dev/sda3
~] #  swapon  -p  1  /dev/sda3          //this will set "1" priority to your swap and also on your
                                                           swap.

~] #  swapon  -s

Filename          Type          Size          Used          Priority
/dev/dm-1          partition      524280     0                -1
/dev/sda3          partition      1048568   0                1

Permanent Way:
~] #  vim  /etc/fstab
        /dev/sda3     swap     swap     defaults,pri=1     0  0
        [save and exit]
~] #  mount -a ~] #  init 6


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

Thanks for reading this article...

No comments:

Post a Comment

Pages