💾 Archived View for gemini.brunofontes.net › blog › partitions-systemd.gmi captured on 2021-12-05 at 23:47:19. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2020-11-07)

-=-=-=-=-=-=-

Mounting partitions with systemd

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:

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

To check the unit status, you can run systemctl status run-media-bruno-Multimedia.mount

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:

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.

Back