逻辑卷管理器LVM(LogiclVolume Manager),通过使用 逻辑卷管理器对硬盘存储设备进行管理,可以实现硬盘 空间的动态划分和调整;
LVM概述
• LVM机制的基本概念
• PV(Physical Volume,物理卷) • 整个硬盘,或使用fdisk等工具建立的普通分区 • 包括许多默认4MB大小的PE(Physical Extent,基本单元)
• VG(Volume Group,卷组) • 一个或多个物理卷组合而成的整体
• LV(Logical Volume,逻辑卷) • 从卷组中分割出的一块空间,用于建立文件系
创建卷组 • 使用vgcreate命令创建卷组
创建逻辑卷 • 使用lvcreate命令创建逻辑卷 • 使用mkfs命令创建ext3文件系统
逻辑卷扩容 • 使用lvextend命令为逻辑卷mail扩充容量• 使用resize2fs命令更新系统识别的文件系统大小
1.在虚拟机上新添加一个磁盘。将磁盘分4个区,并格式化为8e(Linux LVM)
[[email protected] ~]# fdisk -l
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: 0x302fa02d
Device Boot Start End Blocks Id System
Disk /dev/sda: 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: 0x000b4721
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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
[[email protected] ~]# 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.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (10487808-41943039, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G
Partition 2 of type Linux and of size 5 GiB is set
Command (m for help): t
Partition number (1,2, default 2):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (20973568-41943039, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +5G
Partition 3 of type Linux and of size 5 GiB is set
Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): p
Selected partition 4
First sector (31459328-41943039, default 31459328):
Using default value 31459328
Last sector, +sectors or +size{K,M,G} (31459328-41943039, default 41943039):
Using default value 41943039
Partition 4 of type Linux and of size 5 GiB is set
Command (m for help): t
Partition number (1-4, default 4):
Hex code (type L to list all codes): 8e
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: 0x302fa02d
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM
/dev/sdb2 10487808 20973567 5242880 8e Linux LVM
/dev/sdb3 20973568 31459327 5242880 8e Linux LVM
/dev/sdb4 31459328 41943039 5241856 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
2.将4个分区转换为PV(物理卷
[[email protected] ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb2" successfully created.
[[email protected] ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
[[email protected] ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created.
[[email protected] ~]# pvcreate /dev/sdb4
Physical volume "/dev/sdb4" successfully created.
[[email protected] ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4863
Free PE 0
Allocated PE 4863
PV UUID ouVpNF-vZnY-Jj7y-7QEi-Dnt1-coZ3-2X0evG
"/dev/sdb3" is a new physical volume of "5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb3
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID LgiDXZ-Geye-1sS8-AKu7-Z1jy-qOtu-ISj2Tw
"/dev/sdb2" is a new physical volume of "5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Gq4mB3-VFcK-MSXA-zwpu-9eyr-teQK-XyQM41
"/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 s5bbTg-OIIE-n8mF-mK9B-2m7r-mUZC-0rjaLC
"/dev/sdb4" is a new physical volume of "<5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb4
VG Name
PV Size <5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID FgWs0v-jRMZ-QSe5-XTSa-xYLa-Vj7p-KA8fEd
[[email protected] ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<19.00 GiB / 0 free]
PV /dev/sdb3 lvm2 [5.00 GiB]
PV /dev/sdb2 lvm2 [5.00 GiB]
PV /dev/sdb1 lvm2 [5.00 GiB]
PV /dev/sdb4 lvm2 [<5.00 GiB]
Total: 5 [<39.00 GiB] / in use: 1 [<19.00 GiB] / in no VG: 4 [<20.00 GiB]
3.创建包含/dev/sdb1,/dev/sdb2,/dev/sdb3这3个PV的VG(卷组)test
[[email protected] ~]# vgcreate test /dev/sdb1 /dev/sdb2 /dev/sdb3
Volume group "test" successfully created
[[email protected] ~]# vgdisplay
--- Volume group ---
VG Name test
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 <14.99 GiB
PE Size 4.00 MiB
Total PE 3837
Alloc PE / Size 0 / 0
Free PE / Size 3837 / <14.99 GiB
VG UUID LaOpiA-q26c-NBz3-zBAk-QYwM-YPAP-OS44X3
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <19.00 GiB
PE Size 4.00 MiB
Total PE 4863
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 0 / 0
VG UUID AqAfii-dhHG-GqqL-vFrl-YHOX-mmq2-eVS42J
[[email protected] ~]# vgscan
Reading volume groups from cache.
Found volume group "test" using metadata type lvm2
Found volume group "centos" using metadata type lvm2
4.在卷组test上创建逻辑卷LV1,LV2,大小都为6G
[[email protected] ~]# lvcreate -L 6G -n LV1 test
Logical volume "LV1" created.
[[email protected] ~]# lvcreate -L 6G -n LV2 test
Logical volume "LV2" created.
[[email protected] ~]# lvscan |grep test
ACTIVE '/dev/test/LV1' [6.00 GiB] inherit
ACTIVE '/dev/test/LV2' [6.00 GiB] inherit
5.将LV1格式化为ext4,LV2格式化为xfs
[[email protected] ~]# mkfs -t ext4 /dev/mapper/test-LV1
[[email protected] ~]# mkfs -t xfs /dev/mapper/test-LV2
6.创建两个文件夹/LV1,/LV2 分别用于挂载逻辑卷LV1,LV2,之后逻辑卷就可以使用了
[[email protected] ~]# cd /
[[email protected] /]# mkdir LV1
[[email protected] /]# mkdir LV2
[[email protected] /]# cd
[[email protected] ~]# mount -t ext4 /dev/test/LV1 /LV1
[[email protected] ~]# mount -t xfs /dev/test/LV2 /LV2
[[email protected] ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 17G 4.0G 14G 24% /
devtmpfs devtmpfs 974M 0 974M 0% /dev
tmpfs tmpfs 991M 0 991M 0% /dev/shm
tmpfs tmpfs 991M 11M 981M 2% /run
tmpfs tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 166M 849M 17% /boot
tmpfs tmpfs 199M 12K 199M 1% /run/user/42
tmpfs tmpfs 199M 0 199M 0% /run/user/0
/dev/mapper/test-LV1 ext4 5.8G 24M 5.5G 1% /LV1
/dev/mapper/test-LV2 xfs 6.0G 33M 6.0G 1% /LV2
7.ext4文件系统LVM逻辑卷LV1的扩容
step1:确认目前LV1的大小及卷组test的大小
[[email protected] ~]# df -Th|grep LV1
/dev/mapper/test-LV1 ext4 5.8G 24M 5.5G 1% /LV1
[[email protected] ~]# lvscan |grep LV1
ACTIVE '/dev/test/LV1' [6.00 GiB] inherit
[[email protected] ~]# vgdisplay
--- Volume group ---
VG Name test
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size <14.99 GiB
PE Size 4.00 MiB
Total PE 3837
Alloc PE / Size 3072 / 12.00 GiB
Free PE / Size 765 / <2.99 GiB
VG UUID LaOpiA-q26c-NBz3-zBAk-QYwM-YPAP-OS44X3
step2:这里我们先把VG进行扩大,把之前创建的PV /dev/sdb4加进去
[[email protected] ~]# vgextend test /dev/sdb4
Volume group "test" successfully extended
[[email protected] ~]# vgdisplay
--- Volume group ---
VG Name test
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 4
Act PV 4
VG Size 19.98 GiB
PE Size 4.00 MiB
Total PE 5116
Alloc PE / Size 3072 / 12.00 GiB
Free PE / Size 2044 / 7.98 GiB
VG UUID LaOpiA-q26c-NBz3-zBAk-QYwM-YPAP-OS44X3
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <19.00 GiB
PE Size 4.00 MiB
Total PE 4863
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 0 / 0
VG UUID AqAfii-dhHG-GqqL-vFrl-YHOX-mmq2-eVS42J
step3:之前将LV1格式化为了ext4并且挂载到了/LV1目录下,现在我们在LV1挂载的情况下,将LV1从6G扩容到10G,然后再将文件系统重新识别扩大的LV1的大小
[[email protected] ~]# lvextend -L +4G /dev/test/LV1
Size of logical volume test/LV1 changed from 6.00 GiB (1536 extents) to 10.00 GiB (2560 extents).
Logical volume test/LV1 successfully resized.
[[email protected] ~]# lvscan |grep LV1
ACTIVE '/dev/test/LV1' [10.00 GiB] inherit
[[email protected] ~]# resize2fs /dev/test/LV1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/test/LV1 is mounted on /LV1; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/test/LV1 is now 2621440 blocks long.
[[email protected] ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 17G 4.0G 14G 24% /
devtmpfs devtmpfs 974M 0 974M 0% /dev
tmpfs tmpfs 991M 0 991M 0% /dev/shm
tmpfs tmpfs 991M 11M 981M 2% /run
tmpfs tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 166M 849M 17% /boot
tmpfs tmpfs 199M 12K 199M 1% /run/user/42
tmpfs tmpfs 199M 0 199M 0% /run/user/0
/dev/mapper/test-LV1 ext4 9.8G 27M 9.3G 1% /LV1
/dev/mapper/test-LV2 xfs 6.0G 33M 6.0G 1% /LV2
8.ext4文件系统LVM逻辑卷LV1的缩减
step1:卸载LV1
[[email protected] ~]# umount /LV1
[[email protected] ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 17G 4.0G 14G 24% /
devtmpfs devtmpfs 974M 0 974M 0% /dev
tmpfs tmpfs 991M 0 991M 0% /dev/shm
tmpfs tmpfs 991M 11M 981M 2% /run
tmpfs tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 166M 849M 17% /boot
tmpfs tmpfs 199M 12K 199M 1% /run/user/42
tmpfs tmpfs 199M 0 199M 0% /run/user/0
/dev/mapper/test-LV2 xfs 6.0G 33M 6.0G 1% /LV2
step2:对文件系统识别的大小进行缩减,从10G缩减到5G
[[email protected] ~]# e2fsck /dev/test/LV1
e2fsck 1.42.9 (28-Dec-2013)
/dev/test/LV1: clean, 11/655360 files, 80815/2621440 blocks
[[email protected] ~]# resize2fs -f /dev/test/LV1 5G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/test/LV1 to 1310720 (4k) blocks.
The filesystem on /dev/test/LV1 is now 1310720 blocks long.
step3:再对LV1进行缩减,然后再挂载到/LV1目录下
[[email protected] ~]# lvreduce -L 5G /dev/test/LV1
WARNING: Reducing active logical volume to 5.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce test/LV1? [y/n]: y
Size of logical volume test/LV1 changed from 10.00 GiB (2560 extents) to 5.00 GiB (1280 extents).
Logical volume test/LV1 successfully resized.
[[email protected] ~]# mount -t ext4 /dev/test/LV1 /LV1
[[email protected] ~]# df -Th|grep LV1
/dev/mapper/test-LV1 ext4 4.8G 24M 4.6G 1% /LV1
9.xfs文件系统LVM逻辑卷LV2的扩容
step1:之前将LV2格式化为了xfs并且挂载到了/LV2目录下,现在我们在LV2挂载的情况下,将LV2从6G扩容到8G,然后再将文件系统重新识别扩大的LV2的大小
[[email protected] ~]# df -Th|grep LV2
/dev/mapper/test-LV2 xfs 6.0G 33M 6.0G 1% /LV2
[[email protected] ~]# lvextend -L +2G /dev/test/LV2
Size of logical volume test/LV2 changed from 6.00 GiB (1536 extents) to 8.00 GiB (2048 extents).
Logical volume test/LV2 successfully resized.
[[email protected] ~]# xfs_growfs /dev/test/LV2
meta-data=/dev/mapper/test-LV2 isize=512 agcount=4, agsize=393216 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=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 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 1572864 to 2097152
[[email protected] ~]# df -Th|grep LV2
/dev/mapper/test-LV2 xfs 8.0G 33M 8.0G 1% /LV2