📖

Linuxのディスク拡張&パーティション拡張(仮想マシン)

2021/08/16に公開

前提

  • Hyper-V VM
  • Debian 10

手順

  • Hyper-V等でディスク容量を増やす部分については割愛(GUIでできるから)
  • ディスクの末尾にあるパーティション以外には適用できない

増量されたディスクを認識させる

fdisk -l /dev/sda を実行して、想定した容量が表示されていればこの項目はスキップしてよい。

sudo su -

# sda はディスクによって変更すること
echo 1 > /sys/block/sda/device/rescan 

パーティション作り直し

作り直しといってもGPTのパーティション情報を作り直すだけ。(MBRでもおそらく同じ)

fdisk /dev/sda

# GPT PMBR size mismatch (67108863 != 73400319) will be corrected by write.
# The backup GPT table is not on the end of the device. This problem will be corrected by write.
# 上記のメッセージが表示されるが無視してよい

Command (m for help): p (情報表示)

Disk /dev/sda: 35 GiB, 37580963840 bytes, 73400320 sectors
Disk model: Virtual Disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 202AC417-46EC-4192-AC0F-8EAFBD9326FA

Device       Start      End  Sectors  Size Type
/dev/sda1     2048  1050623  1048576  512M EFI System
/dev/sda2  1050624 67106815 66056192 31.5G Linux filesystem

Command (m for help): d   (パーティション削除)
Partition number (1,2, default 2): (何も入力せずにENTER)

Partition 2 has been deleted.

Command (m for help): n (新しいパーティションを作成)
Partition number (2-128, default 2): (何も入力せずにENTER)
First sector (1050624-73400286, default 1050624): (何も入力せずにENTER)
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-73400286, default 73400286): 

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

Do you want to remove the signature? [Y]es/[N]o: n (EXT4のシグネチャを削除されると困るのでno)

Command (m for help): p

Disk /dev/sda: 35 GiB, 37580963840 bytes, 73400320 sectors
Disk model: Virtual Disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 202AC417-46EC-4192-AC0F-8EAFBD9326FA

Device       Start      End  Sectors  Size Type
/dev/sda1     2048  1050623  1048576  512M EFI System
/dev/sda2  1050624 73400286 72349663 34.5G Linux filesystem
#                  ^^^^^^^^          ^^^^^ 変更部分

Command (m for help): w (書き込み)
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

ファイルシステムの拡張

まだEXT4のファイルシステムのサイズは拡張されていないので拡張する

resize2fs /dev/sda2

Discussion