📜 ⬆️ ⬇️

Linux basics from the founder of Gentoo. Part 4 (1/4): File Systems, Partitions, and Block Devices

The first excerpt of the fourth part of the beginner's guide series tells about block devices, partitions, and file systems. You will learn how to partition a hard disk using the fdisk utility, create file systems and mount them. Get to know the syntax of the fstab configuration file.



Navigating Linux basics from the founder of Gentoo:

Part I: 1 , 2 , 3 , 4

Part II: 1 , 2 , 3 , 4 , 5

Part III: 1 , 2 , 3 , 4

Part IV
  1. File Systems, Partitions, and Block Devices (Intro)
  2. System Boot and Boot Levels
  3. File System Quotas
  4. System logs (totals and links)



Foreword


About this guide


Welcome to System Administration, the last of the four parts of the tutorial for preparing for the “101 Linux Professional Institute's” exam. In this part, you will learn about Linux administration skills such as file systems, the boot process, launch levels, file quotas, and system logs (logs).



This guide is especially useful for those who want to try themselves as a system administrator for the first time, as there are many basic issues that system administrators need to know. If you are new to Linux, we recommend that you start learning from Part 1 . For some, much of this material will be new, but even more experienced Linux users can find something new in this guide that can be a great way to update their knowledge of Linux system administration and prepare for the next level of LPI certification.


')

By the end of this series of tutorials (there are eight of them for LPI 101 and 102 exams), you will have the knowledge necessary to become a Linux system administrator and will be ready to attain the first level of LPIC certification from the “Linux Professional Institute” if you wish.



File systems, partitions and block devices


Introduction to block devices


In this section, we will discuss aspects of working with disks on Linux, including file systems, partitions, and block devices. As soon as you are familiar with the advantages and disadvantages of disks and file systems, we will analyze the process of setting up partitions and file systems on Linux.



First, let's get acquainted with the "block devices". The most well-known block device is likely to be the first IDE disk on a Linux system, which will be called: /dev/ h da



If you have SCSI disks on your system (or, more likely, you are using the modern libATA driver - ed.), Then it will be called: /dev/ s da



Abstraction levels


Block devices represent an abstract interface to a disk. User programs can use these block devices to interact with the disk without worrying about what kind of disks you have: IDE, SCSI, or some other. Programs can easily address disk space as a sequence of blocks of 512 bytes with random access.



Sections


In Linux, file systems (FS) are created using the special mkfs command (or mke2fs , mkreiserfs , etc.), specifying a specific block device as an argument.



However, although it is possible to use block devices representing the entire disk, such as /dev/hda or /dev/sda , for a single file system, this is rarely used in practice. Instead, disk block devices are divided into more convenient block devices of a smaller size, called partitions. Partitions are created using a tool called fdisk , which is used to create and edit partition tables located on each disk. The partition table determines exactly how space is divided on the whole disk.



Introduction to fdisk


We can take a look at the disk partition table by running fdisk , specifying as an argument the block device representing the entire disk.



Note:

Alternative means to access the partition table: cfdisk , parted and partimage . I recommend that you avoid using cfdisk (despite what may be said in the fdisk manual) since it sometimes incorrectly calculates the disk geometry.



# fdisk /dev/hda
# fdisk /dev/sda

Important!

Do not save or make any changes to the disk table of partitions if one of them contains a file system that is currently being used or that stores important data. These actions are likely to result in data loss on the disk.



Inside fdisk


After running fdisk , you will be greeted by an invitation that looks something like this:



  Command (m for help): 

Enter p to display the current partition table of your disk:



  Command (m for help): p

 Disk / dev / hda: 240 heads, 63 sectors, 2184 cylinders
 Units = cylinders of 15120 * 512 bytes

    Device Boot Start End Blocks Id System
 / dev / hda1 1 14 105808+ 83 Linux
 / dev / hda2 15 49 264600 82 Linux swap
 / dev / hda3 50 70 158760 83 Linux
 / dev / hda4 71 2184 15981840 5 Extended
 / dev / hda5 71 209 1050808+ 83 Linux
 / dev / hda6 210 348 1050808+ 83 Linux
 / dev / hda7 349 626 2101648+ 83 Linux
 / dev / hda8 627 904 2101648+ 83 Linux
 / dev / hda9 905 2184 9676768+ 83 Linux

 Command (m for help): 

This disk is configured to host seven Linux file systems (each on a corresponding partition labeled as “Linux”), as well as a swap partition (labeled as “Linux swap”).



Overview of block devices and sections


Note the names of the block devices on the left, corresponding to the section, starting with /dev/hda1 to /dev/hda9 . At the beginning of the PC era, markup programs allowed for a maximum of four sections (called primary). This was too little, and an extended section was invented to circumvent this limitation. The extended partition is very similar to the primary one and counts in the limit for 4 primary partitions. But an extended section can contain any number of so-called. logical partitions within themselves, effectively bypassing the four partition restriction.



Disk partitioning


All sections from hda5 and further are logical sections. Numbers hda1 to hda4 reserved for primary or extended partitions.



In our example, sections c hda1 through hda3 are primary partitions. hda4 is an extended partition that contains logical partitions from hda5 to hda9 . You will not use /dev/hda4 to store the file system - it just acts as a container for the hda5 - hda9 .



Types of sections


Also note that each section has an " Id ", also called a section type. Whenever you create a new partition, you must ensure that the partition type is set correctly. The value 83 is valid for Linux FS partitions, and 82 for podkachki sections. To set the type value, use the “t” option in fdisk . The Linux kernel uses partition type settings for autodetection on disk during file system loading and paging.



Using fdisk to create partitions


Now that you have an understanding of the disk partitions in Linux, it's time to start the process of creating partitions on the disk and filesystem for installing Linux. We will set up partitions on the disk, and then create file systems on them. At this stage, we will completely clear the disk of the data, and will use it to install a new copy of the Linux system.



Important!

To perform these actions, you must have a hard disk that does not contain any important information, since, at this stage, the data on the disk will be deleted . If this is all new to you, you can only read these steps, or use the Linux boot disk on a test system (for example, in a virtual machine - ed.), So the data will not be in danger.



What the disk will look like after the breakdown


After we go through the process of creating partitions on your disk, your partition table will look something like this:



  Disk / dev / hda: 30.0 GB, 30005821440 bytes
 240 heads, 63 sectors / track, 3876 cylinders
 Units = cylinders of 15120 * 512 = 7741440 bytes

    Device Boot Start End Blocks Id System
 / dev / hda1 * 1 14 105808+ 83 Linux
 / dev / hda2 15 81 506520 82 Linux swap
 / dev / hda3 82 3876 28690200 83 Linux

 Command (m for help): 

Comment on an example


In the new configuration we proposed, we have three sections. The first is the small partition ( /dev/hda1 ) at the beginning of the disk, called the boot partition. The purpose of the boot partition is to store all the important boot-related data — the GRUB boot loader, as well as your Linux kernel (s). The boot partition provides us with a safe place to store any information related to the boot. During normal operation, the boot partition must remain unmounted for security. If you have SCSI disks (or the modern libATA library is used - approx. Ed.), The boot partition will most likely be named /dev/sda1 .



It was recommended to keep the boot partition (containing everything you need to boot) at the beginning of the disk. This is not necessary, since it takes its origins from the past, when the LILO bootloader could not load the kernel from file systems that were located behind the 1024 cylinder of the disk.



The second partition ( /dev/hda2 ) is used for swap. The kernel uses disk swap space as virtual memory when there is not enough space in RAM. The size of the partition is relatively not very large, usually around 512 MB. For SCSI systems (as well as with the new libATA - ed.) This section will be called /dev/sda2 .



The third partition ( /dev/hda3 ) is large and occupies the rest of the disk. This partition will be our root partition, and will serve to store the main Linux file system. For SCSI disks (or new libATA - approx. Ed.) This section will be called /dev/sda3 .



Beginning of work


Now, to create partitions following the example above, enter fdisk /dev/hda or fdisk /dev/sda , depending on whether you use IDE or SCSI disks (or modern libATA disks - ed.), Respectively. Then enter “p” to view the current partition table. Is there anything on the disc that needs to be saved? If yes, stop now. If you continue, all existing information on the disk will be destroyed.



Important!

The following instructions will destroy all existing data on the disk! If there is any data on the disc, make sure that the information is not critical to you. Also make sure that you have chosen the correct disk in order not to mistakenly erase data from another disk.



Deleting existing partitions


Now it's time to delete all existing partitions. To do this, type “d” and press Enter. You will be prompted to select the partition number to be deleted. To delete an existing partition /dev/hda1 you must enter:



Command (m for help): d

Partition number (1-4): 1


A partition will be scheduled for deletion. It will no longer be displayed if you enter “p”, but it will not be deleted until you save your changes. If you make a mistake and want to undo actions, type “q” and press Enter, and your section will not be deleted.



Now, assuming that you really want to delete all the partitions on your system, type “p” to list the partitions again, and then type “d” and the partition number to delete. As a result, you will get an empty partition table:



  Disk / dev / hda: 30.0 GB, 30005821440 bytes
 240 heads, 63 sectors / track, 3876 cylinders
 Units = cylinders of 15120 * 512 = 7741440 bytes

 Device Boot Start End Blocks Id System

 Command (m for help): 

Creating a boot partition


Now that the partition table in the memory is empty, we are ready to create the boot partition. To do this, enter “n” to create a new partition, then enter “p” to tell fdisk that you want the primary partition. After that, enter “1” to create the first primary partition. When asked about the first cylinder, press Enter. When asked about the last cylinder, enter “+ 100M” to create a partition of 100 MB in size. Conclusion of the actions performed:



Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1

First cylinder (1-3876, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-3876, default 3876): +100M


Now enter “p”, you should see the following partition table:



Command (m for help): p

Disk /dev/hda: 30.0 GB, 30005821440 bytes
240 heads, 63 sectors/track, 3876 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes

Device Boot Start End Blocks Id System
/dev/hda1 1 14 105808+ 83 Linux


Creating a swap partition


Now, let's create a swap partition. To do this, enter “n” to create a new partition, then “p” to tell fdisk that you want to create a primary partition. Then enter “2” to create the second primary partition, /dev/hda2 in our example. Then you will be prompted to enter the number of the first cylinder, press Enter, when prompted to enter the number of the last cylinder, enter “+ 512M” to create a swap partition of 512 MB. After you do this, enter “t” to set the partition type, and then enter “82” to set the type of “Linux swap”. After completing these steps, enter “p” to view the partition table, it should look like this:



Command (m for help): p

Disk /dev/hda: 30.0 GB, 30005821440 bytes
240 heads, 63 sectors/track, 3876 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes

Device Boot Start End Blocks Id System
/dev/hda1 1 14 105808+ 83 Linux
/dev/hda2 15 81 506520 82 Linux swap


Making it bootable


Finally, we need to set the “bootable” flag on our boot partition and write the changes to the disk. To mark the /dev/hda1 partition as a “boot” partition, enter “a” in the menu and then “1” as the partition number. If you enter “p” now, you will see that /dev/hda1 contains the character “*” in the Boot column. Now let's write our changes to disk. To do this, type “w” and then Enter. Your disk partitions are now properly configured to install Linux.



Comment:

If fdisk prompts you to reboot, please do so in order for your system to determine the new partition configuration.



Extended and logical partitions


In the example above, we created one primary partition that will contain filesystems for storing all our data. This means that after installing Linux, the main file system will be mounted in “/” and will contain a directory tree that contains all of our files.



Although this is a general approach, there is another approach with which you should also be familiar. This approach uses several sections, as a place for several filesystems, and which together form a file system tree. For example, it is quite common to put /home and /var into separate filesystems.



We could create hda2 as an extended, not as a primary partition. Then we would create logical partitions hda5 , hda6 , hda7 (technically they would be located inside hda2 ), which would contain filesystems for /, /home and /var respectively.



You can learn more about these types of multisystem configuration by reviewing the resources below.



Creating file systems


Now that the partitions are already created, it is time to install the filesystem on the boot and root partitions so that they can be used to store data. We will also set up a swap partition to maintain the swap data.



Linux supports various types of filesystems; Each of them has its advantages and disadvantages and its characteristics. We will look at creating ext2, ext3, XFS, JFS and ReiserFS file systems in this guide. Before creating a file system using our example, we will briefly review the various file systems available in Linux.



Note Ed .: Unfortunately, due to the fact that this cycle was written by the authors around 2003, it doesn’t mention many modern and actual file systems that have appeared since, such as, for example, ext4 and btrfs. But, I believe that a curious reader will be able to easily find information about them elsewhere.

Ext2 file system


ext2 is a Linux-proven file system, but it does not have metadata journaling tools, which means that the time it takes to check the file system during startup can be quite long. Currently, there is a wide range of journaling file systems that can be checked for integrity very quickly, and therefore preferable to their non-journaling counterparts. A journaling file system allows you to avoid long delays at system startup, when the integrity of your file system is violated (for example, in the event of a power failure, approx. Red.).



Ext3 file system


ext3 is the journaling version of the ext2 filesystem, which provides metadata journaling for quick recovery, as well as other journaling modes, such as full journaling of all data and ordered journaling. ext3 - very good and reliable FS. It offers decent performance in most cases. Since it uses few “trees” in its internal structure, it doesn’t scale well, which means that this type of file system is not the best choice for very large file systems, or in conditions where you will process large files or a large number of files in one directory. But when used in the conditions for which it was designed, ext3 is an excellent file system.



One of the nice features of ext3 is that existing ext2 systems can be upgraded “on site” to ext3 quite simple. This allows you to smoothly update existing Linux systems that already use ext2.



ReiserFS file system


ReiserFS is a B-tree based file system that has very good performance and is far superior to ext2 and ext3 when working with small files (files less than 4 kB), often 10-15 times. ReiserFS also scales well and has metadata journaling. Starting from kernel 2.4.18 and higher, ReiserFS is stable and recommended, both as a general purpose file system and in extreme cases, such as creating large file systems, using for many small files, for huge files, and also for directories with tens of thousands of files. We recommend the ReiserFS FS to be used by default for all non-bootable partitions.



Xfs file system


XFS is a file system with metadata journaling. It has a specific set of features and is optimized for scaling. We recommend using this file system exclusively on Linux systems with high-end SCSI and / or Fiber Channel drives and uninterruptible power supplies. Since XFS aggressively caches data in RAM, an inappropriately designed program (that is, one that does not take due care when writing to disk (there are very few)) can lose a decent amount of data if the system unexpectedly crashes.



JFS


JFS IBM . , , .




, ext3. – ReiserFS; ext3 ReiserFS , .



, :



# mke2fs -j /dev/hda1
# mkswap /dev/hda2
# mkreiserfs /dev/hda3


ext3 /dev/hda1 , . mkswap /dev/hda2 – . /dev/hda3 ReiserFS, . .




mkswap – :



# mkswap /dev/hda2

, . , swapon :



# swapon /dev/hdc6

Linux . , swapon, , , . , cat /proc/swaps .



ext2, ext3, ReiserFS


ext2 mke2fs :



# mke2fs /dev/hda1

ext3, mke2fs -j :



# mke2fs -j /dev/hda3

ReiserFS mkreiserfs :



# mkreiserfs /dev/hda3

XFS JFS


XFS mkfs.xfs :



# mkfs.xfs /dev/hda3

Note:

mkfs.xfs : “-d agcount=3 -l size=32m”. “-d agcount=3” . XFS 4 , , , 20, “agcount=5”. “-l size=32m” 32 , .



Note : . , , , 6 , (allocation group) XFS .

JFS, mkfs.jfs :



# mkfs.jfs /dev/hda3


, , mount :



# mount /dev/hda3 /mnt

, , « » – . «» . /mnt . , . , /mnt ReiserFS, .



, /mnt . , :



# mkdir /mnt/boot
# mount /dev/hda1 /mnt/boot


, /mnt/boot . /mnt/boot , ext3 , /dev/hda1 . /mnt , /mnt/boot, ReiserFS /dev/hda3 . /mnt , ( ), Linux .



, mount . mount Linux , , :



 /dev/root on / type reiserfs (rw,noatime)
none on /dev type devfs (rw)
proc on /proc type proc (rw)
tmpfs on /dev/shm type tmpfs (rw)
usbdevfs on /proc/bus/usb type usbdevfs (rw)
/dev/hde1 on /boot type ext3 (rw,noatime) 

cat /proc/mounts . /dev/hda3 /dev/hda3. /dev/hda3 , /dev/root , , :



# ls -l /dev/root
lr-xr-xr-x 1 root root 33 Mar 26 20:39 /dev/root -> ide/host0/bus0/target0/lun0/part3

# ls -l /dev/hda3
lr-xr-xr-x 1 root root 33 Mar 26 20:39 /dev/hde3 -> ide/host0/bus0/target0/lun0/part3



, /dev/ide/host0… ? , , devfs /dev, , Linux . , /dev/ide/host0/bus1/target0/lun0/part7 /dev/hdc7 , /dev/hdc7 – () . devfs, /dev/.devfsd ; – devfs .



mount , . , -t , :



# mount /dev/hda1 /mnt/boot -t ext3

or


# mount /dev/hda3 /mnt -t reiserfs


. , « » “ro”:



# mount /dev/hdc6 /mnt -o ro

/dev/hdc6 , /mnt – . «/» « », remount :



# mount /mnt -o remount,ro

, , . . mount /mnt /dev/hdc6 . , «/»:



# mount /mnt -o remount,rw

, , /mnt - . mount Linux, man mount .



fstab


. , Linux, , ? , Gentoo Linux . , /dev/hda3 ? - – , , – , ?



, Linux , , Linux . , Linux , /etc/fstab . .



fstab


/etc/fstab :



 <fs> <mountpoint> <type> <opts> <dump/pass>

/dev/hda1 /boot ext3 noauto,noatime 1 1
/dev/hda3 / reiserfs noatime 0 0
/dev/hda2 none swap sw 0 0
/dev/cdrom /mnt/cdrom iso9660 noauto,ro,user 0 0
# /proc should always be enabled
 proc / proc proc defaults 0 0 

/etc/fstab , , , , . , dump . , dump , . fsck , . fsck .



/dev/hda1 ; , /dev/hda1 ext3, /boot . opts. “noauto” /dev/hda1 ; /dev/hda1 /boot .



“noatime”, atime ( ) . , atime .



/proc “defaults”. “defaults”, . . . /etc/fstab , .



/dev/hda2 /etc/fstab . /dev/hda2 . , . /etc/fstab , /dev/hda2 , .



/dev/cdrom , /etc/fstab CD-ROM . :



# mount -t iso9660 /dev/cdrom /mnt/cdrom -o ro

:



# mount /dev/cdrom

, /etc/fstab “user”. , . , CD-ROM. , root CD-ROM.




, . , .



, . , , , - . , umount , :



# umount /mnt

or


# umount /dev/hda3

, /mnt , , .



fsck


- , . , . , fsck , , /etc/fstab .



Important!

, fsck , “pass” ( ) /etc/fstab . , “1”, , . , , “pass” “2” . , ReiserFS, “0”, ( fsck ) .



, , fsck . , , , fsck , . fsck , . , “y” () , fsck .



fsck


fsck , , ( ) , . , , fsck .



, , , . . , . , . , , . Linux. Funtoo Filesystem Guide, part 1: Journaling and ReiserFS .



andrewww . To be continued...




Daniel Robbins


— Gentoo Gentoo Linux. - . Funtoo , IBM developerWorks , Intel Developer Services C/C++ Users Journal.



Chris Houser


UNIX c 1994 , (, ), . , -, , UNIX . Sentry Data Systems. , Gentoo Linux Clojure, The Joy of Clojure .



Aron Griffis


, Hewlett-Packard , UNIX- Tru64, Linux, Xen KVM , — HP ePrint . , , « ».

Source: https://habr.com/ru/post/116907/


All Articles