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

はわわーっ

はわわわわっ

LVMを使う

linux lvm

LVMの使い方とかよく知らなかったので、調べながらいろいろやってみる。こういうときvirtualboxとか便利ですね。

まず、用語から。
物理ボリューム(physical volume):ハードディスクに作ったLVM用のパーティションのこと。
ボリュームグループ(volume group):物理ボリュームをまとめて管理するための管理単位のこと。
論理ボリューム(logical volume):ボリュームグループに作られる仮想的なパーティションのようなもの。

だいたいこんな感じらしい。

簡単なところから使ってみる。ディスク1つを丸々使って1つのLVをつくる。
ディスクのフォーマットする。タイプを8eにするのを忘れずに。

root@ubuntu:~# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xdc33f7b0.
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)

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): 
Using default value 1
First sector (2048-16777215, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215): 
Using default value 16777215

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

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

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

root@ubuntu:~# fdisk -l /dev/sdb

Disk /dev/sdb: 8589 MB, 8589934592 bytes
86 heads, 1 sectors/track, 195083 cylinders, total 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 identifier: 0xdc33f7b0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    16777215     8387584   8e  Linux LVM

PVをつくる。

root@ubuntu:~# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

root@ubuntu:~# pvdisplay /dev/sdb1
  "/dev/sdb1" is a new physical volume of "8.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               8.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               KVupMY-AfDA-uyIX-Dle5-aRYK-qxhT-FySfv3

VGをつくる。/dev/sdb1のPVをtest-vgというVGに加える。

root@ubuntu:~# vgcreate test-vg /dev/sdb1
  Volume group "test-vg" successfully created

root@ubuntu:~# pvdisplay /dev/sdb1
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               test-vg
  PV Size               8.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2047
  Free PE               2047
  Allocated PE          0
  PV UUID               KVupMY-AfDA-uyIX-Dle5-aRYK-qxhT-FySfv3
   
root@ubuntu:~# vgdisplay test-vg
  --- Volume group ---
  VG Name               test-vg
  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               8.00 GiB
  PE Size               4.00 MiB
  Total PE              2047
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2047 / 8.00 GiB
  VG UUID               yQOPbO-W8rz-DCnM-oPvK-B1wp-vmbN-FJ7Gmo

LVをつくる。名前はlv1にしてサイズは4GBにする。

root@ubuntu:~# lvcreate -L 4G -n lv1 test-vg
  Logical volume "lv1" created

root@ubuntu:~# lvdisplay /dev/test-vg/lv1 
  --- Logical volume ---
  LV Path                /dev/test-vg/lv1
  LV Name                lv1
  VG Name                test-vg
  LV UUID                I37S36-4kEx-oAPY-lGD0-JRM2-RAY9-nCGg7B
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2014-11-23 11:57:03 +0900
  LV Status              available
  # open                 0
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:2

これでtest-vgに4GBのlv1というLVができた。
あとは、普通のパーティションと同じようにフォーマットしてマウントして使える。

root@ubuntu:~# mkfs -t ext4 /dev/test-vg/lv1 
mke2fs 1.42.9 (4-Feb-2014)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
262144 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736

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

root@ubuntu:~# mount /dev/test-vg/lv1 /mnt
root@ubuntu:~# grep lv1 /proc/mounts 
/dev/mapper/test--vg-lv1 /mnt ext4 rw,relatime,data=ordered 0 0

残りの領域でもう1つLVを作ってみる。lvcreateのときに-L 100%FREEにすると空き領域を全部使うことができる。

root@ubuntu:~# lvcreate -n lv2 -l 100%FREE test-vg
  Logical volume "lv2" created
root@ubuntu:~# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/test-vg/lv1
  LV Name                lv1
  VG Name                test-vg
  LV UUID                I37S36-4kEx-oAPY-lGD0-JRM2-RAY9-nCGg7B
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2014-11-23 11:57:03 +0900
  LV Status              available
  # open                 0
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:2
   
  --- Logical volume ---
  LV Path                /dev/test-vg/lv2
  LV Name                lv2
  VG Name                test-vg
  LV UUID                OenbeG-q8FA-4vat-vAH2-yjNU-1dZg-70XB7H
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2014-11-23 12:09:33 +0900
  LV Status              available
  # open                 0
  LV Size                4.00 GiB
  Current LE             1023
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:3

ディスクを1つ追加してlv1を拡張してみる。LVMパーティション作ってPV作るところまでは同じ。

root@ubuntu:~# pvdisplay /dev/sdc1
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               test-vg
  PV Size               8.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2047
  Free PE               2047
  Allocated PE          0
  PV UUID               IGiohW-lRm3-XmMP-c0Tm-j8tE-QXh1-CPx9Dq

test-vgに/dev/sdc1を追加する。これでtest-vgの容量が増える。

root@ubuntu:~# vgextend test-vg /dev/sdc1
  Volume group "test-vg" successfully extended

root@ubuntu:~# vgdisplay test-vg
  --- Volume group ---
  VG Name               test-vg
  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               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               15.99 GiB
  PE Size               4.00 MiB
  Total PE              4094
  Alloc PE / Size       2047 / 8.00 GiB
  Free  PE / Size       2047 / 8.00 GiB
  VG UUID               yQOPbO-W8rz-DCnM-oPvK-B1wp-vmbN-FJ7Gmo

lv1を拡張する。

root@ubuntu:~# lvextend -l +100%FREE /dev/test-vg/lv1
  Extending logical volume lv1 to 12.00 GiB
  Logical volume lv1 successfully resized

root@ubuntu:~# lvdisplay /dev/test-vg/lv1
  --- Logical volume ---
  LV Path                /dev/test-vg/lv1
  LV Name                lv1
  VG Name                test-vg
  LV UUID                I37S36-4kEx-oAPY-lGD0-JRM2-RAY9-nCGg7B
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2014-11-23 11:57:03 +0900
  LV Status              available
  # open                 0
  LV Size                12.00 GiB
  Current LE             3071
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:2

あとはリサイズすればOK。

root@ubuntu:~# resize2fs /dev/test-vg/lv1
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/test-vg/lv1 is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/test-vg/lv1 is now 3144704 blocks long.