7

I am using initramfs to boot Centos via PXE. The initramfs used memory is listed within the "cached" value in /proc/meminfo or via free.

Since I need to calculate performance data, I need to know whether the memory used by the initramfs is reclaimable (i.e. can be swapped out to disk) or not. Typically only a very small part of the / filesystem tree is actually in use, so a majority of the initramfs could be swapped out.

Reading on this I got conflicting information. Some sources claimed that initramfs behaves like initrd and is based on ramfs, which means claimed memory cannot be paged out to swap. Other sources claim that initramfs is essentially tmpfs which in turn would imply that it can be paged out to swap.

Which is true? Can the unused parts of the initramfs filesystem be paged out to swap space?

  • I guess that's easy to find out with a VM with little memory... – Hauke Laging May 29 '13 at 11:26
6

EDIT: Answer updated/corrected.

Although the kernel documentation about this topic says that "Rootfs is a special instance of ramfs (or tmpfs, if that's enabled) [...]", it is in reality still a ramfs, as a short look in the code shows (rootfs is not mentioned in mm/shmem.c).

Some patches (see e.g. here and here) were sent to the Linux kernel mailing list (lkml) to change this. But they were not accepted. One reason was, that you normally do not have enabled swap during the initramfs phase or in embedded systems.

The initramfs image is extracted to the rootfs. Before user space (usually switch_root called from /init) switches to the new root, it deletes the content of the rootfs such that only the minimal memory amount of an empty ramfs is remains. So after this, you can basically ignore its memory usage and the question if it can be swapped out, is nearly irrelevant.

  • Thank you for the information. I should clarify: i do not perform switch_root, the initramfs is a 2GB cpio archive of a full CentOS Installation booted via PXE, it stays as rootfs. Therefore i would like to know whether a local swap device could free some of the unused 2GB. – dfens123 May 29 '13 at 12:28
  • Also, according to gossamer-threads.com/lists/linux/kernel/1463249: "Rootfs is a special instance of ramfs (or tmpfs, if that's enabled)" but it has always been a lie, until now. So i am not sure whether rootfs is actually a tmpfs or still ramfs, how to check? – dfens123 May 29 '13 at 12:33
  • 1
    @dfens123, instead of having the client download the whole system into a massive rootfs, why don't you just mount root over NFS? – psusi May 29 '13 at 13:33
  • @dfens123 I corrected my answer. – jofel May 29 '13 at 15:54
  • 1
    i suppose i could extract the cpio archive into an actual tmpfs and switch_root from the ramfs/rootfs – dfens123 Jun 5 '13 at 13:19
1

As you say, rootfs file data can only be paged out if it is implemented with tmpfs, not ramfs.

Since Linux v3.12, tmpfs is used in some cases. If I read the code correctly, this only happens if you do not pass a root= option. You can force ramfs to be used by passing rootfstype=ramfs, but you cannot force tmpfs by passing `rootfstype=tmpfs.

If you have the stat command, stat -f . will confirm whether you are on a ramfs equivalent, or a tmpfs equivalent, or a different type of filesystem altogether.


This was implemented in:

6e19eded3684 "initmpfs: use initramfs if rootfstype= or root= specified "

Command line option rootfstype=ramfs to obtain old initramfs behavior, and use ramfs instead of tmpfs for stub when root= defined (for cosmetic reasons).

It is a bit confusing, for two reasons. Firstly, it effectively overrides the logic in the commit that comes immediately before it:

16203a7a942 "initmpfs: make rootfs use tmpfs when CONFIG_TMPFS enabled"

Conditionally call the appropriate fs_init function and fill_super functions.

Secondly, the test for root= does not seem to be mentioned in the documentation, even in v4.17:

https://github.com/torvalds/linux/blob/v4.17/Documentation/filesystems/ramfs-rootfs-initramfs.txt

If CONFIG_TMPFS is enabled, rootfs will use tmpfs instead of ramfs by default. To force ramfs, add "rootfstype=ramfs" to the kernel command line.

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.