Systemd: When to use _netdev mount option?

20 July 2015

Introduction

Manual page for mount command says:

_netdev The filesystem resides on a device that requires network access (used to prevent the system from attempting to mount these filesystems until the network has been enabled on the system).

After switching to systemd (replacement for SysV init scripts), there has been a dilemma whether it's necessary to add _netdev to fstab entry when mounting network filesystems. As man page suggests, this option implies that network should be brought up before trying to mount filesystems. Then again, systemd will recognize, for example, that NFS, CIFS filesystems require network without even adding _netdev option to fstab entry. This brings the question when exactly to use _netdev option in case of systemd?

Analyzing systemd source code

Take a look at mount_needs_network function (systemd-221):

src/core/mount.c:

static bool mount_needs_network(const char *options, const char *fstype) {
    if (fstab_test_option(options, "_netdev\0"))
        return true;

    if (fstype && fstype_is_network(fstype))
        return true;

    return false;
}

From above, using _netdev option means that this mount needs a network.

Then, if not specified, systemd auto-detects fs types that need network accessibility. That's why you don't need to specifically add _netdev to fstab entry for some fs types.

Let's see what are those fs types. Take a look at fstype_is_network function:

src/basic/util.c

bool fstype_is_network(const char *fstype) {
    static const char table[] =
        "afs\0"
        "cifs\0"
        "smbfs\0"
        "sshfs\0"
        "ncpfs\0"
        "ncp\0"
        "nfs\0"
        "nfs4\0"
        "gfs\0"
        "gfs2\0"
        "glusterfs\0";

    const char *x;

    x = startswith(fstype, "fuse.");
    if (x)
        fstype = x;

    return nulstr_contains(table, fstype);
}

From above, systemd knows that afs, cifs, smbfs, sshfs, ncpfs, ncp, nfs, nfs4, gfs, gds2, glusterfs fs types require a network before trying to mount them. If mounting any of these fs types, you don't need to specify _netdev option.

Mounting on demand

Sometimes, you don't need to mount all network filesystems on boot. On some Linux systems you can use autofs to mount filesystem on demand. If network is available it will try to mount filesystem on first access.

There is even an easier way to do the same thing by using systemd automount option.

Just add this to fstab entry and systemd will try to mount filesystem on demand:

noauto,x-systemd.automount

Conclusion

You can't make a mistake if you always specify _netdev to fstab entry, but for network filesystems only. Otherwise, systemd will try to detect whether fs type needs a network access. You must know what fs types can be auto-detected. Alternatively, consider using systemd-automount option, it's similar to what autofs does, but it's easier to configure.

By one coder