1. 程式人生 > >怎麼解fdisk切出來的partition比實際的小 « Terrence的宅宅幻想

怎麼解fdisk切出來的partition比實際的小 « Terrence的宅宅幻想

今天跟人要了一批機器,在初始化硬碟的時候遇到了用fdisk切出來的partition比用fdisk -l看的還小很多的情況

花了兩個小時研究終於找到解法,這邊紀錄一下

由於這次要的這批機器是別人用過的機種,在format硬碟的時候遇到了兩個狀況

狀況1: cannot write disk label: Invalid argument

partition的label無法寫入

$ fdisk -l /dev/sdb
Command (m for help): n
Partition number (1-128, default 1):
First sector (34-750157790, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-750157790, default 750157790):
Created partition 1

Command (m for help): w
fdisk: cannot write disk label: Invalid argument

狀況2: 切出來的partition比實際小很多

使用fdisk -l看的時候有1.3TB左右

$ fdisk -l /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sdb: 1438.7 GB, 1438679826432 bytes, 2809921536 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 label type: gpt
Disk identifier: 8A5FB6FB-4D7D-4BDC-8BFE-BCE7994F319F

但實際切出來只有300多GB

$ fdisk /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition number (1-128, default 1):
First sector (34-750157790, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-750157790, default 750157790):
Created partition 1


Command (m for help): p

Disk /dev/sdb: 1438.7 GB, 1438679826432 bytes, 2809921536 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 label type: gpt
Disk identifier: 8A5FB6FB-4D7D-4BDC-8BFE-BCE7994F319F


#         Start          End    Size  Type            Name
 1         2048    750157790  357.7G  Linux filesyste

初步猜測是MBR裡面有一些舊資訊妨礙了fdisk

然後上網查了查詢到有一個更好用的工具gdisk,可以顯示更多資訊然後幫我們重建

試了一下,去切另一顆1.8TB的硬碟

$ yum install -y gdisk
$ gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.6

Caution: invalid backup GPT header, but valid main header; regenerating
backup header from main header.

Partition table scan:
  MBR: hybrid
  BSD: not present
  APM: not present
  GPT: damaged

Found valid MBR and corrupt GPT. Which do you want to use? (Using the
GPT MAY permit recovery of GPT data.)
 1 - MBR
 2 - GPT
 3 - Create blank GPT

Your answer: 1

Warning! Secondary partition table overlaps the last partition by
33 blocks!
You will need to delete this partition or resize it in another utility.

Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-3904896990, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-3904896990, default = 3904896990) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdc: 3904897024 sectors, 1.8 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): 053E1BA2-8455-4B03-A39D-CDA708C1F8C5
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 3904896990
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048      3904896990   1.8 TiB     8300  Linux filesystem

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.

$ mkfs.ext4 /dev/sdc1

成功了!!,切出了原本disk的大小

主要多做了一個動作

Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y

在切partition之前先建立一個新的MBR,就解決了上面的問題