Wednesday, 9 May 2018

Mount disk on boot with systemd

What:

Linux with systemd

Problem:

Instead of using fstab to mount devices or nfs shares on boot use systemd, which works similarly like autofs, so it worn't stop system from booting in case incorrect entries in fstab.

Solution:

I use VDO (Virtual Data Optimizer) on my Centos linux server,  which enables compression and deduplication. Vdo volumes depend on vdo service, which means that vdo service needs to be running before we can mount volume.
Without that dependency in fstab system won't boot, so using systemd to do the job seems like a good idea, you can mount any disk/partiton/nfs share with it.

Create new file
vi /etc/systemd/system/mnt-vdo_name.mount
with the following content:
[Unit]
Description = VDO unit file to mount file system
name = vdo_name.mount
Requires = vdo.service
After = multi-user.target
Conflicts = umount.target

[Mount]
What = /dev/mapper/vdo_name
Where = /mnt/vdo_name
Type = xfs
Options = defaults,discard,noatime

[Install]
WantedBy = multi-user.target
For NFS mounts remove Requires and replace After with:
After=network.target
Important. filename needs to have a path to our mount point, where slashes are replaced with dashes.

mount point
/mnt/vdo_name
translates to the following filename:
mnt-vdo_name.mount
Reload systemd deamon, so it can pick up new file, enable unit to start at boot and finally start it:
systemctl daemon-reload
systemctl enable mnt-vdo.mount
systemctl start mnt-vdo.mount

No comments:

Post a Comment