Use cases for multiple swap partitions on single and multiple disks?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What use cases would require a system to have
multiple swap partitions and/or swap files (on a single disk?) ?
In order of performance:
0. multiple disks, raw partitions
1. md raid0 pair, raw partition. high risk if either partition fails
2. md raid10,f2 pair, raw partition. best balance of performance and safety
3. md raid1, raw partition. better safety than single disk
4. single disk, least used, raw partition
5a. raw partition
5b. swap file
Thank you for your response.
It is a little cryptic for me....
So here are my questions... so I can understand your previous response better.
Quote:
In order of performance:
0. multiple disks, raw partitions
You kept mentioning raw partitions multiple times.
Not sure what do you mean by raw partitions.
We cannot use raw partitions until we format it with a filesystem.
So why are the (unusable) raw partitions relevant to performance?
How does raw partition relate to swap partition and disk performance?
Here is scenario for your case:
On a single Linux system, you have swap partitions on multiple disk.
So you have these hard drives: sda, sdb, sdc, sdd.
And you create these corresponding swap partitions:
sda4,sdb4, sdc4, sdd4.
Does Linux know all these are to be used as swap partitions?
How to inform Linux these partitions are to behave as a single swap file?
Quote:
1. md raid0 pair, raw partition. high risk if either partition fails
2. md raid10,f2 pair, raw partition. best balance of performance and safety
3. md raid1, raw partition. better safety than single disk
I do not know much about raids to ask questions for now.
Quote:
4. single disk, least used, raw partition
What do you mean by least used?
Quote:
5a. raw partition
5b. swap file
When you say swap file, does it also mean swap partition?
'swap' is a filesystem type ; look into your /etc/fstab file.
After you create a 'swap' type partition it must be added as an entry in /etc/fstab for persistence.
You'd use http://linux.die.net/man/8/swapon to activate a swap partition after you create it.
Well, multiple swap partitions are pretty rare, but one use case is this:
If you've built a system (inc a swap partition) and subsequently need more swap space AND there is no unused disk space at the end of the swap partition.... Rather than rebuild the entire system, just add some swap elsewhere on the next available disk space.
Hypothetically, I can see if you have a LOT of disks on a system and you know you'll need swap, you could spread the load over multiple drives. This prevents I/O bottlenecking... in fact you'd want to spread the I/O load over multiple disk controller cards / buses, not necessarily just the drives.
This example is a real niche case, but it could happen.
My primary swap area is an SSD partition. I don't want to devote a lot of SSD space to swap, so my "just in case" swap area is a partition on a rotating disk. And no, I'm not worried about wearing out my SSD. If you have enough swap activity to significantly impact the life of a modern SSD, your system would be unusably slow if the swap were on anything except an SSD.
You kept mentioning raw partitions multiple times.
Not sure what do you mean by raw partitions.
We cannot use raw partitions until we format it with a filesystem.
So why are the (unusable) raw partitions relevant to performance?
How does raw partition relate to swap partition and disk performance?
Swap uses disk blocks without any filesystem. It can use a whole unformatted disk or an unformatted partition.
Quote:
Here is scenario for your case:
On a single Linux system, you have swap partitions on multiple disk.
So you have these hard drives: sda, sdb, sdc, sdd.
And you create these corresponding swap partitions:
sda4,sdb4, sdc4, sdd4.
Does Linux know all these are to be used as swap partitions?
How to inform Linux these partitions are to behave as a single swap file?
See the man pages swapon(8), and mkswap(8), and mount(8)
Quote:
I do not know much about raids to ask questions for now.
You ask about multiple swap partitions. Multiple swap partitions are typically used for high performance situations, md raid is used to give higher than single disk performance, but not required.
Quote:
What do you mean by least used?
If a disk is heavily used it is not a good pick for swap.
Quote:
When you say swap file, does it also mean swap partition?
No. A swap file is a file created on a filesystem. It is usually used when there are no raw (unformatted) partitions available. It will be slower than a raw partition.
You asked a question that requires some knowledge of disk I/O and memory use, usually in the server setup domain.
From the end-user perspective, swap files in versions 2.6.x and later of the Linux kernel are virtually as fast as swap partitions; the limitation is that swap files should be contiguously allocated on their underlying file systems. To increase performance of swap files, the kernel keeps a map of where they are placed on underlying devices and accesses them directly, thus bypassing the caching and avoiding the filesystem overhead.[15][16] However, Red Hat recommends swap partitions to be used.[17]
Not sure there is much or any difference in speeds of a swap partition and a swap file for most users in modern kernels. I guess if you get a swap file in a place that has heads moving way past data then yes. Same could be said for a swap partition that is the other side of a data partition that gets swapped a lot.
From the end-user perspective, swap files in versions 2.6.x and later of the Linux kernel are virtually as fast as swap partitions; the limitation is that swap files should be contiguously allocated on their underlying file systems. To increase performance of swap files, the kernel keeps a map of where they are placed on underlying devices and accesses them directly, thus bypassing the caching and avoiding the filesystem overhead.[15][16] However, Red Hat recommends swap partitions to be used.[17]
Contiguous??? I don't think so. The maximum contiguous space possible in an ext2/3/4 filesysem is one block group, and that can be no larger than 128MiB. A swap file does need to be fully allocated (i.e., non-sparse), but there is not requirement that it be contiguous. I presume that a badly fragmented swap file would hurt performance, though.
On my old laptop (not a high performance best, by any means) I use just the scenario suggested by Chris in Post #6. I have two swap partition because it was set up with one, and that turned out not to be big enough. So, there are two, and basically you just don't notice the difference between two and one.
If the two were widely separated on the (spinning rustware) disk, there would be a performance impact (that's a further performance impact over and above the perf impact from just using swap), due to head movement, so they're not.
Swap uses disk blocks without any filesystem. It can use a whole unformatted disk or an unformatted partition.
Well,,, no. Swap uses a partition that has been identified for use with swap AND has a "superblock" that identifies itself as swap. I believe it also includes a "free list" for allocation/hibernation/crash dump support.
Quote:
See the man pages swapon(8), and mkswap(8), and mount(8)
You ask about multiple swap partitions. Multiple swap partitions are typically used for high performance situations, md raid is used to give higher than single disk performance, but not required.
Yes and no. md raid is only used to provide error recovery (useful for non-stop operations). It still slows the paging activity.
Contiguous??? I don't think so. The maximum contiguous space possible in an ext2/3/4 filesysem is one block group, and that can be no larger than 128MiB. A swap file does need to be fully allocated (i.e., non-sparse), but there is not requirement that it be contiguous. I presume that a badly fragmented swap file would hurt performance, though.
Nope, it must be a contiguous extent, which is why a separate partition is so convenient.
As for the original q, if you have the need allocate multiples, else not. Why all the hand-wringing ?.
There are NO guarantees in any native Linux filesystem that a file be contiguous, in fact, doing so makes it slower (not to mention that when the size crosses block cluster units it has to skip the cluster allocation sectors, and thus is not contiguous anyway).
It does need to be fully allocated - but that is only due to the filesystem supporting the swap file causing paging activity to allocate blocks in a sparse file (and hence a possible deadlock).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.