13

Are there any conventions on where to put swap files on Linux?

I usually just create them in /var/swapfile, but that seems a bit too high up in the hierarchy.

Jo Liss
  • 2,818
  • 1
  • 22
  • 23

4 Answers4

11

AFAIK there is no location defined in the File System Hierarchy Standard.

Because /var is for often-changed files, I would say it is okay to place it here. But remember that if you use a flat-file swap instead of a partition, consider to use a fast file system for /var.

Malte Bublitz
  • 426
  • 3
  • 5
  • 8
    According to this: http://lkml.iu.edu/hypermail/linux/kernel/0507.0/1690.html You don't have to worry at all about the filesystem! – Paul Nov 24 '15 at 06:05
7

The conventional way is to have a partition dedicated to swap. If you use a file it's at your own discretion, but Malte's suggestion of the file system being important is a good one.

Caleb
  • 11,583
  • 4
  • 35
  • 49
  • +1 for it's own partition. I usually have swap as a partition at the beginning of the drive. /boot then swap is my general approach. – egorgry Jun 10 '11 at 18:30
  • 7
    I personally don't really care about the last few percentage points of swap performance enough to take the extra complexity of a separate partition. If my server starts seriously swapping, I have a problem anyway. The swap space is more of a last-resort safety net in case some process starts gobbling up memory. – Jo Liss Jun 11 '11 at 14:10
  • That's fine, you're welcome to use a file. I find partitions quite easy to manage but I see your reasoning. You just asked what the convention was and that's pretty much the default scenario. – Caleb Jun 11 '11 at 14:18
  • @egorgry - Didn't BSD historically try and always put swap on the 2nd partition to be in the middle? – Jé Queue Mar 01 '12 at 17:52
  • I find it quite useful to put swap space on a file in /var, because it makes a little easier to move system to new storage (one thing less to care about). I don't care about performance, since I almost don't use swap any more. Maybe its main use today is for laptop hibernation, and obviously as "safety net". – gerlos Dec 15 '14 at 15:47
  • @gerlos That get's gnarly if you you do things like encrypt your filesystem. Having swap on it's own partition makes things like that easier, not harder. Worst case scenario if the partition isn't available you don't get any swap enabled. – Caleb Dec 15 '14 at 15:53
  • @Caleb You're right, I didn't thought of encryption, but usually you encrypt your /home and not your system volume. Also I found quite annoying tinkering with partitions (for something I almost don't use) so usually I prefer put it in my /var dir, so I can move it around with /var or just / whenever I need to replace system volume, without the need to touch my /etc/fstab. – gerlos Dec 16 '14 at 17:01
1

Just to add my 2 cents to Malte's and Caleb's answers, and also to the excelent reference by Paul:

Using swap partitions instead of swapfiles was mentioned often. You have to see that the main issue with swapfiles is not performance (the kernel takes care of that), but instead needing contagious non-sparse disk space. Therefore it is best to create all swapfiles right after the creation of the underlying filesystem. Once the filesystem is in use, with file fragments scattered all around, it will be hard to get hold of some contiguous space. Still, in contrast a partition does not offer anything better: it is guaranteed to be a continuous space, but you also have to create it early on, and it is a major pain to change later.

Also, to address the main question of where to put the swapfile. Just like with swap partitions, the contents of a swapfile is highly confidential in nature, therefore using encryption is recommended. As in case of whole partitions where mostly LVM volumes over LUKS is the practice, swapfiles should be placed on a filesystem which is on top of a LUKS encrypted device. You have to keep in mind, that there are various problems with hibernation on encrypted swap. Also, don't forget access rights. Just like you don't want someone to read or write to your memory, protecting swapfiles is essential. Possibly the best place is inside a directory access-able only by root. (e.g. /var/swap/swapfile01_8G)

As mentioned before creating swapfiles after the system is in use might be a problem. Therefore using this for scaling up after finding out later that more swap is needed could be unfeasible. On the other hand scaling back while having the total swap space made up of multiple swapfiles would be easy. For this reason, it might worth allocating swap space more judiciously, and in multiple files, because it will be easy to scale back if you confirm later that it is significantly oversized. (Buy more disk space for god's sake)

-2

If you want to mirror the Windows scenario, try /. If you want to share the file in a multi-OS environment, try putting it on a /home partition.

jiggunjer
  • 97
  • 2