LVMを使ってみたよ

Ubuntuをインストールする際に「LVMが〜」と聞かれると思うのですが、気になって調べてみたらHDDを論理的に扱えるらしいではないですか。
面白そうなのと、少し前に必要になったので今のうちに調べておくかーと調べてみたのでした。あ、あとこの機能、かなり昔から存在してたのですね。
環境:Ubuntu 12.04 LTS 64bit

略称について

LVMについて調べてみたら略称がよく出るのでメモ。

PV 物理ボリューム
VG ボリュームグループ
LV 論理ボリューム

現在の状態を表示してみる

上に書いた表のものはそれぞれ、pvscan, vgscan, lvscanというコマンドで現在の状態を表示できます。

$ sudo pvscan
  PV /dev/sda5   VG Ubuntu-VM   lvm2 [7.76 GiB / 32.00 MiB free]
  Total: 1 [7.76 GiB] / in use: 1 [7.76 GiB] / in no VG: 0 [0   ]
$ sudo vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "Ubuntu-VM" using metadata type lvm2
$ sudo lvscan
  ACTIVE            '/dev/Ubuntu-VM/root' [7.23 GiB] inherit
  ACTIVE            '/dev/Ubuntu-VM/swap_1' [508.00 MiB] inherit
$ df /
Filesystem                  1K-blocks    Used Available Use% Mounted on
/dev/mapper/Ubuntu--VM-root   7557248 1227292   5950872  18% /

pvscanで表示したものからは、/dev/sda5はUbuntu-VMという名前のVGであるということの確認ができる。
vgscanで表示したものからは、Ubuntu-VMという名前のVGがあることが確認ができる。
lvscanで表示したものからは、LVの情報が確認できる。
とかいろいろ書いてるけど詳しくはわかってない……

/dev/Ubuntu-VM/rootの容量を増量させてみる

ここで、LVの一つである/dev/Ubuntu-VM/rootの容量を増量させてみる。
この環境はVMなのだけど、最初にインストールしたHDD以外に後からもうひとつHDDを追加してみた。

$ sudo fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x000dd174

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    16775167     8136705    5  Extended
/dev/sda5          501760    16775167     8136704   8e  Linux LVM

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/mapper/Ubuntu--VM-root: 7763 MB, 7763656704 bytes
255 heads, 63 sectors/track, 943 cylinders, total 15163392 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: 0x00000000

Disk /dev/mapper/Ubuntu--VM-root doesn't contain a valid partition table

Disk /dev/mapper/Ubuntu--VM-swap_1: 532 MB, 532676608 bytes
255 heads, 63 sectors/track, 64 cylinders, total 1040384 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: 0x00000000

Disk /dev/mapper/Ubuntu--VM-swap_1 doesn't contain a valid partition table
$ sudo fdisk -l /dev/sdb

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

/dev/sdbが追加したHDDで、追加して何もしていないのでパーティションが無いと言われてます。
で、このHDDをLVとして使えるように、パーティションを切ります。

$ sudo 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 0x1db245a8.
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): t
No partition is defined yet!

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
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 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): p

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x1db245a8

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

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

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

途中でパーティションのタイプを8eと指定しているけれど、これを指定するとLinux LVMというタイプのパーティションになるみたい。


これでpvcreateをすると……

$ sudo pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
$ sudo pvscan
  PV /dev/sda5   VG Ubuntu-VM       lvm2 [7.76 GiB / 32.00 MiB free]
  PV /dev/sdb1                      lvm2 [8.00 GiB]
  Total: 2 [15.76 GiB] / in use: 1 [7.76 GiB] / in no VG: 1 [8.00 GiB]

LVMから扱えるPVになりました。


次はVGに追加します。ちなみにvgから始まるコマンドは以下の通り。
全然関係ないのも混ざってるかもw

$ sudo vg
vgcfgbackup    vgconvert      vgextend       vgmknodes      vgs
vgcfgrestore   vgcreate       vgimport       vgreduce       vgscan
vgchange       vgdisplay      vgimportclone  vgremove       vgsplit
vgck           vgexport       vgmerge        vgrename
$ sudo vgcreate temp /dev/sdb1
  Volume group "temp" successfully created
$ sudo pvscan
  PV /dev/sdb1   VG temp        lvm2 [8.00 GiB / 8.00 GiB free]
  PV /dev/sda5   VG Ubuntu-VM   lvm2 [7.76 GiB / 32.00 MiB free]
  Total: 2 [15.75 GiB] / in use: 2 [15.75 GiB] / in no VG: 0 [0   ]

Ubuntu-VMの容量を増量するために、vgmergeする必要があります。とりあえず追加するのでtempという名前でVGを追加してます。
もしかしたらもっと別な方法があるのかもしれないけど、まだよくわからないのでとりあえず。


で、Ubuntu-VMにtempをマージします。

$ sudo vgmerge Ubuntu-VM temp
  Volume group "temp" successfully merged into "Ubuntu-VM"
$ sudo pvscan
  PV /dev/sda5   VG Ubuntu-VM   lvm2 [7.76 GiB / 32.00 MiB free]
  PV /dev/sdb1   VG Ubuntu-VM   lvm2 [8.00 GiB / 8.00 GiB free]
  Total: 2 [15.75 GiB] / in use: 2 [15.75 GiB] / in no VG: 0 [0   ]

これで両方ともUbuntu-VMのVGになりました。


で、現在のLVが以下の通り。

$ sudo lvscan
  ACTIVE            '/dev/Ubuntu-VM/root' [7.23 GiB] inherit
  ACTIVE            '/dev/Ubuntu-VM/swap_1' [508.00 MiB] inherit


lvから始まるコマンドは以下の通り。
またしても関係ないものが混ざってるかもw

$ lv
lvchange     lvdisplay    lvmchange    lvmdump      lvreduce     lvresize
lvconvert    lvextend     lvmconf      lvmsadc      lvremove     lvs
lvcreate     lvm          lvmdiskscan  lvmsar       lvrename     lvscan


で、lvextendコマンドで拡張します。

$ sudo lvscan
  ACTIVE            '/dev/Ubuntu-VM/root' [7.23 GiB] inherit
  ACTIVE            '/dev/Ubuntu-VM/swap_1' [508.00 MiB] inherit
$ sudo lvextend -L +3G /dev/Ubuntu-VM/root
  Extending logical volume root to 10.23 GiB
  Logical volume root successfully resized
$ sudo lvscan
  ACTIVE            '/dev/Ubuntu-VM/root' [10.23 GiB] inherit
  ACTIVE            '/dev/Ubuntu-VM/swap_1' [508.00 MiB] inherit
  1. 3Gを指定したので7GiBから10GiBに増量!やったね!

でもdfをしてみると……

$ df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/Ubuntu--VM-root  7.3G  1.2G  5.7G  17% /
udev                         237M  4.0K  237M   1% /dev
tmpfs                         99M  296K   99M   1% /run
none                         5.0M     0  5.0M   0% /run/lock
none                         246M     0  246M   0% /run/shm
/dev/sda1                    228M   25M  192M  12% /boot

7GiBから変わってない……
どうも、アンマウントしてからfsck -fとresize2fsをする必要があるみたいです。
ext3とかの場合はどうするんだろう?)
今回増量したパーティションがrootなため、確認が面倒なのでここで諦めました。


でもとりあえず増量できたのでよかったかなーと。
案外面倒でもなかったかも!これから使う機会があったら使っていきたいなー。
あとスナップショット的な機能もあるみたい。これも使ってみたいかも!


参考:
LVM のパーティションを管理する - いますぐ実践! Linuxシステム管理 / Vol.164
LVM を導入する - いますぐ実践! Linuxシステム管理 / Vol.165
LVM のスナップショット機能を使ってみる - いますぐ実践! Linuxシステム管理 / Vol.166