日期:2014-05-16  浏览次数:20536 次

Linux中LVM的使用

LVM 是 "逻辑卷管理 (Logical Volume Manager)" 的简称。简单点说,就是将多个 "物理卷(Physical Volume, PV, 通常是物理硬盘分区)" 合并成一个更大的 "逻辑卷组(Volume Group, VG)",然后在其上切分和传统分区相同用途的 "逻辑卷 (Logical Volume, LV)"。逻辑卷允许我们在不中断系统运行的情况随时调整文件系统容量大小,理论上具备 "无限" 的热扩容能力。

操作步骤:

(1) 物理硬盘分区(fdisk)
(2) 创建物理卷(pvcreate)
(3) 创建卷组(vgcreate)
(4) 激活卷组(vgchange)
(5) 创建逻辑卷(lvcreate)
(6) 卷组扩容(vgextend) 或 逻辑卷扩容(lvextent)

下面是一个在虚拟机上的操作演示。

1. 对新硬盘分区

将新硬盘 /dev/sdb 划分为一个独立主分区。

$ sudo fdisk /dev/sdb

Command (m for help): n

Command action
   e   extended
   p   primary partition (1-4)
p

Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610):
Using default value 2610

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

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

$ sudo fdisk -l

Disk /dev/sdb: 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: 0xe7b472ca

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        2610    20964793+  83  Linux


2. 创建卷组

实际操作时,我们可以省略用 "sudo pvcreate /dev/sdb1" 创建物理卷的过程。
创建名为 myvg 的卷组,并将物理卷 /dev/sdb1 添加到该组。卷组使用前需要激活。

$ sudo vgcreate myvg /dev/sdb1
  No physical volume label read from /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
  Volume group "myvg" successfully created

$ sudo vgchange -a y myvg
  0 logical volume(s) in volume group "myvg" now active

$ sudo vgdisplay
  --- Volume group ---
  VG Name               myvg
  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               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       0 / 0
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               O7APOI-T0b1-T1my-PkKg-zbu3-ZBHn-KX7hCm


3. 创建逻辑卷

创建一个名为 mywww 大小 10G 的逻辑卷,用于 /var/www 数据存储。别忘了创建文件系统,否则无法使用。建议使用 ext4 或 reiserfs,否则热扩容可能失败。

$ sudo lvcreate -L 10G -n mywww myvg
  Logical volume "mywww" created

$ sudo lvdisplay
  --- Logical volume ---
  LV Name                /dev/myvg/mywww
  VG Name                myvg
  LV UUID                dK9aNo-0GDu-2sxP-i1kM-Nni6-u58k-IMWAtT
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           251:2

$ sudo mkfs -t ext4 -j /dev/myvg/mywww

mke2fs 1.41.11 (14-Mar-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 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, 1605632

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

This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.


4. 挂载逻辑卷

将 /dev/myvg/mywww 挂载到 /var/www。