https://bryanapperson.com/blog/mounting-rbd-at-boot-under-centos-7/

Mounting RBD at Boot Under CentOS 7

2 minute read Modified: 18 Feb, 2020 A quick and dirty guide on how to mount a ceph RBD at boot under CentOS 7 This tutorial covers mounting an RBD image at boot under CentOS 7. Make sure to unmount the RBD you want to have mount at boot before following this tutorial. This tutorial requires a CentOS 7 client with a client or admin keyring from Ceph, and a working Ceph cluster. This tutorial also assumes you have already created the RBD image you want to be mounted at boot. Let’s begin!

Assumptions

This tutorial assumes the node you are implementing this on has connectivity to a working ceph cluster and also assumes that kernel module RBD is enabled. For the purposes of this tutorial I will place variables, the values specified here:

  1. export poolname = your_pools_name
  2. export rbdimage = the_name_of_your_rbd_image
  3. export mountpoint = place_to_mount_the rbd

Create A systemd service to map and mount automatically on boot / demand

You will want to automatically load the kernel module, map the appropriate rbd storage to a local device and mount the ceph image. Here is a simple script for mounting and un-mounting RBD images create one at /usr/bin/mount-rbd-$poolname-$rbdimage for each of your RBD images:

  1. #!/bin/bash
  2. # Image mount/unmount and pool are passed from the systems service as arguments
  3. # Determine if we are mounting or unmounting
  4. if [ "$1" == "m" ]; then
  5. modprobe rbd
  6. rbd map --pool $poolname $rbdimage --id admin --keyring /etc/ceph/ceph.client.admin.keyring
  7. mkdir -p $mountpoint
  8. mount /dev/rbd/$poolname/$rbdimage $mountpoint
  9. fi
  10. if [ "$1" == "u" ]; then
  11. umount $mountpoint
  12. rbd unmap /dev/rbd/$poolname/$rbdimage
  13. fi

Create a new systemd service unit (/etc/systemd/system/mount-rbd-$poolname-$rbdimage.service) for each of your remote rbd images:

  1. [Unit]
  2. Description=RADOS block device mapping for $rbdimage in pool $poolname"
  3. Conflicts=shutdown.target
  4. Wants=network-online.target
  5. After=NetworkManager-wait-online.service
  6. [Service]
  7. Type=oneshot
  8. RemainAfterExit=yes
  9. ExecStart=/usr/bin/mount-rbd-$poolname-$rbdimage m
  10. ExecStop=/usr/bin/mount-rbd-$poolname-$rbdimage u
  11. [Install]
  12. WantedBy=multi-user.target

Make sure your target RBD is unmounted. Start the service and check whether /dev/rbd0 is created or not:
systemctl start mount-rbd-$poolname-$rbdimage

Mounting an RBD at Boot Under CentOS 7 is Easy!

If everything seems to be fine, enable the service to start on boot:
systemctl enable mount-rbd-$poolname-$rbdimage
You now have a working RBD mount at boot time! I wil be following this up with a complete tutorial on the entire process of creating an RBD at some point in the future. Leave your thoughts in the comments below.


Published by Bryan Apperson 8 Apr, 2015 and tagged centos, ceph, rbd, rhel and systemd using 364 words.