linux fdisk(磁盘分区)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux fdisk(磁盘分区)相关的知识,希望对你有一定的参考价值。
命令 : fdisk
fdisk 是Linux下硬盘的分区工具,是一个非常实用的命令,但是fdisk只能划分小于2T的分区。
语法 : fdisk [-l ] [设备名称] 选项只有一个。
“-l” 后边不跟设备名会直接列出系统中所有的磁盘设备以及分区表,加上设备名会列出该设备的分区表。
[[email protected] ~]# fdisk -l Disk /dev/sda: 17.2 GB, 17179869184 bytes 255 heads, 63 sectors/track, 2088 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: 0x00018d63 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 2089 14576640 83 Linux Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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 [[email protected] ~]# fdisk -l /dev/sda Disk /dev/sda: 17.2 GB, 17179869184 bytes 255 heads, 63 sectors/track, 2088 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: 0x00018d63 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 2089 14576640 83 Linux
可以看到刚才阿铭加的一块磁盘 /dev/sdb 的信息。
“fdisk” 如果不加 “-l” 则进入另一个模式,在该模式下,可以对磁盘进行分区操作。
[[email protected] ~]# 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‘). 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’, ‘w’, ‘q’.
“p” 打印当前磁盘的分区情况。
Command (m for help): p Disk /dev/sda: 17.2 GB, 17179869184 bytes 255 heads, 63 sectors/track, 2088 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: 0x00018d63 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 2089 14576640 83 Linux
‘n’ 建立一个新的分区。
‘w’ 保存操作。
‘q’ 退出。
‘d’ 删除一个分区
下面阿铭会把刚才增加的磁盘/dev/sdb进行分区操作。先使用 ‘p’ 命令看一下/dev/sdb的分区状况:
[[email protected] ~]# 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 0xf4121235. 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: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0xf4121235 Device Boot Start End Blocks Id System Command (m for help):
可以看到目前/dev/sdb没有任何分区,下面阿铭给它建立第一个分区:
Command (m for help): n Command action e extended p primary partition (1-4)
使用 ‘n’ 命令新建分区,它会提示是要 ‘e’ (扩展分区) 还是 ‘p’ (主分区) [1] 阿铭的选择是 ‘p’, 于是输入 ‘p’ 然后回车
Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-1044, default 1): 1 Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +1000M
输入 ‘p’ 后,会提示分区数,这里写 ‘1’, 因为是第一个分区,当然您也可以写 ‘2’ 或 ‘3’, 如果您直接回车的话,会继续提示您必须输入一个数字,接着又提示第一个柱面从哪里开始,默认是 ‘1’, 您可以写一个其他的数字,不过这样就浪费了空间,所以还是写 ‘1’ 吧,或者您直接回车也会按 ‘1’ 处理,接着是让输入最后一个柱面的数值,也就是说您需要给这个分区分多大空间,关于柱面是多大不再细究,您只需要掌握教给您的方法即可,即写 “+1000M”, 这样即方便又不容易出错。用 ‘p’ 查看已经多出了一个分区:
Command (m for help): p Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x0600660a 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-1044, default 385): Using default value 385 Last cylinder, +cylinders or +size{K,M,G} (385-1044, default 1044): Using default value 1044 Command (m for help): p Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0xef267349 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 1044 5301450 5 Extended
扩展分区,在最后一列显示为 “Extended”, 接下来继续创建分区:
Command (m for help): n First cylinder (385-1044, default 385): Using default value 385 Last cylinder, +cylinders or +size{K,M,G} (385-1044, default 1044): +1000M Command (m for help): p Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0xef267349 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 1044 5301450 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: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x7b9a6af3 Device Boot Start End Blocks Id System /dev/sdb2 129 256 1028160 83 Linux /dev/sdb3 257 384 1028160 83 Linux /dev/sdb4 385 1044 5301450 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: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x7b9a6af3 Device Boot Start End Blocks Id System /dev/sdb2 129 256 1028160 83 Linux /dev/sdb3 257 384 1028160 83 Linux /dev/sdb4 385 1044 5301450 5 Extended Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) l First cylinder (385-1044, default 385): Using default value 385 Last cylinder, +cylinders or +size{K,M,G} (385-1044, default 1044): +1000M Command (m for help): p Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x7b9a6af3 Device Boot Start End Blocks Id System /dev/sdb2 129 256 1028160 83 Linux /dev/sdb3 257 384 1028160 83 Linux /dev/sdb4 385 1044 5301450 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: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x7b9a6af3 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),4是扩展分区(sdb4),5是逻辑分区 [1] (sdb5),如果输入5,则直接把逻辑分区sdb5删除掉,但是如果输入4的话,会把整个扩展分区sdb4干掉,当然也包含扩展分区里面的逻辑分区sdb5。在刚才的分区界面直接 Ctrl + C 退出来,这样刚刚的分区全部都取消了,咱们重新来做分区:
[[email protected] ~]# fdisk /dev/sdb 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: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x7b9a6af3 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-1044, default 1): 1 Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): 1044 Command (m for help): p Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x7b9a6af3 Device Boot Start End Blocks Id System /dev/sdb1 1 1044 8385898+ 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-1044, default 1): 1 Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +1000M Command (m for help): p Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x7b9a6af3 Device Boot Start End Blocks Id System /dev/sdb1 1 1044 8385898+ 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-1044, default 129): 129 Last cylinder, +cylinders or +size{K,M,G} (129-1044, default 1044): +1000M Command (m for help): p Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x7b9a6af3 Device Boot Start End Blocks Id System /dev/sdb1 1 1044 8385898+ 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 查看分区情况:
[[email protected] ~]# fdisk -l /dev/sdb Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 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: 0x7b9a6af3 Device Boot Start End Blocks Id System /dev/sdb1 1 1044 8385898+ 5 Extended /dev/sdb5 1 128 1028097 83 Linux /dev/sdb6 129 256 1028128+ 83 Linux
通过以上操作,相信您也学会了用fdisk来分区了吧。要提醒您,不要闲着没事分区玩儿,这操作的危险性是很高的,一不留神就把服务器上的数据全部给分没有了。所以在您执行分区操作的时候,请保持百分之二百的细心,切记切记!
本文出自 “Linux菜鸟” 博客,请务必保留此出处http://490617581.blog.51cto.com/11186315/1757796
以上是关于linux fdisk(磁盘分区)的主要内容,如果未能解决你的问题,请参考以下文章