一、LVM简介
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauelshagen在Linux 2.4内核上实现。LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。
与传统的磁盘与分区相比,LVM为计算机提供了更高层次的磁盘存储。它使系统管理员可以更方便的为应用与用户分配存储空间。在LVM管理下的存储卷可以按需要随时改变大小与移除(可能需对文件系统工具进行升级)。LVM也允许按用户组对存储卷进行管理,允许管理员用更直观的名称(如”sales’、 ‘development’)代替物理磁盘名(如’sda’、’sdb’)来标识存储卷。
二、 LVM基本术语
前面谈到,LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。首先我们讨论以下几个LVM术语:
物理存储介质(The physical media):这里指系统的存储设备:硬盘,如:/dev/hda1、/dev/sda等等,是存储系统最低层的存储单元。
物理卷(physical volume):物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
卷组(Volume Group):LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
逻辑卷(logical volume):LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
PE(physical extent):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
LE(logical extent):逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
简单来说就是:
PV:是物理的磁盘分区
VG:LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘。
LV:也就是从VG中划分的逻辑分区
三、 安装LVM
首先确定系统中是否安装了lvm工具:
[root@www root]# rpm –qa|grep lvm
lvm-1.0.3-4
如果命令结果输入类似于上例,那么说明系统已经安装了LVM管理工具;如果命令没有输出则说明没有安装LVM管理工具,则需要从网络下载或者从光盘装LVM rpm工具包。
四:建立LVM
[root@localhost ~]# fdisk /dev/vdb ##建立3个大小为1G的分区,调整系统标识符为8e(LVM的标识符)
[root@localhost ~]# partprobe
[root@localhost ~]# pvcreate /dev/vdb1 ##用于初始化分区(或其他物理设备),使其作为物理卷与LVM结合使用。直接在物理卷中创建用于存储LVM配置数据的表头
[root@localhost ~]# vgcreate vg0 /dev/vdb1 ##将创建名为vg0且由物理卷/dev/vdb1组成的卷组,可以在创建时指定其他物理卷(以空格隔开),也可以随后使用vgextend添加新物理卷
[root@localhost ~]# lvcreate -L 300M -n lv0 vg0 ##从vg0上可用物理区块创建名为lv0,容量为300M的新逻辑卷
[root@localhost ~]# mkfs.xfs /dev/vg0/lv0 ##将在新逻辑卷上创建文件系统
[root@localhost ~]#mkdir /weixindata ##建立所需目录为挂载点
[root@localhost ~]# mount /dev/vg0/lv0 /weixindata/ ##挂载
[root@localhost ~]# ll /dev/mapper/vg0-lv0
[root@localhost ~]# ll /dev/vg0/lv0
监控
[root@localhost ~]# watch -n 1 'echo ==pvmessage==;pvs;echo ==vgmessage==;vgs;echo ==lvm message==;lvs;echo ====;df /weixindata'
操作
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3151600 7322300 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 16996 925664 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
[root@localhost ~]# fdisk /dev/vdb ##建立3个大小为1G的分区,调整系统标识符为8e(LVM的标识符)
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
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):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (2099200-20971519, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-20971519, default 20971519): +1G
Partition 2 of type Linux and of size 1 GiB is set
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (4196352-20971519, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): +1G
Partition 3 of type Linux and of size 1 GiB is set
Command (m for help): t
Partition number (1-3, default 3): 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): t
Partition number (1-3, default 3): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe
[root@localhost ~]# mount /dev/vdb1 /weixindata/
mount: unknown filesystem type 'linux_raid_member' ##还在占用,/dev/md0 中还在占用/dev/vdb{1..3}, 红帽企业版7和7.2中的bug,已经关掉了/dev/md0,但在打开的时候还会自动启动,这是需要再关掉,然后将分区全部删除,在重启
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Sat Apr 22 06:43:22 2017
Raid Level : raid1
Array Size : 102272 (99.89 MiB 104.73 MB)
Used Dev Size : 102272 (99.89 MiB 104.73 MB)
Raid Devices : 2
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Sat Apr 22 07:42:35 2017
State : clean
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1
Name : localhost:0 (local to host localhost)
UUID : 2ff5d09a:a24cd459:c86113ee:f7b509bb
Events : 39
Number Major Minor RaidDevice State
2 253 19 0 active sync /dev/vdb3
1 253 18 1 active sync /dev/vdb2
3 253 17 - spare /dev/vdb1
[root@localhost ~]# mdadm -S /dev/md0 停止服务
mdadm: stopped /dev/md0
[root@localhost ~]# pvcreate /dev/vdb1 ##用于初始化分区(或其他物理设备),使其作为物理卷与LVM结合使用。直接在物理卷中创建用于存储LVM配置数据的表头
Physical volume /dev/vdb1 not found
WARNING: linux_raid_member signature detected on /dev/vdb1 at offset 4096. Wipe it? [y/n] y
Wiping linux_raid_member signature on /dev/vdb1.
WARNING: xfs signature detected on /dev/vdb1 at offset 0. Wipe it? [y/n] y
Wiping xfs signature on /dev/vdb1.
Physical volume "/dev/vdb1" successfully created
[root@localhost ~]# vgcreate vg0 /dev/vdb1 ##将创建名为vg0且由物理卷/dev/vdb1组成的卷组,可以在创建时指定其他物理卷(以空格隔开),也可以随后使用vgextend添加新物理卷
Volume group "vg0" successfully created
[root@localhost ~]# lvcreate -L 300M -n lv0 vg0 ##从vg0上可用物理区块创建名为lv0,容量为300M的新逻辑卷
Logical volume "lv0" created
[root@localhost ~]# mkfs.xfs /dev/vg0/lv0 ##将在新逻辑卷上创建文件系统
meta-data=/dev/vg0/lv0 isize=256 agcount=4, agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]#mkdir /weixindata ##建立所需目录为挂载点
[root@localhost ~]# mount /dev/vg0/lv0 /weixindata/ ##挂载
[root@localhost ~]# ll /dev/mapper/vg0-lv0
lrwxrwxrwx. 1 root root 7 Apr 22 07:51 /dev/mapper/vg0-lv0 -> ../dm-0
[root@localhost ~]# ll /dev/vg0/lv0
lrwxrwxrwx. 1 root root 7 Apr 22 07:51 /dev/vg0/lv0 -> ../dm-0