1、LVM 是建立在软件基础上的,如果发生文件误删除,很难恢复。(在普通硬盘上,误删数据后,恢复可能性更大)
2、LVM 赞成使用和反对使用的人五五开。LVM 受到红帽的大力推崇(因为这就是红帽开发的)。
3、CentOS 7一般不会使用 LVM,因为 CentOS 7支持的 btrfs 文件系统就有类似功能。
逻辑卷管理器(LVM:Logical Volume Manager)是允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小。并允许在多个物理设备之间重新组织文件文件系统。
Linux LVM 的设备名称是 /dev/dm-# 。
**dm(device mapper)**将一个或多个底层块设备组织成一个逻辑设备的模块。
同时为了便于操作和记忆,LVM 还有两个软连接项目。
LVM 的真正的设备文件路径:
# /dev/dm-#
例如:# /dev/dm-0 /dev/dm-1
两个软链接文件:
#1 /dev/mapper/VG_NAME-LV_NAME
例如:# /dev/mapper/vol0-root
#2 /dev/VG_NAME/LV_NAME
例如:# /dev/vol0/root
LVM 可弹性更改文件系统的容量,实现方法:通过交换PE来进行资料的转换,将原来LV内的PE转移到其他的设备中以降低LV的容量,或将其他设备中的PE加到LV中以加大容量。
# 显示 pv 管理命令
# pv # 双击 TAB 键可查看所有的 pv 管理命令
# 显示 pv 信息
# pvs # 简要pv信息显示
# pvdisplay # 详细pv信息显示
# 创建 pv
# pvcreate /dev/DEVICE
-f:强制创建物理卷,不需要用户确认
-u:指定设备的UUID
-y:所有的问题都回答“yes”
-Z:是否利用前4个扇区
# 删除 pv
# pvremove /dev/DEVICE
# 显示 vg 管理命令
# vg # 双击 TAB 键可查看所有的 vg 管理命令
# 显示卷组
# vgs # 简要vg信息显示
# vgdisplay # 详细vg信息显示
# 创建卷组
# vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
-l:卷组上允许创建的最大逻辑卷数
-p:卷组中允许添加的最大物理卷数
-s:卷组上的物理卷的PE大小
# 管理卷组:扩展PV
# vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
# 管理卷组:移除PV
# vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
# 移除PV注意事项:
# 先做 pvmove,再做 vgremove
# 显示 vg 管理命令
# vg # 双击 TAB 键可查看所有的 vg 管理命令
# 显示逻辑卷
lvs # 简要lv信息显示
lvdisplay # 详细lv信息显示
# 创建lv
# lvcreate -L #[mMgGtT] -n NAME VolumeGroup
# lvcreate-l 60%VG -n mylv testvg
# lvcreate-l 100%FREE -n yourlv testvg
-L:指定逻辑卷的大小,单位为“kKmMgGtT”字节
-l:指定逻辑卷的大小(LE数)
# 移除lv
# lvremove /dev/VG_NAME/LV_NAME
# 扩展逻辑卷:
# lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
# 带+号,代表扩展多少;不带+号,代表扩展到多少
# resize2fs /dev/VG_NAME/LV_NAME
# lvresize -r -l +100%FREE /dev/VG_NAME/LV_NAME
# 缩减逻辑卷(注意操作顺序):
# umount /dev/VG_NAME/LV_NAME # 卸载lv
# e2fsck -f /dev/VG_NAME/LV_NAME # 强制进行文件系统检查
# resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT] # 修改文件系统大小,注意大小不能小于现有数据
# lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME # 缩减lv
# 带-号,代表缩减多少;不带-号,代表缩减到多少
# mount # 重新挂载
#1、创建物理卷
# pvcreate/dev/sda3
#2、为卷组分配物理卷
# vgcreatevg0 /dev/sda3
#3、从卷组创建逻辑卷
# lvcreate-L 256M -n data vg0
# mkfs.xfs-j /dev/vg0/data
#4、挂载
# mount /dev/vg0/data /mnt/data
快照是特殊的逻辑卷,它是在生成快照时存在的逻辑卷的准确拷贝。
对于需要备份或者复制的现有数据临时拷贝以及其它操作来说,快照是最合适的选择。
快照只有在它们和原来的逻辑卷不同时才会消耗空间。
1、在生成快照时会分配给它一定的空间,但只有在原来的逻辑卷或者快照有所改变才会使用这些空间。
2、当原来的逻辑卷中有所改变时,会将旧的数据复制到快照中。
3、快照中只含有原来的逻辑卷中更改的数据或者自生成快照后的快照中更改的数据。
4、建立快照的卷大小只需要原始逻辑卷的15%~20%就够了,也可以使用lvextend放大快照。
快照就是将当时的系统信息记录下来,就好像照相一般,若将来有任何数据改动了,则原始数据会被移动到快照区,没有改动的区域则由快照区和文件系统共享。
由于快照区与原本的LV共用很多PE的区块,因此快照与被快照的LV必须在同一个VG中.系统恢复的时候的文件数量不能高于快照区的实际容量。
创建快照命令如下:
# lvcreate -L #[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name
可按以下顺序实现快照:
# 为现有逻辑卷创建快照
# lvcreate-l 64 -s -n data-snapshot -p r /dev/vg0/data
# 挂载快照
# mkdir-p /mnt/snap
# mount -o ro/dev/vg0/data-snapshot /mnt/snap
# 恢复快照
# umount/dev/vg0/data-snapshot
# umount/dev/vg0/data
# lvconvert--merge /dev/vg0/data-snapshot
# 删除快照
# umount/mnt/databackup
# lvremove/dev/vg0/databackup
#1、在旧系统中,umount所有卷组上的逻辑卷
#2、禁用卷组
# vgchange –a n vg0
# lvdisplay
#3、导出卷组
# vgexportvg0
# pvscan
# vgdisplay
#4、拆下旧硬盘
#5、 在新系统中安装旧硬盘,并导入卷组 # vgimport vg0
#6、启用 # vgchange –ay vg0
#7、 mount所有卷组上的逻辑卷
示例要求:
准备3个大小都为 1G 的分区格式为 8e 的分区。
Disk /dev/sdh: 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: 0x2a6ea9da
Device Boot Start End Blocks Id System
/dev/sdh1 2048 2099199 1048576 8e Linux LVM
/dev/sdh2 2099200 4196351 1048576 8e Linux LVM
/dev/sdh3 4196352 6293503 1048576 8e Linux LVM
创建物理卷:
[root@LeeMumu ~]# pvcreate /dev/sdh1
Physical volume "/dev/sdh1" successfully created.
[root@LeeMumu ~]# pvcreate /dev/sdh2
Physical volume "/dev/sdh2" successfully created.
[root@LeeMumu ~]# pvcreate /dev/sdh3
Physical volume "/dev/sdh3" successfully created.
查看物理卷:
[root@LeeMumu ~]# pvdisplay
"/dev/sdh3" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdh3
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID osG8Ak-Yx7i-PzHT-mWFo-6IjO-FNaz-RsPQQm
"/dev/sdh2" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdh2
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID d2C9gf-7ihg-GJOv-in8A-OemL-R3zO-38y4ma
"/dev/sdh1" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdh1
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Rn9ThV-6Sww-I6AS-XuW1-90SN-cAXA-gTTeaj
使用物理卷 /dev/sdh1 、/dev/sdh2 、/dev/dsh3 三个物理卷,创建名为 myvg 的卷组。
[root@LeeMumu ~]# vgcreate myvg /dev/sdh{1,2,3}
Volume group "myvg" successfully created
查看卷组:
[root@LeeMumu ~]# vgdisplay
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 3
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 3
Act PV 3
VG Size <2.99 GiB
PE Size 4.00 MiB
Total PE 765
Alloc PE / Size 0 / 0
Free PE / Size 765 / <2.99 GiB
VG UUID SRipaW-lslz-8qKk-koPu-nQLw-EWdA-CzhSeh
在 myvg 卷组中,创建一个 100M 大小的、名称为 mylv 的 lv 。
[root@LeeMumu ~]# lvcreate -L 100M -n mylv myvg
Logical volume "mylv" created.
查看 lv:
[root@LeeMumu ~]# lvdisplay
--- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID XbGZPo-d93G-Vop1-ul3G-5bd2-wNZR-uNsZTz
LV Write Access read/write
LV Creation host, time LeeMumu, 2019-07-28 05:26:30 -0400
LV Status available
# open 0
LV Size 100.00 MiB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
[root@LeeMumu ~]# mke2fs -t ext4 /dev/myvg/mylv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@LeeMumu ~]# mount /dev/myvg/mylv /mylv/
[root@LeeMumu ~]# mount
/dev/mapper/myvg-mylv on /mylv type ext4 (rw,relatime,seclabel,data=ordered)
调整逻辑卷大小是 LVM 功能中最有用的一个功能。
现在可以把 mylv 的逻辑卷大小扩展为 200M 。具体操作顺序如下。
[root@LeeMumu ~]# umount /dev/myvg/mylv
[root@LeeMumu ~]# lvresize -L 200M /dev/myvg/mylv
Size of logical volume myvg/mylv changed from 100.00 MiB (25 extents) to 200.00 MiB (50 extents).
Logical volume myvg/mylv successfully resized.
[root@LeeMumu ~]# e2fsck -f /dev/myvg/mylv
e2fsck 1.42.9 (28-Dec-2013)
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/mylv: 11/25688 files (9.1% non-contiguous), 8896/102400 blocks
[root@LeeMumu ~]# resize2fs /dev/myvg/mylv
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylv to 204800 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 204800 blocks long.
[root@LeeMumu ~]# lvdisplay
--- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID XbGZPo-d93G-Vop1-ul3G-5bd2-wNZR-uNsZTz
LV Write Access read/write
LV Creation host, time LeeMumu, 2019-07-28 05:26:30 -0400
LV Status available
# open 0
LV Size 200.00 MiB
Current LE 50
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
[root@LeeMumu ~]# mount /dev/myvg/mylv /mylv/
[root@LeeMumu ~]# mount | grep "mylv"
/dev/mapper/myvg-mylv on /mylv type ext4 (rw,relatime,seclabel,data=ordered)
缩减时一定要注意先缩减文件系统大小,再缩减 lv 大小。
[root@LeeMumu ~]# umount /dev/myvg/mylv
[root@LeeMumu ~]# e2fsck -f /dev/myvg/mylv
e2fsck 1.42.9 (28-Dec-2013)
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/mylv: 11/49400 files (9.1% non-contiguous), 11884/204800 blocks
[root@LeeMumu ~]# resize2fs /dev/myvg/mylv 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylv to 102400 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 102400 blocks long.
[root@LeeMumu ~]# lvresize -L 100M /dev/myvg/mylv
WARNING: Reducing active logical volume to 100.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myvg/mylv? [y/n]: y
Size of logical volume myvg/mylv changed from 200.00 MiB (50 extents) to 100.00 MiB (25 extents).
Logical volume myvg/mylv successfully resized.
[root@LeeMumu ~]# lvdisplay
--- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID XbGZPo-d93G-Vop1-ul3G-5bd2-wNZR-uNsZTz
LV Write Access read/write
LV Creation host, time LeeMumu, 2019-07-28 05:26:30 -0400
LV Status available
# open 0
LV Size 100.00 MiB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
[root@LeeMumu ~]# mount /dev/myvg/mylv /mylv/
[root@LeeMumu ~]# mount | grep "mylv"
/dev/mapper/myvg-mylv on /mylv type ext4 (rw,relatime,seclabel,data=ordered)
Disk /dev/sdh: 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: 0x2a6ea9da
Device Boot Start End Blocks Id System
/dev/sdh1 2048 2099199 1048576 8e Linux LVM
/dev/sdh2 2099200 4196351 1048576 8e Linux LVM
/dev/sdh3 4196352 6293503 1048576 8e Linux LVM
/dev/sdh4 6293504 41943039 17824768 5 Extended
/dev/sdh5 6295552 8392703 1048576 8e Linux LVM
[root@LeeMumu ~]# partx /dev/sdh5
NR START END SECTORS SIZE NAME UUID
5 6295552 8392703 2097152 1G
[root@LeeMumu ~]# partx /dev/sdh
NR START END SECTORS SIZE NAME UUID
1 2048 2099199 2097152 1G
2 2099200 4196351 2097152 1G
3 4196352 6293503 2097152 1G
4 6293504 41943039 35649536 17G
5 6295552 8392703 2097152 1G
[root@LeeMumu ~]# pvcreate /dev/sdh5
Physical volume "/dev/sdh5" successfully created.
[root@LeeMumu ~]# pvdisplay
"/dev/sdh5" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdh5
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 87a6Fg-pJOR-kxHq-IHVQ-4LuD-gGfd-yiGqq3
[root@LeeMumu ~]# vgextend myvg /dev/sdh5
Volume group "myvg" successfully extended
[root@LeeMumu ~]# vgdisplay
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 7
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 4
Act PV 4
VG Size 3.98 GiB
PE Size 4.00 MiB
Total PE 1020
Alloc PE / Size 25 / 100.00 MiB
Free PE / Size 995 / <3.89 GiB
VG UUID SRipaW-lslz-8qKk-koPu-nQLw-EWdA-CzhSeh