CentOS7 KVMのゲストOSでディスクサイズを拡張

以下のような環境で、KVMでゲストOSを作成した後にディスクを拡張したい場合の手順です。
なお、ゲストOSに割り当てている仮想HDDは1つだけで、ファイルシステムはXFSです。

OS:CentOS7
Kernel:3.10.0-123.4.2.el7.x86_64
qemu-kvm:1.5.3

1. ゲストOSの電源停止
2. ゲストOSの仮想HDDを拡張
3. ゲストOSを起動
4. fdiskでパーティションを拡張
5. ゲストOSを再起動
6. 物理ボリューム(PV)を拡張
7. 論理ボリューム(LV)を拡張
8. ファイルシステムを拡張

1. ゲストOSの電源停止
動的にディスク拡張できませんので、ゲストOSをシャットダウン(shutdown -h now)します

2. ゲストOSの仮想HDDを拡張
・ゲストOSのディスク状態を確認します
 なお、仮想HDDはシンプロビジョニングで割り当ててますのでdisk sizeは1GBだけとなります。

# qemu-img info /var/lib/libvirt/images/centos7.img
image: /var/lib/libvirt/images/centos7.img
file format: raw
virtual size: 8.0G (8589934592 bytes)
disk size: 1.0G

・ゲストOSのディスクサイズを8GBから10GBへ拡張します

# qemu-img resize /var/lib/libvirt/images/centos7.img 10G
Image resized.

・ゲストOSのディスクが拡張されたか、状態を確認します

# qemu-img info /var/lib/libvirt/images/centos7.img
image: /var/lib/libvirt/images/centos7.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 1.0G

3. ゲストOSを起動

4. fdiskでパーティションを拡張
・ゲストOSの仮想HDD(/dev/sda)でパーティションサイズを変更します
 なお、パーティションの開始セクタを間違えない限りは作業前後でデータが消えたりはしません

# fdisk /dev/sda
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 でヘルプ): p

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

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    16777215     7875584   8e  Linux LVM

コマンド (m でヘルプ): d
パーティション番号 (1,2, default 2): 2
Partition 2 is deleted

コマンド (m でヘルプ): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
パーティション番号 (2-4, default 2): 2
最初 sector (1026048-20971519, 初期値 1026048):1026048
Last sector, +sectors or +size{K,M,G} (1026048-20971519, 初期値 20971519):20971519
Partition 2 of type Linux and of size 9.5 GiB is set

コマンド (m でヘルプ): p

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

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20971519     9972736   83  Linux

コマンド (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)
ディスクを同期しています。

5. ゲストOSを再起動

6. 物理ボリューム(PV)を拡張
・LVMで物理ボリュームの状態を確認します

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos_centos7
  PV Size               7.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1922
  Free PE               0
  Allocated PE          1922
  PV UUID               JG0Pq1-9lsR-XSuP-nlaa-jAbz-PVHd-Dq8FgD
# pvs
  PV         VG             Fmt  Attr PSize PFree
  /dev/sda2  centos_centos7 lvm2 a--  7.51g    0

・物理ボリュームを拡張します

# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

・物理ボリュームを拡大した後の状態を確認します

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos_centos7
  PV Size               9.51 GiB / not usable 2.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              2434
  Free PE               512
  Allocated PE          1922
  PV UUID               JG0Pq1-9lsR-XSuP-nlaa-jAbz-PVHd-Dq8FgD
# pvs
  PV         VG             Fmt  Attr PSize PFree
  /dev/sda2  centos_centos7 lvm2 a--  9.51g 2.00g

7. 論理ボリューム(LV)を拡張
・論理ボリュームの状態を確認します

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos_centos7/swap
  LV Name                swap
  VG Name                centos_centos7
  LV UUID                0R7X3e-2N00-3RPn-QlIA-0yk3-Oh5Y-71DS4d
  LV Write Access        read/write
  LV Creation host, time localhost, 2014-08-06 19:26:13 +0900
  LV Status              available
  # open                 2
  LV Size                820.00 MiB
  Current LE             205
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/centos_centos7/root
  LV Name                root
  VG Name                centos_centos7
  LV UUID                33MAH2-hRjI-XWAV-mtO8-uDHM-mVpF-nBFP1A
  LV Write Access        read/write
  LV Creation host, time localhost, 2014-08-06 19:26:14 +0900
  LV Status              available
  # open                 1
  LV Size                6.71 GiB
  Current LE             1717
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

・空いている物理ボリュームを全て割り振ります

# lvextend -l +100%FREE /dev/centos_centos7/root
  Extending logical volume root to 8.71 GiB
  Logical volume root successfully resized

・拡張後の論理ボリュームの状態を確認します

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos_centos7/swap
  LV Name                swap
  VG Name                centos_centos7
  LV UUID                0R7X3e-2N00-3RPn-QlIA-0yk3-Oh5Y-71DS4d
  LV Write Access        read/write
  LV Creation host, time localhost, 2014-08-06 19:26:13 +0900
  LV Status              available
  # open                 2
  LV Size                820.00 MiB
  Current LE             205
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/centos_centos7/root
  LV Name                root
  VG Name                centos_centos7
  LV UUID                33MAH2-hRjI-XWAV-mtO8-uDHM-mVpF-nBFP1A
  LV Write Access        read/write
  LV Creation host, time localhost, 2014-08-06 19:26:14 +0900
  LV Status              available
  # open                 1
  LV Size                8.71 GiB
  Current LE             2229
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

8. ファイルシステム(XFS)を拡張
・ファイルシステムの使用状況を確認します

# df -h
ファイルシス                    サイズ  使用  残り 使用% マウント位置
/dev/mapper/centos_centos7-root   6.7G  794M  6.0G   12% /
devtmpfs                          491M     0  491M    0% /dev
tmpfs                             498M     0  498M    0% /dev/shm
tmpfs                             498M  6.6M  491M    2% /run
tmpfs                             498M     0  498M    0% /sys/fs/cgroup
/dev/sda1                         497M   97M  401M   20% /boot

・XFSファイルシステムを拡張します
今回は2GBだけの拡張ですが、どんなサイズも一瞬で終わります

# xfs_growfs /dev/centos_centos7/root
meta-data=/dev/mapper/centos_centos7-root isize=256    agcount=4, agsize=439552 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=1758208, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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 1758208 to 2282496

・拡張した後のファイルシステムの使用状況を確認します
 ルートパーティション(/)が拡張されてればOKです

# df -h
ファイルシス                    サイズ  使用  残り 使用% マウント位置
/dev/mapper/centos_centos7-root   8.7G  794M  8.0G    9% /
devtmpfs                          491M     0  491M    0% /dev
tmpfs                             498M     0  498M    0% /dev/shm
tmpfs                             498M  6.6M  491M    2% /run
tmpfs                             498M     0  498M    0% /sys/fs/cgroup
/dev/sda1                         497M   97M  401M   20% /boot