LVM的创建与快照和SSM存储管理器的使用

邮差的信 提交于 2019-11-25 23:27:42

LVM:Logical Volume Manager(逻辑卷管理)

概念(名词):
PV:Physical Volume(物理卷)
VG:Volume Group(卷组)
LV:Logical Volume(逻辑卷)

最小存储单位为:PE
总结:
名称 最小存储单位
硬盘 扇区(512B)
文件系统 block(1K,4K)
RAID chunk (512) mdadm -c
LVM PE (16M 自定义)

创建LVM:
准备分区:
#fdisk /dev/sdb 分三个分区: sdb1,2,3

创建PV
[root@localhost ~]# pvcreate /dev/sdb{1,2}
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created

创建VG
[root@localhost ~]# vgcreate Vg1 /dev/sdb{1,2}
Volume group "Vg1" successfully created

创建LV
[root@localhost ~]# lvcreate -n LV1 -L 1.5G Vg1
Logical volume "LV1" created.

-n 指定LV名称,-L 指定大小

各种查看
#pvs #pvscan #pvdisplay
#vgs #vgscan #vgdisplay
#lvs #lvscan #lvdisplay

[root@localhost ~]# pvdisplay
PV Name /dev/sdb1
VG Name Vg1
PV Size 1.00 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 255
Free PE 0
Allocated PE 255
PV UUID CdTAEq-AdQ1-eyoO-ZBZu-EIh0-2J3a-FeMWdv

指定PE的大小:-s:
[root@localhost ~]# vgcreate -s 16M VGrm /dev/sdb3
Physical volume "/dev/sdb3" successfully created
Volume group "VGrm" successfully created

使用LVM:
[root@localhost ~]# mkfs.xfs /dev/Vg1/LV1
[root@localhost ~]# mkdir /lv1
[root@localhost ~]# mount /dev/Vg1/LV1 /lv1/

LV扩容:
首先,确定一下是否有可用的扩容空间
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VGrm 1 0 0 wz--n- 1008.00m 1008.00m
Vg1 2 1 0 wz--n- 1.99g 504.00m
rhel 1 2 0 wz--n- 12.00g 4.00m

扩容逻辑卷
[root@localhost ~]# lvextend -L +300M /dev/Vg1/LV1
Size of logical volume Vg1/LV1 changed from 1.50 GiB (384 extents) to 1.79 GiB (459 extents).
Logical volume LV1 successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
LV1 Vg1 -wi-ao---- 1.79g
root rhel -wi-ao---- 10.00g
swap rhel -wi-ao---- 2.00g

RHEL7对文件系统进行扩容
[root@localhost ~]# xfs_growfs /dev/Vg1/LV1
RHEL6对文件系统进行扩容
[root@localhost ~]# resize2fs /dev/Vg1/LV1

VG扩容:
[root@localhost ~]# vgextend Vg1 /dev/sdb3
Physical volume "/dev/sdb3" successfully created
Volume group "Vg1" successfully extended

[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 12.00g 4.00m
/dev/sdb1 Vg1 lvm2 a-- 1020.00m 0
/dev/sdb2 Vg1 lvm2 a-- 1020.00m 204.00m
/dev/sdb3 VGrm lvm2 a-- 1008.00m 1008.00m

LVM缩减
lvm支持在线缩小,但是xfs文件系统不支持在线缩小。btrfs支持在线缩小

扩展:
Btrfs 简介
一直使用 ext2/3,ext 文件系统以其卓越的稳定性成为了事实上的 Linux 标准文件系统。近年来 ext2/3 暴露出了一些扩展性问题,于是便催生了 ext4 。 ext4 的作者 Theodore Tso 也盛赞 btrfs 并认为 btrfs 将成为下一代 Linux 标准文件系统。
btrfs 的特性
首先,是扩展性 (scalability) 相关的特性,btrfs 最重要的设计目标是应对大型机器对文件系统的扩展性要求。其整体性能而不会随着系统容量的增加而降低。
其次是数据一致性 (data integrity) 相关的特性。
第三是和多设备管理相关的特性。 Btrfs 支持创建快照 (snapshot),和克隆 (clone) 。
最后,总结一些特性: 这些特性都是比较先进的技术,能够显著提高文件系统的时间 / 空间性能,包括延迟分配,小文件的存储优化,目录索引等。

LV可以缩减
把LV1缩减到1G
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
LV1 Vg1 -wi-ao---- 1.79g
root rhel -wi-ao---- 10.00g
swap rhel -wi-ao---- 2.00g

[root@localhost ~]# lvreduce -L 1G /dev/Vg1/LV1
WARNING: Reducing active and open logical volume to 1.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LV1? [y/n]: y
Size of logical volume Vg1/LV1 changed from 1.79 GiB (459 extents) to 1.00 GiB (256 extents).
Logical volume LV1 successfully resized.

VG缩减:
注:缩减时,可以不卸载正在使用中的LV。另外,只能缩减没有被使用的pv。否则会提示以下内容:
[root@localhost ~]# vgreduce Vg1 /dev/sdb1
Physical volume "/dev/sdb1" still in use

缩减之前先确认物理卷是否被使用
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 12.00g 4.00m
/dev/sdb1 Vg1 lvm2 a-- 1020.00m 0
/dev/sdb2 Vg1 lvm2 a-- 1020.00m 1016.00m
/dev/sdb3 VGrm lvm2 a-- 1008.00m 1008.00m

[root@localhost ~]# vgreduce Vg1 /dev/sdb2
Removed "/dev/sdb5" from volume group "Vg1"

LVM删除
删除之前必须把设备进行卸载,否则会产生以下错误
[root@localhost ~]# lvremove /dev/Vg1/LV1
Logical volume Vg1/LV1 contains a filesystem in use.

卸载设备
[root@localhost ~]# umount /dev/Vg1/LV1
删除LV
[root@localhost ~]# lvremove /dev/Vg1/LV1
Do you really want to remove active logical volume LV1? [y/n]: y
Logical volume "LV1" successfully removed
删除VG
[root@localhost ~]# vgremove Vg1
Volume group "Vg1" successfully removed
删除PV
[root@localhost ~]# pvremove /dev/sdb{1,2,}
Labels on physical volume "/dev/sdb1" successfully wiped
Labels on physical volume "/dev/sdb2" successfully wiped

LVM快照
lvm快照有两大用途,一是用来克隆虚拟机,例如做xen虚拟机时,可先新建一台完整虚拟机,假设大小为10G,然后我们可以在这台10G的虚拟机上创建1个3G的快照,接着通过这个3G的快照启动虚拟机,做完实验后,可以直接删除快照而保持原来完整虚拟机的纯净。
lvm快照的第二大用途是实时备份(moment-in-time),即为了保持系统的一致性,我们先做一个快照冻结当前系统状态,这样快照里面的内容可暂时保持不变,系统本身继续运行,通过备份快照来实现不中断服务的的备份。
首先准备一个LV
[root@localhost ~]# vgcreate vg1 /dev/sdb{1,2}
[root@localhost ~]# lvcreate -n lv1 -L 1.5G vg1

格式化lv1并进行挂载
[root@localhost ~]# mkfs.xfs /dev/vg1/lv1
[root@localhost ~]# mount /dev/vg1/lv1 /lv1/
准备一个测试文件
[root@localhost ~]# cp /etc/passwd /lv1/
[root@localhost ~]# ls /lv1/
passwd

针对lv1创建一个300M快照
lvcreate -s -n 快照名 -L 快照大小 创建快照的设备
[root@localhost ~]# lvcreate -s -n lv1_sp -L 300M vg1/lv1
Logical volume "lv1_sp" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 10.00g
swap rhel -wi-ao---- 2.00g
lv1 vg1 owi-aos--- 1.50g
lv1_sp vg1 swi-a-s--- 300.00m lv1 0.00

使用快照
[root@localhost ~]# mount /dev/vg1/lv1_sp /opt/
mount: wrong fs type, bad option, bad superblock on /dev/mapper/vg1-lv1_sp,
missing codepage or helper program, or other error

   In some cases useful info is found in syslog - try
   dmesg | tail or so.

[root@localhost ~]# umount /dev/vg1/lv1
[root@localhost ~]# mount /dev/vg1/lv1_sp /opt/
[root@localhost ~]# ls /opt/
passwd

系统存储管理器的使用
系统存储管理器(又称ssm)是RHEL7/CentOS7 新增的功能,是一种统一的命令界面,由红帽公司开发,用于管理各种各样的存储设备。目前,有三种可供ssm使用的卷管理后端:LVM、brtfs和crypt

1)安装ssm系统存储管理器
[root@localhost ~]# yum -y install system-storage-manager.noarch

查看ssm命令的帮助信息:
[root@localhost ~]# ssm --help
usage: ssm [-h] [--version] [-v] [-f] [-b BACKEND] [-n]
{check,resize,create,list,add,remove,snapshot,mount} ...

System Storage Manager

optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
-v, --verbose Show aditional information while executing.
-f, --force Force execution in the case where ssm has some doubts
or questions.
-b BACKEND, --backend BACKEND
Choose backend to use. Currently you can choose from
(lvm,btrfs,crypt).
-n, --dry-run Dry run. Do not do anything, just parse the command
line options and gather system information if
necessary. Note that with this option ssm will not
perform all the check as some of them are done by the
backends themselves. This option is mainly used for
debugging purposes.

Commands:
{check,resize,create,list,add,remove,snapshot,mount}
check Check consistency of the file system on the device.
resize Change or set the volume and file system size.
create Create a new volume with defined parameters.
list List information about all detected, devices, pools,
volumes and snapshots in the system.
add Add one or more devices into the pool.
remove Remove devices from the pool, volumes or pools.
snapshot Take a snapshot of the existing volume.
mount Mount a volume with file system to specified locaion.
列出:设备
[root@localhost ~]# ssm list

Device Free Used Total Pool Mount point

/dev/sda 20.00 GB PARTITIONED
/dev/sda1 500.00 MB /boot
/dev/sda2 40.00 MB 19.47 GB 19.51 GB rhel
/dev/sdb 20.00 GB
/dev/sdb1 3.20 GB 1.79 GB 5.00 GB vg1
/dev/sdb2 5.00 GB 0.00 KB 5.00 GB vg1
/dev/sdb3 4.98 GB 0.00 KB 5.00 GB VGrm
/dev/sdc 20.00 GB
/dev/sdd 20.00 GB


##列出存储池
Pool Type Devices Free Used Total

VGrm lvm 1 4.98 GB 0.00 KB 4.98 GB
rhel lvm 1 40.00 MB 19.47 GB 19.51 GB
vg1 lvm 2 8.20 GB 1.79 GB 9.99 GB


##列出卷
Volume Pool Volume size FS FS size Free Type Mount point

/dev/rhel/root
rhel 17.47 GB xfs 17.46 GB 16.26 GB linear /
/dev/rhel/swap
rhel 2.00 GB linear
/dev/vg1/lv1 vg1 1.50 GB xfs 1.49 GB 1.49 GB linear
/dev/sda1 500.00 MB xfs 496.67 MB 397.52 MB part /boot


##列出快照
Snapshot Origin Pool Volume size Size Type

/dev/vg1/lv1_sp lv1 vg1 300.00 MB 2.01 MB linear

将物理磁盘sdc 添加到LVM存储池
语法格式:ssm add -p 存储池 设备
[root@localhost ~]# ssm add -p vg1 /dev/sdc
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VGrm 1 0 0 wz--n- 1008.00m 1008.00m
rhel 1 2 0 wz--n- 12.00g 4.00m
vg1 3 2 1 wz--n- 21.99g 20.20g

扩容LV(不能针对创建了快照的lv进行扩容)
[root@localhost ~]# ssm add -p rhel /dev/sdd
[root@localhost ~]# ssm resize -s+10G /dev/rhel/root

注:
使用ssm存储管理器扩容,不需要再针对文进系统进行扩容

实例:
创建一个名为vg2的存储池,并在其上创建一个名为lv2,大小为1G的lvm卷,格式化为xfs文件系统,并将其挂载/lv2目录下

创建目录
[root@localhost ~]# mkdir /lv2

[root@localhost ~]# ssm create -s 1G -n lv2 --fstype xfs -p vg2 /dev/sde /lv2
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 40535: /usr/bin/python
Physical volume "/dev/sde" successfully created
Volume group "vg2" successfully created
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 40535: /usr/bin/python
Logical volume "lv2" created.
meta-data=/dev/vg2/lv2 isize=256 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

查看挂载状况
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 20G 3.2G 17G 16% /
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 84K 2.0G 1% /dev/shm
tmpfs 2.0G 9.0M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sr0 3.8G 3.8G 0 100% /mnt
/dev/sda1 197M 125M 72M 64% /boot
tmpfs 394M 16K 394M 1% /run/user/42
tmpfs 394M 0 394M 0% /run/user/0
/dev/mapper/vg1-lv1_sp 1.5G 33M 1.5G 3% /opt
/dev/mapper/vg2-lv2 1014M 33M 982M 4% /lv2

创建快照
[root@localhost ~]# ssm snapshot -s 500M -n lv2_sp /dev/vg2/lv2
使用快照
[root@localhost ~]# umount /lv2/
[root@localhost ~]# mkdir /lv2_snapshot
[root@localhost ~]# mount /dev/vg2/lv2_sp /lv2_snapshot/

移除LVM卷
[root@localhost ~]# ssm remove /dev/vg2/lv2_sp
Device '/dev/vg2/lv2_sp' is mounted on '/lv2_snapshot' Unmount (N/y/q) ? Y
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 40749: /usr/bin/python
Do you really want to remove active logical volume lv2_sp? [y/n]: y
Logical volume "lv2_sp" successfully removed

注:删除已经挂载的卷,ssm会自动先将其卸载

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!