How to Use fdisk for instance explanation

Source: Internet
Author: User
Tags hex code
Author: North South
From: linuxsir. org
-----------------------------------

Abstract: fdisk is a powerful partitioning tool and one of the most popular tools in UNIX operating systems. It is a time-honored partition tool. This article only introduces its simplest partitioning operation functions;

1. fdisk introduction;
Fdisk-Partition Table Manipulator for Linux
In Chinese, it means the disk partition table operation tool. I did not translate very well and did not read the Chinese documentation. It is actually a partition tool;
Fdsik can divide disks into Several partitions and specify the partition file system for each partition, such as Linux, FAT32, Linux, and Linux swap.
Fat16 and the file system of Unix-like operating systems; of course, we use fdisk
When operating a partition on a disk, it is not an end point. We also need to format the file system required for the partition to be used. This is similar to fdisk in DOS;

2. reasonably plan your hard disk partition;

Before partitioning, we need to understand the theory of Hard Disk partitioning, such as the calculation of hard disk capacity and partition size; for how to plan partitions for a hard disk, please refer to the following documents. Thank you;
Rational Planning of your hard disk partition

3. fdisk-l view the hard disk and partition information;

Through rational planning of your hard disk partitions, we know that the total number of primary partitions (including extended partitions) cannot exceed four, nor can we enclose extended Partitions between primary partitions. According to this principle, it is easier to divide hard disk partitions and reduce unnecessary troubles in the future;

1. Use fdisk-L to view the number of hard disks mounted to the machine and the partition information;[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        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

Based on the above information, we know that two hard disks (or mobile hard disks) are attached to this machine, one of which is hda and the other is SDA.
To view the status of a single hard disk, use fdisk-L/dev/hda1 or fdisk-L/dev/sda1.
-L indicates the hard disk ID output;
Hda has three primary partitions (including extended partitions), namely the primary partition hda1 hda2 and hda3 (Extended partitions), and the logical partitions are hda5 to hda10;
SDA has two primary partitions (including extended partitions), namely hda1 and hda2, and the logical partition is sda5 hda6;
Total disk capacity = total capacity of the primary partition (including extended partitions)
Extended partition capacity = total capacity of logical partitions
Through the above example, we can know that hda = hda1 + hda2 + hda3, where hda3 = hda5 + hda6 + hda7 + hda8 + hda9 + hda10 ......

2. Description of some fdisk-L values;

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 80 GB, with 255 magnetic surfaces; 63 sectors; 9729 magnetic columns; the capacity of each cylinder (magnetic column) is 8225280 bytes = 8225.280 K (about) = 8.225280 M (about );

Partition sequence boot starts to terminate 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:
Hard Disk Partition representation: in Linux, HD * X or SD * X indicates A, B, and C ...... the numbers 1, 2, and 3 represented by X ...... hdds are mostly IDE hard disks; SD is mostly SCSI or mobile storage;
Boot: indicates the boot partition. In the preceding example, hda1 is the boot partition;
Start (start): indicates that a partition starts from X cylinder (magnetic column;
End: indicates that a partition ends with y cylinder (magnetic column;
ID and system indicate the same meaning. ID does not seem intuitive. We need to specify the ID to confirm the partition type when fdisk is a partition. For example, if the ID is 7, NTFS is used; this must be specified through the T function in fdisk. The following parts will be mentioned;
Blocks; the value of a partition capacity comes from the following formula;
Blocks = (end value of the corresponding partition-start value of the corresponding partition) Capacity of x unit cylinder (magnetic column) So let's calculate the size of hda1
Blocks size:
Hda1 blocks = (765-1) x8225.280 = 6284113.92 K = 6284.113.92m
Note: The conversion unit is counted from the 10th carried value provided by the hard disk manufacturer. If the conversion unit is calculated in binary format of the operating system, the partition capacity should be less. The obtained value is the same as what we see through fdisk-L.
The value of/dev/hda1 is roughly equivalent, because the conversion methods are different, so it is impossible to be as accurate as possible; in addition, the loss of the partition, and so on, sometimes it is large or small;
When we look at the partition size or file, we still use decimal to calculate it more intuitively. The calculation method is that byte is pushed forward to the decimal point of three digits, that is, K.
, The value in the unit of K is pushed forward to the decimal point 3 is m, and m is pushed forward to the decimal point 3 is g ...... it is generally not much difference; this is enough;

3. estimate whether a storage device is fully divided;

We estimate whether a hard disk is fully divided. We only need to check the end and
Whether the start of the next partition is a continuous number. Also, let's take a look at the start part of fdisk-L for each hard disk device.
Cylinders (column) value;
For example, for the hda device, we see 9729 cylinders. We can see the end value of the previous partition in the hda Partition Table + 1.
Is the start value of the next partition. For example, the start value of hda2 is the end value of hda1 + 1, which proves that hda1 and hda2
There is no blank partition in the middle, it is continuous, and so on; In hda10, we can see that the end value is 9729, And the fdisk-l header information also has 9729
Cylinders, proving that the hard disk has been fully 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

Me
Let's see if the SDA mobile storage is fully divided. SDA has 125 cylinders.
There is a primary partition and an extended partition. In the extended partition, we can see that the end value is 125, and the cylinder of the mobile hard disk is also 125, this indicates the hard disk.
No primary partition can be added. According to the above-mentioned sda1 sda2 sda5 sda6, there is no unpartitioned space, but the sda6 Cylinders
The end value of the (column) is 76, while the total cylinders (column) of SDA has 125. As a result, SDA has unpartitioned areas behind sda6;
As for how much unpartitioned space SDA has, we will know it. The total capacity of the extended partition is 806400 K, which is about m, while that of the logic partition sda5
The sum of sda6 and sda6 is about M, so there is still about m of unpartitioned space, and can only be divided into chain logical partitions;

4. fdisk operations on hard disks and partitions enter the fdisk operation phase;

We can partition the hard disk on the premise that you have understood fdisk-L; through fdisk-l, we can find the number of all hard disks and device names on the machine; for example, in the above example, we can see two devices:/dev/hda and/dev/SDA;

The command format for fdisk hard disk operation is as follows:[Root @ localhost 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 partitions, we can use

[Root @ localhost beinan] # fdisk/dev/hda
Or
[Root @ localhost beinan] # fdisk/dev/SDA

Note: In the following example, we will use the/dev/SDA device as an example to explain how to use fdisk to add or delete partitions;

1. Description of fdisk;

When you access the corresponding device through the fdisk device, you will find the following prompt: Take the fdisk/dev/SDA device as an example, the same as below;

[Root @ localhost 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 operation deletes a partition;
L List known partition types Note: l lists the partition types for us to set the corresponding partition types;
M print this menu Note: m lists help information;
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 partition tables;
Q quit without saving changes Note: Do not save and exit;
S create a new empty sun disklabel
T change a partition's System ID Note: T changes 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: extends the application and expert functions;

In fact, we only use Chinese annotations. Other functions are not commonly used (Oh, I will not use them, or we will sell them out early). x extension functions are not commonly used; generally, you only need to know d l m p q t w;
The following is a detailed description of instance operations. If there are no examples, you cannot understand them;

2. List the partitions of the current 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. Use the D command of fdisk to delete a partition;

Command (M for help): p Note: List partition information;

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: run the delete partition to specify;
Partition Number (1-6): 6 Note: To delete sda6, enter 6 here;

Command (M for help): p Note: Check the hard disk partition and check whether the partition is 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. Check the sequence number of the partition. If you delete an extended partition, all logical partitions under the extended partition will be deleted. Therefore, be careful when performing operations; if you know that your operation is wrong, please do not panic, do not save Q and exit; Remember to remember !!!! When the partition operation is incorrect, do not enter W to save and quit !!!

4. Add a partition using the N command 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: to add a logical partition, the partition number must be greater than 5. Why is it greater than 5 because sda5 already exists;
P primary partition (1-4) Note: Add a primary partition; the number ranges from 1 to 4; but sda1 and sda2 are occupied, so they can only start from 3;
P
Partition Number (1-4): 3
No free sectors available Note: Why is the failure failed?

Note: I tried to add a primary partition. It seems that it failed. Why did it fail? Because we can see that the primary partition and the extended partition use up the entire disk, look at the end value of the extended partition, and then look at the P output information with 125 cylinders; it is best to look at the previous part; it is mentioned there;
Therefore, 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 partition logic partition stage;
First cylinder (51-125, default 51): Note: This is the start value of the partition. Press enter here. If you enter a non-default number, this will cause a waste of space;
Using default value 51
Last
Cylinder or + size or + sizem or + sizek (51-125, default 125): + 200 m
Note: The partition size is defined. + 200 m means the size is 200 M. Of course, you can also calculate the unit cylinder size according to the P prompt, and then specify
The value of end. Let's look back at how it is calculated. We can use the + M method to add it, which is more intuitive. If you want to add a partition of about 10 Gb, enter + 10000 m
;

Command (m for help):

5. Use the fdisk t command to specify the partition type;

Command (M for help): t Note: use t to specify the partition type;
Partition Number (1-6): 6 Note: Which partition type should be changed? I specified 6, that is, 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): B Note: If I want this partition to be of the w95 FAT32 type, I can see through l that B indicates yes, So I entered B;
Changed system type of partition 6 to B (w95 FAT32) Note: The system information is changed successfully. If yes, check it with 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          75      201568+   b  W95 FAT32

6. Exit fdisk. Use Q or W;

Q is not saved and exited, W is saved and exited;

Command (M for help): W
Or
Command (M for help): Q

7. An example of adding a partition;

In this example, we will add two primary M partitions. The others are extended partitions. In the extended partitions, we will add two M logical partitions;

Command (M for help): p Note: List partition tables;

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 a primary partition;
Partition Number (1-4): 1 Note: add primary partition 1;
First cylinder (1-125, default 1): Note: Press enter directly to start the position of primary partition 1. The default value is 1. The default value is good;
Using default value 1
Last cylinder or + size or + sizem or + sizek (1-125, default 125): + 200 m Note: Specify the partition size, with + 200 m to specify the size of m

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): + 200 m Note: Specify the partition size, with + 200 m to specify the size of m

Command (M for help): n
Command action
E extended
P primary partition (1-4)
E Note: Add extended partitions;
Partition Number (1-4): 3 Note: It is specified as 3. Because the primary partition has already been divided into two, this is also the primary partition, starting from 3;
First cylinder (51-125, default 51): Note: Press enter;
Using default value 51
Last cylinder or + size or + sizem or + sizek (51-125, default 125): Note: Press enter to add all the remaining space to the extended partition;
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 partitions;
First cylinder (51-125, default 51 ):
Using default value 51
Last cylinder or + size or + sizem or + sizek (51-125, default 125): + 200 m Note: Add a m partition;

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): + 200 m Note: Add a m partition;

Command (M for help): p lists partition tables;

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 use the T command to change the partition type;
Do not forget to save and exit;

5. Format and load partitions;
Run mkfs. BFS mkfs. ext2 mkfs. JFS mkfs. msdos mkfs. vfatmkfs. cramfs.
Mkfs. ext3 mkfs. minix mkfs. reiserfs mkfs. XFS
If sda6 is an ext3 file system, enter it;

[root@localhost beinan]# mkfs.ext3 /dev/sda6

If I want to load sda6 to the current system to access files, there should be a mount command, but first you must create a mount directory; for example,/mnt/sda6;

[Root @ localhost beinan] # mkdir/mnt/sda6
[Root @ localhost beinan] # Mount/dev/sda6/mnt/sda6
[Root @ localhost beinan] # DF-lH
Filesystem capacity in use available % mount point
/Dev/hda8 11g 8.4g 2.0g 81%/
/Dev/SHM 236 M 0 236 m 0%/dev/SHM
/Dev/hda10 16g 6.9g 8.3g 46%/mnt/hda10
/Dev/sda6 191 M 5.6 m 176 m 4%/mnt/sda6

In this way, we can access the/mnt/sda6 directory and then access the file;
In the future, I will write a post specifically for specific permission methods and more detailed usage of Mount. It is a bit difficult to put all the content in a post;
Postscript:
After writing this article, I want to write another partition tool parted; In fedora 4.0, cfdisk is removed; so I don't want to introduce this tool;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.