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.在操作菜單下,創建一個新的分區

[root@CentOS6 桌面]# 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.在操作菜單下,刪除 一個分區

[root@CentOS6 桌面]# 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。

  下面展示完整情況內容

 

[root@xuexi ~]# 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)
正在同步磁盤。
[root@xuexi ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb1
[root@xuexi ~]# partprobe /dev/sdb  //通知操作系統分區表已更改
[root@xuexi ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb1  /dev/sdb2

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

  注意:其實partprobe是更新分區表,但kpartx其實是更新分區映射(涉及到了鏡像文件,例如.img文件),不過還有partx可以用來更新分區表(-a選項新增,-d選項刪除,-u選項更新(好像不能在刪除時使用))。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM