前提要求:
虚拟机:centos7 虚拟机软件:VMware Workstation 12
在安装Centos系统时,磁盘选择为LVM逻辑卷。当选择为LVM后才能创建逻辑卷等(必须)
数据格式选择的是xfs,很多讲解使用的是ext3,但以后xfs的优势会比ext越来越大。(可选)
参考原博主文章:https://blog.csdn.net/geoffreychan/article/details/77418111
LVM是Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对硬盘分区进行管理的一种机制。LVM是建立在硬盘和分区上的逻辑层,它允许跨硬盘、分区创建逻辑卷并创建文件系统,可以很方便执行增加、扩展、删除等操作。
1、物理卷
物理卷(Physical Volume,PV):就是指硬盘分区,也可以是整个硬盘或已创建的软RAID,是LVM的基本存储设备。在创建逻辑卷之前需要先创建物理卷,然后将多个物理卷组合创建卷组,最后在卷组的基础上创建逻辑卷。
可以将空白硬盘或分区加入主机,系统识别到新的硬盘sdb后,使用pvcreate对硬盘创建PV。
添加新硬盘或直接扩展分区的方式:直接扩展/或直接新添加一个硬盘
代码实现:
[root@Centos7T ~]#lsblk /dev/sdb #你新建的分区可能存在该分区下
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
[root@Centos7T ~]#fdisk /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 0x6200a0ac.
Command (m for help): p #查看信息
Disk /dev/sdb: 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: 0x6200a0ac
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e # 修改系统ID为 Linux LVM
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdb: 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: 0x6200a0ac
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 8e Linux LVM
Command (m for help): w #保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
#先重启机器再执下一句,否则会找不到新建的磁盘
[root@Centos7T ~]#pvcreate /dev/sdb1 # 创建物理卷
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
[root@Centos7T ~]#pvdisplay
"/dev/sdb1" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
在上面的代码中Hex code (type L to list all codes): 8e
指定分区的系统ID为Linux LVM。空白为分区磁盘直接使用pvcreate /dev/sdb
命令,系统ID自动转成Linux LVM。
2、卷组
卷组(Volume Group,VG):是由一个或多个物理卷所组成的存储池,在卷组上能创建一个或多个逻辑卷。现在使用vgcreate
命令对上文创建的PV(单个物理卷)创建卷组。
代码实现:
[root@Centos7T ~]#vgcreate -s 10M vg0 /dev/sdb1 # 创建卷组vg0,指定物理块PE大小10M,并将pv:/dev/sdb1加入vg0.
Volume group "vg0" successfully created
[root@Centos7T ~]#vgdisplay # 查看所有卷组的信息.后面接具体卷组名,可查看指定卷组信息
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 9.99 GiB
PE Size 10.00 MiB
Total PE 1023
Alloc PE / Size 0 / 0
Free PE / Size 1023 / 9.99 GiB
VG UUID 46Z4tB-d71l-nfir-t6sB-cZPo-SBfl-eFCDIj
[root@Centos7T ~]#pvdisplay # 查看所当前物理卷的信息.后面可接具体物理卷设备,可查看指定物理卷信息
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg0 # 已加入卷组vg0
PV Size 10.00 GiB / not usable 10.00 MiB
Allocatable yes
PE Size 10.00 MiB # pe大小
Total PE 1023 # 当前物理卷的pe总数
Free PE 1023 # 当前物理卷的未被划分逻辑卷的pe总数
Allocated PE 0
PV UUID xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
3、创建逻辑卷
逻辑卷(Logical Volume,LV):类似于非LVM系统中的硬盘分区,它建立在卷组之上,是一个标准的块设备,在逻辑卷之上可以建立文件系统。可以使用lvcreate
命令创建逻辑卷
代码实现:
[root@Centos7T ~]#lvcreate -n lv0 -l 100%vg vg0 # 使用vg0的全部空间创建逻辑卷lv0
Logical volume "lv0" created.
[root@Centos7T ~]#lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID zGNz6W-N4QC-m3ZF-ey3h-mP7i-gFJ3-DjGMX1
LV Write Access read/write
LV Creation host, time Centos7.magedu.com, 2017-08-19 21:27:34 +0800
LV Status available
# open 0
LV Size 9.99 GiB
Current LE 1023
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@Centos7T ~]#pvdisplay
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg0
PV Size 10.00 GiB / not usable 10.00 MiB
Allocatable yes (but full)
PE Size 10.00 MiB
Total PE 1023
Free PE 0
Allocated PE 1023
PV UUID xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
[root@Centos7T ~]#lsblk -f /dev/sdb
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
└─sdb1 LVM2_member xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
└─vg0-lv0
会在/dev下生成一个新的设备文件:
[root@Centos7T ~]#ll /dev/vg0/lv0
lrwxrwxrwx. 1 root root 7 Aug 19 21:27 /dev/vg0/lv0 -> ../dm-0 # 软链接文件
[root@Centos7T ~]#ll /dev/dm-0
brw-rw----. 1 root disk 253, 0 Aug 19 21:27 /dev/dm-0
4、文件系统
在逻辑卷lv0创建完成后话需要创建文件系统并挂载到制定目录才能够使用,当然还需要将挂载信息写入/etc/fstab文件
代码实现:
[root@Centos7T ~]#mkfs.xfs /dev/vg0/lv0 # 创建文件系统
meta-data=/dev/vg0/lv0 isize=512 agcount=4, agsize=654720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2618880, 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@Centos7T ~]#lsblk /dev/sdb -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
└─sdb1 LVM2_member xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
└─vg0-lv0 xfs 549cfbaa-a6dc-4161-a765-5f55af78131a
[root@Centos7T ~]#lsblk /dev/vg0/lv0 -f
NAME FSTYPE LABEL UUID MOUNTPOINT
vg0-lv0 xfs 549cfbaa-a6dc-4161-a765-5f55af78131a
[root@Centos7T ~]#mkdir /mnt/lv0 && mount /dev/vg0/lv0 /mnt/lv0 # 将逻辑卷挂载到制定目录
[root@Centos7T ~]#df -T # 查看挂载点
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda2 xfs 52403200 4605416 47797784 9% /
/dev/sda1 xfs 1038336 172116 866220 17% /boot
/dev/sda5 xfs 52403200 376288 52026912 1% /app
/dev/sr0 iso9660 8086368 8086368 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0 xfs 10465280 32944 10432336 1% /mnt/lv0
[root@Centos7T ~]#blkid /dev/vg0/lv0
/dev/vg0/lv0: UUID="549cfbaa-a6dc-4161-a765-5f55af78131a" TYPE="xfs"
[root@Centos7T ~]#df -T /dev/vg0/lv0
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg0-lv0 xfs 10465280 32944 10432336 1% /mnt/lv0
[root@Centos7T ~]#echo "UUID=549cfbaa-a6dc-4161-a765-5f55af78131a /mnt/lv0 xfs defaults 0 0" >> /etc/fstab
[root@Centos7T ~]#cat /etc/fstab | egrep "UUID"
UUID=806ef114-dff2-4c17-aff7-c95bc67faf40 / xfs defaults 0 0
UUID=3df87bb6-2f6f-4d3f-af94-5ec47d2a9796 /app xfs defaults 0 0
UUID=1e7ba91a-85c8-442b-b105-bc61c00ddbee /boot xfs defaults 0 0
UUID=cc0e9678-d369-4bb9-9039-fd2f322df8b4 swap swap defaults 0 0
UUID=549cfbaa-a6dc-4161-a765-5f55af78131a /mnt/lv0 xfs defaults 0 0
5、扩展逻辑卷
我们可以很方便的多逻辑卷进行扩展,不论是使用新的空白磁盘还是已有的空白分区。其大致实现原理为:对空白分区创建物理卷,然后将物理卷加入到需要扩展逻辑卷所在的卷组,最后扩大逻辑卷空间。如果逻辑卷所在的卷组还有空闲的空间(PE)可被使用且足够大,则甚至不需要另外的分区或者磁盘。
代码实现:
修改系统ID并创建PV
[root@Centos7T ~]#lsblk -f /dev/sdc
NAME FSTYPE LABEL UUID MOUNTPOINT
sdc
└─sdc1 xfs 6ef26358-ce07-4b0c-b28b-07caa90838bd
[root@Centos7T ~]#fdisk /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.
Command (m for help): p
Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 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: 0xe20e9d16
Device Boot Start End Blocks Id System
/dev/sdc1 2048 62914559 31456256 83 Linux
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 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: 0xe20e9d16
Device Boot Start End Blocks Id System
/dev/sdc1 2048 62914559 31456256 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@Centos7T ~]#pvcreate /dev/sdc1 # 在指定分区上创建物理卷
WARNING: xfs signature detected on /dev/sdc1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdc1.
Physical volume "/dev/sdc1" successfully created.
[root@Centos7T ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg0 lvm2 a-- 9.99g 0
/dev/sdc1 lvm2 --- 30.00g 30.00g
将PV加入到指定VG
[root@Centos7T ~]#vgextend vg0 /dev/sdc1 # 将物理卷加入已有的卷组
Volume group "vg0" successfully extended
[root@Centos7T ~]#vgs
VG #PV #LV #SN Attr VSize VFree
vg0 2 1 0 wz--n- 39.98g 29.99g # VFREE为30G,即为新增物理卷的空间
[root@Centos7T ~]#pvdisplay /dev/sdc1
--- Physical volume ---
PV Name /dev/sdc1
VG Name vg0
PV Size 30.00 GiB / not usable 9.00 MiB
Allocatable yes
PE Size 10.00 MiB
Total PE 3071
Free PE 3071
Allocated PE 0
PV UUID Ez6ZFl-xjWR-gQHp-O3Yj-vTza-2OCE-h7Jccy
可知,将新建的PV加入到vg0后,vg0也有了30G的可用空间。同时/dev/sdc1的PE大小自动与/dev/sdb1同步为10M。
扩展逻辑卷LV
[root@Centos7T ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 9.99g
[root@Centos7T ~]#lvextend -l 100%free /dev/vg0/lv0 # 扩展逻辑卷,使用卷组的全部可用空间
Size of logical volume vg0/lv0 changed from 9.99 GiB (1023 extents) to 29.99 GiB (3071 extents).
Logical volume vg0/lv0 successfully resized.
[root@Centos7T ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 29.99g
[root@Centos7T ~]#lvdisplay
--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID zGNz6W-N4QC-m3ZF-ey3h-mP7i-gFJ3-DjGMX1
LV Write Access read/write
LV Creation host, time Centos7.magedu.com, 2017-08-19 21:27:34 +0800
LV Status available
# open 1
LV Size 29.99 GiB
Current LE 3071
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@Centos7T ~]#xfs_growfs /dev/vg0/lv0 #如果为xfs需要用这种方式写入系统才能生效
[root@Centos7T ~]#lsblk -f /dev/sd{b,c}
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
└─sdb1 LVM2_member xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
└─vg0-lv0 xfs 549cfbaa-a6dc-4161-a765-5f55af78131a /mnt/lv0
sdc
└─sdc1 LVM2_member Ez6ZFl-xjWR-gQHp-O3Yj-vTza-2OCE-h7Jccy
└─vg0-lv0 xfs 549cfbaa-a6dc-4161-a765-5f55af78131a /mnt/lv0
5 删除逻辑卷
本文由于更多是演示,删除逻辑卷会卷组、物理卷一并彻底删除。但实际生产中可能只是单独删除逻辑卷,卷组、物理卷还会保留。当然,实际生产中很少需要删除逻辑卷。在删除逻辑卷之前,所有有用的资料都需要备份转移。
代码实现:
[root@Centos7T ~]#sed -i '/lv0/d' /etc/fstab && umount /mnt/lv0/ # 删除配置信息
[root@Centos7T ~]#cat /etc/fstab | egrep "UUID"
UUID=806ef114-dff2-4c17-aff7-c95bc67faf40 / xfs defaults 0 0
UUID=3df87bb6-2f6f-4d3f-af94-5ec47d2a9796 /app xfs defaults 0 0
UUID=1e7ba91a-85c8-442b-b105-bc61c00ddbee /boot xfs defaults 0 0
UUID=cc0e9678-d369-4bb9-9039-fd2f322df8b4 swap swap defaults 0 0
[root@Centos7T ~]#df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda2 xfs 52403200 4605388 47797812 9% /
/dev/sda1 xfs 1038336 172116 866220 17% /boot
/dev/sda5 xfs 52403200 376288 52026912 1% /app
/dev/sr0 iso9660 8086368 8086368 0 100% /run/media/root/CentOS 7 x86_64
[root@Centos7T ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 29.99g
[root@Centos7T ~]#lvremove /dev/vg0/lv0 # 移除逻辑卷
Do you really want to remove active logical volume vg0/lv0? [y/n]: y
Logical volume "lv0" successfully removed
[root@Centos7T ~]#lvs
[root@Centos7T ~]#vgs
VG #PV #LV #SN Attr VSize VFree
vg0 2 0 0 wz--n- 39.98g 39.98g
[root@Centos7T ~]#vgremove vg0 # # 移除卷组
Volume group "vg0" successfully removed
[root@Centos7T ~]#vgs
[root@Centos7T ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 10.00g 10.00g
/dev/sdc1 lvm2 --- 30.00g 30.00g
[root@Centos7T ~]#pvremove /dev/sdb1 /dev/sdc1 # 移除物理卷
Labels on physical volume "/dev/sdb1" successfully wiped.
Labels on physical volume "/dev/sdc1" successfully wiped.
[root@Centos7T ~]#pvs
[root@Centos7T ~]#lsblk -f /dev/sd{b,c}
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
└─sdb1
sdc
└─sdc1 LVM2_member Ez6ZFl-xjWR-gQHp-O3Yj-vTza-2OCE-h7Jccy
至此,逻辑卷删除完成。当然,/dev/sdc1显示LVM2_member(实际上sdb1也是的),这是由于我们前面将/dev/sdc1的系统ID改为8e导致的,重新将系统ID改回83即可。