Search code examples
linuxmount

Mount multiple folders linked together


I have three folders, /ftp/A, /ftp/B, /ftp/C but logically C is a subdirectory of B and B is a subdirectory of A.

I try to resolv this using a script at system startup that binds the folders. I have a text file in /ftp/dirFolder where there are the relationships between the folders. Like

B C
A B C
A B

The first column is the father, the second one is the son and the third one is the nephew. The script is this:

case "$1" in
start)  while IFS=' ' read -r x y z
    do
        if [ -z "$z" ]; then
            mkdir -p /ftp/$x/$y
            mount -t none --bind /ftp/$y /ftp/$x/$y  > /dev/null 2>&1
        else
            mkdir -p /ftp/$x/$y/$z
            mount -t none --bind /ftp/$z /ftp/$x/$y/$z  > /dev/null 2>&1
        fi
    done < /ftp/dirFolder
    ;;
stop)   while IFS=' ' read -r x y z
    do
        if [ -z "$z" ]; then
            umount /ftp/$x/$y  > /dev/null 2>&1
        else
            umount /ftp/$x/$y/$z  > /dev/null 2>&1
        fi
    done < /ftp/dirFolder
    ;;

The problem is: when I start the script, the folders are created and mounted correctly, but if inside C there is a folder or a file, it won't be visible inside /ftp/A/B/C but they will inside /ftp/B/C.

mount command gives me this:

/ftp/C on /ftp/B/C type none (rw,bind)
/ftp/C on /ftp/A/B/C type none (rw,bind)

Is there a way to make the folder C available from both folder A and B?


Solution

  • Solved, it was an error on the dirFolder file.

    First you need to mount the nephew folder in the son's folder, then mount the son's folder in the father's one.

    I was:

    B C
    A B C
    A B
    

    It should be:

    B C
    A B
    A B C
    

    Hope it helps someone!