首页 > 代码库 > fdisk命令

fdisk命令

fdisk 命令

fdisk用来分区大小小于2T的磁盘,如果大于2T需要用parted工具。

在分区前,现增加一块虚拟磁盘。首先关闭正在运行的Linux系统。关机命令为 init 0

点击”编辑虚拟机设置“,然后点击左下角的添加按钮,在弹出的窗口选择硬盘,然后下一步。

技术分享

虚拟磁盘类型,保持默认scsi,点击下一步。再点下一步,磁盘大笑着值为10G。

技术分享

技术分享

技术分享

技术分享


磁盘文件名和路径保持默认,点完成,最后点确定。

fdisk命令语法:fdisk  [-l] [设备名称] 选项只有一个。

-l :后边不跟设备名会直接列出系统中所有的磁盘设备以及分区表,加上设备名会列出该设备的分区表。

[root@qiangzi ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x0003b5c1

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          13      102400   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              13         274     2097152   82  Linux swap / Solaris

Partition 2 does not end on cylinder boundary.

/dev/sda3             274        1306     8285184   83  Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x00000000

[root@qiangzi ~]# fdisk -l /dev/sda

Disk /dev/sda: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x0003b5c1

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          13      102400   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              13         274     2097152   82  Linux swap / Solaris

Partition 2 does not end on cylinder boundary.

/dev/sda3             274        1306     8285184   83  Linux

可以看到刚才加的一块磁盘/dev/sdb的信息。

fdisk如果不加 -l 则进入另一个模式,在该模式下,可以对磁盘进行分区操作。

[root@qiangzi ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to

switch off the mode (command ‘c‘) and change display units to

sectors (command ‘u‘).

如果输入m会列出常用的命令:

Command (m for help): m

Command action

a   toggle a bootable flag

b   edit bsd disklabel

c   toggle the dos compatibility flag

d   delete a partition      删除一个分区

l   list known partition types

m   print this menu

n   add a new partition       添加一个新分区

o   create a new empty DOS partition table

p   print the partition table       打印分区表

q   quit without saving changes

s   create a new empty Sun disklabel

t   change a partition‘s system id

u   change display/entry units

v   verify the partition table

w   write table to disk and exit

x   extra functionality (experts only)

常用的有 p、n、d

p:打印当前磁盘的分区情况

Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x0003b5c1

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          13      102400   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              13         274     2097152   82  Linux swap / Solaris

Partition 2 does not end on cylinder boundary.

/dev/sda3             274        1306     8285184   83  Linux

n:建立一个新的分区

w:保存操作

q:退出

d:删除一个分区

下面把刚才增加的磁盘/dev/sdb进行分区操作。

先使用p命令看一下/dev/sdb的分区状况:

[root@qiangzi ~]# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0xbf9d301b.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won‘t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to

switch off the mode (command ‘c‘) and change display units to

sectors (command ‘u‘).

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xbf9d301b

Device Boot      Start         End      Blocks   Id  System

可以看到目前/dev/sdb没有任何分区,下面给他建立第一个分区:

Command (m for help): n

Command action

e   extended   扩展分区

p   primary partition (1-4)    主分区

使用n命令新建分区,他会提示是要e扩展分区)还是p主分区)。我的选择是p,然后回车。

p

Partition number (1-4): 1

First cylinder (1-1305, default 1): 1

Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +1000M

输入p后,会提示分区数,这里我写1,因为是第一个分区。当然你也可以写2或3,如果直接回车的话,会继续提示你必须输入一个数字,接着又提示第一个柱面从哪里开始,默认是1。在这里,可以写一个其他的数字,不过这样就浪费了空间,所以还是写1吧,或者直接回车也会按1处理。接着是让输入最后一个柱面的数值,也就是说需要给这个分区分多大的空间。在后面写“+1000M”,这样既方便又不容易出错。用p查看已经多出了一个分区:

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xbf9d301b

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         128     1028128+  83  Linux

继续上面的操作,一直创建主分区到4,然后再一次创建分区的时候会提示:

Command (m for help): n

You must delete some partition and add an extended partition first

这是因为,在Linux系统中最多只能创建4个主分区。那如果你想多创建几个分区如何做?

在创建完第三个分区后,创建第四个分区时选择扩展分区。

Command (m for help): n

Command action

e   extended

p   primary partition (1-4)

e

Selected partition 4

First cylinder (385-1305, default 385):

Using default value 385

Last cylinder, +cylinders or +size{K,M,G} (385-1305, default 1305):

Using default value 1305

Command (m for help): P

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x6aa6edfe

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         128     1028128+  83  Linux

/dev/sdb2             129         256     1028160   83  Linux

/dev/sdb3             257         384     1028160   83  Linux

/dev/sdb4             385        1305     7397932+   5  Extended

扩展分区在最后一行显示为 Extended

Command (m for help): n

First cylinder (385-1305, default 385):

Using default value 385

Last cylinder, +cylinders or +size{K,M,G} (385-1305, default 1305): +1000M

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x6aa6edfe

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         128     1028128+  83  Linux

/dev/sdb2             129         256     1028160   83  Linux

/dev/sdb3             257         384     1028160   83  Linux

/dev/sdb4             385        1305     7397932+   5  Extended

/dev/sdb5             385         512     1028128+  83  Linux

这时候再分区和以前有区别了,不再选择是主分区还是扩展分区了,而是直接定义大小

当分完三个主分区后,第四个扩展分区需要把剩余的磁盘空间全部划分个扩展分区,不然的话剩余的空间会浪费。

因为分完扩展分区后,再划分新的分区时是在已经划分的扩展分区里来分的。

其中/dev/sdb4扩展分区,这个分区是不可以格式化的。可以把它看成一个空壳子,能使用的为/dev/sdb5,其中/dev/sdb5/dev/sdb4子分区,这个子分区叫做逻辑分区

如果发现分区分的不合适,想删除掉某个分区,使用d命令:

Command (m for help): d

Partition number (1-5): 1

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x6aa6edfe

Device Boot      Start         End      Blocks   Id  System

/dev/sdb2             129         256     1028160   83  Linux

/dev/sdb3             257         384     1028160   83  Linux

/dev/sdb4             385        1305     7397932+   5  Extended

/dev/sdb5             385         512     1028128+  83  Linux

Command (m for help): d

Partition number (1-5): 5

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x6aa6edfe

Device Boot      Start         End      Blocks   Id  System

/dev/sdb2             129         256     1028160   83  Linux

/dev/sdb3             257         384     1028160   83  Linux

/dev/sdb4             385        1305     7397932+   5  Extended

Command (m for help): n

Command action

l   logical (5 or over)

p   primary partition (1-4)

l

First cylinder (385-1305, default 385):

Using default value 385

Last cylinder, +cylinders or +size{K,M,G} (385-1305, default 1305): +1000M

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x6aa6edfe

Device Boot      Start         End      Blocks   Id  System

/dev/sdb2             129         256     1028160   83  Linux

/dev/sdb3             257         384     1028160   83  Linux

/dev/sdb4             385        1305     7397932+   5  Extended

/dev/sdb5             385         512     1028128+  83  Linux

Command (m for help): d

Partition number (1-5): 4

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x6aa6edfe

Device Boot      Start         End      Blocks   Id  System

/dev/sdb2             129         256     1028160   83  Linux

/dev/sdb3             257         384     1028160   83  Linux

输入d会提示要删除哪个分区,可以选择1-5,其中1-3是主分区(sdb1、sdb2、sdb3)。sdb4是扩展分区,sdb5是逻辑分区。

如果输入5,则直接把逻辑分区sdb5删掉,但是如果输入4的话会把整个扩展分区sdb4删掉当然也包含扩展分区里的逻辑分区sdb5

在刚才的分区界面直接Ctrl+C退出来,这样刚才新建的分区全部都消失了,重新来做分区:

[root@qiangzi ~]# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0xcadd895c.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won‘t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to

switch off the mode (command ‘c‘) and change display units to

sectors (command ‘u‘).

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xcadd895c

Device Boot      Start         End      Blocks   Id  System

Command (m for help): n

Command action

e   extended

p   primary partition (1-4)

e

Partition number (1-4): 1

First cylinder (1-1305, default 1): 1

Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): 1305

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xcadd895c

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1        1305    10482381    5  Extended

Command (m for help): n

Command action

l   logical (5 or over)

p   primary partition (1-4)

如果把第一个分区分为扩展分区,并且把全部空间都分给扩展分区,再继续分区的话,会提示的分区类型为主分区还是逻辑分区(logical),用 l 表示逻辑分区,逻辑分区id是从5开始的因为前四个id为主分区或者扩展分区。既然把所有的磁盘空间都分为了扩展分区,如果在这里选择p会报错:

Command action

l   logical (5 or over)

p   primary partition (1-4)

p

Partition number (1-4): 2

No free sectors available   【没有可用的领域】

这是因为没有足够的空间给主分区了,那就分逻辑分区:

Command (m for help): n

Command action

l   logical (5 or over)

p   primary partition (1-4)

l

First cylinder (1-1305, default 1): 1

Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +1000M

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xe1c3b28b

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1        1305    10482381    5  Extended

/dev/sdb5               1         128     1028097   83  Linux

Command (m for help): n

Command action

l   logical (5 or over)

p   primary partition (1-4)

l

First cylinder (129-1305, default 129): 129

Last cylinder, +cylinders or +size{K,M,G} (129-1305, default 1305): +1000M

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xe1c3b28b

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1        1305    10482381    5  Extended

/dev/sdb5               1         128     1028097   83  Linux

/dev/sdb6             129         256     1028128+  83  Linux

分完区后,需要输入w命令来保存我们的设置:

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

再使用fdisk -l /dev/sdb查看分区情况:

[root@qiangzi ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xe1c3b28b

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1        1305    10482381    5  Extended

/dev/sdb5               1         128     1028097   83  Linux

/dev/sdb6             129         256     1028128+  83  Linux

提示:

分区时一定要细心,谨慎,分区的危险性很高,一不小心就把服务器上的数据全部分没了。


本文出自 “12350027” 博客,谢绝转载!

fdisk命令