LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   'mount' command permissions retention (https://www.linuxquestions.org/questions/linux-newbie-8/mount-command-permissions-retention-4175502791/)

s.verma 04-24-2014 12:44 AM

'mount' command permissions retention
 
I created a filesystem on file 'file.img' formatted with ext4, a mountpoint 'temp/' in the current directory.

Commands Issued
Code:

$ dd if=/dev/urandom of=file.img bs=1024 count=10000
10000+0 records in
10000+0 records out
10240000 bytes (10 MB) copied, 1.06567 s, 9.6 MB/s
$ mkdir temp
$ ls -l
total 10004
-rw-r--r-- 1 username users 10240000 Apr 24 10:56 file.img
drwxr-xr-x 2 username users    4096 Apr 24 10:57 temp/
$ mkfs.ext4 file.img
mke2fs 1.42.8 (20-Jun-2013)
file.img is not a block special device.
Proceed anyway? (y,n) y
Discarding device blocks: done                           
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
2512 inodes, 10000 blocks
500 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=10485760
2 block groups
8192 blocks per group, 8192 fragments per group
1256 inodes per group
Superblock backups stored on blocks:
        8193

Allocating group tables: done                           
Writing inode tables: done                           
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
$ sudo mount file.img temp/
$ ls -l
total 1173
-rw-r--r-- 1 username users 10240000 Apr 24 10:59 file.img
drwxr-xr-x 3 root  root      1024 Apr 24 10:59 temp/

As I can not write inside temp/ as normal user, I issued
Code:

$ sudo chmod 757 temp/
$ ls -l
total 1173
-rw-r--r-- 1 username users 10240000 Apr 24 10:59 file.img
drwxr-xrwx 3 root  root      1024 Apr 24 10:59 temp/

Now when I umount it, temp/ returns back to its permissions
Code:

$ sudo umount temp
$ ls -l
total 1176
-rw-r--r-- 1 username users 10240000 Apr 24 11:02 file.img
drwxr-xr-x 2 username users    4096 Apr 24 10:57 temp/

Now when I again mount file.img
Code:

$ sudo mount file.img temp
$ ls -l
total 1173
-rw-r--r-- 1 username users 10240000 Apr 24 11:03 file.img
drwxr-xrwx 3 root  root      1024 Apr 24 10:59 temp/

This time temp/ has retained 757 permission without my changing it. How it happened?
Where mount (or linux) store information what permission to give to a mountpoint when it is mounted?

jpollard 04-24-2014 08:03 AM

When you have mount point, chmod refers to the filesystem so mounted. When dismounted, the file reference returns to what it was BEFORE the mount.

When you remounted the filesystem, the access modes are from the chmod done when it was originally mounted.

One way to think about this is that the directory file reference (in memory) contains two parts - the name, and its location in the directory tree... The other half is an inode reference containing the connection to the contents of the directory (the inode carries the access mode).

When you mount a filesystem onto a directory, the top half (the one with the name and location in the directory tree) remains unchanged... But the other half is replaced by a reference to the now mounted filesystem, which has the inode of the root directory of that filesystem. And that inode has the access mode for the filesystem, which is now visible due to the mount.

The original access mode is covered up. It is still in the system buffers though, so when you dismount the filesystem the original inode reference is restored, and the original access mode is visible again.

When you change the access mode while a filesystem is mounted, the change is done to the inode from that mounted filesystem. So a dismount restores the visibility of the original mode. The inode from the previously mounted filesystem carries the access mode that was changed.

Shadow_7 04-24-2014 08:37 AM

If you add the mount to /etc/fstab with the option users you can do the mount as the user and it will have the permissions of the user. You can also pass -o users to the mount command as root. Just bear in mind that ext4 is a filesystem who's only default permissions belong to root (aka / + lost+found). So you'd have to mount it, and change the permissions to be written to by the user, before it would be of any use to a user. Also on my system, mkfs.ext4 is not available to userland (debian defaults).

s.verma 04-25-2014 05:51 AM

Dear jpollard,

Quote:

chmod refers to the filesystem so mounted.
Quote:

...a reference to the now mounted filesystem, which has the inode of the root directory of that filesystem...
Quote:

...the change is done to the inode from that mounted filesystem...
OK as far as I understood it, I am restating it in my words.
"The temp/ after mount become root of the mounted filesystem, and hence when I am changing permissions after mount then the permissions are changed for the root of file.img, and not the actual temp/ directory created. Hence comes the two different inodes. The inode of file.img is changed, while the inode of temp/ directory before mount is not."

Please let me know if I have got it correctly (as I do not know much about inodes, except that they are some kind of references to files).


Thanks Shadow_7,
But I was just trying to find out how the changed permissions of root of filesystem was retained in second mount which I think is answered by jpollard if I have been able to understand him/her correctly.

jpollard 04-25-2014 06:14 AM

The only error is the "inode of file.img" which isn't involved - the inode being modified by the chmod is from the root inode of the filesystem WITHIN the file.img file. The inode data is stored in the file.

The inode OF the "file.img" is not touched other than when mounting the file to locate where the data for the filesystem being mounted resides.

s.verma 04-25-2014 09:15 AM

OK I got it, it is inode of root inside filesystem and not 'file.img'.

Errata:
Quote:

The inode of file.img is changed,
to be replaced by
Quote:

The inode of root of filesystem contained in file.img
Thanks for the help!


All times are GMT -5. The time now is 07:35 PM.