LVM利用Linux内核的device-mapper来实现存储系统的虚拟化(系统分区独立于底层硬件)。通过LVM,你可以实现存储空间的抽象化并在上面建立虚拟分区(virtual partitions),可以更简便地扩大和缩小分区,可以增删分区时无需担心某个硬盘上没有足够的连续空间, LVM是用来方便管理的,不会提供额外的安全保证。
LVM的基本组成块(building blocks)如下:
物理卷Physical volume (PV):可以在上面建立卷组的媒介,可以是硬盘分区,也可以是硬盘本身或者回环文件(loopback file)。物理卷包括一个特殊的header,其余部分被切割为一块块物理区域(physical extents)。Think of physical volumes as big building blocks which can be used to buildyour hard drive.
卷组Volume group (VG):将一组物理卷收集为一个管理单元。Groupof physical volumes that are used as storage volume (as one disk). They containlogical volumes. Think of volume groups as hard drives.
逻辑卷Logical volume (LV):虚拟分区,由物理区域(physicalextents)组成。A"virtual/logical partition" that resides in a volume group and iscomposed of physical extents. Think of logical volumes as normal partitions.
物理区域Physical extent (PE):硬盘可供指派给逻辑卷的最小单位(通常为4MB)。A small part of a disk (usually 4MB) that can be assigned toa logical Volume. Think of physical extents as parts of disks that can be allocatedto any partition.
示例 1:
两块物理硬盘
硬盘1(/dev/sda):
_ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|分区1 50GB (物理卷) |分区2 80GB (物理卷) |
|/dev/sda1 |/dev/sda2 |
|_ _ _ __ _ _ _ _ _ _ _ _ _ _|_ _ _ _ _ _ _ _ _ _ _ _ _ _ __|
硬盘2(/dev/sdb):
_ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|分区1 120GB (物理卷) |
|/dev/sdb1 |
| _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|
LVM方式
卷组VG1(/dev/MyStorage/ = /dev/sda1 + /dev/sda2 + /dev/sdb1):
_ __ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ __
|逻辑卷115GB |逻辑卷235GB |逻辑卷3200GB |
|/dev/MyStorage/rootvol |/dev/MyStorage/homevol |/dev/MyStorage/mediavol |
|_ _ _ __ _ _ _ _ _ _ _ _ _ __|_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ |_ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _|
示例 2:
LVM的优缺点:
优点:
比起正常的硬盘分区管理,LVM更富于弹性:
使用卷组(VG),使众多硬盘空间看起来像一个大硬盘。
使用逻辑卷(LV),可以创建跨越众多硬盘空间的分区。
可以创建小的逻辑卷(LV),在空间不足时再动态调整它的大小。
在调整逻辑卷(LV)大小时可以不用考虑逻辑卷在硬盘上的位置,不用担心没有可用的连续空间。Itdoes not depend on the position of the LV within VG, there is no need to ensuresurrounding available space.
可以在线(online)对逻辑卷(LV)和卷组(VG)进行创建、删除、调整大小等操作。LVM上的文件系统也需要重新调整大小,某些文件系统也支持这样的在线操作。
无需重新启动服务,就可以将服务中用到的逻辑卷(LV)在线(online)/动态(live)迁移至别的硬盘上。
允许创建快照,可以保存文件系统的备份,同时使服务的下线时间(downtime)降低到最小。
缺点:
只能在Linux上使用。对于其他操作系统(如FreeBSD,Windows等),尚未有官方支持。
在系统设置时需要更复杂的额外步骤。
假如你使用的是btrfs文件系统,那么它所提供的子卷(subvolume)实际上已经时一层可动态调整的存储层,此时再用LVM就显得多余了。
LVM的管理和配置:
Initializing disks ordisk partitions:
To use LVM, partitions andwhole disks must first be converted into physical volumes (PVs) using the pvcreate command.If a Linux partition is to be converted make surethat it is given partition type 0x8E using the fdisk command.
pvcreate /dev/sdb1
pvcreate /dev/sdb2
示例 3:
Displaying PhysicalVolumes:
There are three commandsyou can use to display properties of LVM physical volumes: pvs, pvdisplay, and pvscan.
The pvs command provides physical volume information in aconfigurable form, displaying one line per physical volume. The pvs command provides a great deal of format control, and isuseful for scripting.
The pvdisplay command provides a verbose multi-line output for eachphysical volume. It displays physical properties (size, extents, volume group,etc.) in a fixed format.
The following example showsthe output of the pvdisplay command for a single physical volume.
示例 4:
The pvscan command scans all supported LVM block devices in the systemfor physical volumes.
The following command showsall physical devices found:
示例 5:
Removing Physical Volumes:
If a device is no longerrequired for use by LVM, you can remove the LVM label with the pvremove command. Executing the pvremove command zeroes theLVM metadata on an empty physical volume.
If the physical volume youwant to remove is currently part of a volume group, you must remove it from thevolume group with the vgreduce command.
示例 6:
Creating 、extend and reduce a volume group:
Once you have one or morephysical volumes created, you can create a volume group from these PVs usingthe vgcreate command. The following command:
vgcreate [-s 4MB] myvg/dev/sdb1 /dev/sdb2
Creates a new VG called myvg with two partitions, /dev/sdb1 and /dev/sdb2, and 4 MB PEs. If both /dev/sdb1 and /dev/sdb2 are 6 GB in size, then the VG myvg will have a total of 3074 physical extents that can be allocated to logical volumes.
Additional PVs can be addedto this volume group using the vgextend command. Thefollowing commands convert /dev/sdb3 into a PV and thenadds that PV to myvg:
pvcreate /dev/sdb3
vgextend myvg /dev/sdb3
This same PV can be removedfrom myvg by the vgreduce command:
pvmove /dev/sdb3
vgreduce myvg /dev/sdb3
Note that any logicalvolumes using physical extents from PV /dev/sd3 will be removed aswell.
示例 7:
Displaying Volume Groups:
There are two commands youcan use to display properties of LVM volume groups: vgs and vgdisplay.
The vgs command provides volume group information in a configurableform, displaying one line per volume group. The vgs command provides a greatdeal of format control, and is useful for scripting.
The vgdisplay command displays volume group properties (such as size,extents, number of physical volumes, etc.) in a fixed form. The followingexample shows the output of a vgdisplay command for the volume group myvg. If you do not specify a volume group, all existing volume groupsare displayed.
示例 8:
Scanning Disks for VolumeGroups to Build the Cache File:
The vgscan command scans all supported disk devices in the systemlooking for LVM physical volumes and volume groups. This builds the LVM cachein the /etc/lvm/cache/.cache file, which maintains a listing of current LVMdevices.
LVM runs the vgscan command automatically at system startup and at other timesduring LVM operation, such as when you execute a vgcreate command or when LVM detects an inconsistency.
You mayneed to run the vgscan command manually whenyou change your hardware configuration and add or delete a device from a node, causing new devices to bevisible to the system that were not present at system bootup. This may benecessary, for example, when you add new disks to the system on a SAN orhotplug a new disk that has been labeled as a physical volume.
The following example showsthe output of a vgscan command.
示例 9:
Creating a logical volume:
We use the lvcreate command to create a new logical volume using the freephysical extents in the VG pool. Continuing our example using VG myvg (with two PVs /dev/sdb1 and /dev/sdb3 and a total capacity of 12 GB), we could allocate nearly allthe PEs in the volume group to a single linear LV called myvg with the following LVM command:
lvcreate -n lv1 -L 12Gmyvg
Instead of specifying theLV size in GB we could also specify it in terms of logical extents. First weuse vgdisplay to determine the number of PEs in the myvg:
vgdisplay myvg | grep "TotalPE" which returns Total PE 3074
Then the following lvcreatecommand will create a logical volume with 3074 logical extents andfill the volume group completely:
lvcreate -n lv1 -l 3074myvg
It is possible to allocatea logical volume from a specific physical volume in the VG by specifying the PVor PVs at the end of the lvcreate command. If you want the logical volume tobe allocated from a specific physical volume in the volume group, specify thePV or PVs at the end of the lvcreate command line. For example, this command:
lvcreate -L 5G -n lv2 myvg/dev/sdb1
An LV can be removed from aVG through the lvremove command, but first the LV must be unmounted:
umount /dev/myvg/lv1
lvremove /dev/myvg/lv1
Note that LVM volume groupsand underlying logical volumes are included in the device special filedirectory tree in the /dev directory with the following layout:
/dev/<volume_group_name>/<logical_volume_name>
示例 10:
Displaying LogicalVolumes:
There are three commandsyou can use to display properties of LVM logical volumes: lvs, lvdisplay, and lvscan.
The lvs command provides logical volume information in aconfigurable form, displaying one line per logical volume. The lvs command provides a great deal of format control, and is useful forscripting.
The lvdisplay command displays logical volume properties (such as size,layout, and mapping) in a fixed format.
The following command showsthe attributes of lv1 in myvg. If snapshot logicalvolumes have been created for this original logical volume, this command showsa list of all snapshot logical volumes and their status (active or inactive) aswell.
示例 11:
The lvscan command scans for all logical volumes in the system andlists them, as in the following example.
示例 12:
Creating the File System:
The following commandcreates a ext4 file system on the logical volume.
示例 13:
The following commandsmount the logical volume and report the file system disk space usage.
示例 14:
Extending a logicalvolume:
An LV can be extended byusing the lvextend command. You can specify either anabsolute size for the extended LV or how much additional storage you want toadd to the LVM. For example:
lvextend -L 15G /dev/myvg/lv1 will extend LV /dev/myvg/lv1 to 15 GB,while
lvextend -L+3G /dev/myvg/lv1 will extend LV /dev/myvg/lv1 by anadditional 3 GB.
Once a logical volume hasbeen extended, the underlying file system can be expanded to exploit theadditional storage now available on the LV. With Red Hat Enterprise Linux 6, itis possible to expand both the ext4fs file systems online, without bringing thesystem down.To resize ext4fs, the following command
resize2fs /dev/myvg/lv1
示例 15:
Shrinkingly alogical volume:
First, as is always thecase when you’re modifying disk volumes, partitions, orfile systems, you should really have a recent backup. A typo in one thefollowing commands could easily destroy data. You have been warned!
All of the required stepsmust be performed on an unmounted volume. If want to reduce the size of lv1 volume, simply unmount it.
Next, force a file systemcheck on the volume in question.
Third, resize the actualfile system.
Finally, reduce the size ofthe logical volume.
示例 16:
Creating Snapshot Volumes:
Use the -s argument of the lvcreate command to create asnapshot volume. A snapshot volume is writable.
The following commandcreates a snapshot logical volume that is 100 MB in size named /dev/myvg/lv1-snap. This creates a snapshot of the origin logical volume named /dev/myvg/lv1.
示例 17:
If the originallogical volume contains a file system, you can mount the snapshot logicalvolume on an arbitrary directory in order to access the contents of the filesystem to run a backup while the original file system continues to get updated.
示例 18:
示例 19:
总结:以上就是关于LVM的简单介绍和用法。