Vitualbox上のUbuntuのディスク容量の拡張

はじめに

Vitualbox上でディスク容量が足りなくなったため、拡張する方法を記載する。

環境

  • Windows11(ホストOS)
  • Ubuntu22.04.3(ゲストOS、Virtualbox上に構築)
  • Vitualbox 7.0

操作手順

Virtualbox上での操作

ゲストOSをシャットダウンする。次に、[ツール] - [プロパティ]にて、任意の容量に変更する。

今回は以下の変更を実施する。

  • 変更前:10GB
  • 変更後:15GB

実施後、[適用]を押下する。

Linux上での操作

ゲストOSを起動する。 次に、パーティションの拡張、論理ボリュームの拡張の順に行う。 まず、パーティションの拡張を行う。

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              197M  1.1M  196M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  8.1G  6.5G  1.2G  85% /
tmpfs                              983M     0  983M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          1.7G  129M  1.5G   8% /boot
tmpfs                              197M  4.0K  197M   1% /run/user/1000
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/25e040cf8dca6556cb1ac1c4b70f2e709c6f
6a4ab6b42f69321414e296d8a973/shm

# パーティションの確認、今回は/dev/sda3を最大限に拡張する。
$ sudo sfdisk -Vl /dev/sda
GPT PMBR size mismatch (20971519 != 31457279) will be corrected by write.
The backup GPT table is not on the end of the device.
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 sectors
Disk model: VBOX HARDDISK
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: 5451389A-90BC-4F3F-AF84-8CA44F3CE21D

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  3674111  3670016  1.8G Linux filesystem
/dev/sda3  3674112 20969471 17295360  8.2G Linux filesystem

# パーティション拡張
$ sudo growpart /dev/sda 3
CHANGED: partition=3 start=3674112 old: size=17295360 end=20969472 new: size=27783135 end=31457247

#/dev/sda3の拡張を確認
$ sudo sfdisk -Vl /dev/sda
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 sectors
Disk model: VBOX HARDDISK
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: 5451389A-90BC-4F3F-AF84-8CA44F3CE21D

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  3674111  3670016  1.8G Linux filesystem
/dev/sda3  3674112 31457246 27783135 13.2G Linux filesystem
No errors detected.
Header version: 1.0
Using 3 out of 128 partitions.
A total of 2014 free sectors is available in 1 segment.

次に論理ボリュームの拡張を行う。

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              197M  1.1M  196M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  8.1G  6.5G  1.2G  85% /
tmpfs                              983M     0  983M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          1.7G  129M  1.5G   8% /boot
tmpfs                              197M  4.0K  197M   1% /run/user/1000
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/25e040cf8dca6556cb1ac1c4b70f2e709c6f
6a4ab6b42f69321414e296d8a973/shm

# dev/mapper/ubuntu--vg-ubuntu--lvの拡張を行う。5GBを拡張したので、その分だけ拡張を行う。
$ sudo lvextend -L+5G /dev/mapper/ubuntu--vg-ubuntu--lv --resizefs
Size of logical volume ubuntu-vg/ubuntu-lv changed from <10.25 GiB (2623 extents) to <13.25 GiB (3391 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 3472384 (4k) blocks long.

# /dev/mapper/ubuntu--vg-ubuntu--lvの拡張を確認
df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              197M  1.1M  196M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   13G  6.5G  5.9G  53% /
tmpfs                              983M     0  983M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          1.7G  129M  1.5G   8% /boot
shm                                 64M     0   64M   0% /run/k3s/containerd/io.containerd.grpc.v1.cri/sandboxes/f2d84d3eb2a9680e924537bc23e75530c6b8d37ef5b1a6f33e04123f2ebbca65/shm
tmpfs                              197M  4.0K  197M   1% /run/user/1000

参考

www.baeldung.com

knowledge.sakura.ad.jp