Linux fdisk command usage

Source: Internet
Author: User
Tags hex code

Fdisk

FDISK hard disk partitioning is essentially a format of the hard disk. When we create a partition, we have set the physical parameters of the hard disk, specifying the storage location of the master boot record (MasterBootRecord, MBR) and boot record backup of the hard disk. For file systems and other operating systems, the required information for disk management is achieved through the advanced formatting, that is, the Format command. In an image metaphor, partitioning is like drawing a generous box on a piece of white paper. Formatting is like adding a grid in the box. Installing various software is like writing words in the grid. Partitioning and formatting are equivalent to laying the foundation for software installation. In fact, they are used to mark and locate data stored on the hard disk of the computer.

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;

Fdisk description

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;

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

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 !!!
 
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;

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 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 ):

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

4. 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.

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.