30

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?

muru
  • 197,895
  • 55
  • 485
  • 740
Sinoosh
  • 2,031
  • 9
    Swap does not need a file system. It does not store files. – Pilot6 Nov 06 '16 at 10:57
  • does it store extra data from RAM? – Sinoosh Nov 06 '16 at 11:00
  • 3
    It stores RAM pages. – Pilot6 Nov 06 '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 06 '16 at 11:09
  • I want to know how it is possible to write in a storage space without 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? – Sinoosh Nov 06 '16 at 11:17
  • 8
    "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 06 '16 at 16:28
  • 5
    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 07 '16 at 01:31
  • Question is meaningless. 'Swap space' is space within a file sytem or other storage device. In the past it was common for an entire disk, e.g. a fixed-head device, to be dedicated to swap space. – user207421 Nov 07 '16 at 08:40
  • 2
    I don't see how this question is meaningless, unless of course you already know the answer. It might sound reasonable to use files to store RAM pages in swap, so "it does not store files" is not obvious. And I use a lot of things within a file system that have file systems in them (disk images), so "swap space is a space within a file system" does not make this question meaningless. – JiK Nov 07 '16 at 13:20
  • 2
    Another common use of non-file disk storage: some databases can skip the filesystem as well. That is to say, a database already organizes data in tables and rows. With that level of organization, it doesn't need the additional organization offered by a file system. – MSalters Nov 07 '16 at 14:14

3 Answers3

32

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.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 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 06 '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 06 '16 at 14:20
  • @Anwar ,Yes but i got it – Sinoosh Nov 06 '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 07 '16 at 01:38
  • @mckenzm What exactly is your point as far as swap is concerned ? – Sergiy Kolodyazhnyy Nov 07 '16 at 01:41
  • See "You've also asked...". Scope is not limited to the swap question. – mckenzm Nov 07 '16 at 01:44
  • @mckenzm ah , but in case of ISO that's still a filesystem, so that's different; swap technically isn't structured and doesn't have a filesystem, so I think you can't really extend the scope there. I don't know enough about tar , so I won't say anything about that. – Sergiy Kolodyazhnyy Nov 07 '16 at 01:47
  • 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 07 '16 at 02:06
  • @mckenzm yes, exactly that ! Well phrased – Sergiy Kolodyazhnyy Nov 07 '16 at 02:11
15

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.

Jake
  • 552
14

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.

thomasrutter
  • 36,774