mattgadient.com

How To: Using “systemd” to mount NBD devices on boot (Ubuntu)

An old Scaleway C1 that I previously used had a bit of an issue when mounting the 50GB NBD storage device… if mounted via /etc/fstab the system would hang on boot because the system would try to mount the device before the network was up.

Long story short, to solve this, the route I went involved using systemd to mount the drive after the network was up. I recently had to muddle through it again so I figured I’d put this up here for my own future use (and for anyone else who might benefit from it).

Step 1: Get the UUID of the NBD drive.

I’ll assume the drive’s already been formatted (via mkfs.ext4 or otherwise) and that a directory exists where the mount point will be ( /mnt/myDriveYay for example ).

Either lsblk -f or blkid should spit out the UUID of the drives available – grab the correct one.

 

Step 2: Create the .mount file – /etc/system/systemd/____.mount

IMPORTANT – the file name itself has to be named in a special way – it MUST correspond to the location of the mount point with “-” instead of “/”.

Examples (color-coded to make it easier to follow):

  • /etc/systemd/system/mnt-myDriveYay.mount for /mnt/myDriveYay
  • /etc/systemd/system/var-www-www.mywebsite.com.mount for /var/www/www.mywebsite.com
  • /etc/systemd/system/home-johndoe-nbddrive.mount for /home/johndoe/nbddrive

If the location of your mount contains dashes or anything that falls outside of standard alphanumeric characters, you’ll need to use systemd-escape to figure out the proper name. For example, systemd-escape -p –suffix=mount /mnt/i-like-dashes will result in mnt-i\x2dlike\x2ddashes.mount. Because creating that file involves extra escaping craziness like:

nano /etc/systemd/system/mnt-i\\x2dlike\\x2ddashes.mount

…I’d be inclined to just avoid dashes, stay with alphanumeric characters, and keep things simple. But it’s up to you.

Step 3: Put stuff in your new .mount file

A short example:

[Unit]
Description=Mount my NBD Volume at Boot after the network is up but before nginx
Before=nginx.service
After=network-online.target

[Mount]
What=UUID="123b123f-b123-12cd-1234-1ee12cba12ce"
Where=/mnt/myMountedDrive
Type=ext4
Options=defaults,noatime

[Install]
WantedBy=multi-user.target

Nearly everything in there will likely be changed depending on what you want to do, so just in case you’re new to systemd stuff, here we go line by line:

  • Description – A basic description of what this does.
  • Before – In this example, I want the drive mounted BEFORE nginx (the web server) starts. Reason is because in this example nginx expects the drive to be there and I don’t want nginx getting tripped up. If you don’t need the drive mounted before any other specific service starts you can just remove this whole line.
  • After – The important bit in this example! I want the drive mounted AFTER the network is up and running. You can remove this line (or change it to something else) depending on whether you need the drive to be mounted after something specific.
  • What – Replace the UUID above with the UUID of the drive you want mounted (from Step 1).
  • Where – The location for your mount. I’m not sure if this has to be escaped (like the file name created in Step 2) or not.
  • Type & Options – These should be changed to correspond to your file system.

Save the file.

Step 4: Have systemd auto-start the new mount on boot.

As an example for a file created at /etc/systemd/system/mnt-myMountedDrive.mount :

systemctl enable mnt-myMountedDrive.mount

…you may want to manually start the mount before rebooting just to make sure all is well. If you manually mounted the drive, unmount it, and then run:

systemctl start mnt-myMountedDrive.mount
systemctl status mnt-myMountedDrive.mount

…check to see if the drive is mounted and the file system is working as expected. If things look good, reboot the system, and you should hopefully be good to go!

0 Comments

 | Leave a Comment

    Leave a Comment

    You can use an alias and fake email. However, if you choose to use a real email, "gravatars" are supported. You can check the privacy policy for more details.

    To reduce spam, I manually approve all comments, so don't panic if your comment doesn't show up immediately.