nemunemu_zzzの日記

備忘録的な何かになればいいな

KVMの拡張!

何かとKVMの拡張をすることがあるのでメモ代わりに...

 

環境は CentOS7

 

1. 拡張前のディスクパーティションを確認

まず, fdiskでパーティションの確認をします.

※ sudoに上がらないと何も表示されない

rootで作業するからsudoは省いてます.

# fdisk -l

 

 出力結果は他にも長々とでるけど今回みたいのは上の方にあるここ

/dev/vda の容量が10.7GBなのが確認できると思います

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008ce71

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    20971519     9972736   8e  Linux LVM

 

2. 仮想領域の拡張

拡張する出来るだけのスペースを空ける

VMを一度止める

# virsh shutdown <VM名>

 

VMの領域を拡張する

ここでは50G容量をプラスしている

# qemu-img resize <VM名> +50G
結果

 

Image resized.

とでればOK

 

VMを起動する

# virsh start <VM名>

VMを起動させながらでも拡張することは出来るが再起動をしないと反映されない

 

 

3.パーティションの設定

では、ちゃんと領域が増えているかを確認してみましょう!

# fdisk -l

拡張前と違い, /dev/vdaの領域が増えていることが確認できましたね

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 size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008ce71

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    20971519     9972736   8e  Linux LVM

 

ファイルシステムの容量を確認するコマンド「df」を使ってみます

# df

dev/mapper/centos-root が拡張されてないことがわかります.
つまり, ファイルシステムには反映されていません.


Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root   8869888 3891612   4978276  44% /
devtmpfs                   498388       0    498388   0% /dev
tmpfs                      508456       0    508456   0% /dev/shm
tmpfs                      508456    6808    501648   2% /run
tmpfs                      508456       0    508456   0% /sys/fs/cgroup
/dev/vda1                  508588  166788    341800  33% /boot
tmpfs                      101692       0    101692   0% /run/user/1000

では, 実際にパーティションテーブルの設定を行ってきます.

サイトによってvdaだったり, sdaだったりするけど仮想なのか物理なのか違いでしかないらしい


# fdisk /dev/vda

すると

Command (m for help):

このように表示されると思います. まずはpを押してみてください.

Command (m for help): p
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 size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008ce71

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    20971519     9972736   8e  Linux LVM

先程までやってたfdisk -lと同じ結果が出てきますね.

pでパーティションの状態がわかります.

次に

 

Command (m for help): n

パーティションを新規作成します.



Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p):  
Using default response p
Partition number (3,4, default 3):
First sector (20971520-125829119, default 20971520):
Using default value 20971520
Last sector, +sectors or +size{K,M,G} (20971520-125829119, default 125829119):
Using default value 125829119
Partition 3 of type Linux and of size 50 GiB is set

拡張されたことがわかりますね.
基本的にdefaultの設定があるのでenterを押すだけでいいのですが, それでも確認はしてください.特に開始セクターがちゃんと前のやつと続いているかなど.
ではもう一度パーティションの状況を確認してみましょう.

Command (m for help): p


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 size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008ce71

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    20971519     9972736   8e  Linux LVM
/dev/vda3        20971520   125829119    52428800   83  Linux

/dev/vda3が追加されているのがわかりますね.
そして/dev/vda3のパーティションタイプをLinux LVMに変更します


Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

確認してもちゃんと変更されていますね
Command (m for help): p

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 size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008ce71

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    20971519     9972736   8e  Linux LVM
/dev/vda3        20971520   125829119    52428800   8e  Linux LVM

最後はwで設定を反映させて終わります

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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)
Syncing disks.

 

ここで一度, 再起動をしておきましょう.

# reboot

4. 物理ボリュームを作成, ボリュームグループを拡大して, 論理ボリュームを拡張する

さっき作った/dev/vda3を物理ボリュームに追加します

# pvcreate /dev/vda3

Physical volume "/dev/vda3" successfully created

と出ればOK

 

物理ボリュームに/dev/vda3が追加されているかを確認する

# pvdisplay

 

ちゃんと/dev/vda3が50Gで入っていることが確認できますね

  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               centos
  PV Size               9.51 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2434
  Free PE               7
  Allocated PE          2427
  PV UUID               ZDBL6e-IAxE-rI3V-E5Tp-fiAC-0Zw2-RYgKYV
   
  "/dev/vda3" is a new physical volume of "50.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vda3
  VG Name               
  PV Size               50.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               NYol9g-mc0r-Vodf-G5k4-XEZd-2Wm0-06xccb
   

 

次にボリュームグループの確認をする.

# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.51 GiB
  PE Size               4.00 MiB
  Total PE              2434
  Alloc PE / Size       2427 / 9.48 GiB
  Free  PE / Size       7 / 28.00 MiB
  VG UUID               ETrNs9-H1yq-Ccvp-YBnV-F5eg-YmbU-RYY5ew

 

次に、ボリュームグループに/dev/vda3を追加して拡張します

vgextend centos /dev/vda3

Volume group "centos" successfully extended

 

が出ればOK

 

論理ボリューム名を確認する.

※今回は/dev/centos/rootだけが必要なので他は省略している

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                TdCtD6-vJuj-8WMS-ahZU-XtOV-dBqJ-4Hluqe
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-09-20 19:54:35 +0900
  LV Status              available
  # open                 1
  LV Size                8.47 GiB
  Current LE             2168
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

 

すでにある論理ボリュームに追加した50G分を当てていきます

# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from 8.47 GiB (2168 extents) to 58.49 GiB (14974 extents). Logical volume root successfully resized.

がでればOK

 

最後にファイルシステムの拡張を行います

ここで少しやっかいなのが, centOS7は今までのresize2fsではなくxfs_growfsに変わったこと

わかっていればそんな詰まることではないのだけれど,僕はここで悩まされました(笑)

# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=555008 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=2220032, 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 2220032 to 15333376

がでればOK


あとはdfで確認すれば

# df
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root  61323264 3893524  57429740   7% /
devtmpfs                   498388       0    498388   0% /dev
tmpfs                      508456       0    508456   0% /dev/shm
tmpfs                      508456    6812    501644   2% /run
tmpfs                      508456       0    508456   0% /sys/fs/cgroup
/dev/vda1                  508588  166788    341800  33% /boot
tmpfs                      101692       0    101692   0% /run/user/1000

拡張されていることがわかりますね!