[Ubuntu] vm主機做 resize 時遇到的問題

前情提要:

公司舊有主機當初用 vmware 建立時分配到 100G,然而實際上能用的只有(/dev/sda1)50G,但時間久了,容量不夠用,這時候從 vmware 介面去加大硬碟之後,一樣沒辦法將 /dev/sda1 變大。

# fdisk -l







處理方法與過程:
依照以前在 aws 上面的處理經驗,理論上是使用
growpart /dev/sda 1
resize2fs /dev/sda1

就可以解決,然侯事情不是我想的這麼簡單,過程中都出現 NO CHANGE
看起來是行不通了,於是!
查了一下各路文章發現,需要把 partition table 整個洗掉,重新建立新的 partition table。

過程:
第一件最重要的事情,就是備份。如果不備份,失敗的話就可以跑路了XD

再來使用指令

# fdisk /dev/sda

使用了之後,會進去 fdisk 的互動模式,接著介紹指令
p => 印出目前的指定硬碟的分割表
d => 刪除指定的分割,ex: 輸入1就代表刪除 /dev/sda1 的 partition 的位置
(使用 d 的話,後面會問你要刪除哪個硬碟裝置代號)
n => 新增指定的分割,ex: 輸入1就代表新增 /dev/sda1 的 partition 的位置
(使用 n 的話,後面會問你這個新的硬碟裝置代號,sector(磁區) 要從哪裡開始)
w => 寫入 partition table
以下是大概會出現

moses@xxx:~$ sudo su -
root@xxx:~# fdisk -l
Disk /dev/sda: 150 GiB, 161061273600 bytes, 314572800 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
Disklabel type: dos
Disk identifier: 0x9e13d85c

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048 102856703 102854656   49G 83 Linux
/dev/sda2       102858750 104855551   1996802  975M  5 Extended
/dev/sda5       102858752 104855551   1996800  975M 82 Linux swap / Solaris
=============================================================================
### 列出目前分割區狀態

root@xxx:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sda: 150 GiB, 161061273600 bytes, 314572800 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
Disklabel type: dos
Disk identifier: 0x9e13d85c

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048 102856703 102854656   49G 83 Linux
/dev/sda2       102858750 104855551   1996802  975M  5 Extended
/dev/sda5       102858752 104855551   1996800  975M 82 Linux swap / Solaris
=============================================================================
### 刪除所有分割區,因為不全部清空的話,/dev/sda1 會被 /dev/sda2 的分割表卡住

Command (m for help): d
Partition number (1,2,5, default 5): 5

Partition 5 has been deleted.

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
=============================================================================
### 確認是否刪除所有分割區

Command (m for help): p
Disk /dev/sda: 150 GiB, 161061273600 bytes, 314572800 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
Disklabel type: dos
Disk identifier: 0x9e13d85c
=============================================================================
### 產生 /dev/sda1,sector從2048開始,並設定大小為 100G

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-314572799, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-314572799, default 314572799): +100G  

Created a new partition 1 of type 'Linux' and of size 100 GiB.
=============================================================================
### 產生 /dev/sda2,sector從/dev/sda1 後面開始,所以使用預設值,並設定大小為 10G

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2): [這邊直接按 enter, 使用預設值]
First sector (209717248-314572799, default 209717248): [這邊直接按 enter, 使用預設值]
Last sector, +sectors or +size{K,M,G,T,P} (209717248-314572799, default 314572799): +10G

Created a new partition 2 of type 'Extended' and of size 10 GiB.
=============================================================================
### 產生 /dev/sda5,sector從/dev/sda2 後面開始,所以使用預設值

Command (m for help): n
Partition type
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l

Adding logical partition 5
First sector (209719296-230688767, default 209719296): [這邊直接按 enter, 使用預設值]
Last sector, +sectors or +size{K,M,G,T,P} (209719296-230688767, default 230688767): 

Created a new partition 5 of type 'Linux' and of size 10 GiB.
=============================================================================
### 寫入 partition table

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

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).
=============================================================================



寫入 partition table 之後,一定要重開機
# reboot

這時候開機會有點慢是正常的,因為 Linux 會重新處理 partition table 的變動
接下來使用
partprobe /dev/sda 
(partprobe 用來重新初始化記憶體中內核的partition table 的資訊)

resize2fs /dev/sda1

完成

Ref: 


留言

這個網誌中的熱門文章

[翻譯] 介紹現代網路負載平衡與代理伺服器

[MySQL] schema 與資料類型優化