0

I am creating swap file using fallocate -l "$memory" "$swapfilename" command.

I have some questions:

  1. On which file system is the swap file created if multiple file systems are present? Can we externally specify from which file system to take?

  2. I have two file systems tmpfs and rootfs, and by default swap file is created by using rootfs memory; even tmpfs has more size. What is tmpfs?

  3. Before creating swap file how to check if space is there or not?

[root@rhev6 scripts]# df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs          3.1G  2.8G  123M  96% /
tmpfs           1.5G   76K  1.5G   1% /dev/shm
0

The file is created in the directory indicated by $swapfilename, by default the current directory. The command fallocate has no idea of what you want to do with the file; it just creates it and allocates the amount of space requested. To use the file as a paging area you need to run mkswap on it.

tmpfs is a memory-based file system used for (small) temporary files. Since it is memory-based there is seldom a lot of space in tmpfs filesystems. rootfs is another memory-based filesystem, used only for holding the root directory before mounting something on it. In production there is usually a disk-based filesystem mounted on /.

You want to make your swap file on a disk-based filesystem, such as an ext4 filesystem.

10
  • thanks alex. can we use tmpfs for creating swap files? and how to check how much total space is available before creating swap. and how to check which is large filesystem. can we create swap using multiple filesystems?
    – saidesh
    Jan 10 '17 at 9:35
  • Why would you use a tmpfs for swap file? tmpfs is memory-based; i don't see any useful purpose in creating a swap file in memory. Swap files are supposed to provide additional virtual memory.
    – AlexP
    Jan 10 '17 at 9:37
  • it is larger than rootfs. that why i got doubt.
    – saidesh
    Jan 10 '17 at 9:38
  • Please add the result of df -h to your question.
    – AlexP
    Jan 10 '17 at 9:39
  • If the posted df -h is all you have then you really don't want any swap file.
    – AlexP
    Jan 10 '17 at 12:54
0

Answering your questions in order

On which file system is the swap file created if multiple file systems are present? Can we externally specify from which file system to take?

A swapfile can be placed on any local filesystem. Such a filesystem should be backed by physical disk.

I have two file systems tmpfs and rootfs, and by default swap file is created by using rootfs memory; even tmpfs has more size. What is tmpfs?

tmpfs is a temporary filesystem that comes directly from physical memory. If you have 8GB of physical memory could might decide to have a tmpfs filesystem of 2GB. There is a direct tradeoff between the amount of memory available in a tmpfs filesystem and the amount of memory available for the running system.

Your rootfs is a minimal memory-based boot image. Usually I would expect this to have been replaced with a true disk-based root filesystem but that hasn't happened here.

Do not create a swapfile on either the rootfs or tmpfs filesystems!

Before creating swap file how to check if space is there or not?

The df -h command will show you available space per mountpoint. You can choose one (any one) that is backed by physical disk and that has enough space. In the tiny screenshot snippet there is nowhere shown that is suitable as you've only included the rootfs and tmpfs filesystems.

2
  • Hi roaima. i am able to create swap file but evry time it is taking space from one file system only is there any way to create swap file from particular filesystem
    – saidesh
    Jan 17 '17 at 8:49
  • @saidesh yes of course. Let me know which part of my answer isn't clear and I'll see if I can expand it.
    – roaima
    Jan 17 '17 at 10:03
0

On which file system is the swap file created if multiple file systems are present? Can we externally specify from which file system to take?

The fallocate command is used to create, enlarge or shrink a file to a specified size. It will create that file wherever you tell it to do. It can also be used to "dig holes" in files, reducing the number of blocks they use on disk.

I have two file systems tmpfs and rootfs, and by default swap file is created by using rootfs memory; even tmpfs has more size. What is tmpfs?

Despite popular belief, tmpfs is not a RAM based file system but a virtual memory based file system. That means its back-end is the sum of the on-disk swap area(s), which might be partitions or plain files, and the portion of RAM which is not used or reserved for other purposes. Of course, if you have not created any swap area, tmpfs is similar to a RAM based file system.

Before creating swap file how to check if space is there or not?

Before creating a swap file, you need to find a file system with enough space. Virtual file systems like rootfs or tmpfs are not suitable for swap. When rootfs is backed by RAM (ramfs), creating a substantial swap file on it will have disastrous effect, otherwise that would be like creating a swap file on tmpfs which is absolutely pointless, just like filling a tank with a liquid taken from this very tank.

8
  • Hi jlliagre. i am able to create swap file but evry time it is taking space from one file system only is there any way to create swap file from particular filesystem
    – saidesh
    Jan 17 '17 at 8:50
  • That doesn't make sense. A file belongs to a single file system by design. Please better explain what is the problem you are trying to solve, and do not accept answers until one really answers to your question.
    – jlliagre
    Jan 17 '17 at 8:54
  • that means a swap file will be created by default from only one file system? is it like default?. i need to check which file system has more space and i want to create swap file from that file system only. is there any way to get it correct me if i am wrong
    – saidesh
    Jan 17 '17 at 9:11
  • A swap file is a file. A file belong to a single file system. If you want to use a specific file system, create the swap file on that file system. If you want to use more than one file system, you can create multiple swap files. Only disk based file systems are suitable to create swap files.
    – jlliagre
    Jan 17 '17 at 9:13
  • for that simply we need to move to that file system and execute fallocate -l "$size" "$swapfilename" right? or
    – saidesh
    Jan 17 '17 at 9:32

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.