Detailed explanation of linux disk partition

If you want to understand linux partitions, be sure to read this article carefully. For beginners, linux partitions are not as easy to understand as under windows

In the process of learning Linux, installing Linux is the first threshold for every beginner. In the middle of this process, the biggest confusion is partitioning the hard disk. Although various Linux distributions have provided a friendly graphical interface, many people still feel that they have no idea where to start. The main reason for this is that it is not clear about the partition rules of Linux and the use of Fdisk, the most effective partition tool under it.

First of all, we need to have some preliminary understanding of the basic concepts of hard disk partitions. Hard disk partitions are mainly divided into two types: primary partitions and extension partitions. The sum of the number of basic partitions and extended partitions cannot be greater than four. . And the basic partition can be used immediately but cannot be re-partitioned. The extended partition must be partitioned before it can be used, that is to say, it must be partitioned twice. So what is divided by the extended partition? It is a logical partition (logical partition), and there is no limit to the number of logical partitions.

When learning linux installation, the notes of the linux installation partition are temporarily recorded for your own review. The virtual machine has a total of 12GB.

Partition first

The first partition: /boot fixed size, 100M, forced to be the main partition, boot program.
Second partition: / root directory 4000M
Third partition: /usr 4500M, which is equivalent to Program Files under Windows C drive, used for software installation.
The fourth partition: /var 1500M, where the log is stored, the user logs in, and reads the disk log.
Fifth partition: select the file system type swap, 1200M. Memory replacement space, virtual memory.
The sixth partition: /www 500M, for the website. Add whatever you want.

The SWAP partition should be between 1 and 1.5 of the real memory. The larger the physical memory, the smaller it can be set;

For users who are accustomed to using dos or windows, there are several drives in several partitions, and each partition is given a letter identifier, which can then be used to specify the files and directories on this partition, Their file structures are independent and very easy to understand. But for these first-time users of red hat linux, it can be a bit annoying. Because for red hat linux users, no matter how many partitions there are and which directory they are assigned to, in the final analysis, there is only one root directory, an independent and unique file structure. Each partition in red hat linux is used to form a part of the entire file system, because it adopts a processing method called "loading", and its entire file system contains a complete set of files and directories, and will A partition is associated with a directory. A partition to be loaded at this time will have its storage space available in a directory.

For Windows users, the operating system must be installed in the same partition, it is commercial software! So you have no choice! For red hat linux, you have a lot of choice. You can install the system files in several partitions (the loading point must be specified), or they can be installed in the same partition (the loading point is "/").

Let's start from these two aspects to explain this problem that troubles everyone.

Partitioning rules for Linux

  1. Device management

In Linux, every hardware device is mapped to a system file, and IDE or SCSI devices such as hard disk and CD-ROM are no exception. Linux assigns various IDE devices a file consisting of the hd prefix; and for various SCSI devices, assigns a file consisting of the sd prefix.

For IDE hard disk, the drive identifier is "hdx~", where "hd" indicates the type of the device where the partition is located, here refers to IDE hard disk. "x" is the disk number (a is the basic disk, b is the basic slave disk, c is the auxiliary master disk, d is the auxiliary slave disk), "~" represents the partition, the first four partitions are represented by numbers 1 to 4, which are Primary or extended partitions, starting from 5 are logical partitions. For example, hda3 is represented as the third primary or extended partition on the first IDE hard disk, and hdb2 is represented as the second primary or extended partition on the second IDE hard disk. For scsi hard disks, it is marked as "sdx~", scsi hard disks use "sd" to indicate the type of the device where the partition is located, and the rest are the same as IDE hard disks.

For example, the first IDE device, Linux is defined as hda; the second IDE device is defined as hdb; and so on. The SCSI device should be sda, sdb, sdc and so on.

  1. Number of partitions

To partition, it is necessary to operate for each hardware device, which may be an IDE hard disk or a SCSI hard disk. For each hard disk (IDE or SCSI) device, Linux assigns a serial number from 1 to 16, which represents the partition number on the hard disk.
  For example, the first partition of the first IDE hard disk is mapped to hda1 under Linux, and the second partition is called hda2. For SCSI hard disks it is sda1, sdb1, etc.

  1. The role of each division

It is stipulated in Linux that each hard disk device can have up to 4 main partitions (including extended partitions), and any extended partition must occupy a main partition number, that is, in a hard disk, the main partition and extended partition have a total of at most is 4.
  For early DOS and Windows (versions before Windows 2000), the system only recognizes one primary partition, and the partition can be further refined by adding a logical drive letter (logical partition) to the extended partition.

The role of the primary partition is that the computer is used to start the operating system, so the startup of each operating system, or the boot program, should be stored on the primary partition.

This is the biggest difference between primary partition and extended partition and logical partition.  

When we specify the bootloader to install and guide Linux, we must specify it on the primary partition, which is the best example.
  Linux specifies that primary partitions (or extended partitions) occupy the first 4 numbers from 1 to 16. Taking the first IDE hard disk as an example, the primary partition (or extended partition) occupies hda1, hda2, hda3, hda4, and the logical partition occupies 12 numbers such as hda5 to hda16.
  Therefore, each hard disk under Linux has a total of up to 16 partitions.
  For logical partitions, Linux dictates that they must be created on extended partitions (and on DOS and Windows systems as well), not primary partitions.
  Therefore, we can see that the extended partition can provide a more flexible partition mode, but cannot be used as a boot for the operating system. Removing the differences between the various partitions above, we can simply treat them as the same.

  1. partition indicator  

For each Linux partition, the size of the partition and the type of partition are the main indicators. The size of the capacity is easy for readers to understand, but the type of partition is not so easy to accept. The type of the partition specifies the format of the file system on the partition.
  Linux supports a variety of file system formats, including the familiar FAT32, FAT16, NTFS, HP-UX, and various Linux-specific Linux Native and Linux Swap partition types.
  In Linux systems, these different types of partitions can be distinguished by their partition type numbers. Various types of numbers will be introduced when introducing the use of Fdisk.

5 Commonly used partitions

/boot partition, it contains the kernel of the operating system and the files used in the process of starting the system. It is necessary to build this partition, because most PCs are currently restricted by bios, and if there is a separate The /boot boot partition, even if there is a problem with the main root partition, the computer can still boot. The size of this partition is about 50mb-100mb. But if you want to use lilo to start the red hat linux system, the partition containing /boot must be completely below the cylinder 1023. And because the data lilo after 8gb cannot be read, so red hat linux should be installed within the area of ​​8gb.
The /usr partition is where the software is stored in the red hat linux system. If possible, the maximum space should be allocated to it.
The /home partition is where the user's home directory is located. The size of this partition depends on how many users there are. If multiple users use a computer together, this partition is absolutely necessary, and the root user can also control the use of the computer by ordinary users, such as limiting the use of hard disks to users or user groups, and restricting which files ordinary users can access. Wait. In fact, it is necessary for a single user to establish this partition, because without this partition, you can only log in to the system as the root user, which is dangerous, because the root user has absolute right to use the system, but once you have access to the system If you make a mistake, trouble will come.
The /var/log partition is the system log recording partition. If this separate partition is set up, even if there is a problem with the system's log files, they will not affect the main partition of the operating system.
The /tmp partition is used to store temporary files. This is necessary for multi-user systems or web servers. In this way, even if a large number of temporary files are generated when the program is running, or the user performs wrong operations on the system, the rest of the file system is still safe. Because this part of the filesystem is still subject to read and write operations, it usually breaks down faster than other parts.
The /bin partition holds standard system utilities.
The /dev partition is used to store device files.
The /opt partition holds optional installed software.
The /sbin partition holds standard system management files.

Several commonly used partitions are introduced above. Generally speaking, we need a swap partition, a /boot partition, a /usr partition, a /home partition, and a /var/log partition. Of course, there are no rules, it is entirely up to you. But remember to have at least two partitions, a swap partition and a / partition.

Detailed explanation of Fdisk usage

The following introduces the use of Fdisk to consolidate the knowledge about Linux partitions learned above.
  Fdisk is the most commonly used partitioning tool in various Linux distributions. It is defined as an Expert-level partitioning tool, which makes it a little prohibitive for beginners.
  1. Fdisk parameter description
   When running Fdisk, the first thing that catches the eye is the welcome interface. Users can operate Fdisk by entering command parameters in this interface. The user can display the description of each parameter of the Fdisk command by typing "m" at the prompt. Readers can see that Fdisk has many parameters, but only a few are often used. If readers are proficient in these parameters, they can smoothly use Fdisk to partition Linux hard disks. We first briefly introduce the meaning of each parameter, and then explain several key parameters in detail.
  When users partition in Linux, the most commonly used parameters are d, l, m, n, p, q, t, w and so on.

2. Partition with Fdisk

In the Linux partitioning process, generally, the hard disk partition table information is displayed first through the p parameter, and then the future partition is determined according to the information. If you want to completely change the partition format of the hard disk, you can delete the existing hard disk partitions one by one through the d parameter.

For example d1, d2. After the deletion is completed, you can add a new partition through the n parameter. When "n" is pressed, we can see the newly added partition. Here to select the new partition type, is the primary partition or extended partition; then select p or e. Their differences have been explained above. Then is to set the size of the partition. It should be reminded that if there are extended partitions on the hard disk, you can only add logical partitions, not extended partitions. When adding partitions, the type is the default Linux Native. If you need to change some of the partitions For other types, such as Linux Swap or FAT32, it can be changed by command t. When pressing "t" to change the partition type, the system will prompt which partition to change, and what type to change (if you want to know what the system supports Partition type, type l). For the partition type numbers supported by Linux and their corresponding partition types, please refer to Table 2 (the information can be obtained with the l command). After changing the partition type, you can press "w" to save and exit. If you don't want to save, you can select "q" to exit directly.

fdisk is a powerful disk operation tool, from the util-linux software package, here we only talk about how to view the disk partition table and partition structure; parameter -l, through the -l parameter, you can get the number of all disks in the machine , can also list all disk partitions;

[root@localhost beinan]# fdisk -l
Disk /dev/hda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 765 6144831 7 HPFS/NTFS
/dev/hda2 766 2805 16386300 c W95 FAT32 (LBA)
/dev/hda3 2806 7751 39728745 5 Extended
/dev/hda5 2806 3825 8193118+ 83 Linux
/dev/hda6 3826 5100 10241406 83 Linux
/dev/hda7 5101 5198 787153+ 82 Linux swap / Solaris
/dev/hda8 5199 6657 11719386 83 Linux
/dev/hda9 6658 7751 8787523+ 83 Linux

In the above Blocks, it represents the size of the partition. The unit of Blocks is byte. We can convert it into M. For example, if the size of the first partition /dev/hda1 is converted into M, it should be 6144831/1024=6000M, which is About 6G, in fact, it is not so troublesome. A cursory look at moving the decimal point forward three places will give you the approximate size.
The file system represented by System, such as /dev/hda1 is in NTFS format; /dev/hda2 represents is a file system in fat32 format; .

In this example, we should pay special attention to the /dev/hda3 partition, which is an extended partition; it contains a logical partition below it, in fact, this partition is equivalent to a container; hda9;

We also noticed that, why is there no hda4? Why is hda4 not included in the extended partition? A disk has at most four primary partitions; hda1-4 are all primary partitions; hda4 cannot be included in extended partitions, and extended partitions are also primary partitions; in this example, there is no hda4 partition, of course we can One of the partitions is set as the primary partition, but I didn't do it when I partitioned;

Let’s take a closer look at the statistics, let’s see if there is still space on this disk? hda1+hda2+hda3=The volume that has actually been partitioned, so we can calculate hda1+hda2+hda3=6144831+16386300+39728745 = 62259876 (b), converted into M units, the decimal point is moved forward by three places, so it has been divided The approximate volume occupied by a good partition is 62259.876(M), in fact, the most accurate calculation is 62259876/1024=60800.67(M); and the size of this disk is 80.0 GB (80026361856byte), in fact, the actual size is 78150.744(M); According to the series of calculations, we can conclude that the hard disk still has space in use; there is still about 18G of unpartitioned space;

We can also specify fdisk -l to view the partition status of one of the hard disks;

[root@localhost beinan]# fdisk -l /dev/sda

Disk /dev/sda: 60.0 GB, 60011642880 bytes
64 heads, 32 sectors/track, 57231 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 57231 58604528 83 Linux

From the above situation, we can know that there is only one partition in the /dev/sda disk; the usage is almost 100%;

We can also look at /dev/hda

[root@localhost beinan]# fdisk -l /dev/hda

Try it yourself?

We can also view all disks and partitions in the current machine through: cat /proc/partitions

df command;
df comes from the coreutils package, which comes with it when the system is installed; we can use this command to view the disk usage and where the file system is mounted;

Example:

[root@localhost beinan]# df -lh

Filesystem capacity used free used % mount point

/dev/hda8 11G 6.0G 4.4G 58% /
/dev/shm 236M 0 236M 0% /dev/shm
/dev/sda1 56G 22G 35G 39% /mnt/sda1

We can see from this that the system is installed in /dev/hda8; there is also a 56G disk partition /dev/sda1 mounted in /mnt/sda1;

For other parameters, please refer to man df

Through the above two aspects of learning, I believe that for beginners, partitioning is no longer a stumbling block in Linux advanced.

Reprinted from: http://www.jb51.net/LINUXjishu/11011.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325980701&siteId=291194637