Instructor: Hi, this topic is about
adding or extending swap space.
Now what exactly swap space is, or swap is?
So according to CentOS.org,
"Swap space in Linux is used
when the amount of physical memory, RAM, is full.
If the system needs more memory resources
and the RAM is full, inactive pages in memory
are moved to the swap space.
While swap space can help machines
with a small amount of RAM
it should not be considered
as a replacement for more RAM,
swap space is located on hard drives
which have a slower access time than physical memory."
So this is a very easy
and straightforward definition of a swap.
In my own words, I would say
every machine has a memory, right?
It comes with a memory, one gig, 10 gig
of whichever the number of memory
that you have in your laptop.
When you run out of memory
then there is a way that you could add
a virtual memory to it,
which is not like a physical real memory,
it's a virtual memory, that comes from your hard drive.
So in that virtual memory
in Microsoft or Windows environment
it's called virtual memory,
in Linux environment it's called swap.
So the recommended swap space by the operating system
it's twice the size of RAM, which means like,
let's say if you have two gig of physical memory
in your system then you should have a two gig of swap space.
So the idea is, or the formula is
if M is less than two, by the way, M is,
let's take an example of M equal amount of RAM in gigabytes.
S is the amount of swap space in gigabyte.
So if M, which is the RAM, is less than two gig,
then the size of swap, which is S,
is the memory times two, else swap will be memory plus two.
So that's a general rule of thumb.
The commands that we will be using
to extend the swap space are dd to create a file,
mkswap, swapon or swapoff.
So let's do that by logging into our machine
and our Linux terminal and see how the swap space works.
My terminal open the Linux console GUI.
Now before we get into the swap
I want you to run df -h command.
And in this command you will see
you'll get the entire disk space partition, right?
Your file system has a total size of 10 gig.
So eight gig is assigned to root,
one gig is assigned to boot, so that is nine.
So where did the one gig go?
So that one gig is actually assigned to swap space.
So that swap space plus the one gig
is its own physical memory.
So if you add these up, 480, 496, 496, 496,
and these 200 and a hundred, all of these six of them
and you're gonna add them, it's gonna equal to two gig.
Now, why two gig?
The reasons why it's two gig is
because one gig is actually the physical memory.
We actually allocated that physical memory
when we were actually creating the virtual machine.
And the one gig of swap was automatically created
when we built the Linux operating system.
So that's why we only have eight gig here left.
So when you run free -m,
that's the command to check how much memory we have,
you will see the memory.
This is a physical memory.
We have a one gig, which is estimated numbers,
uses 256 and frees 81.
The swap, which was added during the installation
is 1023, which is also equal to one gig,
25 is used, and this is a free space.
Now we are assuming here that this swap space right here
and this memory, they are reaching almost full.
They are getting full,
the utilization is almost a hundred percent.
Now how can we extend or add more swap space?
Well, one way is that you, again,
if you have a physical system,
you could add a new memory DIMM to your system.
A DIMM is actually a physical slice of memory.
So you could add that to increase more memory.
If it's a virtual machine, you could actually power down
your machine and increase the virtual memory.
That's perfect.
But what if you do not have those options available?
Then what you could do is you could slice some of that disk
from your disk drive and use it for swap.
So let's say if I wanted to increase the size of this swap
which is 1023 and I wanted to add another one gig.
So how it's gonna work?
First I have to take one gig.
Let's say I wanted to add one more gig to it.
So I wanted to take one gig from my disk space
and dedicate that to swap.
So to do that I have to run the command dd.
By the way, to run that command, I have to be root.
So become root.
Okay, now the command is dd, space, if, equal sign,
dev, zero, space, of, equals sign, slash new swap.
Now this is the name that I'm picking.
You could pick any name and I'm putting it in under slash.
So this is the name of the file.
BS means the size, the byte size.
One meg, and the count equal 1024.
Now let me explain you this command, how it works.
Most of the time you will have to write this command as is.
Why it is written like that, well, that's how it is.
(chuckles) It is what it is. That's what usually people say.
So to explain to you dd is just a command
to create a new file.
If stands for read from file instead of standard input,
which is dev zero, you have to have this by default,
of meaning write to a file instead of standard output.
So you're gonna write everything
about your virtual pages in this file.
BS means read and write by types at a time.
And the count is the total size of the file, of course.
So when you hit enter
this should create a file with the size one gig.
See you have information coming here is
records in records out and the file size is created 1.1 gig,
which is of course approximately one gig.
So let's go to "cd /" and do "ls -ltr",
and you will see right here
this is our new file that is created.
Okay, now one thing you should be wondering,
"Hey, why we cannot create that file
just with a touch command?"
Well that's a very good question.
Well, the touch command creates an empty file.
You don't want to assign an empty file to swap, right?
You want to take away some chunk from your disk
and dedicate it to that swap, and how do you do that?
You would have to use a DD command.
That's why you cannot use touch.
Anyway, so now you see this new space file has been created.
The second step is to adjust the permissions.
You don't want to allow people to read that file.
You don't want to allow others, sorry,
group and others to read that file.
The owner, which is root, read and write is okay.
So to do that you have to do change mod
with group and other, "-r", newswap.
Now this should take away the read
and read for group and from others.
Another way you could have done is by change mod 600
and then the name of the file,
which would perform the same function.
Anyways, so the correct permissions have been assigned.
Perfect. Now to make the swap.
What do you want it to do with this file now?
It's just a empty, it's just a file
that is holding a space from the disk,
and that space from the disk is one gig.
So what do you want us to do with that, right?
So let's make swap.
The command do that is mkswap, space, newswap, hit enter,
and this will tell you setting up swap space version one,
size this and that, label, no label, usual ID,
and it'll give you some information about the newswap.
So now run "free -m" to see if it's the new swap is added.
Hmm, no it's not added. Why?
Because you have to enable or turn on the swap.
So the swap, you are telling this operating system
that this is a file
and this file will be used for swap, right?
You created a file system which is make swap,
but now what do you want it to do with that?
You want to give it a swap.
So type the command swap on newswap, hit enter,
and this should add the swap space.
So now do "free -m", and you'll see now the file,
the size of the swap has increased.
Before it was 1023 and now it's 2047.
If you want to have the swap space be enabled
during boot time, you would have to specify that file
in vi, in etc/fstab file.
You'll go into the file and then you come down all the way
and put a new line and you put newswap, space,
the file system type, which is swap.
That's, sorry, where is it gonna mount it to? Swap.
We are not mounting it, it's just a swap.
What type of file system is that? It is a swap, again.
Default, zero, zero, hit escape, and save the file.
Be very careful with etc/fstab.
If you make a mistake, your system will not boot up.
So make sure you typed in correctly,
check it a couple of times before you go ahead
and reboot your system.
Now you could go ahead and reboot your system
and you could verify that your swap will be added again
at your boot time.
So this is how you create a swap space.
Try it out with different values
and this will be very helpful
in corporate environment when you run out of memory.