LoginSignup
0
0

More than 5 years have passed since last update.

CentOS7 の仮想マシンで使用している qcow2 形式の仮想ディスクを拡張する

Posted at

はじめに

仮想マシンで使用しているqcow2のディスク容量が足りなくなってしまったので、
拡張してみました。

環境

ホスト
* CentOS7.3

仮想マシン
* HDD 10GB
* CentOS7.3

対象の仮想ディスク /data/hdd/kubernetes/centos-minion-1.qcow2
仮想マシンの IP 192.168.121.65
仮想マシンのユーザ kube
仮想マシンのユーザのパスワード kube
仮想ディスクのデバイス /dev/vda

qcow2 の拡張

qcow2 のディスクを拡張します。ホストマシンの root ユーザーで作業します。拡張対象のファイルは
/data/hdd/kubernetes/centos-minion-1.qcow2 とします。手順は次のとおりです。

  1. qcow2 を raw イメージに変換
  2. raw イメージを拡張
  3. raw イメージを qcow2 に変換
# cd /data/hdd/kubernetes
# qemu-img convert -f qcow2 centos-minion-1.qcow2 -O raw centos-minion-1.raw
# qemu-img resize centos-minion-1.raw +10G
# mv centos-minion-1.qcow2 centos-minion-1.qcow2.bak
# qemu-img convert -f raw centos-minion-1.raw -O qcow2 centos-minion-1.qcow2
# rm centos-minion-1.raw centos-minion-1.qcow2.bak

LVM の設定

qcow2 を拡張しても、仮想マシンで認識されないので設定します。手順は以下のとおりです。

  1. 仮想マシンを起動
  2. root ユーザーでログイン
  3. fdisk でパーティションを作成
    • ファイルシステムのタイプは 8e
  4. リブート
  5. 追加したパーティションにボリュームを作成
  6. LVM を拡張
    • ボリュームグループに拡張したパーティションを追加
    • ファイルシステムを拡張

仮想マシンを起動してログインします。

$ ssh kube@192.168.121.65
kube@192.168.121.65's password: kube
$ sudo su
[sudo] password for kube:

fdisk で パーティションを作成します。

# 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.


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

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

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
Connection to 192.168.121.65 closed by remote host.
Connection to 192.168.121.65 closed.
$ ssh kube@192.168.121.65
kube@192.168.121.65's password: kube
$ sudo su
[sudo] password for kube: kube

マウントの状態を確認します。

# df
Filesystem                            1K-blocks    Used Available Use% Mounted on
/dev/mapper/cl_centos--minion--1-root   8374272 4952316   3421956  60% /
devtmpfs                                 497280       0    497280   0% /dev
tmpfs                                    508244       0    508244   0% /dev/shm
tmpfs                                    508244    7476    500768   2% /run
tmpfs                                    508244       0    508244   0% /sys/fs/cgroup
/dev/vda1                               1038336  172456    865880  17% /boot

追加したパーティションにボリュームを作成します。

# pvcreate /dev/vda3
  Physical volume "/dev/vda3" successfully created

ボリューム名を確認します。

# vgdisplay
  --- Volume group ---
  VG Name               cl_centos-minion-1
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.00 GiB
  PE Size               4.00 MiB
  Total PE              2303
  Alloc PE / Size       2303 / 9.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               riyt4v-I8o0-J8Ek-ZXka-Tllh-pOqG-qP7cBa

vmdisplay で確認した cl_centos-minion-1 に生成したボリュームを追加します。

# vgextend cl_centos-minion-1 /dev/vda3
  Volume group "cl_centos-minion-1" successfully extended

(df で確認した /dev/mapper/cl_centos--minion--1-root を拡張します。)
# lvextend -L 17G /dev/mapper/cl_centos--minion--1-root
  Size of logical volume cl_centos-minion-1/root changed from 8.00 GiB (2047 extents) to 17.00 GiB (4352 extents).
  Logical volume cl_centos-minion-1/root successfully resized.
# xfs_growfs /dev/mapper/cl_centos--minion--1-root
meta-data=/dev/mapper/cl_centos--minion--1-root isize=512    agcount=4, agsize=524032 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2096128, 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 2096128 to 4456448

拡張されたことを確認します。

# df
Filesystem                            1K-blocks    Used Available Use% Mounted on
/dev/mapper/cl_centos--minion--1-root  17815552 4957188  12858364  28% /
devtmpfs                                 497280       0    497280   0% /dev
tmpfs                                    508244       0    508244   0% /dev/shm
tmpfs                                    508244    7476    500768   2% /run
tmpfs                                    508244       0    508244   0% /sys/fs/cgroup
/dev/vda1                               1038336  172456    865880  17% /boot
#
0
0
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
0
0