La página personal de Juan Gabriel Covas

Herramientas de usuario

Herramientas del sitio


Mover un directorio a otra partición usando bind mount

– Juan Gabriel Covas. 2014

Purpose: Move a directory to another existing partition, using a bind mount instead of a symbolinc link.

Using the special 'bind mount' available from Kernel 2.4+ allows the new directory to function exactly as a real filesystem mounted there, sort of a transparent mirror. Among other things, this allows not being to worry about programs that could not follow symlinks without extra options, etc.

Directory to 'move': /centosrepo
Target directory: /mnt/centosrepo

# mkdir /mnt/centosrepo

Use rsync to make sure everything gets sync'ed and perms preserved

# rsync -avx /centosrepo/ /mnt/centosrepo/

Add this line to /etc/fstab

/mnt/centosrepo                /centosrepo            none rw,bind

Next we delete the 'old' contents, leaving the directory empty

# rm -rf /centosrepo/*

We check the directoy still exists, but empty:

# ls -la /centosrepo

Now we use the mount command to get the new /mnt/centosrepo 'bind' mount from /etc/fstab:

# mount /mnt/centosrepo

An empty response means it's OK, and also gives us the peace of mind that after a reboot, the /etc/fstab file it is ALSO OK. Otherwise we'll get screwed if it's a remote linux box and /etc/fstab is invalid…

Now we check the mount list:

# mount
/mnt/centosrepo on /centosrepo type none (rw,bind)

Now we check that listing the /centosrepo dir give us the expected contents of /mnt/centosrepo

# ls -la /centosrepo

If it works… That's it! We now have plenty of space for that disturbing directory :)

linux/howtos/move-dir-bind-mount.txt · Última modificación: 10/07/2020 17:38 por Juanga Covas