In this video, Kevin Dankwardt discusses the value of swap space. Learn what size should be configured. We configure swap space, enable it, check it, and disable it.
- [Instructor] Let's talk about swapping. Swapping in Linux is the kernel being desperate. That is, it is deciding it doesn't have enough memory for things to be able to run reasonably, so it's going to throw some stuff out of memory, copying things out the disk. Typically, if your system is swapping, that's bad news. It's going to be really pretty slow. It's best if you add more memory or run fewer things, if you can.
But having your system configured to swap means that things may run instead of just being killed. Now, I want to point out that swapping is not the same as paging. Linux is a demand-page system. When you run a program, for example, it doesn't read the whole program into memory, it reads it in as you jump around to different parts of it. When you read a data file, it just reads part of it in to start with, and then as you reference different parts, those are brought in. That's demanding that those pages be brought in.
Swapping is different. Swapping is the kernel saving out to disk in its dedicated place, stuff to make room for other stuff. Linux always pages, but swapping only happens in desperate situations and only if you've enabled it. You don't have any swap space at all, then it's more likely that the kernel may have to kill something, if it runs out of memory.
With swap space, instead of killing something, it can temporarily move something out to disk. Now you can still have out of memory with swap space because you could fill up swap space. So if you want to do swapping, if you want to allow for that, then you need to configure some space for swapping. That can be a disk partition, or a file, or a bunch of them. Now, different administrators have different opinions about how much space to make available for swap.
Traditionally, it's been, say, twice as much as your RAM. Now if you have 64 gigabytes of RAM, and you allocate 128 gigabytes of disk space for swapping, think how long that's going to take to swap that stuff out. Maybe you don't need quite that much, but you probably want to have some. Most systems have swapping turned on. You need to make something usable as swap, a file or a partition, so you say mkswap, and give a path to a partition or a path to a file.
It will turn that whole thing into the swap space, so you don't need to tell it how big it is. In fact, you shouldn't tell it. How big it is, let it figure it out. It's kind of like formatting it, alright? That means you're going to be using that thing for swap, not to mount if it's a partition, and so forth. And then you tell the system that you want to use that for swapping with swapon, and if you no longer want to use that for swapping, then you do swapoff. And at the fstab where you mark things for mounting, you can also mark something as swap space, and then at boot time, the Linux will automatically turn swapping on for that device.
Let's look at making swap and turning it on, and checking the usage of swap and turning it off. Let's swap to a file. So we need to make a file, and we need to make it the size that we want for the swap space that we want to provide, so let's say we want to provide an extra one gig of swap space, then we should make a file that's one gig. Let's just use dd. Do copying at one megabyte block size, and we'll do a thousand of those.
Here we go. So we've got a thousand meg of file there, and we need to make sure that this is secure, so root needs to own that file, and only root should be able to read or write it. Let's just check what we got. It's owned by root, that's good, but hey, everybody could read it. We don't want the whole world being able to read swap space because there might be, you know, important things in there, like cryptographic keys that happen to be in RAM when we swap something out, so let's change the permissions to be 600.
That's what's recommended for swap space. There we go. Now let's tell the system that we want to use that for swap space. If there's other swap space already, this will just add to it. There we go, now it's ready for swapping. We're not actually telling it to use it yet. Now we tell it, since it's ready, with swapon. There we go. And we can check about swap space usage with swapon -s, and we get a little report, so there's my original swap space partition, and it actually got used, and then there's the new one that hasn't been used yet.
The columns don't line up so great with the output there, but it says first use the first partition, then use the file based on the priorities there. There you go.
- Partitioning storage
- Creating, mounting, and unmounting file systems
- Formatting file systems
- Making volumes with LVM
- Adding storage security
- Managing swap spaces
- Backing up and recovering Linux storage systems
- Working with networked file systems like NFS and SSHFS