tk_ch’s blog

インフラエンジニアのブログ

KVMゲスト(qcow2)のルートボリュームを拡張する(LVM利用)

KVMVMゲストのディスクサイズが足りなくなってきたので、40GBから60GBに拡張する。
LVMを使用している環境のため、以下の流れで拡張する。

  1. VMイメージのサイズを拡張する
  2. 拡張されたデバイスからパーティションを作成する
  3. PV(物理ボリューム)を作成する
  4. PVG(物理ボリュームグループ)を拡張する
  5. LV(論理ボリューム)を拡張する
  6. ファイルシステムを拡張する

環境

VMゲストのOS:CentOS7.7
VMホストのOS: CentOS7.4
libvirt 3.2.0

現状の確認

VMホストで現状を確認する。
/(ルート)配下の空き容量が残りわずかになっている。
また、ファイルシステムのタイプはxfsとなっている。

# df -Th
ファイルシス          タイプ   サイズ  使用  残り 使用% マウント位置
devtmpfs              devtmpfs   3.9G     0  3.9G    0% /dev
tmpfs                 tmpfs      3.9G     0  3.9G    0% /dev/shm
tmpfs                 tmpfs      3.9G   17M  3.9G    1% /run
tmpfs                 tmpfs      3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/vg00-root xfs         38G   37G  1.3G   97% /
/dev/vda1             xfs        509M  165M  345M   33% /boot
tmpfs                 tmpfs      799M     0  799M    0% /run/user/0

fdiskコマンドでVMゲストに接続されているデバイスを確認する。
/dev/vdaというデバイスが接続されており、サイズは42.9GBであることが分かる。

# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000c23ad

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/vda1   *        2048     1050623      524288   83  Linux
/dev/vda2         1050624    41943039    20446208   8e  Linux LVM
/dev/vda3        41943040    83886079    20971520   8e  Linux LVM

Disk /dev/mapper/vg00-root: 40.3 GB, 40256929792 bytes, 78626816 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト

Disk /dev/mapper/vg00-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト

拡張作業

まずは、VMホスト側でVMイメージの拡張を行う。
その後、VMゲスト内で拡張された領域を使用するための設定変更を行う。

1. VMイメージのサイズを拡張する

VMホスト上で作業を開始する。
VMゲストを停止する。

# virsh shutdown dev-k8s-001

VMゲストのイメージをバックアップしておく。

# cp -p /var/lib/libvirt/images/dev-k8s-001.img /var/lib/libvirt/images/dev-k8s-001.img_bk

イメージの情報を表示する。
qcow2フォーマットで、現在のサイズは40GB。

# qemu-img info /var/lib/libvirt/images/dev-k8s-001.img
image: /var/lib/libvirt/images/dev-k8s-001.img
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 38G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

イメージを20GB分拡張する。

# qemu-img resize /var/lib/libvirt/images/dev-k8s-001.img +20G
Image resized.

もう一度確認すると、「virtual size」が60GBに増加している。

# qemu-img info /var/lib/libvirt/images/dev-k8s-001.img
image: /var/lib/libvirt/images/dev-k8s-001.img
file format: qcow2
virtual size: 60G (64424509440 bytes)
disk size: 38G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

VMゲストを起動する。

#virsh start dev-k8s-001

2. 拡張されたデバイスからパーティションを作成する

ここからはVMゲスト内で作業する。
イメージサイズを増やしただけなので、まだディスクの空き容量に変化はない。

# df -Th
ファイルシス          タイプ   サイズ  使用  残り 使用% マウント位置
devtmpfs              devtmpfs   3.9G     0  3.9G    0% /dev
tmpfs                 tmpfs      3.9G     0  3.9G    0% /dev/shm
tmpfs                 tmpfs      3.9G  8.7M  3.9G    1% /run
tmpfs                 tmpfs      3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/vg00-root xfs         38G   37G  1.3G   97% /
/dev/vda1             xfs        509M  165M  345M   33% /boot

/dev/vdaデバイスのサイズは、64.4GBに拡張されている。

# fdisk -l

Disk /dev/vda: 64.4 GB, 64424509440 bytes, 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x000c23ad

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/vda1   *        2048     1050623      524288   83  Linux
/dev/vda2         1050624    41943039    20446208   8e  Linux LVM
/dev/vda3        41943040    83886079    20971520   8e  Linux LVM

Disk /dev/mapper/vg00-root: 40.3 GB, 40256929792 bytes, 78626816 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト

Disk /dev/mapper/vg00-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト

/dev/vdaに追加された領域を使い、/dev/vda4というパーティションを作成する。

# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


コマンド (m でヘルプ): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p
Selected partition 4
最初 sector (83886080-125829119, 初期値 83886080):
初期値 83886080 を使います
Last sector, +sectors or +size{K,M,G} (83886080-125829119, 初期値 125829119):
初期値 125829119 を使います
Partition 4 of type Linux and of size 20 GiB is set

コマンド (m でヘルプ): t
パーティション番号 (1-4, default 4):
Hex code (type L to list all codes): L

 0  空              24  NEC DOS         81  Minix / 古い Li bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux スワップ  c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 隠し C: ド c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux 拡張領域  c7  Syrinx
 5  拡張領域        41  PPC PReP Boot   86  NTFS ボリューム da  非 FS データ
 6  FAT16           42  SFS             87  NTFS ボリューム db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux プレーン  de  Dell ユーティリ
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
 9  AIX ブート可能  4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
 a  OS/2 ブートマネ 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad ハ eb  BeOS fs
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
 f  W95 拡張領域 (L 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC
11  隠し FAT12      56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq 診断     5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  隠し FAT16 <32M 61  SpeedStor       ab  Darwin ブート   f2  DOS セカンダリ
16  隠し FAT16      63  GNU HURD または af  HFS / HFS+      fb  VMware VMFS
17  隠し HPFS/NTFS  64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI スワップ   fd  Linux raid 自動
1b  隠し W95 FAT32  70  DiskSecure Mult bb  隠し Boot Wizar fe  LANstep
1c  隠し W95 FAT32  75  PC/IX           be  Solaris ブート  ff  BBT
1e  隠し W95 FAT16  80  古い Minix
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

コマンド (m でヘルプ): w
パーティションテーブルは変更されました!

ioctl() を呼び出してパーティションテーブルを再読込みします。

WARNING: Re-reading the partition table failed with error 16: デバイスもしくはリソースがビジー状態です.
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)
ディスクを同期しています。

再起動する。

# reboot

/dev/vda4が作成されていることを確認する。

# fdisk -l /dev/vda4

Disk /dev/vda4: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト

3. PV(物理ボリューム)を作成する

現状のPVを確認すると、/dev/vda2と/dev/vda3が存在し、どちらもvg00というPVG(物理ボリュームグループ)に所属することが分かる。

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               vg00
  PV Size               <19.50 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4991
  Free PE               0
  Allocated PE          4991
  PV UUID               o4xqmy-mDPr-FvZd-SbAE-vltK-IN40-RWRSTG

  --- Physical volume ---
  PV Name               /dev/vda3
  VG Name               vg00
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               0
  Allocated PE          5119
  PV UUID               mnU3EI-S1od-yyPu-RV05-MehQ-Nm7O-D8CCCd

/dev/vda4に対応するPVを作成する。

# pvcreate /dev/vda4
  Physical volume "/dev/vda4" successfully created.

/dev/vda4というPVが作成されたことを確認する。

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               vg00
  PV Size               <19.50 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4991
  Free PE               0
  Allocated PE          4991
  PV UUID               o4xqmy-mDPr-FvZd-SbAE-vltK-IN40-RWRSTG

  --- Physical volume ---
  PV Name               /dev/vda3
  VG Name               vg00
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               0
  Allocated PE          5119
  PV UUID               mnU3EI-S1od-yyPu-RV05-MehQ-Nm7O-D8CCCd

  "/dev/vda4" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vda4
  VG Name
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               xcRpNe-NClb-gtc7-TbnT-XPbt-ynBn-tyPB65

4. PVG(物理ボリュームグループ)を拡張する

現状のPVGを確認すると、vg00というPVGがある。
「VG Size」が/dev/vda2と/dev/vda3の合計となっていることが分かる。

# vgdisplay
  --- Volume group ---
  VG Name               vg00
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               39.49 GiB
  PE Size               4.00 MiB
  Total PE              10110
  Alloc PE / Size       10110 / 39.49 GiB
  Free  PE / Size       0 / 0
  VG UUID               b4JFus-7Ndn-4N24-jbll-QzWN-kbAm-Rfo3XB

vg00に/dev/vda4を追加する。

# vgextend vg00 /dev/vda4
  Volume group "vg00" successfully extended

追加した/dev/vda4の分、vg00のサイズが増加している。

# vgdisplay
  --- Volume group ---
  VG Name               vg00
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <59.49 GiB
  PE Size               4.00 MiB
  Total PE              15229
  Alloc PE / Size       10110 / 39.49 GiB
  Free  PE / Size       5119 / <20.00 GiB
  VG UUID               b4JFus-7Ndn-4N24-jbll-QzWN-kbAm-Rfo3XB

5. LV(論理ボリューム)を拡張する

現状のLVを確認する。

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg00/swap
  LV Name                swap
  VG Name                vg00
  LV UUID                4GaC3I-Nj0J-NeTE-0RcN-Y2Xl-ColF-z5In15
  LV Write Access        read/write
  LV Creation host, time dev-k8s-001, 2020-03-16 17:48:14 +0900
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/vg00/root
  LV Name                root
  VG Name                vg00
  LV UUID                YWL2aE-K1Hs-zpQe-tckL-Y1kZ-WKKB-3i71D9
  LV Write Access        read/write
  LV Creation host, time dev-k8s-001, 2020-03-16 17:48:14 +0900
  LV Status              available
  # open                 1
  LV Size                37.49 GiB
  Current LE             9598
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

vg00に追加された領域を全てLV「/dev/vg00/root」に追加する。

# lvextend -l +100%FREE /dev/vg00/root
  Size of logical volume vg00/root changed from 37.49 GiB (9598 extents) to <57.49 GiB (14717 extents).
  Logical volume vg00/root successfully resized.

追加されたことを確認する。

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg00/swap
  LV Name                swap
  VG Name                vg00
  LV UUID                4GaC3I-Nj0J-NeTE-0RcN-Y2Xl-ColF-z5In15
  LV Write Access        read/write
  LV Creation host, time dev-k8s-001, 2020-03-16 17:48:14 +0900
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/vg00/root
  LV Name                root
  VG Name                vg00
  LV UUID                YWL2aE-K1Hs-zpQe-tckL-Y1kZ-WKKB-3i71D9
  LV Write Access        read/write
  LV Creation host, time dev-k8s-001, 2020-03-16 17:48:14 +0900
  LV Status              available
  # open                 1
  LV Size                <57.49 GiB
  Current LE             14717
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

6. ファイルシステムを拡張する

ファイルシステムを拡張する。
XFSファイルシステムであるため、xfs_growthコマンドを使う。

# xfs_growfs /dev/vg00/root
meta-data=/dev/mapper/vg00-root  isize=512    agcount=9, agsize=1146624 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=9828352, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 9828352 to 15070208

ディスク容量を確認すると、/(ルート)配下が20GB増えている。

# df -Th
ファイルシス          タイプ   サイズ  使用  残り 使用% マウント位置
devtmpfs              devtmpfs   3.9G     0  3.9G    0% /dev
tmpfs                 tmpfs      3.9G     0  3.9G    0% /dev/shm
tmpfs                 tmpfs      3.9G  8.8M  3.9G    1% /run
tmpfs                 tmpfs      3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/vg00-root xfs         58G   35G   24G   60% /
/dev/vda1             xfs        509M  165M  345M   33% /boot

参考文献

【Linuxの基礎知識】LVMとは?LVMを理解しよう!
3.4. XFS ファイルシステムのサイズの拡大