Anteru's blog
  • Consulting
  • Research
    • Assisted environment probe placement
    • Assisted texture assignment
    • Edge-Friend: Fast and Deterministic Catmull-Clark Subdivision Surfaces
    • Error Metrics for Smart Image Refinement
    • High-Quality Shadows for Streaming Terrain Rendering
    • Hybrid Sample-based Surface Rendering
    • Interactive rendering of Giga-Particle Fluid Simulations
    • Quantitative Analysis of Voxel Raytracing Acceleration Structures
    • Real-time Hybrid Hair Rendering
    • Real-Time Procedural Generation with GPU Work Graphs
    • Scalable rendering for very large meshes
    • Spatiotemporal Variance-Guided Filtering for Motion Blur
    • Subpixel Reconstruction Antialiasing
    • Tiled light trees
    • Towards Practical Meshlet Compression
  • About
  • Archive

Automatic mounts using systemd

May 03, 2019
  • Linux
approximately 3 minutes to read

A few years ago, I set up autofs to handle my network shares. While this works, occasionally I ran into some issues with autofs, and of course it’s one more thing you need to setup & install. Recently I learned that systemd can also handle automatic mounting. Turns out, that’s as easy as setting up autofs and it fixed some of the issues I’ve had with autofs, so without further ado – let’s convert an existing autofs mount to systemd!

In this blog post, I’ll set up a SMB share, pointing to a network share //my-server/myshare. The share will be mounted to /mnt/net/smb/myshare. If you want to follow along, make sure you remember which directory you’re using as the target – this will become important in a second.

Before I continue, a brief interlude – I’ve not pieced this together by myself, instead I heavily relied on several blog posts and of course, ServerFault. For the rest of the post I’ll be linking to the official documentation where you’re supposed to find all this stuff :)

We’ll be saving our configuration in /etc/systemd/system, which is the recommended location for units created by the administrator. The first file we’ll create describes the mount point. This is done using a mount unit. One thing to note here is that the filename needs to follow the naming scheme described in the unit documentation, which basically boils down to replacing / with -. /mnt/net/smb/myshare turns into mnt-net-smb-myshare.mount. The contents are relatively straightforward, with What providing the source path, Where the target, and Type/Options storing the cifs options that will be used to mount the share:

[Unit]
Description=myshare mount

[Mount]
What=//my-server/myshare
Where=/mnt/net/smb/myshare
Type=cifs
Options=rw,file_mode=0700,dir_mode=0700,uid=1000
DirectoryMode=0700

[Install]
WantedBy=multi-user.target

There’s nothing else to do here, you can use systemctl daemon-reload to reload the config, and inspect the mount, but right now it’s a regular mount and no auto-mounting is happening yet. For that, we need an automount unit. It follows the exact same naming convention, except the file extension must be .automount. This file contains the [Automount] section, and it has one mandatory entry, Where:

[Unit]
Description=myshare automount

[Automount]
Where=/mnt/net/smb/myshare

[Install]
WantedBy=multi-user.target

This is the unit we want to enable and start automatically, so we need to perform the following steps:

  • systemctl daemon-reload to reload the configuration
  • systemctl start mnt-net-smb-myshare.automount to start the unit – so we can use it right away
  • systemctl enable mnt-net-smb-myshare.automount to enable the auto-start of the unit

And that’s it, at this point, if we cd into /mnt/net/smb/myshare, we should see the unit get triggered. We can check this using systemctl status mnt-net-smb-myshare.automount. The output will tell you which process triggered the mounting.

And that’s it! Other types of mounts work the same – I’m using the same setup for nfs mounts. Thanks for reading!

Previous post
Next post

Recent posts

  • Data formats: Why CSV and JSON aren't the best
    Posted on 2024-12-29
  • Replacing cron with systemd-timers
    Posted on 2024-04-21
  • Open Source Maintenance
    Posted on 2024-04-02
  • Angular, Caddy, Gunicorn and Django
    Posted on 2023-10-21
  • Effective meetings
    Posted on 2022-09-12
  • Older posts

Find me on the web

  • GitHub
  • GPU database
  • Projects

Follow me

Anteru NIV_Anteru
Contents © 2005-2025
Anteru
Imprint/Impressum
Privacy policy/Datenschutz
Made with Liara
Last updated January 03, 2021