13

In my /etc/fstab file I have an entry for my swap as follows:

/root/swap swap swap sw 0 0

I have other machines and also I've seen online that sometimes they put default or xfs or other options. Then, I'm a little confused on what 'sw' means and what's for, and also which one would be the best option to put there and why.

  • What Unix are you using here? This seems to vary somewhat between the BSDs and Linux. – Faheem Mitha May 18 '17 at 20:15
  • I'm using Centos 7 – VaTo May 18 '17 at 20:23
12

From the fstab manual on my system:

The fourth field, fs_mntops, describes the mount options associated with the filesystem. It is formatted as a comma separated list of options. It contains at least the type of mount (see fs_type below) plus any additional options appropriate to the filesystem type. [...]

If fs_type is “rw”, “rq”, or “ro” then the filesystem whose name is given in the fs_file field is normally mounted read-write or read-only on the specified special file. If fs_type is “sw” then the special file is made available as a piece of swap space by the swapon(8) command at the end of the system reboot procedure.

So basically, sw is used to tell swapon (or swapctl on my system) that this is a valid candidate for use as swap space that will be added as part of the system start-up routine.

From the manual describing swapctl -A:

This option causes swapctl to read the /etc/fstab file for devices and files with an “sw” type, and adds all these entries as swap devices. If no swap devices are configured, swapctl will exit with an error code.

That's on OpenBSD. On the Ubuntu Linux system that I have access to, neither manual mentions sw as a mount option for swap for some reason.

| improve this answer | |
  • 1
    From the available documention on Linux one of two things are possible (a) the sw option doesn't do anything, and is simply a placeholder (b) tells swapon to activate the swap space. But why this should be necessary, I don't know, since the space is already specified as swap by the filesystem type. In any case, in view of the apparently wide usage of sw on Linux, this probably represents a lacuna in the documentation, and should probably be reported. – Faheem Mitha May 18 '17 at 20:18
  • @FaheemMitha The sw may be changed to xx to explicitly disable a swap partition. So it's not completely useless. Again, that's from the fstab manual on OpenBSD. – Kusalananda May 18 '17 at 20:19
  • 1
    I was only talking about Linux. The BSDs might well do things differently. Or it's possible the Linux and the BSDs do the same thing, but the Linux documentation is lacking. I suppose one would have to look at the source code. – Faheem Mitha May 18 '17 at 20:23
  • 1
    I don't understand what is meant by If fs_type is “sw”. The fourth column isn't fs_type, it's mount options. – Faheem Mitha May 18 '17 at 20:39
  • 1
    @FaheemMitha Yes, the fourth field is the mount options (fs_mntops). These options contains the fs_type, the type of mount. The type is read-write (rw and rq (quota)), read-only (ro), or swap (sw) (or xx for disabled). – Kusalananda May 18 '17 at 20:46
4

For Linux, at least, based on a look at the net, this appears to be an example of "cargo culting". The evidence I've see suggests that this option isn't actually necessary or meaningful, but you need to put something there, because the fstab file syntax expects it. You can easily find examples of people using defaults and even just pri=1 or whatever, leaving out both defaults and sw.

And it doesn't make sense that swapon would need to look at the mount options to check that it is swap, because swapon can already see it's swap by looking at the filesystem type. There are certainly mount options for swap that modify behavior, as documented in man swapon, but sw isn't one of them. So the evidence is that it is a placeholder, and foobar would do just as well as a placeholder

I can't be completely sure of this without looking at the source code of course.

Related, a Debian bug report complaining about this exact issue: mount: swapon(8) lacks explanation for sw and defaults options

| 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.