mount bind linux vs application reconfiguring

When configuring Linux systems one of the golden rules is to outsource the configuration and application data to another volume. This can easily be done for example by mounting /opt on another disk and performing installations on /opt.

Installations on other volumes are simple provided you do not have recourse to the repository of the distribution you’re using, in this case we will have settings and data in a few places. In such a situation we can do 2 things:

  1. Wander around all the configuration files of the application in question and modify routes logs, data, settings, etc… a lot of work.
  2. Use mount with the option –bind and not touch anything on the application.

 

The basic syntax of the call is:

Source   
mount --bind source destiny

Where “source” is a directory you want to mount on the “destiny”, for more information see the documentation. It may seem like a symbolic link but it is much better because:

  • To create a symbolic link must destroy the original directory and replace it with a symbolic link.
  • It works with jailed users.

To take an example, we have installed in 5 minutes from the repository a MySQL server and want to outsource everything. In a ubuntu all related information on:

Source   
/etc/mysql
/var/lib/mysql
/var/log/mysql

We stop the application, in this case MySQL. Suppose we want is everything in / opt, the first thing we do is create a structure of directories minimum (let the deepest directory without creating so when copying is created along with the rest of subdirectories):

Source   
mkdir -p /opt/mysql/etc/
mkdir -p /opt/mysql/var/lib
mkdir -p /opt/mysql/var/log

Now copy the original content to the directories to will mount on the destiny (the actual locations):

Source   
cp -a /etc/mysql /opt/mysql/etc/
cp -a /var/lib/mysql /opt/mysql/var/lib/
cp -a /var/log/mysql /opt/mysql/var/log/

It has used the -a option for all permissions, ownership and recursively copied.

Now we can only do bind mount as follows:

Source   
mount --bind /opt/mysql/etc/mysql /etc/mysql
mount --bind /opt/mysql/var/lib/mysql /var/lib/mysql
mount --bind /opt/mysql/var/log/mysql /var/log/mysql

If we look we see that actually mounts /var/log/mysql and /vol/mysql/var/log/mysql are the same thing:

Source   
ubuntu@ipXXXXXXXX:~$ mount
...
/opt/mysql/etc/mysql on /etc/mysql type none (rw,bind)
/opt/mysql/var/lib/mysql on /var/lib/mysql type none (rw,bind)
/opt/mysql/var/log/mysql on /var/log/mysql type none (rw,bind)
ubuntu@ipXXXXXXXX:~$ sudo ls /var/log/mysql
mysql-bin.000001  mysql.log      mysql.log.3.gz  mysql.log.6.gz  mysql-slow.log.1.gz  mysql-slow.log.4.gz  mysql-slow.log.7.gz
mysql-bin.index   mysql.log.1.gz  mysql.log.4.gz  mysql.log.7.gz  mysql-slow.log.2.gz  mysql-slow.log.5.gz
mysql.error.log   mysql.log.2.gz  mysql.log.5.gz  mysql-slow.log  mysql-slow.log.3.gz  mysql-slow.log.6.gz
ubuntu@ipXXXXXXXX:~$ sudo touch /var/log/mysql/test.mount.bind
ubuntu@ipXXXXXXXX:~$ sudo ls /opt/mysql/var/log/mysql
mysql-bin.000001  mysql.log      mysql.log.3.gz  mysql.log.6.gz  mysql-slow.log.1.gz  mysql-slow.log.4.gz  mysql-slow.log.7.gz
mysql-bin.index   mysql.log.1.gz  mysql.log.4.gz  mysql.log.7.gz  mysql-slow.log.2.gz  mysql-slow.log.5.gz  test.mount.bind
mysql.error.log   mysql.log.2.gz  mysql.log.5.gz  mysql-slow.log  mysql-slow.log.3.gz  mysql-slow.log.6.gz

Now you may start the application without problems.

As a last detail to indicate that this mount should be in the /etc/fstab with the lines:

Source   
/opt/mysql/etc/mysql        /etc/mysql    none    bind    0 0
/opt/mysql/var/lib/mysql    /var/lib/mysql    none    bind    0 0
/opt/mysql/var/log/mysql    /var/log/mysql    none    bind    0 0

I hope you simplify life 🙂

Leave a Reply