27

For working with storage devices we need a file system, what about the swap space?

If it doesn't have a file system how does operating system work with it? How is the data (from RAM) written to disk, and how is it accessed again?

  • 7
    Swap does not need a file system. It does not store files. – Pilot6 Nov 6 '16 at 10:57
  • 3
    It stores RAM pages. – Pilot6 Nov 6 '16 at 11:04
  • 2
    Could you please explain exactly what you are trying to do? Are you saying that all you need is a place to store data temporarily? – Jake Nov 6 '16 at 11:09
  • 6
    "I want to know how it is possible to write in a storage space without file system?" – You simply write to the storage space. Think about this: if a file system were required to write to a storage space, then file systems couldn't exist, because file systems need to be able to write to a storage decide without a file system. "according to your answer can i say RAM has a small file system and swap file use this file system for working with pages?" – No. A file system is for storing files. Swap space doesn't store files. – Jörg W Mittag Nov 6 '16 at 16:28
  • 4
    It is quite common to write a single file (ala tar and dd) to unformatted storage, and even more common to have nested filesystems (virtual disks, .iso, squashfs). Games on floppy would sometimes write high scores to an absolute (fixed) location, and some applications are Cylinder, Head, Sector based. Swap partitions are still partitions, but no, there is no filesystem, or "mapping" other than offset and run ? – mckenzm Nov 7 '16 at 1:31
29

Swap technically doesn't have specific filesystem. The whole purpose of filesystem is to structure data in certain way. Swap partition in particular doesn't have structure, but it does have a specific header, which is created by mkswap program. In particular , this (taken from kernel.org):

 25 union swap_header {
 26     struct 
 27     {
 28         char reserved[PAGE_SIZE - 10];
 29         char magic[10];
 30     } magic;
 31     struct 
 32     {
 33         char     bootbits[1024];
 34         unsigned int version;
 35         unsigned int last_page;
 36         unsigned int nr_badpages;
 37         unsigned int padding[125];
 38         unsigned int badpages[1];
 39     } info;
 40 };

Each partition has specific code associated with it, and according to TLDP:

code for ext2 is 0x83 and linux swap is 0x82

When swap file is involved, that's a slightly different story. The kernel must respect the fact that the filesystem may have their own way of structuring data. From the same kernel.org link:

Remember that filesystems may have their own method of storing files and disk and it is not as simple as the swap partition where information may be written directly to disk. If the backing storage is a partition, then only one page-sized block requires IO and as there is no filesystem involved, bmap() is unnecessary.

In conclusion, technically you could call swap space a filesystem of its own type, but it's not quite comparable with filesystems like NTFS or ext4

You've also asked

I want to know how it is possible to write in a storage space without file system

Strictly speaking, there's no need for RAM to be structured. However, portions of RAM can be structured as tmpfs under Unix-like OSes. There's also ramfs, and initramfs , which is what gets loaded during boot process. But the RAM data technically is supposed to be just raw 1s and 0s, so there's no need to structure them in anyway.

| improve this answer | |
  • 1
    Good pointing out that, despite not having a file system, it does have a basic structure that allows recognizing what it is. Prevents overwriting another partition by mistake. – spectras Nov 6 '16 at 13:41
  • I think they mean disk space here "I want to know how it is possible to write in a storage space without file system" instead of RAM maybe? – Anwar Nov 6 '16 at 14:20
  • @Anwar ,Yes but i got it – Sinoosh Nov 6 '16 at 16:18
  • tar can be used to write multiple files as a RAW stream to an unformatted device such as flash drive. Otherwise a single file can be written (but not named) and you will need to reverse the process to write it back. This is commonly done with ISO's to optical media, but the result is an in-place filesystem. – mckenzm Nov 7 '16 at 1:38
  • 1
    Agreed. Essentially it can be described as a mapped area then. It is not an extension of real memory, but a work space to store chunks that can be overlaid back in on demand. – mckenzm Nov 7 '16 at 2:06
14

Swap space is used by the kernel to temporarily store pages of system memory (RAM) as it becomes full. The kernel uses it's own internal tables to "remember" exactly where within the swap disk it put the page. As a result, swap disks do not contain a proper filesystem and are usually just blank partitions on the disk.

What you may be interested in, is a RAM-disk, which is a small filesystem stored in the system's memory. If more memory is needed, the kernel will push it (and other contents) out to the swap space. See here for instructions on setting one up.

| improve this answer | |
13

Swap space is divided into blocks the same size as memory pages (usually 4kB), and a record of the mapping of these pages to application memory forms an extension of the virtual memory subsystem in the CPU and OS.

That is, there is already a mapping system between application memory spaces and the actual physical memory address. An application is given a large memory address space which they can use as much or as little of as they can. As more of this memory address space is actually used, physical memory is mapped to that application to serve as the storage medium.

When memory is swapped to disk, a related system maintains that mapping of an application's memory space to the block on disk.

The mapping table itself is not stored on disk, and the data remaining on the disk is useless after a reboot.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.