35
31

More than 3 years have passed since last update.

VMWare ESXiでUbuntuのファイルシステムサイズをオンライン拡張する

Last updated at Posted at 2021-03-08

VMWare ESXiでHDD容量を増量後、サーバ再起動せずにOS側に反映したい場合の手順を記載します。

環境

  • VMWare ESXi 7
  • OS: Ubuntu 20.04

手順

1.現在の容量の確認

df コマンドで確認できます。
#ゲストOS(Ubuntu)側で実施します
今回の場合、以下のコマンド実行結果の /dev/mapper/ubuntu--vg-ubuntu--lv の 15GBの容量を増量することを目指していきます。

root@test:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               448M     0  448M   0% /dev
tmpfs                               99M  1.2M   97M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   15G  4.2G  9.8G  30% /
tmpfs                              491M     0  491M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              491M     0  491M   0% /sys/fs/cgroup
/dev/sda2                          976M  104M  806M  12% /boot
/dev/loop0                          30M   30M     0 100% /snap/snapd/8542
/dev/loop1                          55M   55M     0 100% /snap/core18/1880
/dev/loop2                          72M   72M     0 100% /snap/lxd/16099
tmpfs                               99M     0   99M   0% /run/user/1000

2.現在のディスク構成の確認

#ゲストOS(Ubuntu)側でparted -l コマンドで確認します。
/dev/sda に物理ディスク(VMWare ESXiなので仮想的な割当ですが)が、/dev/mapper/ubuntu--vg-ubuntu--lv に論理ディスク用の領域が設定されていることがわかります。

Model: VMware Virtual disk (scsi)
Disk /dev/sda: 17.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  2097kB  1049kB                     bios_grub
 2      2097kB  1076MB  1074MB  ext4
 3      1076MB  17.2GB  16.1GB


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  16.1GB  16.1GB  ext4

3.VMWare ESXiでHDD容量の拡張

ESXi側で仮想マシンのHDD容量を増やします。
画面で実施する場合、以下のように仮想マシン > 編集 > ハードディスク1 の容量を増やして保存するとできます。
FireShot Capture 064 - localhost.ase.co.jp_ VMware ESXi - 172.17.0.102.png

4.ゲストOS(Ubuntu)側で設定反映

/dev/sda であれば /sys/class/block/sda/device/rescan に書き込むことでHDD増設をゲストOS(Ubuntu)側に検知させることができます。

root@ubuntu:~# echo 1 > /sys/class/block/sda/device/rescan

5.パーティションの変更

パーティションを変更して物理ディスク設定を変更します。
parted /dev/sda コマンドで /dev/sda のパーティション変更モードに入り、print free で状況確認します。
その際、HDD容量が変わったため警告が出ますが Fix を入力すると表示されます。
以下の場合、16GB→20GBに増やしたので4GB分空き領域が生まれているのがわかります。

root@test:~# parted /dev/sda
GNU Parted 3.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free
Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 8388608 blocks) or continue with the current setting? 
Fix/Ignore? Fix                                                           
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
        17.4kB  1049kB  1031kB  Free Space
 1      1049kB  2097kB  1049kB                     bios_grub
 2      2097kB  1076MB  1074MB  ext4
 3      1076MB  17.2GB  16.1GB
        17.2GB  21.5GB  4296MB  Free Space

ここで、resizepartコマンドで3番の領域を拡張します。
終端の指定が聞かれるので、空き領域の終端の位置を指定します。

(parted) resizepart 3                                                     
End?  [17.2GB]? 21.5GB

この状態で再び print free コマンドを実施すると、空き領域が無くなりディスク領域が結合されていることがわかります。
(ここで空き容量が出ている場合は、unit s で単位をセクタに変更してから上記の作業を行うと良いです。)

※LVMでない場合はparted /dev/sdaの際に論理パーティション(File systemがlogicalのもの)も同様にresizeする必要があります。

(parted) print free
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
        17.4kB  1049kB  1031kB  Free Space
 1      1049kB  2097kB  1049kB                     bios_grub
 2      2097kB  1076MB  1074MB  ext4
 3      1076MB  21.5GB  20.4GB

パーティション作業はもう無いので、quit コマンドでpartedから抜けます。

(parted) q                                                                
Information: You may need to update /etc/fstab.

6.LVM拡張設定

LVMを使用している場合、pvresizeコマンドで物理ディスク領域を拡張設定を行います。

root@test:~# pvresize /dev/sda3
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

pvdisplayコマンドで19GBになっていることが確認できます。

root@test:~# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               <19.00 GiB / not usable 16.50 KiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               1024
  Allocated PE          3839
  PV UUID               JjKDI8-R3a6-selX-j7Az-QV68-HuSr-6u1kNg

7.論理ディスク領域の拡張

今までの作業で物理ディスク領域は拡張できましたが、論理ディスク領域は拡張できていません。
parted -l コマンドで確認すると、/dev/mapper/ubuntu--vg-ubuntu--lv のディスクは16.1GBのままとなっていることがわかります。

root@test:~# parted -l
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  2097kB  1049kB                     bios_grub
 2      2097kB  1076MB  1074MB  ext4
 3      1076MB  21.5GB  20.4GB


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  16.1GB  16.1GB  ext4

lsblklvdisplay コマンドでもsda3領域が19GBなのに対してubuntu--vg-ubuntu--lvの領域は15GBとなってしまっていることが確認できます。

root@test:~# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0                       7:0    0 29.9M  1 loop /snap/snapd/8542
loop1                       7:1    0   55M  1 loop /snap/core18/1880
loop2                       7:2    0 71.3M  1 loop /snap/lxd/16099
sda                         8:0    0   20G  0 disk 
├─sda1                      8:1    0    1M  0 part 
├─sda2                      8:2    0    1G  0 part /boot
└─sda3                      8:3    0   19G  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0   15G  0 lvm  /
sr0                        11:0    1 1024M  0 rom  

root@test:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                Q584Io-Rri5-GLt3-r1br-AOJ7-ujvB-OnMk47
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2021-03-07 08:31:02 +0000
  LV Status              available
  # open                 1
  LV Size                <15.00 GiB
  Current LE             3839
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

そのため、lvextend コマンドで論理ディスク領域を拡張していきます。

root@test:~# lvextend --resizefs -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <15.00 GiB (3839 extents) to <19.00 GiB (4863 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 4979712 (4k) blocks long.

lvdisplayコマンドで確認します。

root@test:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                Q584Io-Rri5-GLt3-r1br-AOJ7-ujvB-OnMk47
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2021-03-07 08:31:02 +0000
  LV Status              available
  # open                 1
  LV Size                <19.00 GiB
  Current LE             4863
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

8.拡張後の容量の確認

df コマンドで確認します。
/dev/mapper/ubuntu--vg-ubuntu--lvの領域が19GBに変更されていることが確認できました。

root@test:~# df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               448M     0  448M   0% /dev
tmpfs                               99M  1.2M   97M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   19G  4.2G   14G  24% /
tmpfs                              491M     0  491M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              491M     0  491M   0% /sys/fs/cgroup
/dev/sda2                          976M  104M  806M  12% /boot
/dev/loop0                          30M   30M     0 100% /snap/snapd/8542
/dev/loop1                          55M   55M     0 100% /snap/core18/1880
/dev/loop2                          72M   72M     0 100% /snap/lxd/16099
tmpfs                               99M     0   99M   0% /run/user/1000

おわりに

VMWare ESXiでUbuntuのファイルシステムサイズをオンライン拡張する方法をまとめました。

35
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
31