Linux命令之fdisk

kflj88發表於2019-09-26

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).選單操作說明:

1
2
3
4
5
6
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的區別

1
2
3
4
sd代表的是SCSI,SATA介面硬碟
hd代表IDE介面的硬碟
sda和hda最後面的a都代表的是第一塊硬碟
sda1和hda1則代表的是該硬碟的第一分割槽

(4).例項

1).使用-l選項,列出所有分割槽表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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選項,用扇區編號取代柱面編號顯示所有分割槽表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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.在操作選單下,建立一個新的分割槽

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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.在操作選單下,刪除 一個分割槽

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
[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.在操作選單下,修改分割槽型別

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
51
52
53
54
55
56
57
58
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).對正在使用的磁碟,再次進行分割槽時會出現以下提示

1
2
3
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。

   下面展示完整情況內容

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[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選項更新(好像不能在刪除時使用))。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31516067/viewspace-2658302/,如需轉載,請註明出處,否則將追究法律責任。

相關文章