Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Is there a way to make a directory not appear if nothing is mounted to it?

  1. #1
    Join Date
    Jul 2006
    Beans
    4,860

    Is there a way to make a directory not appear if nothing is mounted to it?

    Simple question here... Let's say /media/storage is your primary target to mount a 1 TB hard drive. Your actual OS is running on an internal drive 80 GB in size. Of course when mounted, anything you dump in /media/storage goes to the 1 TB drive. When not mounted, anything you put in /media/storage effectively goes onto your 80 GB drive, since nothing mounted to /media/storage would make the 80 GB drive be the new home for that directory.

    Is there a way to make "storage" simply not appear if nothing is mounted to it, as a clear and obvious way that your 1 TB drive is not mounted?

  2. #2
    Join Date
    Apr 2005
    Location
    Finland/UK
    Beans
    Hidden!
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Is there a way to make a directory not appear if nothing is mounted to it?

    Depends on what kind of setup youa re running and what exactly you want to achieve..

    Anyway, don't mount the drive using fstab or create the mount point, but leave it to the automatic mounting instead. And then set the drive's label to "storage" to make the automount system mount it as "/media/storage". That way the mount point will only exist when the drive is connected and mounted.

  3. #3
    Join Date
    Jul 2006
    Beans
    4,860

    Re: Is there a way to make a directory not appear if nothing is mounted to it?

    Quote Originally Posted by mcduck View Post
    Depends on what kind of setup youa re running and what exactly you want to achieve..

    Anyway, don't mount the drive using fstab or create the mount point, but leave it to the automatic mounting instead. And then set the drive's label to "storage" to make the automount system mount it as "/media/storage". That way the mount point will only exist when the drive is connected and mounted.
    I'm glad you brought that up. I never thought of that. In fact, I just conducted a little test here and I'm kind of surprised at the results. I did this because I was in the IRC chat talking to some people about this and they gave me a new idea. Their idea was to rsync the data to one directory lower. In other words, /media/storage is the mount point, but rsync to /media/storage/data instead. That way if the drive in question is missing, "data" should be gone, therefore the sync would fail. That sounds great in theory, but when you actually try it, it's very different.

    Keep in mind, all of this is done using rsync as root. I'm using root because I have multiple user accounts to back up, so it was easier for me to create an ssh key pair for root (had to do it manually, since ssh-copy-id requires root login which doesn't exist on Ubuntu) and just run the whole sha-bang as root. In my little experiment though I was using my laptop and a flash drive to simulate the actual situation I'd be applying this to.

    Both scenarios were using the same rsync command.
    Both scenarios were using root to execute the command.

    The command:
    rsync -az --delete --progress /home/jason/Desktop/ /media/flashdrive/storage/
    If I plug in the flash drive and let it auto mount (the drive is named flashdrive, so it mounts to /media/flashdrive automatically), I can successfully rsync data to it as root. If I unmount the flash drive (right click the icon in the Unity bar and hit eject) and rsync the data as root, it fails, giving me errors. This is good because it won't turn around and fill up my main hard drive which /media/storage would thereby reside on.

    If I mount the flash drive via terminal, sudo mount /dev/sdb1 /media/flashdrive, and rsync via root, it works fine. If I unmount the drive via sudo umount /media/flashdrive, then rsync again as root, it actually rsyncs all of the data over, thereby dumping it on my main hard drive.

    So you have two scenarios. In one, rsync as root just doesn't care and barges its way in and syncs the data. On the other one, if the system auto mounts the drive, it's as if it doesn't care if you're rsyncing as root. If the directory doesn't exist, it won't auto create it, unlike the other option.

    Can anybody shed some light on this?

    Thinking about it now, it sounds like it would make more sense to have my drive named accordingly to what I want it to mount as. For example, instead of using fstab to mount the drive as /media/storage, I should name it "storage" and let it auto mount when I plug it in, just as the suggestion above. I'm failing to see what downside there is to this?

  4. #4
    Join Date
    Oct 2005
    Location
    Lab, Slovakia
    Beans
    10,791

    Re: Is there a way to make a directory not appear if nothing is mounted to it?

    Yup, you are beginning to see the light. Name the volume properly and let the DE system automounter handle it, then add an if directory exist statement before the rsync command.

    http://tldp.org/LDP/abs/html/fto.html

  5. #5
    Join Date
    Jul 2006
    Beans
    4,860

    Re: Is there a way to make a directory not appear if nothing is mounted to it?

    Quote Originally Posted by HermanAB View Post
    Yup, you are beginning to see the light. Name the volume properly and let the DE system automounter handle it, then add an if directory exist statement before the rsync command.

    http://tldp.org/LDP/abs/html/fto.html
    I apologize, but looking at that link you provided I'm not entirely sure if I follow exactly how I would formulate the if statement to ensure the drive is mounted... then proceed to the actual rsync...

    EDIT - What about this? Looks like it's working.
    #!/bin/bash
    if [ $(mount | grep -c /media/STORAGE) == 1 ]
    then
    touch /home/jason/Desktop/HELLOTHISWORKED.txt
    else
    echo "/media/STORAGE already mounted"
    fi
    Then I can just whip it up like:

    #!/bin/bash
    if [ $(mount | grep -c /media/storage) == 1 ]
    then
    rsync -az --delete /media/storage/ root@192.168.1.150:/media/NAS/backup/
    else
    echo insert-whatever-command-to-email-me-subject-RSYNC FAILED
    fi
    Last edited by Roasted; December 10th, 2012 at 10:28 PM.

  6. #6
    Join Date
    Sep 2007
    Location
    Oklahoma, USA
    Beans
    2,378
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Is there a way to make a directory not appear if nothing is mounted to it?

    Try reading "man test" and look at the "-d FILE" option. There's no need to bother with mount and grep to determine whether the directory exists.
    --
    Jim Kyle in Oklahoma, USA
    Linux Counter #259718
    Howto mark thread: https://wiki.ubuntu.com/UnansweredPo.../SolvedThreads

  7. #7
    Join Date
    Jul 2006
    Beans
    4,860

    Re: Is there a way to make a directory not appear if nothing is mounted to it?

    Quote Originally Posted by JKyleOKC View Post
    Try reading "man test" and look at the "-d FILE" option. There's no need to bother with mount and grep to determine whether the directory exists.
    The thread has kind of shifted gears since the start... as I'm now trying to determine if the hard drive mounted to said directory exists. Mostly because one of the two boxes I'm working with is a Raspberry Pi which does not support automatic USB mount, so I'm still going to utilize fstab for that.

  8. #8
    Join Date
    Sep 2007
    Location
    Oklahoma, USA
    Beans
    2,378
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Is there a way to make a directory not appear if nothing is mounted to it?

    Actually I have my USB devices mounted through fstab rather than through the automounting capabilities, with the "noauto" option so that they don't hold up booting and the "user" option so that they don't require root privileges to be mounted. The "-d" option to "test" could be used with a full path to your mount point on the Pi, and should still show whether the named directory existed, I believe. If the drive wasn't there, I would expect the test to show that it didn't exist -- but this is guesswork, untested...
    --
    Jim Kyle in Oklahoma, USA
    Linux Counter #259718
    Howto mark thread: https://wiki.ubuntu.com/UnansweredPo.../SolvedThreads

  9. #9
    Join Date
    Apr 2008
    Location
    LOCATION=/dev/random
    Beans
    5,767
    Distro
    Ubuntu Development Release

    Re: Is there a way to make a directory not appear if nothing is mounted to it?

    The way I tend to handle this sort of situation is as follows.

    First create your mount point (/mnt/data in this example).
    Then with the device unmounted create a file in the directory, for example:
    touch /mnt/data/unmounted
    Now if you need to check whether the drive is mounted or not you can do a quick check to see if the unmounted file exists (it won't be visible once your drive is mounted):
    Code:
    if [ -f /mnt/data/unmounted ]
    then
      echo "Drive is unmounted"
    else
      echo "Drive is mounted"
    fi
    Last edited by Cheesemill; December 11th, 2012 at 03:07 PM.
    Cheesemill

  10. #10
    Join Date
    Apr 2008
    Location
    LOCATION=/dev/random
    Beans
    5,767
    Distro
    Ubuntu Development Release

    Re: Is there a way to make a directory not appear if nothing is mounted to it?

    Quote Originally Posted by JKyleOKC View Post
    I believe. If the drive wasn't there, I would expect the test to show that it didn't exist -- but this is guesswork, untested...
    See my post above.

    The test you are describing would return the same value whether the drive was mounted or not, the mount point still exists whether or not anything is mounted under it or not.
    Cheesemill

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •