Disk managements

Linux uses the swap space configured on one or more hard drive partitions to store infrequently used programs and data. Swap space can extend the amount of effective RAM on your system.

However, if you don't have enough RAM, Linux may use the swap space of hard drive as virtual memory for currently running programs. But you can't just buy extra RAM and eliminate swap space. Linux moves infrequently used programs and data to swap space even if you have gigabytes of RAM.

Normally, Linux (on a 32-bit Intel-style computer) can use a maximum 4GB of swap space in partitions no larger than 2GB. This 4GB can be spread over a maximum of eight partitions. The typical rule of thumb suggests that swap space should be two times the amount of RAM. For example if you have 1 GB of physical ram then need 2GB space for swap.

Generally Linux create swap during installation but for exam prospective you should be able to create it after installation. There are two method for it via fdisk utility of file method. We will use both methods.

Create Swap partition

To accomplish this task you must be login form root account. So first login from root and verify your hard disk status with fdisk –l command ( This command will show that where your hard disk is mounted. You should use the mount point which show in the output of this command. For example if you see /dev/hda then you should use fdisk /dev/hda in next command. Or if you see /dev/sdb then you should use fdisk /dev/sdb in next command.

As you can see in image shown below that My hard disk is mounted as /dev/sda so I will use fdisk /dev/sda)

fdisk linux command

file type of swap space is 82 so change file type to 82 of this newly created partition and save and exit from fdisk with w options. Run partprobe command to update kernel

change file to swap

verify with fdisk -l command that partition is successfully created

fdisk commands

now format swap partition and on it

mkswap swapon

To permanently mount this partition makes its entry in /etc/fstab as shown in image

#vi /etc/fstab
fstab with swap partition

Create swap from file method

To use file method for swap space create a blank file of 100MB

touch

Now format this file with mkswap and on this swap space. To keep it on after reboot make its entry to rc.local file

mkswap swapon
editing in rclocal

You have created swap space with both method you can verify its run time status in /proc file system

proc swap

To remove these partition and files

  • Turn off swap space with swapoff command
  • Remove entry from rc.local
  • Remove entry from /etc/fstab
  • Reboot system