How to set up smartphones and PCs. Informational portal
  • home
  • OS
  • The structure of the file system. Linux file system and directory structure

The structure of the file system. Linux file system and directory structure

Linux file system

One of the most important components in Linux is the file system. IN this section concept will be considered file system and its purpose; file system hierarchy of Linux OS; Linux OS file types. Other issues related to file system administration will be covered in more detail in Module 3.

Purpose of the file system

In Linux, as in any other UNIX operating system, any object is a file stored on the file system. A file system is a device (such as a hard drive) formatted to store files. File systems can be located on hard drives, floppy disks, CD-ROMs, or other media that allow random or sequential access to data.

Conventionally, the Linux OS file system can be divided into the following components.

Namespace - naming methods for file system objects and their hierarchical organization.

Application programming interface (API) - a set of system calls and libraries designed to manage file system objects.

Security model - contains general scheme protection, separation of access rights to objects and sharing objects.

Technical implementation - programming code linking logical models file system with hardware.

The main tasks of the file system are:

organizing stored data;

simple and fast access to stored data;

ensuring the integrity of stored data.

The exact format and ways in which files are stored in Linux does not matter, as the system provides common interface for all types of file systems it recognizes. OS Linux file the default system is ext3fs. When accessing any file system from the OS linux data are represented as a hierarchy of directories with the files they contain, along with owner and group IDs, permission bits, and other attributes.

File system hierarchy

The directory hierarchy of the Linux OS file system complies with the Filesystem Hierarchy System (FHS) standard generally accepted in the UNIX world. Main advantage this standard thing is certain types files are located in their respective directories.

For example, most configuration files are located in the /etc directory, while log files for various services are located in the /var/log directory.

Description of OS file system directories linux

The /bin, /usr/bin, /usr/locl/bin, /sbin, /usr/sbin, and /usr/local/sbin directories contain the commands installed on the system. When working on the system regular user, you will only have access to the /bin, /usr/bin, and /usr/locl/bin directory commands, because the FHS specifies that only administrative commands should be contained in the sbin directories.


Figure 1.2 Directory Structure of Linux OS

The main directory of the Linux OS file system is the root directory /. Below the root directory are all other directories created on the local disk subsystem or mounted from external devices. The procedure for mounting a file system means linking a directory of an existing file system, called a mount point, to the root directory of a new file system.

Mounting a file system to a mount point is done using the mount command. The following listing shows an example of a mount DVD drive a, containing the RedHatEnterpriseLinux 5 OS distribution.

# mount /dev/hdc /mnt/

mount: block device /dev/hdc is write-protected, mounting read-only

As a result of executing the mount command, the system displayed information that the block device (in this case DVD drive) was mounted in read mode.

The list of mounted filesystems is stored in the /etc/fstab file. Thanks to this, it is possible automatic check file system integrity using the fsck command and mounting file systems at the stage bootstrap, as well as running shorthand commands like mount /var/spool. The information contained in this file reflects the locations of the file systems on the disk. The /etc/fstab file will be covered in more detail in Module 3.

Unmounting file systems is done with the umount command. A "locked" filesystem cannot be unmounted. It should not contain any open files, nor the current directories of running processes. If the filesystem being unmounted contains executable programs, they must not be run. The following listing shows an example of unmounting a previously mounted file system in the /mnt directory.

# umount /mnt # Is -1 /mnt total 0

In order to find out which devices are mounted to the system in this moment you must run the mount command without options. The following listing shows an example of determining mounted devices.

# mount /dev/sda2 on /type ext3 (rw)

proc on /proc type proc (rw)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) /dev/hdc on /mnt type iso9660 (ro)

As you can see, the output of the mount command sequentially displays the mounted device, mount point, file system type, and additional options mount.

File types

When working with Linux OS, it is important to understand that any OS object is a file. This is a key feature of the Linux operating system compared to the operating systems of the Windows family.

The files differ both in their structure and in their purpose. Linux defines seven types of files:

regular files (regular files);

directories;

named pipes;

block device files;

character device files.

You can determine the file type using the Is -Id command. The first character in the output line indicates the file type. The following listing gives information about the file

/dev/hdc. # Is -Id /dev/hdc

Regular files

Regular files include binary files, libraries, text files, and various application files. Linux OS does not impose any restrictions on the structure of these files. Their content can be accessed both sequentially and directly.

Catalogs

Linux has what are known as special directories. Special directories such as "." and "..", denote the current working directory and its parent directory, respectively.

In Linux, it is customary to distinguish between symbolic and hard links, each of which has a special meaning.

A symbolic link allows you to specify its alias instead of a file name. In the process of searching for a file through symbolic links, the kernel extracts the names stored in them. A hard link is a direct link, i.e. points directly to the inode of a file, while a symbolic link points to a file by its name. The file addressed by a symbolic link and the link itself are different file system objects.

Symbolic links can contain an arbitrary name, i.e. they are allowed to point to files stored in other file systems and even to non-existent files. Hard links cannot point to a file located on another file system.

Linux OS counts the number of links to each file, and when a file is deleted, it does not release data blocks until the last link to it is deleted.

sockets

A socket is a special file type used by processes to communicate with each other. Socket connections allow processes to communicate without being influenced by other processes. There are several types of sockets in Linux OS, the use of which assumes the presence network infrastructure. Local sockets are available only on the local computer, they are accessed through special file system objects, and not through network ports. Such sockets are called UNIX domain sockets (UNIXdomainsocket). In addition to local sockets, there are network sockets that allow processes to communicate over the network.

Although other processes recognize socket files as directory entries, only processes with an appropriate connection can read and write socket files. Work with local sockets various services Linux OS - CUPS, XWindow and Syslog.

Named pipes ( FIFO)

Files of this type are similar to sockets in that they are also used to communicate between processes, but unlike sockets, named pipes transfer data in only one direction.

Block and character device files

Block and character device files allow applications to access system hardware and peripherals. At the configuration stage, the necessary modules designed to control the system hardware are dynamically loaded to the Linux kernel. For management specific device responds to a special module called a device driver.

Device drivers form a standard interaction interface that is perceived by the user as a set of ordinary files. Upon receiving a request for a character or block device file, the kernel passes the request to the appropriate driver. Block and character device files are not drivers themselves. They can be thought of as gateways through which the driver receives requests to perform the intended operations.

Character device files do not use buffering during I/O operations. All I/O operations are performed immediately as they arrive. Character devices include virtual terminals, modems, and other devices that do not support random data access.

Linux (Linux) is an operating system that today is actually the only alternative replacement Windows OS from Microsoft. Linux originates from 1991, when a young programmer from Finland Linus Torvalds set to work on the very first version of the system, which was named after him. The dawn of Linux's popularity began with its very inception. This is due, first of all, to the fact that the kernel of this OS, like most programs written for it, has very important qualities.

Kernel The kernel is a low-level operating system. The machine-dependent part of the traditional level that processes files that runs from the kernel. The UNIX OS includes the following components: disks, network, and other necessary operations. (while it depends on the features of the equipment); The kernel of the Linux operating system is a monolithic microkernel architecture system. promotion and initialization of the system at a low level with interrupts; When compiling the kernel, you can enable dynamic preprocessing of internal and external management memory (in the part that relates to the loading and unloading of very many kernel components - features of hardware support for virtual so-called modules. At the time of loading the memory module); its code is loaded at the system level and linked to the rest of the kernel. Functions can be used inside a module. any kernel-exported process context switches between user and kernel modes; target platform specific parts of device drivers.

File structure(system) /bin - user base binaries /boot - static boot files /cdrom - historically chosen mount point for CDs /dev - device files /etc - configuration files/home - home directories /lib - core shared libraries /lost+found - recovered files /media - mountable media /proc - kernel and process files /root - home directory root user/srv - data service departments/tmp- temporary files/usr - user binaries and read-only data (shown in the picture)

Interface ( graphical shell) is a program that organizes the interaction of a user with a computer. Provides support for windows, menus, icons, mouse and other known GUI elements - GUI user. More complex graphical environments are built on top of X; the most popular of them are KDE and GNOME. GUI is a set of icons, menus, dialogs, panels, windows, and other graphic elements, allowing users to easily work with the computer and applications. KDE and Gnome are integrated desktop environments. Users work with interface elements and programs.

Utilities Utility (English utility or tool) - auxiliary computer program in Embedded in Linux software utilities and languages. composed of g++ -GNU C++ compiler; Perl is a very powerful scripting language; g 77 - GNU FORTRAN compiler; (parameters, settings, settings), or make the process f 2 c - transcoder from FORTRAN to C; parameter changes (automate it). Fort 77 is a FORTRAN compiler. Performs f 2 c and then general software to perform specialized typical tasks. Utilities provide access to features Types of utilities to communicate with the OS uses gcc or g++; Independent utilities that do not require an operating system for their work, Grep - search for a piece of text in files that matches the typed mask. The mask is defined with System Utilities, included in the delivery of the OS and requiring its help standard system notation, called the presence of " regular expressions"; Types of utilities by function File managers; Archivers (from possible compression data); viewers; Uninstaller - software [; program for Tr - translation utility (in other words - replacement of letters in a text file); Gawk - GNU awk (used to process formatted text files); software removal sed - a utility for processing text files;

It is very different from the device in windows. For starters, there are no C or D drives in Linux. One physical disk(or several) during installation of the system is divided into directories and subdirectories. The main, root directory is indicated by the symbol / (slash) Instead of a swap file, there is a separate partition /swap. Each directory can be formatted into the desired file system, depending on the user's tasks. For example Ext3, ReiserFS, JFS or another.
In different Linux distributions, the scheme and purpose of some directories may differ slightly.
You can create your own directories. I have a folder with movies on a separate hard drive. In the root partition, it looks like /films.

Description of main directories and subdirectories in linux:
/bin- It contains
main command files.

/boot- This directory contains the system bootloader (Grub or Lilo) and the files needed to boot the system.

/dev- The dev directory contains files describing the devices of the computer. On linux, all devices are considered files. Even ports, drives, printers are all file.

/etc- The directory containing the configuration files of the system, programs, startup scripts.
/etc/rc.d- command files that launch applications when the system boots.
/etc/passwd- all user data is encrypted in the file.
/etc/fstab- the file contains information about file systems automatically mounted at system startup

/home- User, home directory. Linux can have multiple users. Each user has his own directory. (for example /home/user) It contains any user files to which he has direct access
write/delete. Also in the home directory are user programs, settings. Their names start with a dot and look, for example, like this - .kde .fonts

/lib- Here are various system libraries, kernel modules
/lib/firmware- non-free kernel modules with closed license
/lib/modules- loadable kernel modules. For example, device drivers, file systems.

/mnt- Various pluggable devices are temporarily mounted in this directory. Flash drives, floppy disks, disks

/opt- Installed in the directory additional packages programs.

/proc- Directory with virtual file system. It, in the form of files, contains information about the processes occurring in the system.
/proc/modules- this file contains information about the list of loaded kernel modules.
/proc/cpuinfo - detailed information about the processor.
/proc/meminfo -
information about random access memory.
/proc/devices- device drivers built into the system kernel.
/proc/uptime- information about system uptime. Those. Operating time without reboot.
/proc/version- the version of the linux kernel used by the system.
All this data can be displayed in the console using the command cat.
For example cat /proc/cpuinfo

/root- Home directory of the administrator (superuser) of the system. Used by the administrator if the main home directory is unavailable for some reason.

/sbin- Here are the main programs executed by root.

/tmp- Directory for temporary storage of data by programs.

/usr- The main directory for installing programs. In addition to programs, it contains documentation files, program source codes, and kernels.
/usr/local- in this directory are
separately installed software packages.
/usr/src- sources installed programs and kernels.
/usr/man- manual files for installed programs.
/usr/lib- immutable configuration files and program data.

/var- The var directory contains data that changes frequently during system operation. For example, logs, program caches.
/var/local- mutable program data, set by the administrator in /usr/local.
/var/log- logs of various system programs.
/var/run- temporary files. Information is stored in them until the next reboot of the system.
/var/tmp- directory for temporary files.

The file structure of Linux is very different from that of Windows, so if you decide to seriously study UNIX-like systems, then, in my opinion, it is better to start with the directory structure.

History knows dozens of cases when an unprepared pilot successfully landed a plane. Or, for example, a novice doctor successfully performed an operation in field conditions. However, I think it is unlikely that anyone would voluntarily agree to take their place ...

What am I leading to? And to the fact that Windows user who saw Linux for the first time, of course, will be able to cope with it (go online or watch a video), however, as soon as any emergency situation arises, he will no longer know what to do!

Therefore, in order not to be such an ignorant user, you need to learn at least the basics and principles of the operating system with which you plan to work. And in today's article, we will look at the Linux directory structure, as well as the features of hard disk layout and partition mounting.

As the proverb says, appearance is deceptive. Modern Linux distributions can outwardly and according to the basic principles of work largely copy the familiar to many Windows environment. However, it is enough just to "dig" deeper and we will immediately see that we have something completely different and incomprehensible :)

It is easy to verify this with an example Linux Mint. Let's open the "Computer" folder on the Desktop and look at its contents:

As you can see, here, in addition to the connected flash drive, there is not a single hard disk partition familiar to us, except for the "File system" device. It is here (or rather, in the root directory called "/", which is sometimes erroneously called "/root/", although it is separate folder to "/") by default and stores all data, including system and user files! Very strange and incomprehensible at first glance. Let's figure it out...

The fact is that Linux uses a fundamentally different approach to organizing and working with the file system from Windows. If in Windows based are hard disk partitions on which we can create arbitrary folders with files, then in Linux everything is based on a clear directory hierarchy that does not depend on the layout of the hard drive and is regulated by the FHS standard (abbr. English "Filesystem Hierarchy Standard" - "file system hierarchy standard ").

The concept of disks and their partitions, of course, also exists in Linux, but here it is the folder structure that is primary, into one of which (usually "/media/", "/dev/" or "/mnt/") and mounted Various types disks. The drives themselves are usually named depending on the type of their connection: SATA - sda (sdb, etc., depending on their number), and IDE - hda (hdb...). Partitions on disks are simply numbered (for example, sda1, hdb2).

Separate Linux directories can be placed on different partitions different disks and even remote network storages. The only condition is that all of them must be mounted before the system boots. This is usually done automatically when Linux startup, but sometimes it may be necessary to manually edit the parameters.

I think that we have figured out a little about the basic principles of organizing information storage in Linux, so I suggest that you familiarize yourself specifically with the directory structure.

Main Linux folders

While there are hundreds of Linux distributions, most of them have a similar directory structure. On the one hand, this provides some compatibility with standards, and on the other hand, ease of use: a user who is used to organizing a file system on one Linux distribution will be quite easy to switch to using another if necessary.

In principle, it is not necessary to know the purpose of all folders by memory, however, such knowledge is welcome. Therefore, I propose to consider in the form of a table the main directories, their purpose and a partial comparison with Windows components(I describe according to the Linux Mint I have, so the list of folders on other systems may differ slightly).

Catalog Purpose Windows equivalent (if any)
/ Stores the entire directory structure of the system Disk C:
/bin/ Stores binary executable files of system utilities C:\Windows\System32
/boot/ Stores the system kernel and other boot files C:\Windows
/cdrom/ Stores mount points for drives -
/dev/ Stores connected device files by type ( hard disks, video devices, etc.) or pseudo devices (e.g. /dev/null and /dev/zero) -
/etc/ Stores some system settings files and settings for installed programs Partly C:\Windows\System32, partly C:\Program Files
/home/ Stores home folders of system users with their settings and data C:\Documents and Settings or C:\Users
/lib/ Stores most libraries standard programs and some established Partial C:\Windows and C:\Windows\System32
/lost+found/ Stores files that, as a rule, as a result of some failures, remained unwritten in any of the directories, but at the same time were not marked for overwriting. The user usually does not have access to it Partially C:\Recycler (Trash)
/media/ Stores mount points for everyone removable media(flash drives, floppy drives, removable hard drives) -
/mnt/ Stores temporary mount points for devices and their file systems that have been manually added -
/opt/ Stores additional packages of various programs C:\Users\Admin\Application Data
/proc/ Stores information about running processes and operation of the system kernel Partial Task Manager
/root/ Stores data for the superuser (root) of the system Partial C:\Users\Admin
/run/ Stores temporary files that are executed during the system boot process
/sbin/ Stores most system programs for administration and OS configuration Partial C:\Windows\System32
/srv/ Stores files responsible for the operation of the server part of the system and various protocols for data transfer over the network (HTTP, FTP, etc.) Partial C:\Windows\System32
/sys/ Stores a virtual file system with data about installed devices and drivers Partial Device Manager
/tmp/ Stores temporary files C:/windows/temp
/usr/ Stores data of system users and files of manually installed programs. Partial C:\Users\
/var/ Stores files with various changing data, logs, caches, etc. -

In addition to the folders in the root itself, you should also pay attention to some subdirectories:

Catalog Purpose Windows equivalent (if any)
/etc/X11/ Stores X Window System settings files -
/etc/samba/ Stores configuration files for the Samba server that is used to communicate with network folders Windows -
/home/username/ Stores files and configuration of a specific user account (Home Folder) C:\Users\Username
/usr/bin/ Stores a number of programs that are needed to work in multi-user mode -
/usr/share/ Stores general data of installed programs -
/usr/src/ Stores kernel source files -
/var/cache/ Stores program caches and software packages downloaded from the repository -
/var/games/ Stores saves and achievements of installed games -
/var/log/ Stores logs (logs) of the system and installed programs -
/var/mail/ Stores settings mailboxes users -
/var/run/ Stores data about running processes and daemons Partial Task Manager
/var/tmp/ Stores temporary files that are saved when the system is rebooted -
/var/www/ Stores web pages rendered by local server tools -

A bit about file systems

Since we decided to more or less thoroughly deal with the Linux directory structure, we cannot fail to mention such an important thing as the file system.

In Windows, we are used to using traditional NTFS and not fooling ourselves (well, FAT32 on flash drives or UDF on disks). In the world of Linux, everything is much more diverse. There is support for all Windows FS, but it is recommended to use special systems, optimized for UNIX-like operating systems.

All file systems can be conditionally divided into two groups: journaled and non-journaled. Journaled file systems allocate a certain place for storing a log with a list of files on the PC, their attributes and location. They are more resilient to failures and guarantee greater data integrity. Non-journaled systems are faster and do not require space for storing the log, however, they do not guarantee stable storage of information, since they perform all actions with files directly, without logging.

In order not to list all the file systems available today, I think it's better to mention only the best of them, which are the best choice for Linux.

  1. Ext4 is a journaling file system that is standard on most modern Linux distributions. It is well protected from fragmentation issues and is optimized to work with big files. If during installation you did not format the partition for the system manually, then most likely you have exactly Ext4.
  2. ext2- non-journaling file system, which was the main one for old Linux distributions (until the 2000s). It has a number of limitations for working with large files, however, at the same time, it is also the fastest FS, so it is often used in various comparative tests as a reference.
  3. Reiser4 is a journaling file system that many advanced users recommend for use on Linux. Its advantage is good stability and high speed work, which, moreover, can be further enhanced by activating a special plug-in for data compression.
  4. btrfs(also B-tree FS) is a journaling file system developed by the well-known company Oracle quite recently (in 2007). Its features include storing the file index in the so-called "B-trees" - hierarchical structures that make the most optimal use of RAM resources due to the small depth of data nesting.
  5. SWAP- a special kind of non-journaling file system that implements a data storage structure similar to the cellular structure of RAM. Due to this, it is used to implement the paging file in Linux.

On most modern distributions, the default HDD formatted in Ext4, as well as SWAP, under which a size similar to the size of the installed RAM is allocated. So, for example, the standard disk layout under Linux Mint looks like, which is done if you do not influence the creation of partitions manually:

However, advanced users are advised to pre-partition the disk in a more "sophisticated" way before installation to ensure better data safety during a forced reinstallation of the system by mounting separate directories on the partitions created. Let's consider several similar "recipes" for partitioning a disk.

Disk partitioning

I think you already understood why all this pandemonium with different sections. If, for example, you break something in the installed in a regular way(per partition) to a Linux system, you will inevitably lose all your user data when reinstalling! However, if they are stored separately from system files, then for reinstallation it will be necessary to affect only the system directories, and then mount the existing user directories.

Just the first markup option is to move the "/home/" directory to a separate disk partition. That is, in fact, we need to partition the disk into three partitions:

If you are planning dual boot with the installation of Windows, then in addition to the three partitions discussed above, it is better to put two partitions formatted in NTFS in place of sda1 and sda2: one for Disk C, and the second for Disk D, respectively.

More advanced users tend to make a more complex disk layout, taking into account its division into separate boot areas and program execution areas:

In principle, some users mount the aforementioned directories on sda4 on separate partitions, and some do not allocate them at all. It all depends on what tasks you plan to solve on your PC. If you have not yet decided how you will use individual directories, then I recommend leaving a couple of tens of gigabytes of hard disk space unallocated "in reserve". So at any time, right from under your system, you can remount the necessary directories to the newly created partitions.

And most importantly - how to create these sections! To do this, I recommend downloading the already mentioned program in the form boot image, burn this image to a USB flash drive or disk and boot from it.

During the boot process from the LiveCD, you will be asked a series of questions about boot options. Everywhere we leave everything by default, except for the screen with the choice of language. There you will need to enter the number corresponding to Russian-language interface(for me it was "22"). In all other questions, just press Enter and as a result we will get to the virtual desktop of the Debian OS with GParted running:

If you have chosen the Russian interface, then you will not have problems with working in the program even if you decide to repartition the disk with installed system. The algorithm of actions is as follows:

  1. Select the existing partition to be split and click the "Resize or Move" button, or similar item in the context menu.
  2. In the window that opens, using the sliders or windows for entering numbers, set the new partition size and its offset from the beginning or end of the disk (remember that it is better to place the root and swap closer to the beginning).
  3. Select the newly created unallocated area and click the "New" button on the toolbar or in the context menu.
  4. In the window that opens, set the type of file system, volume label and other parameters.
  5. We repeat the above steps as many times as necessary, after which we apply all the changes and wait for them to complete.

Now there is little left. Install the system in the root directory (if it has not been installed yet) and boot it. Now all the directories are on the same partition and we need to remount them to other areas of the disk specially created for this. This can be done almost entirely in visual mode or using the terminal. To make it clearer, let's consider the transfer process using the example of the /home/ directory in a semi-visual mode.

If you decide to do everything visually, then open, for example, the /mnt/ or /media/ folder and create a temporary folder in it (let's call it /newhome/) for data transfer. Now we still have to launch the terminal in order to mount the newly created folder to the partition to which we want to transfer the /home/ folder. This is done with a command like:

Naturally, instead of "ext4" you will specify your file system, and instead of "sda5" the partition to which you will transfer the folder. When the partition is mounted and the folder is on it, open it and copy all the current contents of the /home/ directory there. At the end of copying, unmount the partition from the folder with the following command:

$sudo umount /mnt/newhome

Now existing folder/home/ can either be completely deleted (or better, renamed, for example, to /oldhome/) and recreated, or simply completely cleared. The main thing is that we have an empty /home/ directory. Now we will mount our partition with the copied data here:

$sudo mount /dev/sda5 /home

If everything went well, then all the files that we copied will appear in the /home/ directory. That is, in fact, the transfer was completed successfully. It remains only to consolidate the success and make sure that we do not have to manually mount our directory each time the system boots. To do this, we will use editing the fstab file (abbreviated from the English "file systems table" - "file system table"), which is located in the /etc/ directory. Let's open it with any text editor and at the very end add this line:

/dev/sda5 /home ext4 nodev,nosuid 0 2

By doing this, we “tell” the system literally the following: on the /dev/sda5 partition there is an /home/ directory formatted in the ext4 system, in which you cannot create a directory with device configuration (/dev/), and operations with suid and sgid bits are also prohibited, disabled backup and the file system is mounted in the second pass (the root system is always mounted in the first pass).

Reboot and rejoice :) For true geeks, I’ll say that all operations (except for editing fstab) could be done directly in the console with approximately the following sequence of commands:

$sudo mkdir /mnt/newhome

$sudo mount -t ext4 /dev/sda5 /mnt/newhome

$find. -depth -print0 | sudo cpio --null --sparse -pvd /mnt/newhome/

$sudo umount /mnt/newhome

$sudo mv /home /oldhome

$sudo mkdir /home

$sudo mount /dev/sda5 /home

sudo gedit /etc/fstab

conclusions

Understanding directory structure and applying the former console commands gives a novice user a fairly good idea of ​​​​the system structure and teaches them to think Linux categories. In practice, there is nothing particularly difficult in marking and mounting disk partitions, but these actions significantly increase the stability of the OS, so they are recommended to be done first of all when installing a new system.

Spending a little time optimizing your directory structure will make your Linux more secure in terms of storing information and save you a lot of nerves and effort in the future!

P.S. Permission is granted to freely copy and quote this article, provided that the open active link on the source and preservation of the authorship of Ruslan Tertyshny.

Basic concepts

The Linux operating system was developed in accordance with the requirements of the international standard for UNIX-compatible systems IEEE POSIX, so it would be logical to briefly consider the structure of the UNIX file system first.

One of the advantages of the UNIX operating system is that the system is based on small number intuitive concepts. However, despite the simplicity of these concepts, you need to get used to them. Without this, it is impossible to understand the essence of the UNIX operating system.

User

From the very beginning, UNIX was conceived as an interactive system. In other words, UNIX is designed to terminal work. To start working, a person must "log in" to the system by entering his account name (account name) and, possibly, a password (password) from a free terminal. A person who is registered in the system's account files, and therefore has an account name, is called a registered user of the system. The registration of new users is usually performed by the system administrator. The user cannot change his account name, but he can set and/or change his password. Passwords are stored in separate file in coded form. Do not forget your password, even the administrator will not help you to find it again!

All UNIX users work with files either explicitly or implicitly. The UNIX OS file system has a tree structure. The intermediate nodes of the tree are directories with links to other directories or files, and the leaves of the tree correspond to files or empty directories. Each registered user is associated with a directory in the file system called the user's "home" directory. When logged in, the user has unrestricted access to their home directory and all directories and files contained therein. The user can create, delete and modify directories and files contained in the home directory. Access to all other files is potentially possible, but it can be restricted if the user does not have sufficient privileges.

File system

The concept of a file is one of the most important for the UNIX operating system. All files that users can manipulate are located in the file system, which is a tree, the intermediate nodes of which correspond to directories, and the leaves correspond to files and empty directories. An example UNIX file system structure is shown in Figure 2.1. Really on everyone logical drive(section of the physical disk package) is a separate hierarchy of directories and files. To obtain a common tree in dynamics, "mounting" individual hierarchies to a fixed root file system is used.

Comment: In the world of the UNIX operating system, for historical reasons, the term "file system" is overloaded, denoting both the hierarchy of directories and files and the part of the kernel that manages directories and files. Apparently, it would be more correct to call the hierarchy of directories and files an archive of files, and use the term "file system" only in the second sense. However, following the tradition of the UNIX operating system, we will use this term in two senses, distinguishing the meanings by context.

Each directory and file in the file system has a unique full name(on UNIX this name is commonly called full pathname, because it really specifies the full path from the root of the file system through the directory chain to the corresponding directory or file; I will use the term "full name", since there is no euphonious for pathname Russian equivalent). The directory that is the root of the file system (root directory) in any file system has the predefined name "/" (slash). A fully qualified filename such as /bin/sh means that the root directory should contain the bin directory name and the bin directory should contain the sh filename. A short or relative filename (relative pathname) is a name (possibly compound) that specifies the path to the file from the current working directory (there is a command and a corresponding system call that allows you to set the current working directory).

Each directory contains two special names, the name ".", which names the directory itself, and the name "..", which names the directory's "parent" directory, i.e. the directory immediately preceding the given one in the directory hierarchy.

Rice. 2.1. File system directory structure

UNIX supports numerous utilities that allow you to work with the file system and are available as shell commands. Here are some of them (the most common ones):

File system structure

The file system is usually located on disks or other devices external memory having a block structure. In addition to blocks that save directories and files, several more service areas are supported in external memory.

In the UNIX world, there are several different kinds of file systems with their own external memory structure. The most famous traditional file UNIX system System V (s5) and UNIX BSD family file system (ufs). The s5 file system consists of four sections (Figure 2.2,a). In the ufs file system, a logical disk (partition of a real disk) contains a sequence of file system sections (Figure 2.2,b).

Rice. 2.2. External memory structure of s5 and ufs file systems

Let us briefly describe the essence and purpose of each area of ​​the disk.

    The boot block contains a spinup program that is used to initially start the UNIX operating system. In s5 file systems, only the boot block of the root file system is actually used. In secondary file systems, this area is present but not used.

    The superblock is the most important area of ​​the file system, containing information that is necessary to work with the file system as a whole. The superblock contains a list of free blocks and free i-nodes (information nodes). In ufs filesystems, multiple copies of the superblock are maintained to improve resiliency (as shown in Figure 2.2b, one copy per cylinder group). Each superblock copy is 8196 bytes in size, and only one superblock copy is used when mounting the file system (see below). However, if the mount determines that the primary copy of the superblock is corrupted or does not meet the information integrity criteria, the backup copy is used.

    The cylinder group block contains the number of i-nodes specified in the list of i-nodes for that cylinder group and the number of data blocks that are associated with those i-nodes. The block size of a cylinder group depends on the size of the file system. To improve efficiency, the ufs file system tries to place i-nodes and data blocks in the same cylinder group.

    The list of i-nodes (ilist) contains the list of i-nodes corresponding to the files of the given file system. Maximum number files that can be created in the file system is determined by the number of available i-nodes. The i-node stores information describing the file: file access modes, creation and last modification time, user ID and group ID of the file creator, description of the block structure of the file, etc.

    Data blocks - this part of the file system stores the real data of the files. In the case of the ufs file system, all data blocks of one file are trying to fit into the same cylinder group. The data block size is determined when the file system is formatted with the mkfs command and can be set to 512, 1024, 2048, 4096, or 8192 bytes.

Mountable file systems

Files of any file system become available only after that file system is "mounted". Files on an "unmounted" filesystem are not visible to the operating system.

The mount system call is used to mount a file system. Mounting a file system means the following. The tree of directories and files available at the time of mounting must have a leaf node - an empty directory (in UNIX terminology, such a directory used to mount the file system is called directory mount point - mount point). Every file system has a root directory. During the execution of the mount system call, the root directory of the file system being mounted is combined with the directory - the mount point, resulting in a new hierarchy with full names of directories and files.

A mounted file system can later be detached from the shared hierarchy using the umount system call. Successful execution of this system call requires that the file system being detached is not currently in use (that is, no file from this file system has been opened). The root filesystem is always mounted and is not subject to the umount system call.

As I noted above, a separate file system is usually located on a logical drive, i.e. on a physical disk partition. No special system calls are supported to initialize the file system. A new file system is formed on a formatted disk using the mkfs utility (command). The newly created file system is initialized to the state of having only one empty root directory. The mkfs command performs initialization by directly writing the appropriate data to disk.

File system interface

The UNIX kernel supports several system calls for working with files. Among them, the most important are open, creat, read, write, lseek, and close.

It is important to note that although within the file management subsystem a regular file is represented as a set of external memory blocks, it is provided to users that the file is represented as a linear sequence of bytes. This representation allows you to use file abstraction when working in external devices, when organizing interprocess interactions, etc.

A file in system calls that provide real access to data is identified by its descriptor (integer value). The file descriptor is issued by the open (open file) and creat (create file) system calls. The main parameter of file open and create operations is the full or relative file name. In addition, when opening a file, the opening mode is also indicated (read only, write only, write and read, etc.) and a characteristic that determines the possibilities of accessing the file:

open(pathname, oflag [,mode])

One of the flags that can participate in the oflag parameter is the O_CREAT flag, the presence of which indicates the need to create a file if, during the open system call, the file with given name does not exist (the mode parameter is only meaningful if this flag is present). However, for historical reasons and to ensure compatibility with previous versions UNIX OS separately supports the creat system call, which performs almost the same functions.

An open file can be used to read and write sequences of bytes. Two system calls are supported for this:

read(fd, buffer, count) and write(fd, buffer, count)

Here, fd is a file descriptor (obtained from an earlier open or creat system call), buffer is a character array pointer, and count is the number of bytes to be read from or written to the file. The value of the read or write function is an integer that is equal to the value of count if the operation succeeds, zero if the end of file is reached, and negative if errors occur.

Every open file has a current position. Immediately after opening the file is positioned on the first byte. In other words, if a read (or write) system call is made immediately after opening a file, then the first count bytes of the file's contents will be read (or written) (of course, they will only be successfully read if the file actually contains at least count bytes). ). After the read (or write) system call is executed, the file's read/write pointer will be set to count+1, and so on.

Such a purely sequential style of work turns out to be sufficient in many cases, but it is often necessary to read or modify a file from an arbitrary position (for example, how can you store directly indexed arrays of data in a file without such an opportunity?). To explicitly position a file, use the system call

lseek(fd, offset, origin)

As before, here fd is a file descriptor of a previously opened file. The offset parameter specifies the relative offset of the read/write pointer, and the origin parameter specifies from which position the offset should be applied. There are three possible values ​​for the origin parameter. A value of 0 indicates that the offset value should be treated as an offset from the beginning of the file. A value of 1 means that the offset value is an offset from the current position of the file. Finally, a value of 2 indicates that the offset is relative to the end of the file. Note that the data type of the offset parameter is long int. This means that, firstly, sufficiently long offsets can be specified and, secondly, offsets can be positive and negative.

For example, after executing the system call

the read/write pointer of the corresponding file will be set to the beginning (first byte) of the file. System call

UNIX standard Filesystem ... commands. Figure 1.2. Structure OS directories linux main directory file systems OS linux is the root directory...

  • Operating room installation systems linux

    Abstract >> Informatics

    Powerful and flexible control methods file systems linux or linux-like operating systems. In many... one tree structure directories, and this structure may span multiple sections. When file system mounted...

  • Top Related Articles