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!

4 Comments | Leave a Comment

  1. Zoe on June 26, 2020 - click here to reply
    I have tried your method but it did not work!
    the error is because the nbd device do not have /dev/disk/by-uuid symbol link to /dev/nbd0 after mkfs!

    /dev/nbd0: UUID="fc0b7108-f2f7-4d73-945f-d7a366e263ee" TYPE="xfs"

    this is the mount error log:
    Jun 27 08:50:52 systemd[1]: Job mnt-rpc_test.mount/start failed with result 'dependency'.
    Jun 27 08:50:52 systemd[1]: Job dev-disk-by\x2duuid-fc0b7108\x2df2f7\x2d4d73\x2d945f\x2dd7a366e263ee.device/start failed with result 'timeout'.
    I DO NOT know why nbd device cananot auto-generate the uuid link to its device(/dev/nbd*) but rbd can generate automatically!
    Can you figure out what is root cause got nbd device not getnerate symbol link of uuid?
  2. Zoe on June 26, 2020 - click here to reply
    Please ignore the last comment, your method works, i forgot to delete the mount setting in /etc/fstab! thanks you!
  3. Morgan Christiansson on March 31, 2021 - click here to reply
    Systemd adds x-systemd.requires field in fstab(5) opts field. This works for me:

    /etc/fstab:
    /dev/nbd0 /var/lib ext4 x-systemd.requires=nbd@nbd0.service 0 0
  4. Pedro on April 28, 2021 - click here to reply
    Matt

    What if the nbd doesn't have a filesystem but a drive image with partitions?

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.