The use of files and storage devices in Linux differs from that of Windows. Although there are also files and hierarchical directory structures, you need to create a different mindset.
Listing 1. Directory structure
/|--bin|--boot|--dev|--etc|--mnt|--opt| | --ibm| | | --webspherestudio| | '--db2| | --ibmhttpserver|--root|--sbin|--tmp|--usr| | --x11r6| | | --bin| | | --include| | | --lib| | | --man| | '--share| | --bin| | --Dict| | --doc| | --etc| | --include| | --Lib| | --libexec| | --local| | | --openoffice| | | | --Sbin
No drive letters!
There are no drive letters in Linux, which is really quite useful. If you have ever used a Windows system in a complex network environment, and the machine running the system has several devices, you may find that the letters in the alphabet are not enough. In Linux, there is only one file structure. It starts with root (/), all local file systems, all local devices, and all remote file systems are represented as subdirectories in this structure.
When Linux is booted for the first time, it constructs the file structure based on the information in the/etc/fstab file. Windows assigns drive letters to hard drive partitions and other storage devices, and Linux assigns them to directories in the root file structure. This layered structure is fully configurable and can be modified dynamically.
Back to top of page
Loading!
Add a device to the file system, which is referred to as loading . Linux will automatically mount A/(root) file system. There may also be a separate/boot file system, which holds the core kernel boot file. Linux will also load some special file systems. The swap partition is not represented as part of the file system, but the kernel handles it. However, other special file systems, such as Proc, are seen as a regular part of the filesystem and can be processed like normal files.
What is/proc?
The/proc file system is an excellent example of how Windows thought differs from Linux thinking. /proc stores a virtual description of the various aspects of a running system. There is a lot of information about IRQ settings, memory usage, loaded device drivers, network status, and so on. There is even a file called/proc/kcore, which is a virtual description of all the system memory used. Each of these files can be parsed like normal or binary files. You can write some files to change the behavior of the running kernel without rebooting. For example, to turn on IP forwarding for the first Ethernet device enabled in the system, you can use a file command:
Echo 1 >/proc/sys/net/ipv4/conf/eth0/forwarding
The main benefit of this system is that you only need to use simple scripting techniques to perform deep and efficient operations on your running system.
Other file systems, such as removable media or remote file systems, need to be loaded manually. When loading a file system, you need to know the correct way to reference it in Linux, and you need an empty directory as the mount point . For removable media, Linux may have created mount points for you at the time of installation. In Red Hat Linux, the CDROM device is set to mount to the/mnt/cdrom directory. In other words, when you put a CD into the CDROM device, enter the command:
mount /mnt/cdrom
The CD will be added to the file system, and the CDROM device will be locked so that it will not be ejected unexpectedly. You can access the contents of the CD only by going to the/mnt/cdrom directory. When you are no longer using a CD, you can remove it from the file system with the following command:
umount /mnt/cdrom
The/mnt/cdrom directory will become empty and the lock on the CDROM device is removed. You can now safely eject the CD. This is true for other removable media, such as a floppy disk drive (/mnt/floppy).
Running a command without parameters mount
displays the currently mounted file system.
Why do we need locks?
Don't forget that Linux is not only multi-user, but also multi-session. This means that several users can log on to the system at the same time, run programs, and use resources. This is different from using shared files after you log on in Windows. Every user can use the system as if they were sitting in front of the console. To remain stable, Linux does not allow arbitrary release of the file system currently in use, and by locking the CD, the CD will not be ejected until no one is using it.
Back to top of page
/etc/fstab file
The association between the device and its mount point is configured in/etc/fstab. This file can be modified directly, or it can be maintained by a management tool. The following is an example of a/etc/fstab:
Understanding/etc/fstab
/dev /hda5 |
|
ext3 |
defaults |
1 1 |
Td>/dev/hda2
/boot |
ext3 |
exec,dev,duid,rw |
1 2 |
/d Ev/hda6 |
swap |
swap |
defaults |
0 0 |
/dev/scd0 |
/mnt/cdrom |
Auto |
ro,noauto,exec |
0 0 |
None |
/dev/pts |
devpts |
id=5,mode=620 |
0 0 |
None |
/proc |
proc |
defaults |
0 0 |
None |
/dev/shm |
tmpfs |
defaults |
0 0 |
Each row represents a file system to be mounted. The first column indicates the device to be loaded. The second column is the mount point, which is the location of the device in the file system. The third column indicates the type of file system. The fourth column is the option to process the file system. The last column is the file system's flag bit. The first number is 1 or 0, specifying whether the system should be replicated with dump (an option for system backup). The second number is 0, 1, or 2, specifying the order in which the file system is checked at boot time. 0 means no check at all. 1 indicates that the root (/) file system needs to be specified as 1 for the first check. The other file system should be 2.
In the Fstab file listed above, the root file system is located in the fifth partition of the first IDE hard drive, which is the first logical drive of the extended partition. The/boot file system is located in the second primary partition of the first IDE hard drive, which holds the kernel boot files. The swap partition is located on the sixth partition of the first IDE hard drive, which is the second logical drive of the extended partition. The corresponding device for other file systems in the list is "none". We will soon be explaining the problem. Now let's focus on the physical disk.
Everything is a file.
In Linux, the file system is represented by a file-like name. All files in the/dev directory are special files called nodes that are linked to the physical device through the device driver. This allows you to do some interesting things. For example, to make an ISO image of a CD, you can use the cp
(copy) command:
Cp/dev/cdrecorder Mycd.iso
This replicates the file structure of a binary image instead of a CD.
The file-centric approach also allows you to specify a meaningful alias for the device name. For example, there is usually an alias called/dev/cdrom, which points to the physical CDROM device, which is usually/dev/hdc. Once an alias is created, you can access that device through/dev/cdrom, which is better remembered. Alias technology also allows you to standardize your scripts so that they can be used on systems that are physically configured differently.
The options in the fourth column will vary depending on the file system type. In the example above, the Mount option for/and/boot is "default". That is, they use asynchronous I/O to automatically mount in a read-write manner. Only root can load or unloading the device, but the user can execute the binaries and use the "sticky Bit" (described later). The file system will be processed as a block character device. However, for/mnt/cdrom, the options are different. It is not automatically loaded and will be loaded as a read-only file system. Users will be able to execute scripts and programs in the file system.
Back to top of page
Add File System
Add a new line to the/etc/fstab file, and you can add the file system to the/etc/fstab. As a practical example, I have a RAID device that stores the file resources used by the department. This device only has data files and is kept separate from the operating system so that it can be transferred to another system in the event of a hardware failure. RAID is configured to be recognized as/DEV/SDC in Linux, which is a third SCSI device. The ext3 file system with the log is created on the first partition so that we can access it via/DEV/SDC1. I want to automatically mount this RAID to the file system when the computer boots.
I added the following line in the/etc/fstab:
/dev/sdc1 /data ext3 defaults 0 0
This way, the RAID will be loaded as the/boot system. Now I just need to create another directory as the specified mount point:
mkdir /data
Once this empty directory is created, we can mount the file system to it:
mount /data
RAID is now associated to the/data. If the system is rebooted, the/data will be loaded automatically.
Back to top of page
Partition
Partitioning in Linux is essentially the same as in Windows. The console command fdisk
allows you to create and manage partitions. When you use fdisk
it, you must indicate which device it is operating on. You can use commands fdisk -l
to view available devices.
Listing 2. Using Fdisk
[Email protected] root]# fdisk-ldisk/dev/hda:240 heads, sectors, 7752 cylindersunits = cylinders of 15120 * byt Es Device Boot Start End Blocks Id system/dev/hda1 1 8 60448+ 8e Linux lvm/dev/hda2 9 52920 linux/dev/hda3 * 1403 10493280 C Win95 FAT32 (LBA)/dev/hda4 1404 7751 47990880 f Win95 Ext ' d (LBA)/ Dev/hda5 1404 5565 31464688+ , linux/dev/hda6 5566 5635 529168+ swap/dev/hda7 Linux 5636 7751 15996928+ b Win95 FAT32
The above list comes from a laptop, so it shows a somewhat different structure than the server. It shows an IDE hard drive with several partitions. If there are other devices, they will also be listed. For example, a second IDE hard drive may appear as/DEV/HDB.
Specify a device to run again fdisk
, and you'll get a brief hint.
Listing 3. Run Fdisk on a device
[[email protected] root]# fdisk/dev/hdathe number of cylinders for this disk is set to 7752.There are nothing wrong with t Hat, but this is larger than 1024,and could in certain setups cause problems with:1) software The runs at boot time (e.g. , old versions of LILO) 2) booting and partitioning software from the other OSs (e.g., DOS fdisk, OS/2 fdisk) Command (M for Help):
Enter "M" To view the Command menu. You can use "P" to display the current partition table. You can create, delete, and modify the types of existing partitions. "L" lists a full list of available partition types for you. Use "W" to write your changes to the partition table and exit the program, or use "Q" to exit without saving the changes. Some changes will take effect immediately. Some modifications require a system reboot to take effect.
The partitioning rules under Linux are the same as in Windows. You can use 4 primary partitions, each of which can be an extended partition.
Back to top of page
File system type
Linux can handle any type of file system that the kernel can recognize. Quite a few types are compiled by default into the kernel, and new file systems can be added. Here are some of the important file system types:
- ext2: Standard Linux file system
- ext3: Standard Linux file system with logs
- vfat: Microsoft's Fat32 file system
- JFS: IBM's log file system
- ReiserFS: Another popular log file system
Log saves time, preserves data
The log file system helps protect data when it is not properly shut down. If a volume is closed without unloading, it may also leave unfinished work and files in the in-between state. In a typical file system, this volume needs to be thoroughly checked, which takes a long time for larger volumes. The log file system maintains a period of time (such as 5 seconds) of transaction records for each write operation of the disk. When the volume is not fully unloading, the file system only needs to roll back to the last known good state. It takes 20 minutes to recover a volume and now only takes a few seconds!
Back to top of page
formatting partitions
After the partition is created, it is formatted with the appropriate version of the mkfs
command. The file system will have its own version mkfs
, for example, mkfs.ext2
or mkfs.ext3
. These helper scripts allow you to create a file system just by specifying a partition. Here are some examples:
Listing 4. Using MKFS
# Create an ext2 file system in the third # Parition of the first IDE hard Drivemkfs.ext2/dev/hda3 # Create an ext3 file System on the first # partition of the 2nd SCSI hard DRIVEMKFS.EXT2MKFS.EXT3/DEV/SDB1 # Create a JFS file system in an ex Tended # partition on the first IDE hard Drive.mkfs.jfs/dev/hda5
There are some advanced parameters that affect how partitions are formatted, but for normal use, the default parameters are fine. Once the partition has been formatted, it can be loaded into the/file system. A file system must be unloading before it can be reformatted.
Back to top of page
Other file System Tools
Let's take a look at other useful tools.
Console Tools
There are several tools to view the status of disks and file systems.
Df
df
Represents "Disk Free". It reports the size of the used and available disk space on the mounted file system. Useful switches:
Check disk space
df -h |
human-readable format; Displays the size of the file in readable K, M, G, and not in bytes |
df -l |
Lists only the local file system; By default, the remote file system is also listed |
Du
du
Represents "Disk usage". It reports the size of disk space used by specific files and each subdirectory (the directory specified in the parameter). Useful switches:
Check disk usage
du -a |
Lists the space occupied by all files, not just the directory |
du -h |
human-readable format; Displays the size of the file in readable K, M, G, and not in bytes |
du -c |
Outputs a total after all parameters have been processed and can be used to get the total disk space for a given set of files or directories |
du -s |
Show only the sum of the file sizes specified by each parameter |
Fsck
This program is used to check and repair the file system, equivalent to the one in Windows chkdsk
. As is the case mkfs
, it has different versions for different file system types. fsck
it must be run on a volume that has been unloading, although it is seldom used unless the file system is completely unloading. man fsck
and info fsck
, in the final references to this article, provide detailed information.
Linux file system