20

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.

2
  • 1
    What Unix are you using here? This seems to vary somewhat between the BSDs and Linux. Commented May 18, 2017 at 20:15
  • I'm using Centos 7
    – VaTo
    Commented May 18, 2017 at 20:23

3 Answers 3

19

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.

5
  • 2
    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. Commented May 18, 2017 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
    Commented May 18, 2017 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. Commented May 18, 2017 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. Commented May 18, 2017 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
    Commented May 18, 2017 at 20:46
16

For Linux, at least, based on a look at the net, this appears to be an example of "cargo culting", which means including code or words in this case for ritualistic or superstitions reasons, even though they may "serve no real purpose." The evidence I've seen 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 any other 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.

1
  • Upvoted. For Linux at least, including Ubuntu, this seems to be the correct answer. Commented Jun 19 at 17:56
0

I just added this to my answer here: Ask Ubuntu: How do I increase the size of swapfile without removing it in the terminal?:

For the meaning of sw in the fourth field of the /etc/fstab file, on BSD systems it means swapon, and on Linux it means nothing at all and is not documented anywhere, but acts as a "cargo-culted" superstitious and meaningless placeholder for historical reasons. Since having it there ensures this fstab (Filesystem Table) entry is compatible with BSD systems, and since it won't break Linux systems, it's a good idea to include it for maximum compatibility.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .