Swap part 2 How to create swap

To see what swap space you have, use the command swapon -s. The output will look something like this:

Filename  Type       Size       Used Priority
/dev/sda5 partition  859436  0       -1


Each line lists a separate swap space being used by the system. Here, the 'Type' field indicates that this swap space is a partition rather than a file, and from 'Filename' we see that it is on the disk sda5. The 'Size' is listed in kilobytes, and the 'Used' field tells us how many kilobytes of swap space has been used (in this case none). 'Priority' tells Linux which swap space to use first. One great thing about the Linux swapping subsystem is that if you mount two (or more) swap spaces (preferably on two different devices) with the same priority, Linux will interleave its swapping activity between them, which can greatly increase swapping performance.
To add an extra swap partition to your system, you first need to prepare it. Step one is to ensure that the partition is marked as a swap partition and step two is to make the swap filesystem. To check that the partition is marked for swap, run as root:

 If the partition isn't marked as swap you will need to alter it by running fdisk and using the 't' menu option. Be careful when working with partitions -- you don't want to delete important partitions by mistake or change the id of your system partition to swap by mistake.

Once a partition is marked as swap, you need to prepare it using the mkswap (make swap) command as root:

# mkswap /dev/hdb1
 
If you see no errors, your swap space is ready to use. To activate it immediately, type:

# swapon /dev/hdb1

To verify your config run  

# swapon -s 

To mount swap partition automatically at boot time 

#vim /etc/fstab

/dev/hdb1 none swap sw 0 0
============================================================OR example 

/dev/hdb1 swap swap defaults 0 0


To add a swap file:
  1. Determine the size of the new swap file in megabytes 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:
  3. dd if=/dev/zero of=/swapfile bs=1024 count=<required size in KB>
    dd if=/dev/zero of=/swapfile bs=1024 count=65536 
    ------------------------------------------------------------
    for 1GB swapfile
    dd if=/dev/zero of=/swapfile bs=1M count=1024

  4. Setup the swap file with the command:
    mkswap /swapfile
  5. To enable the swap file immediately but not automatically at boot time:
    swapon /swapfile
  6. To enable it at boot time, edit /etc/fstab to include:

    /swapfile swap swap defaults 0 0

    The next time the system boots, it enables the new swap file.
  7. After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command  
    #cat /proc/swaps 
    or

    #free


 
 
 

No comments:

Post a Comment