読者です 読者をやめる 読者になる 読者になる

三蔵開発メモ

iOS、Androidアプリ開発、WEB開発、AWS等の話題を共有していきます。

KVMでHDDを追加する時のメモ

サーバー

CentOSやRHL上で動かすKVMのHDD容量を追加する時の手順。

イメージファイルを増量

まずKVMを動かしているホスト機で、増やしたいVMのイメージを追加する。

$ qemu-img resize vmtest.img +10G

※vmtestというイメージに10G追加したい場合

fdiskでパーティションを増量

VM内にログインし、fdiskコマンドでdisk容量が増えているか確認。

$ssh vmtest
$fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e180d

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       20806     9972736   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

この場合は元が10Gでさらに10G足したので無事増えているので、/dev/vdaのパーティションを区切ります。

$fdisk /dev/vda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
    switch off the mode (command 'c') and change display units to
    sectors (command 'u').

パーティション一覧を確認します。

Command (m for help): p
Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e180d

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       20806     9972736   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

パーティションが2つあるので、3つ目に新たに追加します。

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 3

次にどこのcylinderから始めるかと聞かれるので、この場合は2つ目の最後20806から+1した20807を入力します。

First cylinder (1-41610, default 1): 20807
Last cylinder, +cylinders or +size{K,M,G} (20807-41610, default 41610): 41610

ラストはデフォルトの最後まででOKです。
またpコマンドで無事追加されたのを確認します。

Command (m for help): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e180d

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       20806     9972736   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           20807       41610    10485216   83  Linux

3つ目の追加したパーティションもLVMにします。

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

Command (m for help): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e180d

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       20806     9972736   8e  Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3           20807       41610    10485216   8e  Linux LVM

無事変わったのを確認したらセーブして再起動します。

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

Calling ioctl() to re-read partition table.
$reboot -h now

追加パーティションを物理Vol化&論理Vol拡張

再度VMに入り、追加パーティションを物理ボリューム化します。

$ssh vmtest
$pvcreate /dev/vda3
Physical volume "/dev/vda3" successfully created

追加されたか確認

$pvdisplay
--- Physical volume ---
PV Name               /dev/vda2
VG Name               vg_vmtestimg1
PV Size               9.51 GiB / not usable 3.00 MiB
Allocatable           yes (but full)
PE Size               4.00 MiB
Total PE              2434
Free PE               0
Allocated PE          2434
PV UUID               XCTuf9-07iA-iwnO-CAh0-QT0u-Temb-xymCGY

"/dev/vda3" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/vda3
VG Name
PV Size               10.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               EZN6pR-s8OX-SY1f-faQq-y7T2-VJAe-cnPxgB

VG名を取得

$vgdisplay
--- Volume group ---
VG Name               vg_vmtestimg1
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.51 GiB
PE Size               4.00 MiB
Total PE              2434
Alloc PE / Size       2434 / 9.51 GiB
Free  PE / Size       0 / 0
VG UUID               Wfi6Mw-tK8R-SZmH-xOBm-mmuq-1bmL-hFn40B

VGを拡張&確認

$vgextend vg_vmtestimg1 /dev/vda3
Volume group "vg_vmtestimg1" successfully extended
$vgdisplay
--- Volume group ---
VG Name               vg_vmtestimg1
System ID
Format                lvm2
Metadata Areas        2
Metadata Sequence No  4
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                2
Open LV               2
Max PV                0
Cur PV                2
Act PV                2
VG Size               19.50 GiB
PE Size               4.00 MiB
Total PE              4993
Alloc PE / Size       2434 / 9.51 GiB
Free  PE / Size       2559 / 10.00 GiB
VG UUID               Wfi6Mw-tK8R-SZmH-xOBm-mmuq-1bmL-hFn40B

論理ボリューム名を確認

$lvdisplay
--- Logical volume ---
LV Path                /dev/vg_vmtestimg1/lv_root
LV Name                lv_root
VG Name                vg_vmtestimg1
LV UUID                JtIgxT-mBPj-LO1H-Uc9u-13t3-1gwX-Y11UUR
LV Write Access        read/write
LV Creation host, time dev-pfortune-img1, 2015-09-08 07:16:27 +0900
LV Status              available
# open                 1
LV Size                8.51 GiB
Current LE             2178
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:0

論理ボリュームを追加

$lvextend -l +100%FREE /dev/vg_vmtestimg1/lv_root
Size of logical volume vg_vmtestimg1/lv_root changed from 8.51 GiB (2178 extents) to 18.50 GiB (4737 extents).
Logical volume lv_root successfully resized

$resize2fs /dev/vg_vmtestimg1/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_vmtestimg1/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg_vmtestimg1/lv_root to 4850688 (4k) blocks.
The filesystem on /dev/vg_vmtestimg1/lv_root is now 4850688 blocks long.

centos7の場合は resize2fsではなく、xfs_growfsで拡張を行う

$xfs_growfs /dev/vg_vmtestimg1/lv_root

容量をdfコマンドで確認

$df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/mapper/vg_vmtestimg1-lv_root
                    18968780 1115892  16889024   7% /
tmpfs                   961100       0    961100   0% /dev/shm
/dev/vda1               487652   73701    388351  16% /boot

追加されていれば無事終了です。