How to Use fdisk for Linux

Source: Internet
Author: User
Tags hex code

Fdisk
It 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;

I. 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
You can divide a disk into Several partitions and specify the partition file system for each partition, such as Linux, FAT32, Linux, Linux swap, and fat16.
And the file systems 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, see
Rational Planning of your hard disk partition

Iii. fdisk-l
View hard disk and partition information
;

Plan your hard disk partition properly
, 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.
The other is SDA. To view the status of a single hard disk, you can 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): hda1 hda2 and hda3 (Extended partitions ).
Hda5 to hda10;
SDA has two primary partitions (including extended partitions): hda1 and hda2. The logical partition is sda5 hda6.
; Total disk capacity = total capacity of the primary partition (including extended partitions)
Expanded partition capacity = total logical partition capacity. We can see from the above example 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

This hard disk is 80 GB, with 255 magnetic surfaces; 63 sectors; 9729 magnetic columns; each
The cylinder (magnetic column) capacity 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: in Linux, hard disk partitioning is performed through HD * X or SD * X.
* Indicates a, B, c... X indicates numbers 1, 2, 3 ......
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 a partition to y.
Cylinder (magnetic column) ends;
ID and system indicate a meaning. ID does not seem intuitive. We need to specify an ID to confirm the partition type when fdisk is a partition. For example:
The value 7 indicates the NTFS partition.
It must be specified through the T function. The following parts will be mentioned;
Blocks; the value of a partition capacity comes from the following formula;
Blocks = (the end value of the corresponding partition-The start value of the corresponding partition) the capacity of the X unit cylinder (magnetic column). So let's calculate the blocks size of hda1:
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, it is more intuitive to use decimal to calculate it. The calculation method is
Byte pushes the three decimal places forward to K, the value in the unit of K pushes the three decimal places forward to m, and the three decimal places forward to m is g ...... generally, it cannot be much worse;

3. estimate whether a storage device is fully divided;

We estimate whether a hard disk is fully divided. We only need to check the fdisk
-L whether the end of the last partition and the start of the next partition in the cylinders (column) Output content are consecutive numbers. Also, you need to check the fdisk-L of each hard disk device.
The starting part of cylinders. For example, for hda devices, we can see 9729 cylinders.
In 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 are no blank partitions in the middle of hda1 and hda2, which are continuous, and so on. In hda10, we can see that the end value is 9729, while in the fdisk-l header information, there are also 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

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 that this hard disk cannot be added to any primary partition.
Sda1 sda2 sda5 sda6 does not have any undivided space, but the end value of cylinders (column) of sda6 is 76, while
SDA has 125 cylinders (bars). Therefore, SDA has unpartitioned areas behind sda6.
The total capacity of the extended partition is about 806400 kb, which is about mb. The total size of the logical partitions sda5 and sda6 is
About m, so there is still about m of unpartitioned space, and can only be divided into chain logical partitions;

4. Perform fdisk operations on the hard disk and partition and go to fdisk.
Hard Disk operation phase
;

We can partition the hard disk on the premise that you understand 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 will see two devices:/dev/hda and/dev/SDA;

Fdisk
The command format for operating the hard disk is as follows:

[Root @ localhost beinan] # fdisk Device

For example, we know/dev/hda or
/Dev/SDA device; if you want to add or delete some partitions, you can use

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

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

1. fdisk
Description
;

When you use the fdisk device to access the corresponding device, you will find the following prompt:
The device is used as an example;

[Root @ localhost beinan] # fdisk/dev/SDA
Command (M for help): Here press m
To output help;
Command action
A toggle a bootable flag
B edit BSD
Disklabel
C toggle the DOS compatibility flag
D delete
Partition note: this is the action to delete 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: Extended 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; in general, you only need to understand
D l m P Q T W
The following describes how to operate instances. 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
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: If you want to delete sda6, enter 6 here;

Command (M for help): p
Note: Check the hard disk partition 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
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
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?

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 are used up the entire disk, let's look at the end value of the extended partition.
There are 125 cylinders in P output information; it is better to look at the previous section; as mentioned above, 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 of the partition.
Value; press enter directly. If you enter a non-default number, the space will be wasted;
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
The size is 200 M. Of course, you can also calculate the cylinder size according to the Unit indicated by P, 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
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 w95
For FAT32 type, we can see through l that B Represents yes, so we input B;
Changed system type of partition 6 to B (w95
FAT32) Note: The system information is changed successfully. Check if the system information has changed;

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. fdisk
To exit, 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. 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
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): Enter the start position of primary partition 1. The default value is 1;
Using default value 1
Last
Cylinder or + size or + sizem or + sizek (1-125, default 125): + 200 m
Note: Specify the partition size. + 200 m is used to specify the size as M.

Command (M for help): n Note: Add a new partition;
Command
Action
E extended
P primary partition (1-4)
P
Note: Add a 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: 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 directly;
Using default value 51
Last cylinder or + size or + sizem or + sizek
(51-125, default 125): 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
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 Mbit/s, 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 W;

5. Format and load partitions;

First, let's get a prompt. 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 an ext3 file system, enter;

[Root @ localhost beinan] # mkfs. ext3
/Dev/sda6

If I want to load
The mount command should be available for the system to access files from sda6 to the present, but you must first create a mount directory, such as/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.

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.