1. LVM概念

LVM逻辑卷管理是Linux环境下对磁盘分区进行管理的一种机制,允许用户对硬盘资源进行动态调整。LVM是建立在硬盘或者分区之上的一个逻辑层,为文件系统屏蔽下层磁盘分区布局,从而提高磁盘分区管理的灵活性。通过LVM系统,管理员可以轻松管理磁盘分区,如将若干个磁盘分区连接为一个整块的卷组(volume group),形成一个存储池。管理员可以在卷组上随意创建逻辑卷(logical volume),并进一步在逻辑卷上创建文件系统。管理员通过LVM可以方便地调整卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配。例如,按照使用用途进行定义development和sales,而不是使用物理磁盘名sda和sdb。当系统添加了新的磁盘后,管理员不必将磁盘的文件移动到新的磁盘上,以便充分利用新的存储空间,而是通过LVM直接扩展文件系统跨越磁盘即可。

  1. 术语

物理卷PV
物理卷在LVM系统中处于最底层,可以将其理解为物理硬盘、硬盘分区或者RAID磁盘阵列。卷组建立在物理卷之上,一个卷组可以包含多个物理卷,而且在卷组创建之后,也可以继续向其中添加新的物理卷。物理卷可以是整个硬盘、硬盘上的分区,或从逻辑上与磁盘分区具有同样功能的设备(如RAID)。物理卷是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。

卷组VG
卷组建立在物理卷之上,由一个或多个物理卷组成。卷组创建之后,可以动态地添加物理卷到卷组中,在卷组上可以创建一个或多个LVM分区(逻辑卷)。一个LVM系统中可以只有一个卷组,也可以包含多个卷组。LVM管理的卷组类似于非LVM系统中的物理硬盘。

逻辑卷LV
逻辑卷建立在卷组之上,是从卷组中“切出”的一块空间。逻辑卷创建之后,其大小可以伸缩。LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(如/home或者/usr等)。逻辑卷是用卷组中空闲的资源建立的,并且逻辑卷在建立后可以动态地扩展或缩小空间。

物理区域PE
每一个物理卷被划分为基本单元(称为PE),具有唯一编号的PE,是可以被LVM寻址的最小存储单元。PE的大小可根据实际情况在创建物理卷时指定,默认为4 MB。PE的大小一旦确定将不能改变,同一个卷组中所有物理卷的PE大小一致。

逻辑区域LE
逻辑区域也被划分为可被寻址的基本单位(称为LE)。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。和非LVM系统将包含分区信息的元数据(metadata)保存在位于分区起始位置的分区表中一样,逻辑卷以及卷组相关的元数据也是保存在位于物理卷起始处的卷组描述符区域VGDA中。VGDA 包括PV 描述符、VG 描述符、IV描述签和一些PF描述符。

  1. 实操
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 1. 先查看当前分区
# sda是虚拟机安装时自动划分的分区,sdb是新添加的空白硬盘
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─centos-root 253:0 0 17.5G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
sr0 11:0 1 4G 0 rom

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 2. 对sdb分区
[root@localhost ~]# fdisk /dev/sdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x21cbebeb.

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
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
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): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0x21cbebeb

Device Boot Start End Blocks Id System

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-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 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 (10487808-41943039, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G
Partition 2 of type Linux and of size 5 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0x21cbebeb

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
/dev/sdb2 10487808 20973567 5242880 83 Linux

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

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

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 3. 查看分区后的分区
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─centos-root 253:0 0 17.5G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part
└─sdb2 8:18 0 5G 0 part
sr0 11:0 1 4G 0 rom

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 4. 对新分区创建物理卷
[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdb2
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 5. 查看物理卷
# 查看物理卷组详细信息使用pvdisplay命令
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- 19.51g 40.00m
/dev/sdb1 lvm2 --- 5.00g 5.00g
/dev/sdb2 lvm2 --- 5.00g 5.00g

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 6. 创建卷组并命名为myvg
[root@localhost ~]# vgcreate myvg /dev/sdb[1-2]
Volume group "myvg" successfully created

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 7. 查看卷组
# 查看卷组详细信息使用vgdisplay命令
# centos卷组是系统创建的卷组(LVM模式安装的系统才有)
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- 19.51g 40.00m
myvg 2 0 0 wz--n- 9.99g 9.99g

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 8. 当多个物理卷组合成一个卷组后时,LVM会在所有的物理卷上做类似格式化的工作,将每个物理卷切成一块一块的空间,这一块一块的空间就称为PE(Physical Extent),它的默认大小是4 MB。
#由于受内核限制的原因,一个逻辑卷(Logic Volume)最多只能包含65536个PE(Physical Extent),所以一个PE的大小就决定了逻辑卷的最大容量,4 MB的PE决定了单个逻辑卷最大容量为256 GB,若希望使用大于256 Gb的逻辑卷,则创建卷组时需要指定更大的PE。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 9. 创建PE为16MB的卷组
# 先删除原来的卷组
[root@localhost ~]# vgremove myvg
Volume group "myvg" successfully removed
# 在创建PE为16MB的卷组
[root@localhost ~]# vgcreate -s 16m myvg /dev/sdb[1-2]
Volume group "myvg" successfully created
# 验证新卷组PE大小
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4984 / 19.47 GiB
Free PE / Size 10 / 40.00 MiB
VG UUID qGc2OA-Szbq-Teo3-IkOE-HSqu-pFNJ-GIT29p

--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 9.97 GiB
PE Size 16.00 MiB
Total PE 638
Alloc PE / Size 0 / 0
Free PE / Size 638 / 9.97 GiB
VG UUID voAD6o-sPGX-yFBw-cwDJ-1Ioc-bg0r-dHzvPR

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 10. 向卷组添加物理卷
# 在添加的过程中,会自动将/dev/sdb3创建为物理卷
# 查看分区
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─centos-root 253:0 0 17.5G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 5G 0 part
├─sdb2 8:18 0 5G 0 part
└─sdb3 8:19 0 5G 0 part
sr0 11:0 1 4G 0 rom

# 添加物理卷
[root@localhost ~]# vgextend myvg /dev/sdb3
Physical volume "/dev/sdb3" successfully created
Volume group "myvg" successfully extended

# 验证
[root@localhost ~]# vgdisplay myvg
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 14.95 GiB
PE Size 16.00 MiB
Total PE 957
Alloc PE / Size 0 / 0
Free PE / Size 957 / 14.95 GiB
VG UUID voAD6o-sPGX-yFBw-cwDJ-1Ioc-bg0r-dHzvP

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 11. 创建逻辑卷,名称为mylv,大小5G
[root@localhost ~]# lvcreate -L +5G -n mylv myvg
Logical volume "mylv" created.

# 查看逻辑卷
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 17.47g
swap centos -wi-ao---- 2.00g
mylv myvg -wi-a----- 5.00g

# 扫描上一步创建的逻辑卷
[root@localhost ~]# lvscan
ACTIVE '/dev/centos/root' [17.47 GiB] inherit
ACTIVE '/dev/centos/swap' [2.00 GiB] inherit
ACTIVE '/dev/myvg/mylv' [5.00 GiB] inherit

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 12. 格式化逻辑卷
[root@localhost ~]# mkfs.ext4 /dev/mapper/myvg-mylv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 13. 挂载逻辑卷
[root@localhost ~]# mount /dev/mapper/myvg-mylv /mnt/

# 验证
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 856M 17G 5% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 497M 125M 373M 25% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/myvg-mylv 4.8G 20M 4.6G 1% /mnt

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 14. 对创建的逻辑卷扩容
[root@localhost ~]# lvextend -L +1G /dev/mapper/myvg-mylv
Size of logical volume myvg/mylv changed from 5.00 GiB (320 extents) to 6.00 GiB (384 extents).
Logical volume mylv successfully resized.

# 验证
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 17.47g
swap centos -wi-ao---- 2.00g
mylv myvg -wi-ao---- 6.00g

# 可见LV容量已经改变,文件系统未变
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 856M 17G 5% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 497M 125M 373M 25% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/myvg-mylv 4.8G 20M 4.6G 1% /mnt

# 对文件系统进行扩容
[root@localhost ~]# resize2fs /dev/mapper/myvg-mylv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/myvg-mylv is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/myvg-mylv is now 1572864 blocks long.

# 可见文件系统容量也已经改变了
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 18G 856M 17G 5% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 497M 125M 373M 25% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/myvg-mylv 5.8G 20M 5.5G 1% /mnt

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

至此,逻辑卷的安装扩容完毕!!!