首页 > 代码库 > fdisk 使用方法

fdisk 使用方法


//参照“实例解说 fdisk 使用方法”;http://www.linuxsir.org/main/?q=node/81
//作者:北南南北

1.关于硬盘分区的介绍
硬盘的分区由主分区、扩展分区和逻辑分区组成;
主分区(包括扩展分区)的最大个数是四个;其中扩展分区也算一个主分区;
扩展分区可以继续划分为更多的逻辑分区;
所以主分区(包括扩展分区)范围是从1-4,逻辑分区是从5开始的;

2.通过fdisk -l查看机器挂载的硬盘个数,及分区情况
-----------------------------------------------------------------------
[root@localhost ~]# fdisk -l

Disk /dev/hda: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 13 104391 83 Linux
/dev/hda2 14 9726 78019672+ 8e Linux LVM

Disk /dev/sda: 1003 MB, 1003421696 bytes
1 heads, 63 sectors/track, 31108 cylinders
Units = cylinders of 63 * 512 = 32256 bytes

Device Boot Start End Blocks Id System
/dev/sda1 2 6202 195331+ 83 Linux
/dev/sda2 6203 12403 195331+ c W95 FAT32 (LBA)
/dev/sda3 12404 31108 589207+ 5 Extended
/dev/sda5 12405 21706 293013 7 HPFS/NTFS
--------------------------------------------------------------------------
通过上面的信息,我们可以知道这个机器中有两个硬盘,hda和sda(U盘)
以sda为例:
共有3个主分区:sda1/sda2/sda3(sda3为扩展分区);
在扩展分区sda3下有1个逻辑分区:sda5
Boot:表示引导分区,打“*”星号的是引导分区,上例中/dev/hda1
Start:表示的一个分区从X cylinder(磁柱)开始;
End:表示一个分区到 Y cylinder(磁柱)结束;
id和System 是一个意思,我们要在fdisk 一个分区时,通过t功能指定id来确认分区类型;
Blocks:表示的意思的确是容量的意思,其单位是K;
Blocks = (相应分区End数值 - 相应分区Start数值)x 单位cylinder(磁柱)的容量 / 1024
单位cylinder的容量 = heads * sectors * 512
以sda1为例:
Blocks = (6202 - 2) * (1 * 63 * 512) = (6202 - 2) * 32256 / 1024 = 195300
由于厂商和操作系统对硬盘容量换上方式不同;
并且分区时会有点儿损失,计算得出的数字和通过fdisk - l看到的数字并不完全吻合

3.估算一个存储设备是否被完全划分
要估算存储设备是否被完全划分,只需要看fdisk - l命令输出内容的sylinders值;
输出内容的开头部分有总的sylinders值,如/dev/sda总的sylinders值为31180;
查看磁盘上一个分区的End值和下一个分区的Start值为连接的数字,若有断续,则两个分区之间有未被分配的存储空间
查看最后一个分区的End值是否等于总的sylinders值,若小于,则最后一个分区之后还有未被分配的存储空间
以/dev/sda为例:
总sylinders值为31108;
三个主分区sda1、sda2、sda3的End/Start值相连接,那么他们之间没有空闲空间;
扩展分区sda3的End值为31108,证明sda1、sda2和扩展分区sda3已经将所有磁盘空间充分利用了
逻辑分区sda5的End值为21706 小于 逻辑分区sda3的End值31108,证明sda5之后还有空间可以继续划分一个新的逻辑分区

4.使用fdisk对硬盘进行分区操作
命令格式:fdisk 设备
------------------------------------------------------------------------------
[root@localhost ~]# fdisk /dev/sda

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 //改变分区类型,id/system值
u change display/entry units
v verify the partition table
w write table to disk and exit //保存分区表,退出
x extra functionality (experts only) //扩展功能,专家模式
--------------------------------------------------------------------------------

4.1 列出当前操作磁盘分区情况,p
--------------------------------------------------------------------------------
Command (m for help): p

Disk /dev/sda: 1003 MB, 1003421696 bytes
1 heads, 63 sectors/track, 31108 cylinders
Units = cylinders of 63 * 512 = 32256 bytes

Device Boot Start End Blocks Id System
/dev/sda1 2 6202 195331+ 83 Linux
/dev/sda2 6203 12403 195331+ c W95 FAT32 (LBA)
/dev/sda3 12404 31108 589207+ 5 Extended
/dev/sda5 12405 21706 293013 7 HPFS/NTFS
--------------------------------------------------------------------------------

4.2 删除一个分区,d
--------------------------------------------------------------------------------
Command (m for help): d //删除分区
Partition number (1-5): 5 //指定要删除的分区编号,这里要删除sda5
Command (m for help): p //再查看一下硬盘分区情况,看是否删除了?
--------------------------------------------------------------------------------
如果不小心删错了,不要紧张,用q不保存退出;千万不要用w(write)。

4.3 增加一个分区,n
--------------------------------------------------------------------------------
Command (m for help): n //增加一个分区
Command action
e extended //扩展分区
p primary partition (1-4) //主分区
e //此处选择扩展分区,e,选择p为主分区
Partition number (1-4): 3 //编号,此处为sda3
First cylinder (12404-31108, default 12404): //开始cylinder值,就是Start值,默认即可
Using default value 12404
Last cylinder or +size or +sizeM or +sizeK (12404-31108, default 31108): +500M //分区大小,此处为500M;
//选默认则分配所有剩余空间
Command (m for help): w //写入,保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks
-----------------------创建扩展分区后,可以新建逻辑分区------------------------
Command (m for help): n
Command action
l logical (5 or over) //l,逻辑分区
p primary partition (1-4)
l
First cylinder (12405-27905, default 12405):
Using default value 12405
Last cylinder or +size or +sizeM or +sizeK (12405-27905, default 27905): +200M

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
--------------------------------------------------------------------------------

4.4 指定分区类型,t
--------------------------------------------------------------------------------
Command (m for help): t //指定分区类型,t
Partition number (1-5): 5 //要改变那个分区,这里选择sda5
Hex code (type L to list codes): l //列出分区类型的id

0 Empty 1c Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid
1 FAT12 1e Hidden W95 FAT1 75 PC/IX be Solaris boot
2 XENIX root 24 NEC DOS 80 Old Minix c1 DRDOS/sec (FAT-
3 XENIX usr 39 Plan 9 81 Minix / old Lin c4 DRDOS/sec (FAT-
4 FAT16 <32M 3c PartitionMagic 82 Linux swap c6 DRDOS/sec (FAT-
5 Extended 40 Venix 80286 83 Linux c7 Syrinx
6 FAT16 41 PPC PReP Boot 84 OS/2 hidden C: da Non-FS data
7 HPFS/NTFS 42 SFS 85 Linux extended db CP/M / CTOS / .
8 AIX 4d QNX4.x 86 NTFS volume set de Dell Utility
9 AIX bootable 4e QNX4.x 2nd part 87 NTFS volume set df BootIt
a OS/2 Boot Manag 4f QNX4.x 3rd part 8e Linux LVM e1 DOS access
b W95 FAT32 50 OnTrack DM 93 Amoeba e3 DOS R/O
c W95 FAT32 (LBA) 51 OnTrack DM6 Aux 94 Amoeba BBT e4 SpeedStor
e W95 FAT16 (LBA) 52 CP/M 9f BSD/OS eb BeOS fs
f W95 Ext‘d (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi ee EFI GPT
10 OPUS 54 OnTrackDM6 a5 FreeBSD ef EFI (FAT-12/16/
11 Hidden FAT12 55 EZ-Drive a6 OpenBSD f0 Linux/PA-RISC b
12 Compaq diagnost 56 Golden Bow a7 NeXTSTEP f1 SpeedStor
14 Hidden FAT16 <3 5c Priam Edisk a8 Darwin UFS f4 SpeedStor
16 Hidden FAT16 61 SpeedStor a9 NetBSD f2 DOS secondary
17 Hidden HPFS/NTF 63 GNU HURD or Sys ab Darwin boot fd Linux raid auto
18 AST SmartSleep 64 Novell Netware b7 BSDI fs fe LANstep
1b Hidden W95 FAT3 65 Novell Netware b8 BSDI swap ff BBT

Hex code (type L to list codes): 7 //此处选择ntfs类型“7 HPFS/NTFS”
Changed system type of partition 5 to 7 (HPFS/NTFS)

Command (m for help): w //写入,保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
--------------------------------------------------------------------------------

5.对分区进行格式化操作
[root@localhost ~]# mkfs.ext3 /dev/sda1 //格式化成ext3
[root@localhost ~]# mkfs.ext2 /dev/sda1 //格式化成ext2
[root@localhost ~]# mkfs.vfat /dev/sda1 //格式化成FAT32

fdisk 使用方法