标签:高级文件系统管理之lvm
LVM简介及常用管理
LVM 是 Logical Volume Manager(逻辑卷管理)的简写,它是 Linux 环境下对磁盘分区进行管理的一种机制。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻
辑分区存放不下某个文件时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。简单来说 LVM 就是将物理磁盘融合成一个或几个大的虚拟磁盘存储池,按照我们的需求去存储池划分空间来使用,由于是虚拟的存储池,所以划分空间时可以自由的调整大小,如下图:
(1)PV(Physical Volume)- 物理卷
物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘,也可以是raid设备。
(2)VG(Volumne Group)- 卷组
卷组建立在物理卷之上,一个卷组中至少要包括一个物理卷,在卷组建立之后可动态添加物理卷到卷组中。一个逻辑卷管理系统工程中可以只有一个卷组,也可以拥有多个卷组。
(3)LV(Logical Volume)- 逻辑卷
逻辑卷建立在卷组之上,卷组中的未分配空间可以用于建立新的逻辑卷,逻辑卷建立后可以动态地扩展和缩小空间。系统中的多个逻辑卷可以属于同一个卷组,也可以属于不同的多个卷组。
(4)PE(Physical Extent)- 物理块
LVM 默认使用4MB的PE区块,而LVM的LV最多仅能含有65534个PE (lvm1 的格式),因此默认的LVM的LV最大容量为4M*65534/(1024M/G)=256G。PE是整个LVM 最小的储存区块,也就是说,其实我们的资料都是由写入PE 来处理的。简单的说,这个PE 就有点像文件系统里面的block 大小。所以调整PE 会影响到LVM 的最大容量!不过,在 CentOS 6.x 以后,由于直接使用 lvm2 的各项格式功能,因此这个限制已经不存在了。
(1)添加新的物理磁盘
(2)将物理磁盘分区,或整块硬盘分成一个区
(3)将物理磁盘上的分区建立成物理卷(PV)
(4)将创建好的物理卷组合成卷组(VG),卷组可以随意调整大小,添加或者
删除一块物理卷,来实现卷组大小的调整。
(5)最后将卷组划分成逻辑卷(LV),逻辑卷也是可以随意调整大小的,逻辑
卷相当于真正的分区,那么要使用必须进行格式化和挂载。
这里我添加了三块磁盘,分别是/dev/sdb,/dev/sdc,/dev/sdd
使用fdisk命令对上面的三块磁盘进行分区,我这里把每个硬盘都分一个区。
[root@localhost ~]# fdisk /dev/sdb 给/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 0x47dc9661.
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-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#
[root@localhost ~]# fdisk /dev/sdc 给/dev/sdc硬盘分区
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 0xaa049ee5.
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-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#
[root@localhost ~]# fdisk /dev/sdd 给/dev/sdd硬盘分区
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 0x4f9337af.
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-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#
首先我们检查系统中是否安装lvm2软件,如果没有安装直接使用下面的命令安装即可:
yum -y install lvm2
提示:创建物理卷的可以是单个分区,也可以是整个硬盘。
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created.
[root@localhost ~]# pvcreate /dev/sdd1
Physical volume "/dev/sdd1" successfully created.
[root@localhost ~]# pvscan
PV /dev/sdd1 lvm2 [<5.00 GiB]
PV /dev/sdb1 lvm2 [<5.00 GiB]
PV /dev/sdc1 lvm2 [<5.00 GiB]
Total: 3 [<15.00 GiB] / in use: 0 [0 ] / in no VG: 3 [<15.00 GiB]
最后一行:总共 3 个物理卷,使用了 0 个,空闲 3个卷
下面的命令是:更详细的列出了物理卷的信息
[root@localhost ~]# pvdisplay
"/dev/sdd1" is a new physical volume of "<5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdd1
VG Name
PV Size <5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Bg9vSv-mYCS-wRXq-YszN-z68V-M1sB-b2zh3V
"/dev/sdb1" is a new physical volume of "<5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Fjqh0Q-3qhu-ygEx-7Fvf-io2t-31hs-Iuk3PK
"/dev/sdc1" is a new physical volume of "<5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size <5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID W4bLbn-a2Uu-5JYn-3UIn-tnFJ-5uMH-h3YueV
命令:pvremove 分区设备名称
[root@localhost ~]# pvremove /dev/sdb1
Labels on physical volume "/dev/sdb1" successfully wiped.
[root@localhost ~]# pvremove /dev/sdc1
Labels on physical volume "/dev/sdc1" successfully wiped.
[root@localhost ~]# pvremove /dev/sdd1
Labels on physical volume "/dev/sdd1" successfully wiped.
命令:vgcreate 选项 卷组名 物理卷名
-s 大小:指定 VG 的 PE 大小,即硬盘的 block 的大小
[root@localhost ~]# vgcreate -s 8K vg1 /dev/sdb1 /dev/sdc1
Volume group "vg1" successfully created
上面是使用/dev/sdb1和/dev/sdc1两块磁盘创建的卷组
[root@localhost ~]# vgscan
Reading volume groups from cache.
Found volume group "vg1" using metadata type lvm2
更详细的列出创建的卷组信息
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vg1
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 KiB
Total PE 1310208
Alloc PE / Size 0 / 0
Free PE / Size 1310208 / <10.00 GiB
VG UUID Qv12IJ-diVK-96ED-0BGg-d3qp-KGzV-K4jQiX
命令:vgextend 卷组名 物理卷名
这里把/dev/sdd1磁盘加入vg1卷组中。
[root@localhost ~]# vgextend vg1 /dev/sdd1
Volume group "vg1" successfully extended
查看卷组,可以发现容量变大了。
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vg1
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.99 GiB
PE Size 8.00 KiB
Total PE 1965312
Alloc PE / Size 0 / 0
Free PE / Size 1965312 / 14.99 GiB
VG UUID Qv12IJ-diVK-96ED-0BGg-d3qp-KGzV-K4jQiX
命令:vgreduce 卷组名 物理卷名
这里把刚刚上面添加进去的/dev/sdd1磁盘删除掉,让卷组容量减小。
[root@localhost ~]# vgreduce vg1 /dev/sdd1
Removed "/dev/sdd1" from volume group "vg1"
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name vg1
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 <10.00 GiB
PE Size 8.00 KiB
Total PE 1310208
Alloc PE / Size 0 / 0
Free PE / Size 1310208 / <10.00 GiB
VG UUID Qv12IJ-diVK-96ED-0BGg-d3qp-KGzV-K4jQiX
删除指定卷组内所有空的物理卷
vgreduce -a 卷组名
命令:vgremove 卷组
注:删除时顺序正好是倒序,先创建的先删除,指的是 pv vg lv之间。
[root@localhost ~]# vgremove /dev/vg1
Volume group "vg1" successfully removed
命令:lvcreate 选项 -n 逻辑卷名 卷组名
-L 容量:指定创建的逻辑卷大小,单位:MB,GB,TB 等
-l 个数:按照 PE 个数来指定逻辑卷大小, 算数。。。
-n 逻辑卷名:指定逻辑卷叫什么名
[root@localhost ~]# lvcreate -L 3G -n lv1 vg1
Logical volume "lv1" created.
[root@localhost ~]# lvcreate -L 3.8G -n lv2 vg1
Logical volume "lv2" created.
上面分别创建了两个逻辑卷,一个是lv1逻辑卷3G大小,另外一个是lv2逻辑卷3.8G大小。逻辑卷创建完成后,因为逻辑卷相当于是分区,所以需要进行格式化和挂载才能使用
[root@localhost ~]# mkfs.xfs /dev/vg1/lv1
meta-data=/dev/vg1/lv1 isize=512 agcount=4, agsize=196608 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=786432, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
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 ~]# mkfs.xfs /dev/vg1/lv2
meta-data=/dev/vg1/lv2 isize=512 agcount=4, agsize=393216 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1572864, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
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 ~]# mkdir /mnt/lv1 /mnt/lv2 创建逻辑卷挂载目录
[root@localhost ~]# mount /dev/vg1/lv1 /mnt/lv1 挂载逻辑卷lv1
[root@localhost ~]# mount /dev/vg1/lv2 /mnt/lv2 挂载逻辑卷lv2
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.2G 17G 7% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 297M 114M 183M 39% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 3.0G 33M 3.0G 2% /mnt/lv1 这个就是挂载的逻辑卷lv1
/dev/mapper/vg1-lv2 3.8G 33M 3.8G 1% /mnt/lv2 这个就是挂载的逻辑卷lv2
现在上面创建的两个逻辑卷lv1和lv2都可以像正常硬盘一样使用了
需要开机自动挂载,我们可以把逻辑卷挂载信息写入/etc/fstab文件中,也可以把挂载命令写入/etc/rc.local里面即可。
[root@localhost ~]# lvscan
ACTIVE ‘/dev/vg1/lv1‘ [3.00 GiB] inherit
ACTIVE ‘/dev/vg1/lv2‘ [3.80 GiB] inherit
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID mBSwDB-nc7C-MT0L-EZNw-JEDH-pXUU-TaokJu
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2017-11-18 13:41:18 +0800
LV Status available
# open 1
LV Size 3.00 GiB
Current LE 393216
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/vg1/lv2
LV Name lv2
VG Name vg1
LV UUID cyInJi-acnA-026I-oMC1-2Knx-zdTQ-y9jrvb
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2017-11-18 13:41:43 +0800
LV Status available
# open 1
LV Size 3.80 GiB
Current LE 786432
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
命令:lvresize 选项 逻辑卷设备文件名
-L 容量:按照容量调整大小,单位 KB,GB,TB 等,使用 + - 代表增加和减少多少容量;如果直接写大小没有+-,那么为指定逻辑卷大小。
-l 个数:根据 PE 数调整大小
lvextend是扩展逻辑卷,lvreduce是缩小逻辑卷,lvresize是扩展或缩小逻辑卷
使用 + - 代表增加和减少多少容量;如果直接写大小没有+-,那么为指定逻辑卷大小。
特别注意:
resize2fs命令针对的是ext2、ext3、ext4文件系统(增大和减小分区都支持)
xfs_growfs命令针对的是xfs文件系统(只支持增大分区)
下面是以xfs为例进行的操作:
[root@localhost lv2]# cp /etc/hosts ./
[root@localhost lv2]# cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]# lvextend -L 4.5G /dev/vg1/lv2 把逻辑卷lv2变大成4.5G
Size of logical volume vg1/lv2 changed from 3.80 GiB (498074 extents) to 4.50 GiB (589824 extents).
Logical volume vg1/lv2 successfully resized.
[root@localhost ~]# df –h 查看挂载的分区大小,发现lv2还是3.8G
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.2G 17G 7% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 297M 114M 183M 39% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 3.0G 33M 3.0G 2% /mnt/lv1
/dev/mapper/vg1-lv2 3.8G 33M 3.8G 1% /mnt/lv2
[root@localhost ~]# xfs_growfs /dev/vg1/lv2 这里需要执行这个命令大小调整才能生效
meta-data=/dev/mapper/vg1-lv2 isize=512 agcount=4, agsize=249037 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=996148, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 996148 to 1179648
[root@localhost ~]# df –h 再次查看分区大小,发现分区已经变大了。
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.2G 17G 7% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 297M 114M 183M 39% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 3.0G 33M 3.0G 2% /mnt/lv1
/dev/mapper/vg1-lv2 4.5G 33M 4.5G 1% /mnt/lv2
[root@localhost ~]# cat /mnt/lv2/hosts 可以看到数据完好
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
把分区调小演示(无法调小,后面会报错):
[root@localhost ~]# lvresize -L 4G /dev/vg1/lv2
Size of logical volume vg1/lv2 changed from 2.50 GiB (327680 extents) to 4.00 GiB (524288 extents).
Logical volume vg1/lv2 successfully resized.
[root@localhost ~]# xfs_growfs /dev/vg1/lv2
meta-data=/dev/mapper/vg1-lv2 isize=512 agcount=5, agsize=249037 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1179648, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data size 1048576 too small, old size is 1179648 发现这里报错
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.2G 17G 7% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 297M 114M 183M 39% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/mapper/vg1-lv1 3.0G 33M 3.0G 2% /mnt/lv1
/dev/mapper/vg1-lv2 4.5G 33M 4.5G 1% /mnt/lv2 分区还是这么大,没变
注意:xfs系统只支持lvm逻辑卷增大,不支持减小。并不是说xfs文件系统下不能减小,只是减小后需要重新格式化才能挂载上,这样原来的数据就会丢失。
xfs系统lvm逻辑卷分区如果想减小,就先使用umount卸载该分区,然后执行lvresize命令调整大小,之后再使用mkfs.xfs格式化该分区,然后再挂载上即可。
命令:lvremove 逻辑卷名
注意:删除lvm逻辑卷时一定要先卸载
[root@localhost ~]# umount /mnt/lv1
[root@localhost ~]# umount /mnt/lv2
[root@localhost ~]# lvremove /dev/vg1/lv1
Do you really want to remove active logical volume vg1/lv1? [y/n]: y
Logical volume "lv1" successfully removed
[root@localhost ~]# lvremove /dev/vg1/lv2
Do you really want to remove active logical volume vg1/lv2? [y/n]: y
Logical volume "lv2" successfully removed
本文出自 “成长中的强大” 博客,请务必保留此出处http://longlei.blog.51cto.com/12685458/1983078
标签:高级文件系统管理之lvm
原文地址:http://longlei.blog.51cto.com/12685458/1983078