Creating, Managing & Tuning SWAP memory

SWAP memory in linux can either be a partition or a file. Here I'm gonna show you how to create a SWAP using partition and with file as well. 


Creating SWAP using partition:

# fdisk -l /dev/sda (To view the partition on the system) 
# fdisk /dev/sda
(Press m for help)
Press n for creating new partition
Press p for primary, e for extended depending on your current HDD partitioning scheme.
Press enter to accept the default starting cylinder
Input the required swap size (In my scenario I'm specifying +1024M and press enter)
Press t to specify the type of partition
Specify the newly created partition number in my scenario it is 5 (i.e. /dev/sda5).
Input the SWAP partition type as 82 (82 denotes that created partition will be a SWAP partition)
Press p to print the partition table for correctness.
Press w to write the partition to the disk.

# partprobe /dev/sda5  (In RHEL5)
or 
# partx -a /dev/sda5 (In RHEL6)

To create a SWAP partition
# mkswap /dev/sda5 (Sets a linux swap area on the device partition)
# swapon /dev/sda5  (Enables devices and files for paging and swaping)
# swapon -s   (To view the summary of SWAP partitions)
# swapon -a  (To re-read the swap entries from /etc/fstab)
# free -a (To view the newly created partition that is used as SWAP partition)

To make this partion to be availabe as SWAP everytime the system boots up, we write this entry in /etc/fstab


# vim /etc/fstab
/root/swap      swap    swap   defaults   0  0

Creating SWAP using file:

# dd if=/dev/zero of=/root/swapfile bs=1024M count=1
dd command is used for disk dump
if=input file, of=output file, bs=byte size, count=counter
With this command we can create a swap file of size 1024 MB (1GB).

Making this file to be used as SWAP memory is same as creating the above steps.
# mkswap /root/swapfile
# swapon /root/swapfile
# swapon -s
# free -a

To remove this file SWAP memory
# swapoff /root/swapfile
# swapon -s
# free -a   (To view the summary)


To make this swapfile to be availabe as SWAP everytime the system boots up, we write this entry in /etc/fstab


# vim /etc/fstab
/root/swapfile      swap    swap   defaults   0  0
 

To change the priority of the SWAP partition/file (Performance tuning SWAP partitions):

To view the priority of the swap partition use the following command.
# free -a           (In the last column we can find the priority of each swap entry)

To change priority
# swapoff /dev/sda5
# swapon -s
# swapon -p 10 /dev/sda5
(-p specifies the priority we can set for partition/file, here higher number means higher priority, if two or more swap partitions are given same priority then if its the highest priority then, paging is done in a round robin fashion)
# swapon -a

To make change persistent, we write this entry in /etc/fstab
# vim /etc/fstab
/root/sda5        swap    swap    pri=10   0  0


 

Post a Comment

Mới hơn Cũ hơn