Mounting partitions with systemd

Written by Bruno Fontes, 25 Jul 2018


I had a problem: every time my Windows have a big update, it locks a specific partition that I use to share files between Windows and Linux. After the update, I wasn't able to boot Linux again, as fstab could not mount it.

To avoid using a pen drive or booting any other way just to get my Linux working again, I removed the partition from fstab.

It worked, but I didn't want to manually mount it on every boot. Worst than that: Dropbox app synchronize on that partition. It means that on every single boot it raised an error message that my files were moved. It was definitely not a good way to solve it.

My new solution: create a script to mount the partition and run it at boot. Any error here would not stop my Linux from booting and I would still have it working by the time I logged in after boot. But, where is the best place to run a root script at startup? Of course, systemd! A simple service running a script should do the trick.

Well, with a simple search I found out something even better: systemd can mount partitions using systemd.mount!


How to mount a partition with systemd

To keep it simple, I am going to explain, step by step, the basic usage of systemd.mount with only the options I used myself.

  1. Create a .mount file on /etc/systemd/system/ (I will get back at this point later, for now, just use any name for the file)

  2. Inside the file you should specify the following attributes:

    • Description= A description of your mount script
    • What= The partition that will be mounted
    • Where= Directory where the partition will be mounted
    • Type= The partition file type (ext4, ntfs, fat32 etc)
    • Options= Mount options
    • WantedBy= or RequiredBy= Unit dependencies. It's used by the enable and disable commands of the systemctl
  3. Now comes the tricky part: you have to name your .mount file accordingly with your Where= statement. So to mount it on /run/media/bruno/Multimedia you have to name it run-media-bruno-Multimedia.mount

  4. It's time to test it:

    # systemctl daemon-reload
    # systemctl start run-media-bruno-Multimedia.mount
  5. To check the unit status, you can run systemctl status run-media-bruno-Multimedia.mount

  6. If everything is working fine, just enable it: systemctl enable run-media-bruno-Multimedia.mount and you are good to go.


My mount script

/etc/systemd/system/run-media-bruno-Multimedia.mount

[Unit]
Description=Mount Multimedia out of fstab

[Mount]
What=/dev/disk/by-label/Multimedia
Where=/run/media/bruno/Multimedia
Type=ntfs
Options=defaults

[Install]
WantedBy=multi-user.target


Explaining the attributes

Description=

Just a description of you .mount script.

What=

The partition itself. You can specify it by name, path or UUID.

Examples:

  • /dev/sdb2
  • /dev/disk/by-label/Multimedia
  • /dev/disk/by-uuid/bcebcc2e-3a07-48d8-bd3c-e4eaf98fafc5

Type=

The easiest one after description: the file type of the partition. If you are trying to mount a Windows partition, it will probably be ntfs. If it is a Linux partition, it will probably be ext4.

Options=

Partition options, the same used on fstab.

WantedBy=

I used multi-user.target to start in multi-user runlevel.