Make a swap partition
Prerequisite: Basics of partitioning with fdisk and formatting with mkfs
So you learned a bit about fdisk and are familiar with mkfs a bit so far. But what about giving up some of your hard-drive for swap space? First let’s start off with what is swap space.
Physical memory is a limited resource on a computer. Only so many processes can fit in physical memory at any one time, though many more may actually be ready to run or execute. Swapping and paging algorithms allow processes or portions of processes to move between physical memory and a hard-drive (or in rare moments, usb drives). This frees up space in physical memory.
Swap space (called a paging file in Windows) is an area on disk that temporarily holds a process memory image. When physical memory demand is sufficiently low, process memory images are brought back into physical memory from the swap area on disk. Having sufficient swap space enables the system to keep some physical memory free at all times.
This type of memory management is often referred to as virtual memory and allows the total number of processes to exceed physical memory. Virtual memory enables the execution of a process within physical memory only as needed. However, swapping does have a downside. Compared to memory, disks are very slow. Memory speeds can be measured in nanoseconds, while disks are measured in milliseconds, so accessing the disk can be tens of thousands times slower than accessing physical memory. The more swapping that occurs, the slower your system will be. Sometimes excessive swapping or thrashing occurs where a page is swapped out and then very soon swapped in and then swapped out again and so on. In such situations the system is struggling to find free memory and keep applications running at the same time. In this case only adding more RAM will help.
The swap partition is an independent section of the hard disk used solely for swapping; no other files can reside there. 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/sda4 partition 859436 0 -1
‘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.
Now referring back to our last tutorial on fdisk, we’re going to assume we have the same setup as we did in that one, where you now have a 2nd hard-disk (/dev/hdb) with one partition on it that you created purely for that tutorial. Lets use fdisk to delete that partition (refer back to that tutorial if you must).
Now use fdisk to create a standard ext3 partition on that disk that only take up about 75% of that disc, the ending block size will vary depending on the size of your disk of course.
Once that is done, lets say we want a second swap space and we now have a quarter of the disk, un-partitioned and unformatted waiting for us to do just that. So use the ‘n’ command in fdisk to create another partition, it can be primary or logical, but in our case we’ll make it a primary partition. We’ll use the ‘t’ option to toggle the ID for the partition. We want it to have an ID of 82 for the Linux Swap Partition. Once you are done use ‘w’ to save your changes. Now you should have a partition labeled to be a swap partition in linux named /dev/hdb2.
The mkswap command formats a partition to be used as a swap device. For our disk,
mkswap -c /dev/hdb2
-c has the same meaning to check for bad blocks.
Once the partition is formatted, the kernel can be signalled to use that partition as a swap partition with
swapon /dev/hdb2
and to stop usage,
swapoff /dev/hdb2
You Can have as many of them as you like. You can swapon many different partitions simultaneously. While this tutorial is just for practice for a second swap space on a second disc, generally linux only needs one swap space and it’s good practice to have this swap space to be in between the actual size of your RAM up to double the size of your RAM. Now you know the basics of creating a swap partition in Linux.