Auto-mounting disk on Oracle Public Cloud Compute nodes

November 21, 2016 | 4 minute read
Dolf Dijkstra
Cloud Solutions Architect
Text Size 100%:

In the process of getting your Oracle Public Cloud Compute nodes ready for some real work you often need to attach additional disks to enlarge your disk capacity. The blog shows how you can easily format and mount these disks with a script that is activated through Linux' SystemD.

After you have orchestrated your nodes and the machines have booted you can access the machines over SSH to install and configure the software that you want to run on those nodes. As part of that configuration you normally format and mount disks manually. I'll present here a script that does these steps for you and mounts disks in a known location. After that you can configure your software to make use of those disks or create soft links to locations that your software expects.

 

 

#!/bin/bash 
i=0 
for device in xvd{c..l} 
do   
  if [ -b /dev/${device} ]   
  then     
    ((i++))     
    echo "# /dev/${device} "     
    unset ID_FS_UUID     
    eval $(blkid -o udev /dev/${device})     
    [ -z "$ID_FS_UUID" ] && mkfs -t ext4 /dev/${device} && eval $(blkid -o udev /dev/${device})     
    [ -z "$ID_FS_UUID" ] && exit 6     
    grep $ID_FS_UUID /etc/fstab > /dev/null     
    [ $? -ne 0 ] &&  echo "UUID=$ID_FS_UUID  /mnt/data-$((i+1))     $ID_FS_TYPE     defaults,errors=remount-ro 0       1" | tee -a /etc/fstab     
    [ ! -d /mnt/data-$((i+1)) ] && mkdir /mnt/data-$((i+1))  > /dev/null     
    findmnt /mnt/data-$((i+1)) || mount /mnt/data-$((i+1))   
fi 
done 
mount -a [ -d /var/lib/mysoftware/data ] && mkdir -p /var/lib/mysoftware/data && chown -R myuser:mygroup /var/lib/mysoftware 
for i in {0..9} 
do     
  if [ -d /mnt/data-$((i+1)) ]    
  then     
  [ ! -d /mnt/data-$((i+1))/data ] && mkdir /mnt/data-$((i+1))/data  > /dev/null && chown myuser:mygroup /mnt/data-$((i+1))/data      
  [ ! -L /var/lib/mysoftware/data/volume$((i+1)) ] &&           { [ -d /var/lib/mysoftware/data/volume$((i+1)) ] && rm -Rf /var/lib/mysoftware/data/volume$((i+1)); ln -s /mnt/data-$((i+1))/data  /var/lib/mysoftware/data/volume$((i+1));}    
fi 
done

 

This script looks for devices under /dev/xvdc till /dev/xvdl and mounts them at /mnt/data-1 till /mnt/data-10.

The additional attached volumes at Oracle Public Cloud Compute nodes are added to /dev/xvdc and up. The volumes /dev/xvda is the boot volume. So the /dev/xvdc location is the place to look for the first data volume. The above script formats the devices in an ext4 format.

The second loop in the script creates soft links from /mnt/data-1 to /var/lib/mysoftware/data/volume-1, and continues this for all the mount points it find under /mnt/data-*. The location /var/lib/mysoftware/data/volume-1 was the location where the software in my case would find its data files. For other programs this can be very different, or you can use a volume manager to manage your volumes. I leave that exercise to the reader.

Each time when this script is executed it looks for volumes that are not formatted and mounted. In the case where you add additional devices after the initial boot, and this script is executed, the new volumes will show up.

 

Now the next step is to execute this script once during the phase when the software is installed and once each time the machine is booted. The initial run can be executed by your deployment scripts. For the run at boot time I have chosen to execute this as a SystemD script. SystemD is available in many Linux distributions, for instance in Oracle Linux since version 7.

To execute the above script as part of the boot process it needs to be able to be executed at a known location. For instance at /etc/opc-compute/data-mount.

Next step is to create a SystemD configuration file and to enable this script.

 

[Unit] Description=Mount data volumes 
After=network.target network-online.target 

[Service] 
Type=oneshot 
ExecStart=/etc/opc-compute/data-mount 

[Install] 
WantedBy=multi-user.target 

 

Put this script in /etc/systemd/system/data-mount.service. The last step is to enable this script for SystemD.

sudo systemctl enable data-mount.service sudo systemctl start data-mount.service

These steps have a shown a reliable and predictable way to mount volumes in Oracle Public Cloud Compute nodes.  Each time when the machines boot the list of available volumes is checked and if new volumes are added to the node, they are added as mount points. You will probably have to change the mount script to your needs, but the outlined process should make that much easier.

 

Dolf Dijkstra

Cloud Solutions Architect


Previous Post

Identity Cloud Services Audit Event REST API

Tim Melander | 10 min read

Next Post


Oracle Data Integrator Best Practices: Using Reverse-Engineering on the Cloud and on Premises

Benjamin Perez-Goytia | 9 min read