LVM ディスク増設手順

# 背景
業務でローカルディスクが足りなくなる事象が発生した為、
ディスクの容量追加をしてみました。
LVMだったので、LVMでの増設手順となります。

# そもそもLVMって？
LVM（logical volume manager）とは、
複数のディスクをひとつボリュームグループにまとめ、論理ボリューム（LV）として扱うことのできる機能のこと。
論理的にディスクを扱うため、オンラインでの作業も可能ですね！

変更前のLVM構成はこんな感じ！
![1.jpeg](https://qiita-image-store.s3.amazonaws.com/0/66825/3b5cb12f-7f38-cbe0-8a77-fc84dd284285.jpeg)



今回やることは、上記から、物理デバイス「xvdf1」をLVMグループに追加して、
LVMボリュームも拡張する。手順を記載します。
図にまとめるとこんな感じ！

![2.jpeg](https://qiita-image-store.s3.amazonaws.com/0/66825/aa1ef2a4-41f0-ef71-48a3-f54bb218cf4d.jpeg)



#前提
　・増設するディスクはOSでデバイス認識していること。

# 手順
###１． 状態確認
#####１．１．rootユーザにスイッチする。
```
$ su -
```
#####１．２．増設する物理デバイスを確認する。
```
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       14G   11G  2.6G  80% /　　　　　　　//容量が足りないボリューム。増設します！
tmpfs                 498M     0  498M   0% /dev/shm
/dev/xvda1            477M   47M  406M  11% /boot
```

```
# fdisk -l

Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0002e2c8

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              64        2089    16264192   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 14.9 GB, 14935916544 bytes　　　//既存LVMボリューム
255 heads, 63 sectors/track, 1815 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_swap: 1715 MB, 1715470336 bytes　　　　　
255 heads, 63 sectors/track, 208 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/xvdf: 32.2 GB, 32212254720 bytes　　　　　　　　//既存物理デバイス
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


```


###２． 新規追加する物理デバイスのパーティション設定
#####２．１．新規追加する物理デバイスのfdisk
```
# fdisk /dev/xvdf
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x7c1bfbf1.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-3916, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-3916, default 3916): 
Using default value 3916

Command (m for help): 
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/xvdf: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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 identifier: 0x7c1bfbf1

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdf1              63    62910539    31455238+  8e  Linux LVM

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

Calling ioctl() to re-read partition table.
Syncing disks.

```


#####２．２．パーティション作成確認
```
# fdisk -l
・
・
    Device Boot      Start         End      Blocks   Id  System
/dev/xvdf1               1        3916    31455238+  8e  Linux LVM
```

※ **System** が**Linux LVM**であることが重要です！


###３． LVMに物理デバイス増設
#####３．１．LVMのデバイス状況確認(初期状態確認)
```
# pvscan
  PV /dev/xvda2   VG VolGroup   lvm2 [15.51 GiB / 0    free]　　　　　//今は1個しかない！
  Total: 1 [15.51 GiB] / in use: 1 [15.51 GiB] / in no VG: 0 [0   ]
```
#####３．２．LVMに物理デバイス認識
```
# pvcreate /dev/xvdf1
  Physical volume "/dev/xvdf1" successfully created

```
#####３．３．LVMにデバイスが認識されたことを確認
```
# pvscan
  PV /dev/xvda2   VG VolGroup   lvm2 [15.51 GiB / 0    free]
  PV /dev/xvdf1                 lvm2 [30.00 GiB]　　　　　　　//認識させた！
  Total: 2 [45.51 GiB] / in use: 1 [15.51 GiB] / in no VG: 1 [30.00 GiB]　

```

#####３．４．LVMボリュームグループ状態確認
```
# vgdisplay -v
    DEGRADED MODE. Incomplete RAID LVs will be processed.
    Finding all volume groups
    Finding volume group "VolGroup"
  --- Volume group ---
  VG Name               VolGroup　　　　　//LVMのボリュームグループ
  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               15.51 GiB
  PE Size               4.00 MiB
  Total PE              3970
  Alloc PE / Size       3970 / 15.51 GiB　　　　//現状のボリュームサイズ
  Free  PE / Size       0 / 0　　　　　　　　　　
  VG UUID               4431gC-stvB-LEil-fhM7-WT2F-y4DG-36Uf6w
   
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_root　　　//LVMボリューム(/)
  LV Name                lv_root
  VG Name                VolGroup
  LV UUID                UTPDa9-4C6b-S0Yk-npmP-HMOM-ZGs0-TctA1e
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-03-05 19:27:09 +0900
  LV Status              available
  # open                 1
  LV Size                13.91 GiB
  Current LE             3561
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_swap　　　//LVMボリューム(swap)
  LV Name                lv_swap
  VG Name                VolGroup
  LV UUID                vlCsmT-W0HL-q3wo-3tWf-jL2A-e2mB-C6zAGl
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-03-05 19:27:36 +0900
  LV Status              available
  # open                 1
  LV Size                1.60 GiB
  Current LE             409
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Physical volumes ---
  PV Name               /dev/xvda2　　　　　　//物理デバイス
  PV UUID               QONnAQ-jouG-UzVx-t4OD-jNFQ-ZEXp-6O30i0
  PV Status             allocatable
  Total PE / Free PE    3970 / 0
   
```

#####３．５．LVMのVolume groupへ物理デバイス追加
```
# vgextend VolGroup /dev/xvdf1　
  Volume group "VolGroup" successfully extended

```

#####３．６．LVMの状態確認
```
# vgdisplay -v
    DEGRADED MODE. Incomplete RAID LVs will be processed.
    Finding all volume groups
    Finding volume group "VolGroup"
  --- Volume group ---
  VG Name               VolGroup
  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               45.50 GiB
  PE Size               4.00 MiB
  Total PE              11649
  Alloc PE / Size       3970 / 15.51 GiB
  Free  PE / Size       7679 / 30.00 GiB
  VG UUID               4431gC-stvB-LEil-fhM7-WT2F-y4DG-36Uf6w
   
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_root
  LV Name                lv_root
  VG Name                VolGroup
  LV UUID                UTPDa9-4C6b-S0Yk-npmP-HMOM-ZGs0-TctA1e
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-03-05 19:27:09 +0900
  LV Status              available
  # open                 1
  LV Size                13.91 GiB
  Current LE             3561
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_swap
  LV Name                lv_swap
  VG Name                VolGroup
  LV UUID                vlCsmT-W0HL-q3wo-3tWf-jL2A-e2mB-C6zAGl
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-03-05 19:27:36 +0900
  LV Status              available
  # open                 1
  LV Size                1.60 GiB
  Current LE             409
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Physical volumes ---
  PV Name               /dev/xvda2     
  PV UUID               QONnAQ-jouG-UzVx-t4OD-jNFQ-ZEXp-6O30i0
  PV Status             allocatable
  Total PE / Free PE    3970 / 0
   
  PV Name               /dev/xvdf1　　　　　　　　　　　　　　//新規物理デバイス認識
  PV UUID               Y32Nsg-YHoG-mPvV-EvMx-h5tr-eD3x-t1HwXd
  PV Status             allocatable
  Total PE / Free PE    7679 / 7679

```
   

#####３．７．LVMのボリューム拡張
```
# lvextend -l +100%FREE /dev/VolGroup/lv_root
  Size of logical volume VolGroup/lv_root changed from 13.91 GiB (3561 extents) to 43.91 GiB (11240 extents).
  Logical volume lv_root successfully resized
```

#####３．８．LVMのボリューム拡張確認 (vgdisplay -v)
```
# vgdisplay -v 
    DEGRADED MODE. Incomplete RAID LVs will be processed.
    Finding all volume groups
    Finding volume group "VolGroup"
  --- Volume group ---
  VG Name               VolGroup
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  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               45.50 GiB
  PE Size               4.00 MiB
  Total PE              11649
  Alloc PE / Size       11649 / 45.50 GiB　　　　　　//ディスク増設
  Free  PE / Size       0 / 0   
  VG UUID               4431gC-stvB-LEil-fhM7-WT2F-y4DG-36Uf6w
   
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_root
  LV Name                lv_root
  VG Name                VolGroup
  LV UUID                UTPDa9-4C6b-S0Yk-npmP-HMOM-ZGs0-TctA1e
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-03-05 19:27:09 +0900
  LV Status              available
  # open                 1
  LV Size                43.91 GiB
  Current LE             11240
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_swap
  LV Name                lv_swap
  VG Name                VolGroup
  LV UUID                vlCsmT-W0HL-q3wo-3tWf-jL2A-e2mB-C6zAGl
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-03-05 19:27:36 +0900
  LV Status              available
  # open                 1
  LV Size                1.60 GiB
  Current LE             409
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Physical volumes ---
  PV Name               /dev/xvda2     
  PV UUID               QONnAQ-jouG-UzVx-t4OD-jNFQ-ZEXp-6O30i0
  PV Status             allocatable
  Total PE / Free PE    3970 / 0
   
  PV Name               /dev/xvdf1     
  PV UUID               Y32Nsg-YHoG-mPvV-EvMx-h5tr-eD3x-t1HwXd
  PV Status             allocatable
  Total PE / Free PE    7679 / 0
```

#####３．９．LVMボリュームのフォーマット
```
# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 3
Performing an on-line resize of /dev/VolGroup/lv_root to 11509760 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 11509760 blocks long.
```

#####３．１０．LVMボリュームの拡張確認(df -h)
```
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       44G   11G   31G  26% /　　　　　　　　　//ディスク増設
tmpfs                 498M     0  498M   0% /dev/shm
/dev/xvda1            477M   47M  406M  11% /boot
```