Ceci est une ancienne révision du document !
Table des matières
Version : 2024.01
Last update : 2024/10/11 09:38
RH12401 - File System
Module content
- RH12401 - File System
- Module content
- LAB #1 - Linux File Hierarchy System
- 1.1 - File Types
- 1.2 - The mount command
- 1.3 - The umount command
- 1.4 - The /etc/fstab file
- Mount Options
- LAB #2 - Unix File System
- 2.1 - Superblock
- 2.2 - Inodes
- 2.3 - Data blocks
- 2.4 - Physical links
- 2.5 - Symbolic links
LAB #1 - Linux File Hierarchy System
The Linux file system is organised around a single tree with a starting point called the root, represented by the character /. Below this root are directories containing files and sub-directories. The organisation of directories conforms to a standard called the Linux File Hierarchy System.
[trainee@redhat9 ~]$ cd / [trainee@redhat9 /]$ ls -l total 28 dr-xr-xr-x. 2 root root 6 Aug 10 2021 afs lrwxrwxrwx. 1 root root 7 Aug 10 2021 bin -> usr/bin dr-xr-xr-x. 5 root root 4096 Sep 25 12:30 boot drwxr-xr-x. 20 root root 3320 Sep 25 12:44 dev drwxr-xr-x. 133 root root 8192 Sep 25 12:44 etc drwxr-xr-x. 3 root root 21 Oct 19 2023 home lrwxrwxrwx. 1 root root 7 Aug 10 2021 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Aug 10 2021 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 Aug 10 2021 media drwxr-xr-x. 2 root root 6 Aug 10 2021 mnt drwxr-xr-x. 2 root root 6 Aug 10 2021 opt dr-xr-xr-x. 242 root root 0 Sep 25 12:44 proc dr-xr-x---. 4 root root 4096 Oct 19 2023 root drwxr-xr-x. 44 root root 1160 Sep 25 12:44 run lrwxrwxrwx. 1 root root 8 Aug 10 2021 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Aug 10 2021 srv dr-xr-xr-x. 13 root root 0 Sep 25 12:44 sys drwxrwxrwt. 15 root root 4096 Sep 25 12:48 tmp drwxr-xr-x. 12 root root 144 Oct 19 2023 usr drwxr-xr-x. 20 root root 4096 Oct 19 2023 var
- /afs: Andrew File System (AFS) is a distributed file system that uses a set of trusted servers to present a consistent, location-transparent file namespace.
- /bin: is short for binary or binaries. It contains programs such as ls. Under RHEL 9 it is a symbolic link pointing to /usr/bin.
- /boot: contains the files needed to boot the system.
- /dev: contains the nodes used to access all types of hardware, such as /dev/fd0 for the floppy drive. The udev binary is responsible for dynamically creating and deleting nodes.
- /etc: contains configuration files such as passwd for passwords and fstab, which is the list of file systems to be mounted when the system boots.
- /home: contains the directories for each user except root.
- /lib: contains the common 32-bit libraries used by programs and modules. Under RHEL 9 this is a symbolic link pointing to /usr/lib.
- /lib64: contains the common 64-bit libraries used by programs and modules. Under RHEL 9 this is a symbolic link pointing to /usr/lib64.
- /media: contains directories for each mounted file system (accessible to the Linux system) such as floppy, cdrom etc.
- /mnt: contains directories for each file system temporarily mounted by root.
- /opt: contains optional applications.
- /proc: contains a virtual file system which extracts the information being processed from memory. The contents of the files are created dynamically during consultation. Only root can view all the information in the /proc directory.
- /root: the home of root, the system administrator.
- /run: replaces the /var/run directory.
- /sbin: contains binaries, i.e. programmes, for administering the local system. Under RHEL 9 this is a symbolic link pointing to /usr/sbin.
- /srv: contains data for the services hosted by the system, such as ftp, databases, web, etc.
- /sys: contains a virtual file system whose role is to describe the hardware for udev.
- /tmp: stores temporary files created by programs.
- /usr: contains user commands in /usr/bin, HOWTOs in /usr/share/doc, manuals in /usr/share/man and other major entries.
- /var: contains files of variable size.
1.1 - File Types
There are three main types of file on the Linux system:
- ordinary files
- directories
- special files (or Devices).
Ordinary files are text files, tables or executables.
The length of the file name is limited to 255 characters.
There is a distinction between upper and lower case.
If a file name begins with ., the file becomes hidden.
1.2 - The mount command
In order for Linux to be informed of the presence of a file system, this system must be mounted. To mount a file system, use the mount command:
# mount /dev/<special_file> /mnt/<target_directory>
where /dev/<special_file> is the device to be mounted and /mnt/<target_directory> is the directory that will be used as a ‘window’ to view the contents of the file system. This directory must exist before attempting to mount the file system.
If the mount command is used without options, the system returns a list of all the file systems currently mounted:
[trainee@redhat9 /]$ su - Password: fenestros [root@redhat9 ~]# mount proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel) devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=4096k,nr_inodes=976019,mode=755,inode64) securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel,inode64) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,size=1573912k,nr_inodes=819200,mode=755,inode64) cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,seclabel,nsdelegate,memory_recursiveprot) pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime,seclabel) bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700) /dev/mapper/rhel-root on / type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota) selinuxfs on /sys/fs/selinux type selinuxfs (rw,nosuid,noexec,relatime) systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=29,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=12983) tracefs on /sys/kernel/tracing type tracefs (rw,nosuid,nodev,noexec,relatime,seclabel) debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,relatime,seclabel) mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime,seclabel) hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel,pagesize=2M) none on /run/credentials/systemd-tmpfiles-setup-dev.service type ramfs (ro,nosuid,nodev,noexec,relatime,seclabel,mode=700) fusectl on /sys/fs/fuse/connections type fusectl (rw,nosuid,nodev,noexec,relatime) configfs on /sys/kernel/config type configfs (rw,nosuid,nodev,noexec,relatime) none on /run/credentials/systemd-sysctl.service type ramfs (ro,nosuid,nodev,noexec,relatime,seclabel,mode=700) /dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota) none on /run/credentials/systemd-tmpfiles-setup.service type ramfs (ro,nosuid,nodev,noexec,relatime,seclabel,mode=700) tmpfs on /run/user/42 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=786956k,nr_inodes=196739,mode=700,uid=42,gid=42,inode64) tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=786956k,nr_inodes=196739,mode=700,uid=1000,gid=1000,inode64)
This information is stored in the /etc/mtab file:
[root@redhat9 ~]# cat /etc/mtab proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0 sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0 devtmpfs /dev devtmpfs rw,seclabel,nosuid,size=4096k,nr_inodes=976019,mode=755,inode64 0 0 securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0 tmpfs /dev/shm tmpfs rw,seclabel,nosuid,nodev,inode64 0 0 devpts /dev/pts devpts rw,seclabel,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0 tmpfs /run tmpfs rw,seclabel,nosuid,nodev,size=1573912k,nr_inodes=819200,mode=755,inode64 0 0 cgroup2 /sys/fs/cgroup cgroup2 rw,seclabel,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot 0 0 pstore /sys/fs/pstore pstore rw,seclabel,nosuid,nodev,noexec,relatime 0 0 bpf /sys/fs/bpf bpf rw,nosuid,nodev,noexec,relatime,mode=700 0 0 /dev/mapper/rhel-root / xfs rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota 0 0 selinuxfs /sys/fs/selinux selinuxfs rw,nosuid,noexec,relatime 0 0 systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=29,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=12983 0 0 tracefs /sys/kernel/tracing tracefs rw,seclabel,nosuid,nodev,noexec,relatime 0 0 debugfs /sys/kernel/debug debugfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0 mqueue /dev/mqueue mqueue rw,seclabel,nosuid,nodev,noexec,relatime 0 0 hugetlbfs /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=2M 0 0 none /run/credentials/systemd-tmpfiles-setup-dev.service ramfs ro,seclabel,nosuid,nodev,noexec,relatime,mode=700 0 0 fusectl /sys/fs/fuse/connections fusectl rw,nosuid,nodev,noexec,relatime 0 0 configfs /sys/kernel/config configfs rw,nosuid,nodev,noexec,relatime 0 0 none /run/credentials/systemd-sysctl.service ramfs ro,seclabel,nosuid,nodev,noexec,relatime,mode=700 0 0 /dev/sda1 /boot xfs rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota 0 0 none /run/credentials/systemd-tmpfiles-setup.service ramfs ro,seclabel,nosuid,nodev,noexec,relatime,mode=700 0 0 tmpfs /run/user/42 tmpfs rw,seclabel,nosuid,nodev,relatime,size=786956k,nr_inodes=196739,mode=700,uid=42,gid=42,inode64 0 0 tmpfs /run/user/1000 tmpfs rw,seclabel,nosuid,nodev,relatime,size=786956k,nr_inodes=196739,mode=700,uid=1000,gid=1000,inode64 0 0
Important: Note that the file system for /dev/sda1 and /dev/mapper/rhel-root is xfs.
1.3 - The umount command
To unmount a file system, use the umount command:
# umount /mnt/<target_directory>
or
# umount /dev/cdrom
====1.4 - The /etc/fstab file
If the mount command is used with the -a option, all the file systems listed in a special file called /etc/fstab will be mounted at the same time:
[root@redhat9 ~]# cat /etc/fstab==== # # /etc/fstab # Created by anaconda on Thu Oct 19 16:05:58 2023 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk/’. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info. # # After editing this file, run ‘systemctl daemon-reload’ to update systemd # units generated from this file. # /dev/mapper/rhel-root / xfs defaults 0 0 UUID=6f6c5bb9-30be-4734-bc23-03fed8541616 /boot xfs defaults 0 0 /dev/mapper/rhel-swap none swap defaults 0 0
Each line in this file contains 6 fields:
Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 |
---|---|---|---|---|---|
Special block file or UUID or virtual file system or a label | Mount point | File system type | Comma separated options | Used by dump ( 1 = to dumper, 0 or empty = to ignore ) | The order in which fsck checks file systems at boot time |
The UUID ( Universally Unique Identifier ) is a 128-bit string. UUIDs are created automatically and at random when the filesystem is created on the partition. They can be modified by the administrator.
Mount Options
The most important mount options are :
Option | Filesystems | Description | Default |
---|---|---|---|
defaults | All | Equal to rw, suid, dev, exec, auto, nouser, async | |
auto/noauto | All | Automatic mount/no automatic mount when using mount -a | auto |
rw/ro | All | Mount read-write/read-only | rw |
suid/nosuid | All | SUID and SGID bits are/are not taken into account | suid |
dev/nodev | All | Interprets/does not interpret special device files | dev |
exec/noexec | All | Allow:do not allow programs to run | exec |
sync/async | All | Synchronous/asynchronous mount | async |
user/nouser | All | ||
users | All | Authorise all users to mount/unmount the file system | N/A |
owner | All | Allows the owner of the device to mount it | N/A |
atime/noatime | POSIX standard | Write/unwrite access date | atime |
uid=value | Non-Linux formats | Specifies file owner number for non-Linux file systems | root |
gid=value | Non-Linux Formats | Specifies the owner group number | N/A |
umask=value | Non-Linux Formats | Specifies permissions (access/read/write rights) | N/A |
dmask=value | Non-Linux Formats | Specifies folder usage rights (Obsolete, prefer dir_mode) | umask current |
dir_mode=value | Non-Linux Formats | Specifies folder usage rights | umask current |
fmask=value | Non-Linux Formats | Specifies file usage rights (Obsolete, prefer file_mode) | umask current |
file_mode=value | Non-Linux Formats | Specifies file usage rights | umask current |
LAB #2 - Unix File System
Each partition on a Unix system can host one of the following structures:
- superbloc
- inode
- data block
- indirection blocks
2.1 - Superblock
The superblock contains:
- the size of the blocks
- the size of the file system
- the number of mounts performed for this file system
- a pointer to the root of the file system
- pointers to the list of free inodes
- pointers to the list of free data blocks
2.2 - Inodes
Each file is represented by an inode. The inode contains :
- the type of file, i.e. -, d, l, b, c, p, s
- access rights, for example rwx rw- r–
- the number of physical links, i.e. the number of names
- the creator's UID or the UID assigned by the chown command if there has been a modification
- the GID of the creator process or the GID assigned by the chgrp command
- the size of the file in bytes
- the date on which the inode was last modified, i.e. ctime.
- the date on which the file was last modified, i.e. mtime.
- the date of the last access, i.e. atime.
- the addresses which point to the data blocks in the file.
Graphically, this organisation can be represented as follows:
To understand this better, type the following command:
[root@redhat9 ~]# ls -ld /dev/console /dev/sda1 /etc /etc/passwd crw--w----. 1 root tty 5, 1 Sep 25 12:44 /dev/console brw-rw----. 1 root disk 8, 1 Sep 25 12:44 /dev/sda1 drwxr-xr-x. 133 root root 8192 Sep 25 12:44 /etc -rw-r--r--. 1 root root 2109 Oct 19 2023 /etc/passwd
The first character of each line can be one of the following:
- - - a file
- d - a directory
- l - a symbolic link
- b - a block device
- c - a character device
- p - a named pipe for inter-process communication
- s - a socket in a network context
To view the inode number, use the -i option:
[root@redhat9 ~]# ls -ldi /dev/console /dev/sda1 /etc /etc/passwd 12 crw--w----. 1 root tty 5, 1 Sep 25 12:44 /dev/console 273 brw-rw----. 1 root disk 8, 1 Sep 25 12:44 /dev/sda1 67154049 drwxr-xr-x. 133 root root 8192 Sep 25 12:44 /etc 68914044 -rw-r--r--. 1 root root 2109 Oct 19 2023 /etc/passwd
2.3 - Data blocks
Data is stored in data blocks. In the case of a directory, the data block contains a table which references the inodes and file names in the directory. This table is called a catalogue table.
The name of a file is not stored in the inode but in a catalogue table. This feature allows us to give two different names to the same file. To add a new name to a file, a physical link must be created.
2.4 - Physical (hard) links
A physical or hard link is created using the following command:
- ln file_name additional_name
To illustrate this point, type the following command line:
[root@redhat9 ~]# cd /tmp; mkdir inode; cd inode; touch file1; ls -ali total 4 33625083 drwxr-xr-x. 2 root root 22 Sep 25 13:08 . 33554561 drwxrwxrwt. 16 root root 4096 Sep 25 13:08 . 33625154 -rw-r--r--. 1 root root 0 Sep 25 13:08 file1
Note the inode number of the file file1. Also note that the number in the third field of the file1 line has the value 1 :
33625154 -rw-r--r--. 1 root root 0 Sep 25 13:08 file1
Now create a physical link :
[root@redhat9 inode]# ln file1 file2 [root@redhat9 inode]# ls -ali total 4 33625083 drwxr-xr-x. 2 root root 38 Sep 25 13:09 . 33554561 drwxrwxrwt. 16 root root 4096 Sep 25 13:08 . 33625154 -rw-r--r--. 2 root root 0 Sep 25 13:08 file1 33625154 -rw-r--r--. 2 root root 0 Sep 25 13:08 file2
Note the following two lines:
33625154 -rw-r–r–. 2 root root 0 Sep 25 13:08 file1
33625154 -rw-r–r–. 2 root root 0 Sep 25 13:08 file2
Both files, file1 and file2, are referenced by the same inode. The number of links is therefore increased by 1 (the number in the third field).
Important: A physical link can only be created if the two files are in the same filesystem and the source file exists.
2.5 - Symbolic Links
A symbolic link is a shortcut to another file or directory. A symbolic link is created using the following command:
- ln -s filename shortcut_name
To illustrate this point, type the following command line:
[root@redhat9 inode]# ln -s file1 file3 [root@redhat9 inode]# ls -ali total 4 33625083 drwxr-xr-x. 2 root root 54 Sep 25 13:10 . 33554561 drwxrwxrwt. 16 root root 4096 Sep 25 13:08 . 33625154 -rw-r--r--. 2 root root 0 Sep 25 13:08 file1 33625154 -rw-r--r--. 2 root root 0 Sep 25 13:08 file2 33625156 lrwxrwxrwx. 1 root root 8 Sep 25 13:10 file3 -> file1
Note that the symbolic link is referenced by another inode. The symbolic link points to file1.
Important: A symbolic link can be created even if the two files are in two different filesystems and even if the source file does not exist.
Copyright © 2024 Hugh Norris.