2

If I check with stat the modification date is not up to date

  File: /home/shares/swap.file
  Size: 1962934272  Blocks: 3833864    IO Block: 4096   regular file
Device: 801h/2049d  Inode: 15          Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-09-03 20:36:39.736199577 +0100
Modify: 2019-09-03 20:36:39.646199258 +0100
Change: 2019-09-03 20:36:39.646199258 +0100
 Birth: -

even while top says I use swap:

top - 05:11:23 up  8:04,  5 users,  load average: 1.01, 0.80, 0.41
Tasks: 159 total,   2 running, 157 sleeping,   0 stopped,   0 zombie
%Cpu(s): 29.5 us,  0.8 sy,  0.0 ni, 68.6 id,  1.0 wa,  0.0 hi,  0.1 si,  0.0 st
MiB Mem :    926.1 total,    170.6 free,    327.1 used,    428.4 buff/cache
MiB Swap:   1872.0 total,   1763.7 free,    108.2 used.    532.8 avail Mem 

I am curious about this because I want to make sure the right swapfile (on external HDD) is used on my Raspberry so my sd card will not break.

In case the external HDD is not mounted into the folder then the swapfile will be created in the mountpoint /home/shares which is then on the card and not on the HDD (/dev/sda).

If the mount happens after the creation of the file then I am not able to check without unmounting - which will not be possible sometimes.

bomben
  • 232
  • 2
  • 13

1 Answers1

4

The swapfile is not updated through the filesystem. When you use swapon, the kernel just queries the filesystem about where exactly the file's data areas (extents) are located, then begins directly updating those areas at block device level.

(This is why you cannot have swap files on a multi-device Btrfs volume, cannot have compression or copy-on-write enabled on the file, etc.)

If you have multiple swap areas, the swapon command will tell you whether they're actively used.

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • 1
    The relevant part of `man 8 swapon`: "The swap file implementation in the kernel expects to be able to write to the file directly, without the assistance of the filesystem. This is a problem on preallocated files (e.g. `fallocate(1)`) on filesystems like XFS or ext4, and on copy-on-write filesystems like btrfs." – Kamil Maciorowski Sep 04 '19 at 05:10
  • Thanks! I was wondering if that means that the kernel could use a swapfile that is located *beneath* a mount. As I described in my question it can happen that I the system mounts the external HDD on a mountpoint but the swapfile was already created there before this mount. I never had an error when this happened. – bomben Sep 04 '19 at 05:13
  • 1
    @Ben: The kernel and userspace programs can use _any_ file located beneath a mount. They only need to already have a file handle (file descriptor) open to that file. Once the file is open, future operations (reads/writes) do not need to go through the VFS and do not use the file path anymore. – u1686_grawity Sep 04 '19 at 05:23
  • @grawity Can I tell the kernel to remove a file I no longer *see* because a mount has been done to the directory the file was inside? I am asking because I want to `swapoff /swapfile` and `rm /swapfile` but I need to tell the kernel to use this other file and not the one that is now on the mount. I think if I use the filesystem to `swapoff` it will just find the mounted device and the kernel does not yet use this file. Was I clear in describing the problem? :) – bomben Sep 10 '19 at 17:47
  • @Ben: `mount --bind / /mnt/TheRealRoot`, then `swapoff /mnt/TheRealRoot/swapfile`. – u1686_grawity Sep 10 '19 at 17:57
  • @grawity Awesome, so `--bind` does relate to the underlying directory and not to the `mount` that "has been done to it", right? Otherwise I would just mount the external HDD again and that is probably not what you meant. – bomben Sep 10 '19 at 18:04
  • I just tested it and it is not working as I expected. – bomben Sep 10 '19 at 18:26