Linux命令之fdisk详解程序员

fdisk

fdisk [选项] <disk>  改变分区表 

fdisk [选项] –l <disk>  列出所有分区表

fdisk –s <partition(分区编号)>  以分区块为单位,给出指定分区的大小

  这是一个创建和维护分区的命令,兼容DOS类型的分区表、BSD或SUN类型的磁盘列表。注意fdisk不支持2T以上的硬盘分区,此时需要使用gdisk。

相关了解:

  磁头数(Heads)表示硬盘有几个磁头,也就是有几面盘片,一个硬盘最多有255个磁头

  柱面数(Cylinders)表示硬盘每面盘片上有几条磁道

  扇区数(Sectors)表示每条磁道上有几个扇区,每条磁道最多有63个扇区

(1).选项

-b <size>  指定扇区大小(512,1024,2048或4096 B) 
-c  关闭DOS兼容模式 
-u <size>  以扇区编号取代柱面编号来表示每个分区的起始地址,一般与-l选项联合使用 
-C <number>  指定柱面编号 
-H <number>  指定磁头编号 
-S <number>  指定磁道扇区编号

(2).菜单操作说明:

a 设置可引导标记(活动分区/引导分区之间切换) 
b 编辑BSD磁盘标签 
c 设置DOS操作系统兼容标记(兼容/不兼容之间切换) 
d 删除一个分区 
l 显示已知的分区类型,其中82为Linux swap分区,83为Linux分区 
m 显示帮助信息 
n 增加一个新的分区 
o 创建一个新的空白的DOS分区表 
p 显示磁盘当前的分区表 
q 退出fdisk程序,不保存任何修改 
s 创建一个新的空白的Sun磁盘标签 
t 改变一个分区的系统ID,就是改变分区类型(比如把Linux Swap分区改为Linux分区) 
u 改变显示或输入单位 
v 验证磁盘分区表 
w 将分区表写入磁盘并退出(保存并退出) 
x 额外功能(专家级)

(3).扩展

sda和hda的区别

sd代表的是SCSI,SATA接口硬盘 
hd代表IDE接口的硬盘 
sda和hda最后面的a都代表的是第一块硬盘 
sda1和hda1则代表的是该硬盘的第一分区

(4).实例

1).使用-l选项,列出所有分区表

/*盘/dev/sda:107.4G,107374182400字节(B),255个磁头,63个扇区,13054柱面(磁道) 
* 1单位=16065柱面(磁道)*512=8225280字节(B) 
* 扇区大小(逻辑/物理):512字节(B)/512字节(B) 
*输入/输出大小(最小/最佳):512字节(B)/512字节(B) 
*磁盘标识:0x00079700 
*/ 
Disk /dev/sda: 107.4 GB, 107374182400 bytes 
255 heads, 63 sectors/track, 13054 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: 0x00079700 
 
/*设备     启动标识  开始位置 结束位置  分区块的大小  分区ID  系统(分区类型)  
* /dev/sda1  *     1     64     512000     83    Linux 
* 分区1不以柱面边界结束 
* /dev/sda2        64    13055   104344576   8e    Linux LVM 
*/ 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sda1   *           1          64      512000   83  Linux 
Partition 1 does not end on cylinder boundary. 
/dev/sda2              64       13055   104344576   8e  Linux LVM 
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes 
255 heads, 63 sectors/track, 2610 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 
 
Disk /dev/mapper/vg_centos6-lv_root: 53.7 GB, 53687091200 bytes 
255 heads, 63 sectors/track, 6527 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 
 
Disk /dev/mapper/vg_centos6-lv_swap: 4194 MB, 4194304000 bytes 
255 heads, 63 sectors/track, 509 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 
 
Disk /dev/mapper/vg_centos6-lv_home: 49.0 GB, 48964304896 bytes 
255 heads, 63 sectors/track, 5952 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

2).使用-l和-u选项,用扇区编号取代柱面编号显示所有分区表

Disk /dev/sda: 107.4 GB, 107374182400 bytes 
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 sectors 
Units = sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0x00079700 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sda1   *        2048     1026047      512000   83  Linux 
Partition 1 does not end on cylinder boundary. 
/dev/sda2         1026048   209715199   104344576   8e  Linux LVM 
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes 
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors 
Units = sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0x00000000 
 
Disk /dev/mapper/vg_centos6-lv_root: 53.7 GB, 53687091200 bytes 
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 sectors 
Units = sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0x00000000 
 
Disk /dev/mapper/vg_centos6-lv_swap: 4194 MB, 4194304000 bytes 
255 heads, 63 sectors/track, 509 cylinders, total 8192000 sectors 
Units = sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0x00000000 
 
Disk /dev/mapper/vg_centos6-lv_home: 49.0 GB, 48964304896 bytes 
255 heads, 63 sectors/track, 5952 cylinders, total 95633408 sectors 
Units = sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disk identifier: 0x00000000

3).你们也看到了,我的/dev/sdb还没有开始使用,下面就用这个硬盘简单的演示下fdisk主要用到的操作菜单

  1.在操作菜单下,创建一个新的分区

[[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 0xe8517189. 
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): n 
Command action 
   e   extended            //扩展分区,创建扩展分区会占用一个主分区,并且只有创建扩展分区后才能创建逻辑分区 
   p   primary partition (1-4)   //主分区,最多创建4个 
e 
Partition number (1-4): 1 
First cylinder (1-2610, default 1):   //起始柱面,默认为1 
Using default value 1 
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610):   //终止柱面,默认为最后一个数 
Using default value 2610 
 
Command (m for help): n 
Command action 
   l   logical (5 or over)    //逻辑分区 
   p   primary partition (1-4)  //主分区 
l 
First cylinder (1-2610, default 1): 1 
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G  //终止柱面不仅可以用柱面数表示,还可以用+[数值][单位]的格式表示 
 
Command (m for help): n 
Command action 
   l   logical (5 or over) 
   p   primary partition (1-4) 
l 
First cylinder (1307-2610, default 1307):  
Using default value 1307 
Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2610): 
Using default value 2610 
 
Command (m for help): p 
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes 
255 heads, 63 sectors/track, 2610 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: 0xe8517189 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1               1        2610    20964793+   5  Extended 
/dev/sdb5               1        1306    10490382   83  Linux 
/dev/sdb6            1307        2610    10474348+  83  Linux 
 
Command (m for help):  w      //写入磁盘,保存并退出,否则之前的操作都无效。如果不想写入磁盘就使用q 
The partition table has been altered! 
 
Calling ioctl() to re-read partition table. 
Syncing disks.

  2.在操作菜单下,删除 一个分区

[[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: 21.5 GB, 21474836480 bytes 
255 heads, 63 sectors/track, 2610 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: 0xe8517189 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1               1        2610    20964793+   5  Extended 
/dev/sdb5               1        1306    10490382   83  Linux 
/dev/sdb6            1307        2610    10474348+  83  Linux 
 
Command (m for help): d 
Partition number (1-6): 6  //这里输入的始分区编号,如果删除扩展分区,那扩展分区下的逻辑分区也会被删除 
 
Command (m for help): p 
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes 
255 heads, 63 sectors/track, 2610 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: 0xe8517189 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1               1        2610    20964793+   5  Extended 
/dev/sdb5               1        1306    10490382   83  Linux 
 
Command (m for help): w 
The partition table has been altered! 
 
Calling ioctl() to re-read partition table. 
Syncing disks. 

   3.在操作菜单下,修改分区类型

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): t 
Partition number (1-5): 5  //操作哪一个分区 
Hex code (type L to list codes): L  //这里会提示你使用L查看类型列表 
 
 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris         
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT- 
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT- 
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT- 
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx          
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data     
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / . 
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility    
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt          
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access      
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O         
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor       
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs         
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT             
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/ 
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b 
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor       
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor       
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary   
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS     
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE  
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto 
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep         
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT             
1e  Hidden W95 FAT1 
Hex code (type L to list codes): b 
Changed system type of partition 5 to b (W95 FAT32) 
 
Command (m for help): p  //可以看到由原来的Linux改成了W95 FAT32 
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes 
255 heads, 63 sectors/track, 2610 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: 0xe8517189 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1               1        2610    20964793+   5  Extended 
/dev/sdb5               1        1306    10490382    b  W95 FAT32 
 
Command (m for help): w 
The partition table has been altered! 
 
Calling ioctl() to re-read partition table. 
 
WARNING: If you have created or modified any DOS 6.x 
partitions, please see the fdisk manual page for additional 
information. 
Syncing disks. 

4).对正在使用的磁盘,再次进行分区时会出现以下提示

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. 
The kernel still uses the old table. The new table will be used at 
the next reboot or after you run partprobe(8) or kpartx(8) 

  注意此时分区并不会立刻生效,可以重启系统,也可以使用提示的命令partprobe或kpartx。

  下面展示完整情况内容

 

[[email protected] ~]# fdisk /dev/sdb 
欢迎使用 fdisk (util-linux 2.23.2)。 
 
更改将停留在内存中,直到您决定将更改写入磁盘。 
使用写入命令前请三思。 
 
命令(输入 m 获取帮助):n  //新建分区 
Partition type: 
   p   primary (1 primary, 0 extended, 3 free) 
   e   extended 
Select (default p): p  //创建主分区 
分区号 (2-4,默认 2): 
起始 扇区 (2099200-4194303,默认为 2099200): 
将使用默认值 2099200 
Last 扇区, +扇区 or +size{K,M,G} (2099200-4194303,默认为 4194303): 
将使用默认值 4194303 
分区 2 已设置为 Linux 类型,大小设为 1023 MiB 
 
命令(输入 m 获取帮助):w  //写入磁盘 
The partition table has been altered! 
 
Calling ioctl() to re-read partition table. 
 
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. 
The kernel still uses the old table. The new table will be used at 
the next reboot or after you run partprobe(8) or kpartx(8) 
正在同步磁盘。 
[[email protected] ~]# ls /dev/sdb* 
/dev/sdb  /dev/sdb1 
[[email protected] ~]# partprobe /dev/sdb  //通知操作系统分区表已更改 
[[email protected] ~]# ls /dev/sdb* 
/dev/sdb  /dev/sdb1  /dev/sdb2 

   注意:能reboot重启最好重启,不行再使用命令更新

  注意:其实partprobe是更新分区表,但kpartx其实是更新分区映射(涉及到了镜像文件,例如.img文件),不过还有partx可以用来更新分区表(-a选项新增,-d选项删除,-u选项更新(好像不能在删除时使用))。

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/2849.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论