How to Use fdisk partitions in Linux

Source: Internet
Author: User
Tags hex code
1. fdisk introduction;
Fdisk-Partition Table Manipulator for Linux, translated into Chinese means the disk partition table operation tool; I did not translate very well and did not read the Chinese documentation; in fact, it is actually a partition tool;
Fdsik can divide disks into Several partitions and specify the partition file system for each partition, for example, Linux, FAT32, Linux, Linux swap, fat16, and file systems of Unix-like operating systems. Of course, when we use fdisk to operate partitions on disks, it is not an end point, we need to format the required file system for the partition so that a partition can 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. If we want to view the status of a single hard disk, you can use fdisk-L/dev/hda1 or fdisk-L/dev/sda1. Take the hard disk ID output by fdisk-L as the standard;
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 is used, where * represents a, B, c ...... ...... Numbers 1, 2, 3, and so on ...... ...... 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 the blocks in hda1:
Hda1 blocks = (765-1) x8225.280 = 6284113.92 K = 6284.113.92m
Note: The conversion unit is counted from the 10th digit provided by the hard disk manufacturer. If the unit is measured in binary format of the operating system, the partition capacity should be less, the obtained value is roughly the same as the value of/dev/hda1 we can see through fdisk-L. Because the conversion method is different, it is impossible to be as accurate as possible; in addition, some loss occurs during partitioning, and sometimes 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 pushes the three decimal places forward to K, and the value in K units pushes the three decimal places forward to M, M is pushed forward to the decimal point ...... ...... Generally, it cannot be much worse;
3. estimate whether a storage device is fully divided;
To estimate whether a hard disk is fully divided, we only need to check whether the end of the last partition and the start of the next partition are a continuous number in the cylinders (column) output by fdisk-L, in addition, let's take a look at the starting part of fdisk-L for each hard disk device and its cylinders value;
For example, for the hda device, we see 9729 cylinders. Through the hda partition table, we can see that the end value of the previous partition + 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 there is no blank partition between hda1 and hda2, Which is continuous, and so on; In hda10, we can see that the end value is 9729, And the fdisk-l header information also contains 9729 cylinders, which proves 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
Let's see if the SDA mobile storage is fully divided. SDA has 125 cylinders (bars) and 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 125, which indicates that it is impossible to add any primary partition to the hard disk; according to the above-mentioned sda1 sda2 sda5 sda6, there is no undivided space between them, but the end value of cylinders (column) of sda6 is 76, while the total cylinders (column) of SDA has 125, as a result, SDA has unpartitioned areas behind sda6;
As to the number of unpartitioned spaces of SDA, we will know it. The total capacity of the extended partition is 806400 K, which is about m, and the size of the logical partition sda5 and sda6 is about M, so there is still about MB 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: This defines the partition size, + 200 m indicates that the cylinder size is 200 m. You can also specify the value of end based on the cylinder size indicated by P. 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 10 Gb partition, 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;
A prompt is given 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, if I want to format sda6 as the ext3 file system, enter;
[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;

From: http://www.xxlinux.com/linux/article/accidence/install/20051228/776.html

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.