Partition

在 Linux 中擴展 VMware 磁碟上的分區

  • May 28, 2019

我在嘗試擴展磁碟時遇到了一些問題。我在 ESXi 主機上使用 VMware Debian 9 虛擬機。

將虛擬磁碟大小擴展 32 GB 並重新啟動 VM 後,我看到:

bob@apollo:~$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   0   64G  0 disk 
├─sdb1   8:17   0   16G  0 part /var/lib/jenkins
└─sdb2   8:18   0   16G  0 part /var/www

bob@apollo:~$ mount | grep sdb
/dev/sdb2 on /var/www type ext4 (rw,relatime,data=ordered)
/dev/sdb1 on /var/lib/jenkins type ext4 (rw,relatime,data=ordered)

bob@apollo:~$ sudo fdisk /dev/sdb

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

GPT PMBR size mismatch (67108863 != 134217727) will be corrected by w(rite).
GPT PMBR size mismatch (67108863 != 134217727) will be corrected by w(rite).

Command (m for help): p

Disk /dev/sdb: 64 GiB, 68719476736 bytes, 134217728 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: gpt
Disk identifier: 513133B6-1030-427A-8950-E43374665229

Device        Start      End  Sectors Size Type
/dev/sdb1      2048 33554431 33552384  16G Linux filesystem
/dev/sdb2  33554432 67106815 33552384  16G Linux filesystem

我從潛伏在這個網站上學到的技術是使用 fdisk 刪除分區並創建一個新的更大的分區。我希望必要的寫入也能解決我在 fdisk 中看到的 GPT PMBR 大小不匹配問題。

bob@apollo:~$ sudo fdisk /dev/sdb

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

GPT PMBR size mismatch (67108863 != 134217727) will be corrected by w(rite).
GPT PMBR size mismatch (67108863 != 134217727) will be corrected by w(rite).

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

Partition 2 has been deleted.

Command (m for help): n
Partition number (2-128, default 2): 2
First sector (33554432-67108830, default 33554432): 
Last sector, +sectors or +size{K,M,G,T,P} (33554432-67108830, default 67108830): 134217728
Value out of range.
Last sector, +sectors or +size{K,M,G,T,P} (33554432-67108830, default 67108830): 

Created a new partition 2 of type 'Linux filesystem' and of size 16 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): w
GPT PMBR size mismatch (67108863 != 134217727) will be corrected by w(rite).
fdisk: failed to write disklabel: Invalid argument
bob@apollo:~$ 

所以這裡發生了很多事情

  1. /dev/sdb 現在有 134,217,728 個扇區。但“有效範圍”僅上升到舊的 67,108,830。
  2. 當我使用原始大小“重新創建”分區 2 並嘗試寫入更改時,fdisk 因“無效參數”而崩潰。

如何擴展音量

fdisk不擅長使用 GPT 表。首先使用以下命令修復 GPT 表parted

bob@apollo:~$ sudo parted -l

Warning: Not all of the space available to /dev/sdb appears to be used, you can
fix the GPT to use all of the space (an extra 67108864 blocks) or continue with
the current setting? 
Fix/Ignore? F                                                             
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 68.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
1      1049kB  17.2GB  17.2GB  ext4         primary
2      17.2GB  34.4GB  17.2GB  ext4         primary

其次,停止訪問這些磁碟的所有服務並解除安裝磁碟:

bob@apollo:~$ sudo systemctl stop jenkins.service
bob@apollo:~$ sudo systemctl stop apache2.service
bob@apollo:~$ sudo umount /dev/sdb1
bob@apollo:~$ sudo umount /dev/sdb2

第三,像您最初打算的那樣擴展分區:

bob@apollo:~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.29.2).
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/sdb: 64 GiB, 68719476736 bytes, 134217728 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: gpt
Disk identifier: 513133B6-1030-427A-8950-E43374665229

Device        Start       End   Sectors Size Type
/dev/sdb1      2048  33554431  33552384  16G Linux filesystem
/dev/sdb2  33554432 134217694 100663263  48G Linux filesystem

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

Partition 2 has been deleted.

Command (m for help): n
Partition number (2-128, default 2): 2
First sector (33554432-134217694, default 33554432): 
Last sector, +sectors or +size{K,M,G,T,P} (33554432-134217694, default 134217694): 

Created a new partition 2 of type 'Linux filesystem' and of size 48 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

令人驚訝的是,您不需要從 重新掛載任何東西/dev/sdb,它是在寫入 /dev/sdb 之後完成的。如果您查看掛載點,數據應該仍然存在。再次啟動您的服務,讓一切正常工作。

引用自:https://serverfault.com/questions/969086