Advertisement

Using a file as swap

Started by September 11, 2004 01:24 AM
10 comments, last by C-Junkie 20 years, 2 months ago
First off, I have already decided to do this, and done research on the implications. Don't bother me about whether or not it's a good idea. That said, how can I create a 512 MB file and set it as swap? I know how to create it and also how to make it swap from the command line, but I don't know how to set up /etc/fstab to make it mount that file as swap on boot. Is there any trick, or do I just give it the file as the device?
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
(the following _should_ work, haven't tested it tho)

* create the file: dd if=/dev/null of=<file> bs=512M count=1
* turn it into swap: mkswap <file>
* swapon

you might want to add, something like this to fstab:
<file> swap swap defaults 0 0
(might need the loop option too)


or someyhing like that.. :)

/Nico
Advertisement
NicoDeLuciferi: Nearly right, most of that is fine.

But use /dev/zero instead of /dev/null as a source of data for the swap file.

null has nothing in it (i.e. empty), whereas zero is a special file which has unlimited quantities of zeroes. This is what you want in your swap file (to begin with anyway)

Mark
Quote: Original post by Promit
First off, I have already decided to do this, and done research on the implications. Don't bother me about whether or not it's a good idea.
But it IS a good idea.

iirc, linux 2.6 wiped out any (most?) downsides!
Quote: Original post by markr
NicoDeLuciferi: Nearly right, most of that is fine.

But use /dev/zero instead of /dev/null as a source of data for the swap file.

null has nothing in it (i.e. empty), whereas zero is a special file which has unlimited quantities of zeroes. This is what you want in your swap file (to begin with anyway)

Mark


of course.. a type-o on myside..
Ok, it seems to be working fine. I mainly wasn't sure about the /etc/fstab.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Advertisement
Quote: Original post by C-Junkie
iirc, linux 2.6 wiped out any (most?) downsides!


2.6 eliminated the file system?
Quote: Original post by Shadowdancer
2.6 eliminated the file system?
eh?

Quote: In Linux 2.4 and earlier, swap files are less robust than swap
devices (partitions) because of the need to read metadata from the
filesystem for swap files, making them prone to OOM deadlocks when
allocating pages and buffer_heads to use in swapping.
This has changed in Linux 2.5. Using a swap file has no disadvantage
compared to swapping to a device (partition). The kernel doesn't
need to allocate any memory to get a swapcache page to disk.

This is interesting because swap files are much easier to administer
(add, remove, resize) and easier to stripe. It is now feasible
(in the Linux 2.5.40 timeframe) to eliminate swap devices (partitions)
completely and not be penalized in performance.
Quote: Original post by C-Junkie
Quote: Original post by Shadowdancer
2.6 eliminated the file system?
eh?


Once more, this is proof that working on completely artificial kernels will get one out of touch with reality. The last thing I know was that keeping swap on a regular file system produces performance loss due to FS accesses ("where was that file again?", locking, fragmentation). I guess the fragmentation argument is still valid to some degree unless the kernel keeps a complete mapping in memory. (I know that ext2/3 and other FSs don't usually fragment this much, but I don't think it's avoidable with filesizes of 1GB and more.)

Oh well, at least I don't need to feel bad about that swapfile anymore [smile]
Quote: Original post by Shadowdancer
Quote: Original post by C-Junkie
Quote: Original post by Shadowdancer
2.6 eliminated the file system?
eh?


Once more, this is proof that working on completely artificial kernels will get one out of touch with reality. The last thing I know was that keeping swap on a regular file system produces performance loss due to FS accesses ("where was that file again?", locking, fragmentation). I guess the fragmentation argument is still valid to some degree unless the kernel keeps a complete mapping in memory. (I know that ext2/3 and other FSs don't usually fragment this much, but I don't think it's avoidable with filesizes of 1GB and more.)

Oh well, at least I don't need to feel bad about that swapfile anymore [smile]


It's not like the swap is used a lot anyway... most systems can do without it.

This topic is closed to new replies.

Advertisement