script to check for a directory in /home for all users

Following on from this post:

and getting told off for bumping the thread:(

Please could someone help me with a short script to check is a certain directory is present in /home for all users and if not create it?

Thanks

Posts #1 and #7 in that thread are vague, difficult to understand and appear contradictory. I can see why post #7 stopped the thread dead.

In order to script something there would need to be an existing working manual process or at least a clear definition of the process. I see neither.
I don't think that it is even clear which computer(s) need the process or whether you are clear what "mount" means in the unix world.

Others may differ.

Yes, you are probably right and demonstrates my lack of knowledge as I have only been using Linux for about 18months. I have come a long way in the last 18months as a non-IT proffessional but realise these types of scripts are way beyond my ability, hence the posts on these types of forums - this is probably the best I have found so far.

Basically I have a central server running ubuntu 10.04 server edition. This acts as a file, print, samba, ftp server and gateway for my local network. I have used a mix of ebox, webmin and CLI to administer it. I would like to mount a common directory in the /home directory of all the users at start up. I quickly realised that this was not possible by mounting them via fstab without listing all the mount points individually which is why I have turned to creating a startup script. All the users have real accounts on the server and login via LDAP for samba. In summary I need a script that will:

  1. check if a given directory is present in /home of all users
  2. create the directory if it is not present to act as a mount point
  3. mount a separate directory that resided on a different partition at the mount point that has just been created for all users.

Hopefully this is clearer:)

Thanks

Dir=directory
if ! [ -d "$Dir" ]; then
   mkdir "$Dir"
   # ... other processing 
fi

How to determine what that separate directory is ?
Is it's name the user's name ?
Why mounting ? Can't you just create a symbolic link pointing to the other dir ?

Thanks. So hypothetically if the directory to be mounted was

/media/share

the mount point in the users home is:

/home/{username}/newdir

would the following script do what I want:

Dir=newdir
if ! [ -d "$Dir" ]; then
 mkdir "$Dir"
fi
ls /home | xargs -n1 -I{} mount /media/share /home/{}/newdir
exit 0

I want to jail the user to their home directory for ftp access which is easily done with ProFTP

Thanks again

I'm not totally certain but this should jail the user too :

cd ~user
ln -fs /media/share/ newdir

This done, if you cd newdir you will be in /media/share/ and after if you cd .. you will be in user's home, not in /media/.
Depending on how the user is jailed in his home directory...

Thank you for making the question clear. Sorry if I was a bit abrupt earlier.

Like frans I am having difficulty understanding item 3 in a unix context.

The Linux "mount" command is used to mount a filesystem on a mountpoint.
A mountpoint is an empty directory which acts as a pointer to the filesystem.
We would normally mount the filesystem on system startup using parameters in one line of "fstab". I can't see a reason to make this dynamic.

As frans correctly deduced, the conventional approach in unix is to use a soft link (see "man ln") to point a directory under a user's home directory to a directory in a filesystem which is under a different mountpoint from the user's home directory.
I am unclear whether there is to be one common directory for all users or multiple individual directories.

If we are using soft links, items 1 and 2 only need to be done once for existing accounts and then as required when a new account is created.

(I had not seen posts #5 and #6 before eventually posting).

The problem with symlinks is that they can't be followed if the user is restricted to their home directory in proftp and in samba if wide links are disabled

I was aware of this but am not able to put it so eloquently, I am very much open to suggestion if you have a better method of allowing access to a separate directory without using symlinks and preventing the user wandering around the filesystem.

Basically, I have a directory mounted by fstab as /media/share and I would like this to be available to all users as a directory in their /home dir as /home/USERNAME/newdir . Currently I have a line in the fstab for each user that looks something like /media/share/ /home/barrydocks/newdir auto defaults,bind 0 0 I just thought that there would be a more efficient way of doing it so that I didn't have to edit fstab each time a user is added?

Okay. Time to back off.

Imho. the concept is flawed and does not scale satisfactorily.
We don't know your maximum number of user accounts but I believe that you will potentially create a situation whereby the sheer number of entries in fstab will break the kernel. Also "fsck" after any form of crash will be a nightmare.

Personally (assuming that the aforementioned use of soft links is not allowed) I'd create the private directory under /home and be done with it. If there isn't enough disc space I'd create the home directory under /media/share.
A home directory does not have to be under /home and a jailed account should not be able to find out where they are in the master directory tree. As far as they are concerned their home directory is root.

There will no doubt be experts on this forum who have had good experiences with automounting selected directories from a shared filesystem using Ubuntu Linux on a large scale.