How to use Llinux fdisk partition tool

Time:2022-5-20

1、 Introduction of fdisk;
Fdisk – partition table manipulator for Linux, translated into Chinese, means disk partition tableoperationTools; I didn’t translate very well and didn’t read the Chinese documents; In fact, it is a partition tool; Fdsik can divide the disk into several partitions, and can also specify the partition file for each partitionsystemFor example, Linux, FAT32, Linux, Linux swap, FAT16 and their UNIX classesoperationsystemDocumentssystemEtc; Of course, we use fdisk to the diskoperationPartition is not an end point. We also need to format the files required by the partitionsystem; Such a partition can be used; This is similar to fdisk in DOS;
2、 Reasonably plan your hard disk partition;

stayoperationBefore partitioning, we should understand some theories of hard disk partitioning, such as the calculation of hard disk capacity and partition size; For how to plan the partition of a hard disk, please refer to the following documents, thank you; Rational planning of your hard disk partition
3、 Fdisk -l view hard disk and partition information;
Through “reasonable planning of your hard disk partition”, we know that the total number of primary partitions (including extended partitions) cannot exceed four; The extended partition cannot be enclosed between the primary partitions; According to this principle, it is much easier for us to partition the hard disk; It can also reduce unnecessary trouble for the future;
1. Check the number and partition of hard disks attached to the machine through fdisk -l;
[[email protected] 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 9729 55617030 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
/dev/hda10 7752 9729 15888253+ 83 Linux

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 25 201568+ c W95 FAT32 (LBA)
/dev/sda2 26 125 806400 5 Extended
/dev/sda5 26 50 201568+ 83 Linux
/dev/sda6 51 76 200781 83 Linux

From the above information, we know that there are two hard disks (or mobile hard disks) attached to this machine, one of which is HDA and the other is SDA; If we want to check the condition of a single hard disk, we can use fdisk – L / dev / hda1 or fdisk – L / dev / sda1operation; Subject to the hard disk identification output by fdisk -l; Among them, HDA has three main partitions (including extended partitions), which are main partitions hda1, hda2 and hda3 (extended partitions); The logical partition is hda5 to hda10;
SDA has two main partitions (including extended partitions), namely hda1 and hda2 (extended partitions); The logical partition is sda5 HDA6; Total capacity of hard disk = total capacity of primary partition (including extended partition)
Extended partition capacity = total logical partition capacity. Through the above example, we can know that HDA = hda1 + hda2 + hda3, where hda3 = hda5 + HDA6 + hda7 + hda8 + hda9 + hda10

2. Description of some values of fdisk;

Disk /dev/hda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
The hard disk is 80g and has 255 magnetic surfaces; 63 sectors; 9729 magnetic columns; The capacity of each cylinder is 8225280 bytes = 8225.280 K (about) = 8.225280m (about);
Partition sequence boot start termination capacity partition type ID partition type
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 9729 55617030 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
/dev/hda10 7752 9729 15888253+ 83 Linux
Note: the representation of hard disk partition: in Linux, it is represented by HD * x or SD * x, where * represents a, B, c The numbers 1, 2, 3 represented by X HD is mostly IDE hard disk; SD is mostly SCSI or mobile storage;
Boot: indicates the boot partition. In the above example, hda1 is the boot partition;
Start: indicates that a partition starts from the X cylinder;
End: indicates the end of a partition to y cylinder;
ID and system mean the same thing. ID doesn’t seem intuitive. We need to confirm the partition type by specifying ID when fdisk a partition; For example, 7 represents NTFS partition; This should be specified through the t function in fdisk. It will be mentioned in the following part;
Blocks (capacity): This is my translation, which is actually inaccurate. It really means capacity, and its unit is K; The value of a partition capacity is derived from the following formula; Blocks = (end value of the corresponding partition – start value of the corresponding partition) x the capacity of the unit cylinder, so let’s calculate the size of hda1 blocks: hda1 blocks = (765-1) x8225 280 = 6284113.92 k = 6284.113.92m note: the conversion unit is calculated from the decimal place provided by the hard disk manufacturer, ifoperationsystemIn binary terms, the partition capacity should be less. The obtained value is roughly equivalent to the value of / dev / hda1 we see through fdisk – L, because of conversionmethodDifferent, so it is impossible to be as accurate as possible; In addition, there are some losses in zoning, sometimes large or small; When we check the partition size or file, it is more intuitive to use decimal system to calculate; The calculation method is byte, pushing forward the three decimal places is k, the value of K unit, pushing forward the three decimal places is m, and M pushing forward the three decimal places is g Generally, it’s not much worse; That’s it;
3. Estimate whether a storage device is completely divided;

We estimate whether a hard disk is completely divided. We just need to see whether the end of the previous partition and the start of the next partition of the cylinders in the output of fdisk – L are a continuous number. In addition, we need to look at the beginning of fdisk – L of each hard disk device and the value of its cylinders; For example, for HDA devices, we see 9729 cylinders; Through the partition table of HDA, we can see that the value of end of the previous partition + 1 is the value of start of the next partition; For example, the start value of hda2 is the end value of hda1 + 1, which proves that there is no blank partition between hda1 and hda2, which is continuous, and so on; In hda10, we can see that the value of end is 9729, and there are 9729 cylinders in the fdisk -l header information, which proves that the hard disk has been completely divided;
Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 25 201568+ c W95 FAT32 (LBA)
/dev/sda2 26 125 806400 5 Extended
/dev/sda5 26 50 201568+ 83 Linux
/dev/sda6 51 76 200781 83 Linux
Let’s see if SDA mobile storage is completely divided; SDA consists of 125 cylinders (cylinders), a main partition and an extended partition; In the extended partition, we can see that the value of end is 125, and the cylinder of the mobile hard disk is also 125, which indicates that it is impossible to add any primary partition to the hard disk; According to the above-mentioned sda1, sda2, sda5 and sda6, there is no undivided space between sda6, but the end value of the cylinders of sda6 is 76, while the total cylinders of SDA is 125. Therefore, SDA has an undivided area behind sda6; As for the number of undivided spaces in SDA, we can calculate it; The total capacity of the expansion partition is 806400 K, which is about 806.400m, while the combined size of the logical partitions sda5 and sda6 is about 400m, so there is still about 400m undivided space and can only be divided into chain logical partitions;
4、 Effect of fdisk on hard disk and partitionoperation, enter fdisk to the hard diskoperationStage;
We can partition the hard diskoperation, if you understand fdisk -l; Through fdisk -l, we can find out the number of all hard disks and device names in the machine; For example, in the above example, we will see two devices, one is / dev / HDA and the other is / dev / SDA;

fdisk operationThe hard disk command format is as follows:
[ [email protected] Beinan]# fdisk device
For example, we know the / dev / HDA or / dev / SDA device through fdisk – L; If we want to add or delete some more partitions, we can use
[[email protected] beinan]# fdisk /dev/hda
or
[[email protected] beinan]# fdisk /dev/sda
Note in the following examples, we will take the / dev / SDA device as an example to explain how to use fdiskoperationActions such as adding and deleting partitions;

1. Description of fdisk;

When we go through the fdisk device, we enter the corresponding deviceoperationWhen, you will find the following prompts; Take fdisk / dev / SDA device as an example, the same below;
[[email protected] beinan]# fdisk /dev/sda
Command (m for help): press m here to output help;
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
D delete a partition note: This is the action of deleting a partition;
L list known partition types note: l lists partition types for our referenceset upType of corresponding partition;
M print this menu note: m is the help information listed;
N add a new partition note: add a partition;
o create a new empty DOS partition table
P print the partition table note: P lists the partition table;
Q quit without saving changes note: quit without saving;
s create a new empty Sun disklabel
T change a partition’s system ID note: t change the partition type;
u change display/entry units
v verify the partition table
W write table to disk and exit note: write the partition table to the hard disk and exit;
X extra functionality (experts only) Note: extended application, expert function;
In fact, we only use those with Chinese notes, and we don’t use other functions (oh, mainly because I can’t use them, otherwise I would show off early); X extension function is not commonly used; In general, it is only necessary to know d l m p q t w; The following is an exampleoperationTo elaborate, there is no way without examples, and novices can’t understand it;

2. List currentoperationPartition of hard disk, use p;

Command (m for help): p

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 25 201568+ c W95 FAT32 (LBA)
/dev/sda2 26 125 806400 5 Extended
/dev/sda5 26 50 201568+ 83 Linux
/dev/sda6 51 76 200781 83 Linux

3. Delete a partition through the D instruction of fdisk;

Command (m for help): P note: lists the partition conditions;

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 25 201568+ c W95 FAT32 (LBA)
/dev/sda2 26 125 806400 5 Extended

/dev/sda5 26 50 201568+ 83 Linux
/dev/sda6 51 76 200781 83 Linux

Command (m for help): D note: delete partition assignment;
Partition number (1-6): 6 note: if I want to delete sda6, enter 6 here;

Command (m for help): P note: check the hard disk partition again to see if it has been deleted?

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 25 201568+ c W95 FAT32 (LBA)
/dev/sda2 26 125 806400 5 Extended
/dev/sda5 26 50 201568+ 83 Linux

Command (m for help):
Warning: be careful when deleting a partition. Please watch the serial number of the partition. If you delete an extended partition, the logical partitions under the extended partition will be deleted; thereforeoperationBe careful when; If you know yourselfoperationWrong, please don’t panic and exit with Q without saving; Remember!!!! In partitionoperationWhen you are wrong, do not enter w to save and exit!!!

4. Add a partition through the N instruction of fdisk;

Command (m for help): p

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 25 201568+ c W95 FAT32 (LBA)
/dev/sda2 26 125 806400 5 Extended
/dev/sda5 26 50 201568+ 83 Linux

Command (m for help): n note: add a partition;
Command action
L logical (5 or over) Note: add logical partition, and the partition number should be greater than 5; Why greater than 5, because there is already sda5;
P primary partition (1-4) Note: add a primary partition; Number from 1-4; But sda1 and sda2 are occupied, so we can only start from 3;
p
Partition number (1-4): 3
No free sectors available note: why fail?
Note: I tried to add a primary partition. It seems that I failed. Why did I fail? Because we see that the main partition + extended partition has used up the whole disk. Look at the end value of the extended partition, and then look at the 125 cylinders in the P output information; It’s better to look at the front part; It is mentioned there; So we can only add logical partitions;
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
L note: enter l here to enter the stage of dividing logical partitions;
First cylinder (51-125, default 51): Note: This is the start value of the partition; It’s best to press enter directly here. If you enter a non default number, it will waste space;
Using default value 51
Last cylinder or + size or + sizem or + sizek (51-125, default 125): + 200m note: this defines the partition size, + 200m is the size of 200m; Of course, you can also calculate according to the size of the unit cylinder prompted by P, and then specify the value of end. Look back and see how it is calculated; Still use the + 200m method to add, which can be more intuitive. If you want to add a 10g or so partition, please enter + 10000m;

Command (m for help):

5. Specify the partition type through the T instruction of fdisk;

Command (m for help): tnote: use t to specify the partition type;
Partition number (1-6): 6 note: which partition type do you want to change? I specified 6, which is actually sda6
Hex code (type L to list codes): l note: enter l here to view the ID of the partition type;
Hex code (type L to list codes): bnote: if I want this partition to be w95 FAT32, I can see that B means yes through L, so I enter B;
Changed system type of partition 6 to B (w95 FAT32) Note:systemInformation, change success; Whether it is changed, please use p to check;

Command (m for help): p

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 25 201568+ c W95 FAT32 (LBA)
/dev/sda2 26 125 806400 5 Extended
/dev/sda5 26 50 201568+ 83 Linux
/dev/sda6 51 75 201568+ b W95 FAT32

6. For the exit of fdisk, use Q or W;

Where q is exit without saving and W is exit with saving;
Command (m for help): w
or
Command (m for help): q

7. An example of adding partitions;

In this example, we will add two 200m primary partitions and the others are extended partitions. In the extended partition, we will add two 200m logical partitions;
Command (m for help): P note: lists the partition table;

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

Device Boot Start End Blocks Id System

Command (m for help): n note: add partition;
Command action
e extended
p primary partition (1-4)
P note: add main partition;
Partition number (1-4): 1 note: add primary partition 1;
First cylinder (1-125, default 1): Note: Enter directly, the starting position of main partition 1; The default is 1, which is good by default;
Using default value 1
Last cylinder or + size or + sizem or + sizek (1-125, default 125): + 200m note: specify the partition size, and use + 200m to specify the size as 200m

Command (m for help): n note: add a new partition;
Command action
e extended
p primary partition (1-4)
P note: add primary partition
Partition number (1-4): 2 note: add primary partition 2;
First cylinder (26-125, default 26):
Using default value 26
Last cylinder or + size or + sizem or + sizek (26-125, default 125): + 200m note: specify the partition size, and use + 200m to specify the size as 200m

Command (m for help): n
Command action
e extended
p primary partition (1-4)
E note: add extended partition;
Partition number (1-4): 3 note: specify 3, because the primary partition has been divided into two. This is also the primary partition, starting from 3;
First cylinder (51-125, default 51): Note: Enter directly;
Using default value 51
Last cylinder or + size or + sizem or + sizek (51-125, default 125): Note: press enter directly to divide all other spaces into expansion zones;
Using default value 125

Command (m for help): p

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 25 201568+ 83 Linux
/dev/sda2 26 50 201600 83 Linux
/dev/sda3 51 125 604800 5 Extended

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
L note: add logical partition;
First cylinder (51-125, default 51):
Using default value 51
Last cylinder or + size or + sizem or + sizek (51-125, default 125): + 200m note: add a partition with a size of 200m;

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
L note: add a logical partition;
First cylinder (76-125, default 76):
Using default value 76
Last cylinder or + size or + sizem or + sizek (76-125, default 125): + 200m note: add a partition with a size of 200m;

Command (m for help): P lists the partition table;

Disk /dev/sda: 1035 MB, 1035730944 bytes
256 heads, 63 sectors/track, 125 cylinders
Units = cylinders of 16128 * 512 = 8257536 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 25 201568+ 83 Linux
/dev/sda2 26 50 201600 83 Linux
/dev/sda3 51 125 604800 5 Extended
/dev/sda5 51 75 201568+ 83 Linux
/dev/sda6 76 100 201568+ 83 Linux
Then we change the partition type through the T instruction according to the above; Finally, don’t forget to w save and exit;
5、 Format and load the partition;
Give a hint first; Use mkfs bfs mkfs. ext2 mkfs. jfs mkfs. msdos mkfs. vfatmkfs. cramfs mkfs. ext3 mkfs. minix mkfs. reiserfs mkfs. XFS and other commands to format the partition. For example, I want to format sda6 as an ext3 filesystem, then enter;
[[email protected] beinan]# mkfs.ext3 /dev/sda6
If I want to load sda6 to the presentsystemTo access files, there should be a mount command, but first you have to create a mount directory; For example, / MNT / sda6;
[[email protected] beinan]# mkdir /mnt/sda6
[[email protected] beinan]# mount /dev/sda6 /mnt/sda6
[[email protected] beinan]# df -lh
Filesystem capacity used% mount point available
/dev/hda8 11G 8.4G 2.0G 81% /
/dev/shm 236M 0 236M 0% /dev/shm
/dev/hda10 16G 6.9G 8.3G 46% /mnt/hda10
/dev/sda6 191M 5.6M 176M 4% /mnt/sda6
In this way, we can enter the / MNT / sda6 directory and access the files; Specific permissionsmethod, as well as the more detailed usage of mount, I will write a post in the future; It’s a little difficult to put down all the content in one post; Postscript: after writing this article, I will write another partition tool parted; Cfdisk is removed in Fedora 4.0; So I don’t want to introduce this tool; Linux viewing disk partitions and filessystem, usage commands and related tools
Example explanation fdisk usagemethod
Details of loading NTFS and FAT32 partitions in Fedora Core 4.0
Rational planning of your hard disk partition
systemDiscussion on boot process and hard disk partition structure
Practice of Fedora Core 4.0 Hal configuring plug and play mobile storage (USB and 1394)
Two representations of storage devices in Linux systemmethod