amazon-linuxでLVMを使ってボリュームを拡張する

Logical Volume Managerを利用してみる。
物理ボリュームを論理ボリュームとして扱えるようにして柔軟なディスク管理を実行できるようにする。

![無題.png](https://qiita-image-store.s3.amazonaws.com/0/105926/210ed844-82f5-de2a-2cdf-b87e89d7d2a6.png)

PV(Physical Volume)から
VG(Volume Group)を作り
LV(Logical Volume)として柔軟に扱えるようにする。

手ごろなサンプルをawsのec2を用いて試します。

# amazon-linuxを準備
* 一番小さなインスタンスでよし

## 初期状態

```
# lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk 
└─xvda1 202:1    0   8G  0 part /

# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        236M   56K  236M   1% /dev
tmpfs           246M     0  246M   0% /dev/shm
/dev/xvda1      7.8G  981M  6.7G  13% /
```

## EBSをアタッチする
awsコンソールから15Gぶんをアタッチする。awsコンソール上では/dev/sdfとして見えるが、SSHでコマンドを発行して確認するとxvdfとして認識されている。

```
# lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk 
└─xvda1 202:1    0   8G  0 part /
xvdf    202:80   0  15G  0 disk 

# fdisk -l
Disk /dev/xvda: 8589 MB, 8589934592 bytes, 16777216 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: gpt


#         Start          End    Size  Type            Name
 1         4096     16777182      8G  Linux filesyste Linux
128         2048         4095      1M  BIOS boot parti BIOS Boot Partition

Disk /dev/xvdf: 16.1 GB, 16106127360 bytes, 31457280 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
```

# LVMとして管理できるようにする

## fdiskコマンドで/dev/xvdf全体を設定する
```
# fdisk /dev/xvdf 
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xa7d87ac1.

Command (m for help): p

Disk /dev/xvdf: 16.1 GB, 16106127360 bytes, 31457280 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: 0xa7d87ac1

[root@ip-172-31-8-150 ~]# fdisk /dev/xvdf 
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xa3a6cc9c.

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

Command (m for help): t
Selected partition 1
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.
Syncing disks.
```

## PV(Physical Volume)を作成する
pvcreate, pvdisplayコマンドを利用します

```
# pvcreate /dev/xvdf1 
  Physical volume "/dev/sdf1" successfully created

# pvdisplay
  "/dev/sdf1" is a new physical volume of "15.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdf1
  VG Name               
  PV Size               15.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               gIK9sh-g23t-n0oB-k4yL-pfUe-Af1u-V7CmpQ
```

## VG(Volume Group)を作成する
vgcreate, vgdisplay, vgscanコマンドを利用します。

```
# vgcreate VGroup /dev/xvdf1                                                                              
  Volume group "VGroup" successfully created
# vgdisplay 
  --- Volume group ---
  VG Name               VGroup
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               15.00 GiB
  PE Size               4.00 MiB
  Total PE              3839
  Alloc PE / Size       0 / 0   
  Free  PE / Size       3839 / 15.00 GiB
  VG UUID               zZ2Sl1-uJr0-6U61-3YBW-FeDu-nv9W-ELGxR7
   
# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "VGroup" using metadata type lvm2
```

## LV(Logical Volume)を作成する
lvcreate, lvdisplay, lvscanコマンドを利用します。

```
# lvcreate --name myspace -l 100%FREE VGroup
  Logical volume "myspace" created.

# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/VGroup/myspace
  LV Name                myspace
  VG Name                VGroup
  LV UUID                wMl2eq-Mkjh-TQip-Ntdd-K6Fo-v7HX-Tsm0Vw
  LV Write Access        read/write
  LV Creation host, time ip-172-31-8-150, 2017-01-11 02:40:46 +0000
  LV Status              available
  # open                 0
  LV Size                15.00 GiB
  Current LE             3839
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
# lvscan
  ACTIVE            '/dev/VGroup/myspace' [15.00 GiB] inherit
```

# 現在の状態を確認する

```
# lsblk
NAME               MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda               202:0    0   8G  0 disk 
└─xvda1            202:1    0   8G  0 part /
xvdf               202:80   0  15G  0 disk 
└─xvdf1            202:81   0  15G  0 part 
  └─VGroup-myspace 253:0    0  15G  0 lvm  

# fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/xvda: 8589 MB, 8589934592 bytes, 16777216 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: gpt

#         Start          End    Size  Type            Name
 1         4096     16777182      8G  Linux filesyste Linux
128         2048         4095      1M  BIOS boot parti BIOS Boot Partition

Disk /dev/xvdf: 16.1 GB, 16106127360 bytes, 31457280 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: 0xa3a6cc9c

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdf1            2048    31457279    15727616   8e  Linux LVM

Disk /dev/mapper/VGroup-myspace: 16.1 GB, 16101933056 bytes, 31449088 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
```


# 利用可能にする
## ファイルシステムを作成する

```
# mkfs.ext4 /dev/VGroup/myspace                                                                           
mke2fs 1.42.12 (29-Aug-2014)
Creating filesystem with 3931136 4k blocks and 983040 inodes
Filesystem UUID: bf4aaeb0-e7e9-43cf-b089-6f70c0a717f3
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done  
```

## LV(Logical Volumne)をマウントする
```
# mkdir /mnt/myspace
# mount /dev/VGroup/myspace /mnt/myspace
# df -h
Filesystem                  Size  Used Avail Use% Mounted on
devtmpfs                    236M   68K  236M   1% /dev
tmpfs                       246M     0  246M   0% /dev/shm
/dev/xvda1                  7.8G  981M  6.7G  13% /
/dev/mapper/VGroup-myspace   15G   38M   14G   1% /mnt/myspace
```

## ラベルを作成しfstabに設定する

```
# e2label /dev/VGroup/myspace /myspace

# cat /etc/fstab                                                                                          
#
LABEL=/     /           ext4    defaults,noatime  1   1
LABEL=/myspace /mnt/myspace ext4 defaults 1 2
tmpfs       /dev/shm    tmpfs   defaults        0   0
devpts      /dev/pts    devpts  gid=5,mode=620  0   0
sysfs       /sys        sysfs   defaults        0   0
proc        /proc       proc    defaults        0   0
```

再起動後もmountされた状態で起動していることを確認する

# LV(Logical Volume)をサイズを変更してみる

/dev/xvdfの全部を使い切っている状態なので下記の順番で試してみる

* LVサイズを縮小する
* LVサイズを元に戻す
* EBSを新規にアタッチしてLVを拡張する

## LVサイズを縮小する

ブロックサイズを確認する

```
# blockdev --getbsz /dev/VGroup/myspace 
4096
```

10Gに変更するのでブロック数を検出する
割り切れない場合はブロックサイズを1切り上げればよいと。

```
# echo "(1 * 1024 * 1024 * 1024 * 10) % 4096" | bc
0
# echo "(1 * 1024 * 1024 * 1024 * 10) / 4096" | bc
2621440
```

確認コマンド(e2fsck)を実行してからブロックサイズを変更(resize2fs)

```
# e2fsck -f /dev/VGroup/myspace                                                                           
e2fsck 1.42.12 (29-Aug-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/myspace: 12/983040 files (0.0% non-contiguous), 104065/3931136 blocks

# resize2fs /dev/VGroup/myspace 2621440
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/VGroup/myspace to 2621440 (4k) blocks.
The filesystem on /dev/VGroup/myspace is now 2621440 (4k) blocks long.

# e2fsck /dev/VGroup/myspace                                                                              
e2fsck 1.42.12 (29-Aug-2014)
/myspace: clean, 12/655360 files, 82544/2621440 blocks
```

LVサイズを縮小する(lvreduce)

```
# lvreduce -L10G /dev/VGroup/myspace 
  WARNING: Reducing active logical volume to 10.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myspace? [y/n]: y
  Size of logical volume VGroup/myspace changed from 15.00 GiB (3839 extents) to 10.00 GiB (2560 extents).
  Logical volume myspace successfully resized
```

マウントして確認してみる

```
# mount /dev/VGroup/myspace /mnt/myspace/

# df -h                                                                                                   
Filesystem                  Size  Used Avail Use% Mounted on
devtmpfs                    236M   68K  236M   1% /dev
tmpfs                       246M     0  246M   0% /dev/shm
/dev/xvda1                  7.8G  981M  6.7G  13% /
/dev/mapper/VGroup-myspace  9.8G   34M  9.2G   1% /mnt/myspace

# lsblk
NAME               MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda               202:0    0   8G  0 disk 
└─xvda1            202:1    0   8G  0 part /
xvdf               202:80   0  15G  0 disk 
└─xvdf1            202:81   0  15G  0 part 
  └─VGroup-myspace 253:0    0  10G  0 lvm  /mnt/myspace
```

## LVサイズを元に戻す

細かい計算は省くｗ

```
# umount /dev/VGroup/myspace

# lvextend -L 15G /dev/VGroup/myspace 
  Insufficient free space: 1280 extents needed, but only 1279 available

# lvextend -L 14.9G /dev/VGroup/myspace 
  Rounding size to boundary between physical extents: 14.90 GiB
  Size of logical volume VGroup/myspace changed from 10.00 GiB (2560 extents) to 14.90 GiB (3815 extents).
  Logical volume myspace successfully resized
```

LVを拡張したのでチェックし(e2fsck)、ブロック数を拡張する(resize2fs)

```
# e2fsck -f /dev/VGroup/myspace 
e2fsck 1.42.12 (29-Aug-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/myspace: 12/655360 files (0.0% non-contiguous), 82544/2621440 blocks
# resize2fs /dev/VGroup/myspace 
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/VGroup/myspace to 3906560 (4k) blocks.
The filesystem on /dev/VGroup/myspace is now 3906560 (4k) blocks long.
```

マウントして確認する

```
# mount /dev/VGroup/myspace /mnt/myspace

# df -h                                                                                                   
Filesystem                  Size  Used Avail Use% Mounted on
devtmpfs                    236M   68K  236M   1% /dev
tmpfs                       246M     0  246M   0% /dev/shm
/dev/xvda1                  7.8G  981M  6.7G  13% /
/dev/mapper/VGroup-myspace   15G   38M   14G   1% /mnt/myspace

# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvda               202:0    0    8G  0 disk 
└─xvda1            202:1    0    8G  0 part /
xvdf               202:80   0   15G  0 disk 
└─xvdf1            202:81   0   15G  0 part 
  └─VGroup-myspace 253:0    0 14.9G  0 lvm  /mnt/myspace
```

## EBSを新規にアタッチしてLVを拡張する
awsコンソール上からEBSを新規にアタッチするawsコンソール上から/dev/sdgとして確認できるがコマンド上だと/dev/xvdgとなる

```
# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvda               202:0    0    8G  0 disk 
└─xvda1            202:1    0    8G  0 part /
xvdf               202:80   0   15G  0 disk 
└─xvdf1            202:81   0   15G  0 part 
  └─VGroup-myspace 253:0    0 14.9G  0 lvm  /mnt/myspace
xvdg               202:96   0    7G  0 disk 

# fdisk -l /dev/xvdg                                                                                      
Disk /dev/xvdg: 7516 MB, 7516192768 bytes, 14680064 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
```

パーティションを作成する。前の方の手順と同じ

```
# fdisk /dev/xvdg 
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x3cc33416.

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

Command (m for help): t
Selected partition 1
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.
Syncing disks.

# fdisk -l /dev/xvdg                                                                                      
Disk /dev/xvdg: 7516 MB, 7516192768 bytes, 14680064 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: 0x3cc33416

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdg1            2048    14680063     7339008   8e  Linux LVM
```

PV(Physical Volume)の作成

```
# pvcreate /dev/xvdg1 
  Physical volume "/dev/sdg1" successfully created

# pvdisplay /dev/xvdg1 
  "/dev/sdg1" is a new physical volume of "7.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdg1
  VG Name               
  PV Size               7.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               7mj34f-MimX-sPAX-dYHg-OpqE-L0O8-xH3hjL
```

VG(Volume Group)を拡張する。新しいEBSぶんの7Gが増加している。

```
# vgextend VGroup /dev/xvdg1
  Volume group "VGroup" successfully extended

# vgdisplay 
  --- Volume group ---
  VG Name               VGroup
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               21.99 GiB
  PE Size               4.00 MiB
  Total PE              5630
  Alloc PE / Size       3815 / 14.90 GiB
  Free  PE / Size       1815 / 7.09 GiB
  VG UUID               zZ2Sl1-uJr0-6U61-3YBW-FeDu-nv9W-ELGxR7

# vgextend VGroup /dev/xvdg1
  Volume group "VGroup" successfully extended

# lvextend -L 21.5G /dev/VGroup/myspace 
  Size of logical volume VGroup/myspace changed from 14.90 GiB (3815 extents) to 21.50 GiB (5504 extents).
  Logical volume myspace successfully resized

# e2fsck -f /dev/VGroup/myspace 
e2fsck 1.42.12 (29-Aug-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/myspace: 12/983040 files (0.0% non-contiguous), 104065/3906560 blocks

# resize2fs /dev/VGroup/myspace 
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/VGroup/myspace to 5636096 (4k) blocks.
The filesystem on /dev/VGroup/myspace is now 5636096 (4k) blocks long.
```

拡張できていることを確認する

```
# mount /dev/VGroup/myspace /mnt/myspace/                                                                 
[root@ip-172-31-8-150 ~]# df -h
Filesystem                  Size  Used Avail Use% Mounted on
devtmpfs                    236M   76K  236M   1% /dev
tmpfs                       246M     0  246M   0% /dev/shm
/dev/xvda1                  7.8G  983M  6.7G  13% /
/dev/mapper/VGroup-myspace   22G   42M   20G   1% /mnt/myspace
[root@ip-172-31-8-150 ~]# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvda               202:0    0    8G  0 disk 
└─xvda1            202:1    0    8G  0 part /
xvdf               202:80   0   15G  0 disk 
└─xvdf1            202:81   0   15G  0 part 
  └─VGroup-myspace 253:0    0 21.5G  0 lvm  /mnt/myspace
xvdg               202:96   0    7G  0 disk 
└─xvdg1            202:97   0    7G  0 part 
  └─VGroup-myspace 253:0    0 21.5G  0 lvm  /mnt/myspace
```