Now let's look at how to configure and manage swap space in Linux. In
this lesson, we'll discuss how we can create a so called swap partition.
Swap is an area where Linux can temporarily move some
data from the computer's ram to understand this mechanism.
Let's go through an overly simplified scenario.
Let's imagine we have a computer with four gig of ram.
We open a video editor and this uses up two gig of Ram.
We open an audio editor and this needs another two gig.
Now we have no more free memory
but we have a two gig swap partition.
Although no more Ram is available, we can still open Chrome.
Well, how is this possible?
Well, when we want to open Chrome, the following happens,
Linux sees no more ram is available but it also sees
that we didn't use the video editor in the last hour.
It's basically just sleeping there inactive.
So it decides that it can move the data and memory used by the
video editor to our swap partition by moving data from Ram to swap it,
freed up two gig of ram.
So Chrome can now use that free memory.
Now let's see some commands to check if the system uses any kind of swap areas.
We can use this command
swap on dash dash show
since we don't have swap set up yet, the command will show nothing at this point.
So let's set up a partition to be used to swap.
First, we take a look at what partitions we have available.
Now, in the previous lesson,
we created the partition at B db three specifically to be used to swap
and this is currently empty with no data inside
before it can be used to swap. It has to be prepared.
Now, if you've ever formatted a USB stick with a file system like FAT32 or NTFS,
this is a similar process.
It basically writes some small header data on the partition and labels it.
So the system knows this is meant to be used as a swap area
to format it as swap. We use this command
and now we're telling Linux use this partition as swap.
The verbose option makes the command output more details about what it's doing
and it seems like everything worked perfectly.
We can check with
swapon dash dash show once again and this time
the command will list our swap partition here.
However,
there's still a problem if we reboot this system slash
DEV slash V db three won't be used to swap anymore
because the change we made is temporary.
But we'll see how we can tell Linux to automatically
use this as a swap every time it boots up.
And another lesson when we learn how to mount partitions
to stop using our partition as swap space. We can enter this command
instead of partitions. We can also use simple files as swap.
In fact, if you install
Ubuntu on a personal computer, that's what it'll do by default.
Nowadays, it'll set up swap on a file
to do this manually. First, we need to create an empty file and fill it with zeros.
That is binary zeros. Because in binary, everything is either a zero or a one,
we'll use a utility called DD and we'll have these parameters in the command.
Now this tells it to use the input file at slash DEV slash zero.
That's a special device that generates an infinite number of zeros.
When an application reads from it.
After we read from that file, we need to tell DD rare to write the output.
Basically, it will copy the input file to the output file.
So we'll tell it to write the contents in a file that we store at this path slash swap.
Then we specify the block size to be one megabyte.
Then combined with the next parameter count equals 128.
This tells DD to write a one megabit block 128 times.
So we'll get 100 and 28 megabytes in total. This file is small.
So it will get written fast. But if we have a large file.
We might have to wait a while and DD won't show us how much percentage is written
to do this manually. First, we need to create an empty file and fill it with zeros.
That is binary zeros. Because in binary, we can either have a zero or a one.
We'll use a utility called DD and we'll use this command,
this tells it to use the input file at slash DEV slash zero.
It's a special device file that generates an infinite
number of zeros when an application reads from it.
After we read from that file, we need to tell DD where to write the output.
Basically, it will copy the input file to the output file.
So we'll tell it to write the contents
in a file that will store at FL swap
and of stands for output file.
Then we specify the block size to be one megabyte.
Now, this combined with the next parameter
tells DD to write a one megabyte block 128 times.
So we'll get 128 megabytes in total. And our final command will look like this.
Now, this file is small so it will get written fast.
But if we have a large file, we might have to wait a while.
And DD won't show us how much of a percentage it is written,
but we can add another parameter
status equals progress.
This will make it display the progress it's making while writing to that file.
And here's an example, command writing two gig of data,
regular users should not be allowed to read the swap file.
That's because this potentially gives them access to the memory
contents of programs that other people might be using.
So we'll set the permissions to only allow the root user to read and write to this file
formatting. This as swap is the same as before.
But instead of using a file such as slash DEV slash V db three,
that points to a partition,
we just use a regular file in our command.
And now we can use this as swap on our system
and we can check the active swap areas again
and we'll see it's indeed used as swap
note how we can use multiple things to swap at the same time.
In our case, we use the B db three partition to swap,
but also our file and we can use multiple files or multiple partitions if we want to.