这篇我们来认识一下lvm
它是Linux环境下对磁盘分区进行管理的一种机制,Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。
这篇我们主要讲
1.lvm的基本理解
2.针对不同文件系统lvm设备的扩大和缩减
3.lvm的快照
4.lvm的删除
(一)lvm的基本理解
一.lvm名词定义
lvm 逻辑卷,可以让设备自由伸缩的一个设备管理软件pv 物理卷 (安装lvm软件的物理分区为物理卷)
vg 物理卷组 (多个物理卷的组合)
pe 物理拓展,lvm最小的存储单元 (物理卷中的一个单位)
lvm 逻辑卷,时最终我们使用的可以伸缩的设备 (最后可使用的设备)
二.lvm的制作
0.[root@foundation114 ~]# watch -n 1 'pvs; echo ------------;vgs;echo -----------;lvs;echo -----------; df -h /zl' ##监控命令
1.[root@foundation114 ~]# fdisk /dev/vdb ##新建分区
Command (m for help): p
/dev/vdb1 2048 1026047 512000 83 Linux
/dev/vdb2 1026048 1640447 307200 83 Linux
2.Command (m for help): t ##修改分区标签为lvm
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Device Boot Start End Blocks Id System
/dev/vdb1 2048 1026047 512000 8e Linux LVM
/dev/vdb2 1026048 1640447 307200 8e Linux LVM
3.[root@foundation114 ~]# pvcreate /dev/vdb1 ##在物理分区中安装lvm软件创建物理卷
Physical volume "/dev/vdb1" successfully created
4.[root@foundation114 ~]# vgcreate vg0 /dev/vdb1 ## 用制作好的/dev/vdb1这个物理卷制作一个物理卷组 名字为vg0(自定义)
Volume group "vg0" successfully created
5.[root@foundation114 ~]# lvcreate -L 100M -n lv0 vg0 ##名字为lv0(自定义名字)在vg0这个组中建立出lv0设备 -L 指定设备大小 -n 指定设备名字
Logical volume "lv0" created
6.[root@foundation114 ~]# mkfs.xfs /dev/vg0/lv0 ##格式化设备
meta-data=/dev/vg0/lv0 isize=256 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25600, 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
7.[root@foundation114 ~]# mount /dev/vg0/lv0 /zl ##挂载设备 使设备投入使用
PV VG Fmt Attr PSize PFree ##物理卷组
/dev/vdb1 vg0 lvm2 a-- 496.00m 396.00m
------------
VG #PV #LV #SN Attr VSize VFree ##逻辑卷
vg0 1 1 0 wz--n- 496.00m 396.00m
-----------
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert ##lvm设备
lv0 vg0 -wi-ao---- 100.00m
-----------
Filesystem Size Used Avail Use% Mounted on ##lvm的文件系统
/dev/mapper/vg0-lv0 97M 5.2M 92M 6% /zl
(二)针对不同文件系统lvm设备的扩大和缩减
注意:扩大时 先修改设备再修改文件系统
缩减时 先修改文件系统再修改设备
在格式化设备时 要卸载设备
一.针对xfs文件系统
(1)当vg中容量剩余充足的时候
1.[root@foundation114 ~]# lvextend -L 400M /dev/vg0/lv0 ##扩大lvm设备到400M
Extending logical volume lv0 to 400.00 MiB
Logical volume lv0 successfully resized
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 496.00m 96.00m
------------
VG #PV #LV #SN Attr VSize VFree
vg0 1 1 0 wz--n- 496.00m 96.00m
-----------
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert ##lvm扩大到400M
lv0 vg0 -wi-ao---- 400.00m
-----------
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 97M 5.2M 92M 6% /zl ##文件系统不会直接随着设备大小的改变而改变 需要手动更改
2.[root@foundation114 ~]# xfs_growfs /dev/vg0/lv0 ##设定文件系统大小和设备同步
meta-data=/dev/mapper/vg0-lv0 isize=256 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 25600 to 102400
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 496.00m 96.00m
------------
VG #PV #LV #SN Attr VSize VFree
vg0 1 1 0 wz--n- 496.00m 96.00m
-----------
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 400.00m
-----------
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 397M 5.6M 392M 2% /zl
(2)当vg中剩余容量不足的时候
1.[root@foundation114 ~]# pvcreate /dev/vdb2 ##在pv中增加一块设备/dev/vdb2
Physical volume "/dev/vdb2" successfully created
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 496.00m 96.00m
/dev/vdb2 lvm2 a-- 300.00m 300.00m
2.[root@foundation114 ~]# vgextend vg0 /dev/vdb2 ##把新建立的/dev/vdb2添加到vg0中
Volume group "vg0" successfully extended
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 496.00m 96.00m
/dev/vdb2 vg0 lvm2 a-- 296.00m 296.00m
------------
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 792.00m 392.00m
3.[root@foundation114 ~]# lvextend -L 700M /dev/vg0/lv0 ##扩大lvm设备到700M
Extending logical volume lv0 to 700.00 MiB
Logical volume lv0 successfully resized
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 496.00m 0
/dev/vdb2 vg0 lvm2 a-- 296.00m 92.00m
------------
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 792.00m 92.00m
-----------
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 700.00m
-----------
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 397M 5.6M 392M 2% /zl
4.[root@foundation114 ~]# xfs_growfs /dev/vg0/lv0 ##设定文件系统大小和设备同步
meta-data=/dev/mapper/vg0-lv0 isize=256 agcount=16, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=102400, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 102400 to 179200
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 496.00m 0
/dev/vdb2 vg0 lvm2 a-- 296.00m 92.00m
------------
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 792.00m 92.00m
-----------
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 700.00m
-----------
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 697M 6.0M 691M 1% /zl
二.针对xfs文件系统
0.[root@foundation114 ~]# umount /dev/vg0/lv0 ####卸载设备,因为文件系统拉伸缩减不支持在线操作
[root@foundation114 ~]# mkfs.ext4 /dev/vg0/lv0 ##格式化文件 将xfs改为ext4
[root@foundation114 ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb1: UUID="h4g0uU-8CXy-DaC8-2dxW-Icpb-LzfV-LznnT4" TYPE="LVM2_member"
/dev/vdb2: UUID="NU2g9M-r31f-hIxo-PLSt-VJzY-gJr3-f1P1n6" TYPE="LVM2_member"
/dev/mapper/vg0-lv0: UUID="bec1c795-d3e5-4154-9deb-9c4a39bc3cd0" TYPE="ext4"
(1)对设备
##拉伸
1.[root@foundation114 ~]# lvextend -L 750M /dev/vg0/lv0 ##扩大lvm设备到750M
Rounding size to boundary between physical extents: 752.00 MiB
Extending logical volume lv0 to 752.00 MiB
Logical volume lv0 successfully resized
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 752.00m
-----------
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 673M 1.4M 623M 1% /zl ##文件系统没有改变
2.[root@foundation114 ~]# resize2fs /dev/vg0/lv0 ##设定文件系统大小和设备同步
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /zl; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv0 is now 192512 blocks long.
-----------
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 752.00m
-----------
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 725M 1.4M 675M 1% /zl
##缩减
1.[root@foundation114 ~]# umount /zl ##卸载设备,因为文件系统缩减不支持在线操作
2.[root@foundation114 ~]# e2fsck -f /dev/vg0/lv0 ##检测文件系统,确定文件系统中已有数据大小防止过度缩减
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/vg0/lv0: 11/44832 files (0.0% non-contiguous), 7272/192512 blocks
3.[root@foundation114 ~]# resize2fs /dev/vg0/lv0 200M ##把文件系统缩减到200M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 51200 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 51200 blocks long.
4.[root@foundation114 ~]# mount /dev/vg0/lv0 /zl ##挂载设备 查看文件系统大小
-----------
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 752.00m
-----------
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 181M 720K 167M 1% /zl
5.[root@foundation114 ~]# lvreduce -L 200M /dev/vg0/lv0 ##设备缩减到200M
WARNING: Reducing active and open logical volume to 200.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
Reducing logical volume lv0 to 200.00 MiB
Logical volume lv0 successfully resized
-----------
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 200.00m
-----------
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 181M 720K 167M 1% /zl
(2)对卷组
0.当要移除分区 需要把分区中的数据迁移到其他的物理卷(pv)中
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 496.00m 296.00m ##使用了200M
/dev/vdb2 vg0 lvm2 a-- 296.00m 296.00m ##没有使用
[root@foundation114 ~]# pvmove /dev/vdb1 /dev/vdb2 ##将1中数据迁移到2中
/dev/vdb1: Moved: 22.0%
/dev/vdb1: Moved: 100.0%
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 496.00m 496.00m ##没有使用
/dev/vdb2 vg0 lvm2 a-- 296.00m 96.00m ##使用了200M
1.[root@foundation114 ~]# vgreduce vg0 /dev/vdb1 ##把vg0中的/dev/vdb1删除
Removed "/dev/vdb1" from volume group "vg0"
PV VG Fmt Attr PSize PFree
/dev/vdb1 lvm2 a-- 500.00m 500.00m
/dev/vdb2 vg0 lvm2 a-- 296.00m 96.00m
------------
VG #PV #LV #SN Attr VSize VFree
vg0 1 1 0 wz--n- 296.00m 96.00m ##逻辑卷中pv只剩一个了
-----------
2.[root@foundation114 ~]# pvremove /dev/vdb1 ##把/dev/vdb1上的lvm管理信息删除
Labels on physical volume "/dev/vdb1" successfully wiped
PV VG Fmt Attr PSize PFree ##没有/dev/vdb1信息
/dev/vdb2 vg0 lvm2 a-- 296.00m 96.00m
------------
VG #PV #LV #SN Attr VSize VFree
vg0 1 1 0 wz--n- 296.00m 96.00m
(四)lvm的快照
0.在设备挂载目录下建立文件
[root@foundation114 zl]# touch file{1..5}
[root@foundation114 zl]# ls
file1 file2 file3 file4 file5 lost+found
1.[root@foundation114 ~]# umount /zl ##卸载设备 使设备中的原始数据不被更改
2.[root@foundation114 ~]# lvcreate -L 20M -n lv0backup -s /dev/vg0/lv0 ##给/dev/vg0/lv0建立快照,-L 指定快照大小,-n指定快照名字 -s指定快照模板设备
Logical volume "lv0backup" created
3.[root@foundation114 ~]# mount /dev/vg0/lv0backup /zl ##将快照挂载在目录上 因为快照看的时原设备的文件 所以不需要格式化快照
4.[root@foundation114 ~]# cd /zl
[root@foundation114 zl]# ls
file1 file2 file3 file4 file5 lost+found
注意:当快照设备里的数据被更改,如果想还原,删除快照设备,在重新建立快照即可
0.[root@foundation114 zl]# rm -fr *
[root@foundation114 zl]#ls
1.[root@foundation114 ~]# umount /zl ##卸载设备
2.[root@foundation114 ~]# lvremove /dev/vg0/lv0backup##移除快照
Do you really want to remove active logical volume lv0backup? [y/n]: y
Logical volume "lv0backup" successfully removed
3.[root@foundation114 ~]# lvcreate -L 20M -n lv0backup -s /dev/vg0/lv0##新建快照
Logical volume "lv0backup" created
4.[root@foundation114 ~]# mount /dev/vg0/lv0backup /zl##挂载设备
[root@foundation114 ~]# cd /zl ##查看文件 已恢复
[root@foundation114 zl]# ls
file1 file2 file3 file4 file5 lost+found
(五)删除lvm
0.状态显示
一.正常删除
1.[root@foundation114 ~]# umount /zl ##卸载设备
2.[root@foundation114 ~]# lvremove /dev/vg0/lv0backup ##移除lvm
Do you really want to remove active logical volume lv0backup? [y/n]: y
Logical volume "lv0backup" successfully removed
[root@foundation114 ~]# lvremove /dev/vg0/lv0
Do you really want to remove active logical volume lv0? [y/n]: y
Logical volume "lv0" successfully removed
3.[root@foundation114 ~]# vgremove /dev/vg0 ##移除vg
Volume group "vg0" successfully removed
4.[root@foundation114 ~]# pvremove /dev/vdb2 ##移除pv
Labels on physical volume "/dev/vdb2" successfully wiped
[root@foundation114 ~]# pvremove /dev/vdb1
Labels on physical volume "/dev/vdb1" successfully wiped
5.[root@foundation114 ~]# fdisk /dev/vdb ##删除分区
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): d
Selected partition 1
Partition 1 is deleted
6.[root@foundation114 ~]# fdisk -l ##查看 已删除
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x8466bcb2
Device Boot Start End Blocks Id System
二.错误删除并排错
0.1.状态
0.2.[root@foundation114 ~]# fdisk /dev/vdb ##再未将设备移除lvm vg pv时 直接删除分区
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): d Partition number (1,2, default 2): 2 Partition 2 is deleted [root@foundation114 ~]# partprobe ##同步分区表后 ##出现如下报错 设备丢失![]()
###解决
[root@foundation114 ~]# vgreduce vg0 --removemissing ##删除vg0中丢失的设备
PV rs6qJv-IZmF-2gmb-ZGul-aLkB-0etA-2XcI7V not recognised. Is the device missing?
PV rs6qJv-IZmF-2gmb-ZGul-aLkB-0etA-2XcI7V not recognised. Is the device missing?
Wrote out consistent volume group vg0
##删除所有报错设备
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 496.00m 196.00m
------------
VG #PV #LV #SN Attr VSize VFree
vg0 1 1 0 wz--n- 496.00m 196.00m
-----------
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 300.00m
-----------
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 297M 16M 282M 6% /zl