天天看點

​​Linux之fdisk​​​指令

Linux之fdisk​​指令

fdisk

fdisk [選項] <disk>  改變分區表 

fdisk [選項] –l <disk>  列出所有分區表

fdisk –s <partition(分區編号)>  以分區塊為機關,給出指定分區的大小

  這是一個建立和維護分區的指令,相容DOS類型的分區表、BSD或SUN類型的磁盤清單。注意fdisk不支援2T以上的硬碟分區,此時需要使用gdisk。

相關了解:

  磁頭數(Heads)表示硬碟有幾個磁頭,也就是有幾面盤片,一個硬碟最多有255個磁頭

  柱面數(Cylinders)表示硬碟每面盤片上有幾條磁道

  扇區數(Sectors)表示每條磁道上有幾個扇區,每條磁道最多有63個扇區

(1).選項

1

2

3

4

5

6

​-b <​

​​

​size​

​>  指定扇區大小(512,1024,2048或4096 B)​

​-c  關閉DOS相容模式​

​-u <​

​size​

​>  以扇區編号取代柱面編号來表示每個分區的起始位址,一般與-l選項聯合使用​

​-C <​

​number​

​>  指定柱面編号​

​-H <​

​number​

​>  指定磁頭編号​

​-S <​

​number​

​>  指定磁道扇區編号​

(2).菜單操作說明:

7

8

9

10

11

12

13

14

15

16

​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選項,列出所有分區表

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

​/*盤/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.在操作菜單下,建立一個新的分區

51

52

53

54

55

56

57

58

59

60

​[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​

繼續閱讀