目录
展开
LVM: Logical Volume Manager
lvm即为Logical Volume Manager的缩写,意为逻辑卷管理,用于扩展指定分区磁盘大小。
以下是一些必要概念:
- 物理存储介质(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是相同的,并且一一对应。这里放一张比较容易理解的逻辑图:
本文涉及的命令:
pvcreate
,pvs
,pvdisplay
,pvscan
,pvremove
,pvmove
,vgcreate
,vgextend
,vgreduce
,vgremove
,vgs
,vgdisplay
,lvextend
,lvreduce
,lvdisplay
,lvs
,lvremove
,resize2fs
示例:
-
创建10G:卷组(所有分区加起来为10G就行)
-
使用fdisk创建大小为4G、6G的两个
lvm
分区(分区类型为8e)
0.安装lvm
- 从linux内核2.6.9开始,device-mapper模块就已经包含在内,所以只需加载 模块即可:
[root@server ~]# modprobe dm_mod [root@server ~]# lsmod | grep dm_mod dm_mod 102467 2 dm_mirror,dm_log [root@server ~]# ##出现如上输出则表示加载成功
- 如果内核达标但依旧没有该模块,Centos可使用
yum install device-mapper
安装,Debian系列使用apt install device-mapper
安装,再运行上述两条命令加载内核模块即可.
1.使用pvcreate创建卷组:
[root@server ~]# pvcreate /dev/sdb{1,2}
Physical volume "dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
[root@system ~]# pvs ##显示当前lvm物理卷
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_system lvm2 a-- 19.51g 0
/dev/sdb1 lvm2 a-- 4.01g 4.01g
/dev/sdb2 lvm2 a-- 6.01g 6.01g
[root@system ~]# pvdisplay #显示所有lvm卷更详细的输出
[root@system ~]# pvdisplay /dev/sdb2 #显示sdb2详细输出
"/dev/sdb2" is a new physical volume of "6.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 6.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID C4gUJr-17Iy-hs9X-YbXi-3jy8-x5LP-T3zDFM
[root@system ~]#
2.使用卷组管理命令
[root@system ~]# vgs ##显示当前卷组
VG #PV #LV #SN Attr VSize VFree
vg_system 1 2 0 wz--n- 19.51g 0
[root@system ~]# vgcreate myvg /dev/sdb{1,2} ##创建名为myvg的卷组
Volume group "myvg" successfully created
[root@system ~]# vgs
VG #PV #LV #SN Attr VSize VFree ##PV表示该卷组内的物理卷个数,LV表示逻辑卷的个数,VSIZE则表示该卷组大小
myvg 2 0 0 wz--n- 10.01g 10.01g
vg_system 1 2 0 wz--n- 19.51g 0
[root@system ~]# vgdisplay myvg ##显示myvg的信息
--- 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 10.01 GiB
PE Size 4.00 MiB ##此时PE(盘区)大小默认4M,在创建vg的时候可使用vgcreate -s参数指定PE的大小
Total PE 2562
Alloc PE / Size 0 / 0
Free PE / Size 2562 / 10.01 GiB
VG UUID BHo0L2-oDSC-DjSb-cYU7-xxfU-3O1o-jxpEY4
[root@system ~]#
- 删除现有vg(因为此vg上没有数据,为了方便演示则remove,如果有则谨慎删除)重新vgcreate并且使用-s参数指定PE大小:
[root@system ~]# vgremove myvg
Volume group "myvg" successfully removed
[root@system ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_system 1 2 0 wz--n- 19.51g 0
[root@system ~]# vgcreate -s 8MB myvg /dev/sdb{1,2} ##指定PE大小为8G
Volume group "myvg" successfully created
[root@system ~]# vgdisplay myvg
--- 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 10.00 GiB
PE Size 8.00 MiB
Total PE 1280
Alloc PE / Size 0 / 0
Free PE / Size 1280 / 10.00 GiB
VG UUID LUtTK3-ZuxN-s22z-svqI-2FkG-XXvc-G2SkJa
[root@system ~]# pvdisplay /dev/sdb1
--- Physical volume ---
PV Name /dev/sdb1
VG Name myvg
PV Size 4.01 GiB / not usable 6.51 MiB
Allocatable yes
PE Size 8.00 MiB
Total PE 512
Free PE 512
Allocated PE 0
PV UUID bbcRHk-htgR-uVUE-R9Th-cSgs-9v6N-FUvjc6
[root@system ~]#
3.增加物理卷,扩容
假设空间不够用,可以添加物理卷
使用fdisk
增加一个lvm类型(8e)的分区(不在同一磁盘也没问题)
此时,我增加了一个/dev/sdb3大小为8G的lvm分区
接着,创建物理卷并扩容到卷组:
[root@system ~]# pvcreate /dev/sdb3
dev_is_mpath: failed to get device for 8:19
Physical volume "/dev/sdb3" successfully created
[root@system ~]# vgextend myvg /dev/sdb3
Volume group "myvg" successfully extended
[root@system ~]# 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 18.00 GiB ##已经扩容到18G
PE Size 8.00 MiB
Total PE 2304
Alloc PE / Size 0 / 0
Free PE / Size 2304 / 18.00 GiB
VG UUID LUtTK3-ZuxN-s22z-svqI-2FkG-XXvc-G2SkJa
[root@system ~]#
4.移除物理卷,缩容
比如这里,把大小为4G的/dev/sdb1移除掉
[root@system ~]# pvmove /dev/sdb1 ##把sdb1的数据移到其他数据卷上去
No data to move for myvg ##显示sdb1没有数据,如果有的话则会等待一段时间
[root@system ~]# vgreduce myvg /dev/sdb1
Removed "/dev/sdb1" from volume group "myvg"
[root@system ~]# vgdisplay myvg
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
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 14.00 GiB
PE Size 8.00 MiB
Total PE 1792
Alloc PE / Size 0 / 0
Free PE / Size 1792 / 14.00 GiB
VG UUID LUtTK3-ZuxN-s22z-svqI-2FkG-XXvc-G2SkJa
[root@system ~]# pvremove /dev/sdb1 ##把sdb1物理卷移除,现在sdb1不是pv了
Labels on physical volume "/dev/sdb1" successfully wiped
[root@system ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_system lvm2 a-- 19.51g 0
/dev/sdb2 myvg lvm2 a-- 6.00g 6.00g
/dev/sdb3 myvg lvm2 a-- 8.00g 8.00g
[root@system ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 2 0 0 wz--n- 14.00g 14.00g
vg_system 1 2 0 wz--n- 19.51g 0
[root@system ~]#
5.增加及使用逻辑分区
- 在myvg创建一个大小为3G,名字为testlv1的逻辑卷:
[root@system ~]# lvcreate -L 3G -n testlv1 myvg Logical volume "testlv1" created [root@system ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert testlv1 myvg -wi-a----- 3.00g lv_root vg_system -wi-ao---- 17.51g lv_swap vg_system -wi-ao---- 2.00g [root@system ~]# ls -l /dev/myvg/testlv1 lrwxrwxrwx 1 root root 7 May 5 13:38 /dev/myvg/testlv1 -> ../dm-2 [root@system ~]# ls -l /dev/mapper/myvg-testlv1 lrwxrwxrwx 1 root root 7 May 5 13:38 /dev/mapper/myvg-testlv1 -> ../dm-2 ##划黄色的这两个设备其实都是指向/dev/dm-2这个设备,因此lv是dm设备 [root@system ~]# lvdisplay /dev/myvg/testlv1 ##lvgdisplay显示testlv1逻辑卷具体信息 --- Logical volume --- LV Path /dev/myvg/testlv1 LV Name testlv1 VG Name myvg LV UUID qb6pDw-vmDm-9yio-tPLj-wwSa-fEi5-khSuSX LV Write Access read/write LV Creation host, time system, 2019-05-05 13:38:48 +0800 LV Status available # open 0 LV Size 3.00 GiB Current LE 384 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 [root@system ~]#
- 使用逻辑卷:
[root@system ~]# mke2fs -t ext4 -b 2048 /dev/myvg/testlv1 #格式化逻辑卷,-b指定block大小
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
196608 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=538968064
96 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@system ~]# mount /dev/myvg/testlv1 /mnt
[root@system ~]# ls /mnt
lost+found
[root@system ~]#
6.扩容逻辑分区
- 比如把刚刚的3G扩容到5G大小,使用
lvextend
:
[root@system ~]# vgs ##首先使用vgs查看当前卷组中是否有足够的空闲空间去扩容
VG #PV #LV #SN Attr VSize VFree
myvg 2 1 0 wz--n- 14.00g 11.00g
vg_system 1 2 0 wz--n- 19.51g 0
[root@system ~]# lvextend -L 5G /dev/myvg/testlv1 ##使用-L 5G表示扩容到5G,使用-L +2G则在原来的空间上扩容两G,在这里效果一样
Extending logical volume testlv1 to 5.00 GiB
Logical volume testlv1 successfully resized
[root@system ~]# lvs ##查看当前逻辑卷
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
testlv1 myvg -wi-ao---- 5.00g
lv_root vg_system -wi-ao---- 17.51g
lv_swap vg_system -wi-ao---- 2.00g
[root@system ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_system-lv_root 18G 2.5G 14G 16% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 33M 427M 8% /boot
/dev/mapper/myvg-testlv1 3.0G 75M 2.8G 3% /mnt ##发现扩容之后,/mnt大小并没有发生改变,因为只扩容了物理边界,逻辑边界还没有扩展
[root@system ~]#
- 扩展逻辑边界:
[root@system ~]# resize2fs /dev/myvg/testlv1 ##后面不接大小默认把/dev/myvg/testlv1逻辑边界扩展到和物理边界一样大
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/testlv1 is mounted on /mnt; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/myvg/testlv1 to 2621440 (2k) blocks.
The filesystem on /dev/myvg/testlv1 is now 2621440 blocks long.
[root@system ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_system-lv_root 18G 2.5G 14G 16% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 33M 427M 8% /boot
/dev/mapper/myvg-testlv1 5.0G 75M 4.6G 2% /mnt
[root@system ~]#
7.缩减逻辑分区
- 例如,把testlv1缩减到2G:
[root@system ~]# umount /dev/myvg/testlv1
[root@system ~]# e2fsck -f /dev/myvg/testlv1 #检查磁盘
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/myvg/testlv1: 11/327680 files (0.0% non-contiguous), 79723/2621440 blocks
[root@system ~]# resize2fs /dev/myvg/testlv1 2G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/testlv1 to 1048576 (2k) blocks.
The filesystem on /dev/myvg/testlv1 is now 1048576 blocks long.
[root@system ~]# lvreduce -L 2G /dev/myvg/testlv1
WARNING: Reducing active logical volume to 2.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testlv1? [y/n]: y
Reducing logical volume testlv1 to 2.00 GiB
Logical volume testlv1 successfully resized
[root@system ~]# mount /dev/myvg/testlv1 /mnt
[root@system ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_system-lv_root 18G 2.5G 14G 16% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 33M 427M 8% /boot
/dev/mapper/myvg-testlv1 2.0G 74M 1.8G 4% /mnt
[root@system ~]# lvs ##显示当前逻辑卷
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
testlv1 myvg -wi-ao---- 2.00g
lv_root vg_system -wi-ao---- 17.51g
lv_swap vg_system -wi-ao---- 2.00g
[root@system ~]#
8.创建快照卷,并且通过快照卷备份
首先确保卷组内有足够空间可用(vgs)
[root@system ~]# mount ##查看当前系统挂载状态
/dev/mapper/vg_system-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/myvg-testlv1 on /mnt type ext4 (rw)
[root@system ~]# lvcreate -s -L 50M -n testv1-snap -p r /dev/myvg/testlv1 -p r表示只读
Rounding up size to full physical extent 56.00 MiB
Logical volume "testv1-snap" created
[root@system ~]# mount /dev/myvg/testv1-snap /media/
mount: block device /dev/mapper/myvg-testv1--snap is write-protected, mounting read-only
[root@system ~]# ls /media/ #此时如果修改原卷/mnt下的fstab,快照卷/media下的fstab是不会变的
lost+found fstab
[root@system ~]# tar -Jcf /tmp/media.tar.xz /dedia/* ##备份快照卷下的文件
[root@system ~]# umount /media/ ##卸载快照卷
[root@system ~]# lvremove /dev/myvg/testv1-snap ##移除快照卷
Do you really want to remove active logical volume testv1-snap? [y/n]: y
Logical volume "testv1-snap" successfully removed
[root@system ~]#
总结:
逻辑卷扩展的步骤:
- 1、先确定扩展的大小:并确保所属的卷组有足够的剩余空间(vgdisplay);
- 2、扩展物理边界;
lvextend -L [+]Size /path/to/lv_device
- 3、扩展逻辑边界;
resize2fs /path/to/lv_device
缩减逻辑卷的步骤:
- 1、卸载卷,并执行强制检测
e2fsck -f /path/to/lv_device
- 2、缩减逻辑边界:
resize2fs /path/to/lv_device SIZE
- 3、缩减物理边界:
lvreduce -L [-]Size /path/to/lv_device
- 快照卷:逻辑卷的一种,通常为只读:
lvreate -s -L Size -n Name -p r /path/to/original_lv_device