LVM逻辑卷管理简单介绍和逻辑卷创建流程
LVM是逻辑卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,
LVM是建立在硬盘和分区之上,文件系统之下的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区,如:将若干个磁盘分区连接为一个整块的卷组(volume group),形成一个存储池。
管理员可以在卷组上随意创建逻辑卷组(logical volumes),并进一步在逻辑卷组上创建文件系统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配
PV物理卷(physical volume)
物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),
是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,
却包含有与LVM相关的管理参数
VG卷组(Volume Group)
由一个或多个物理卷组成一个整体,即称为卷组,在卷组中可以动态的添加或移除物理卷,
许多个物理卷可以分别组成不同的卷组,卷组名称由用户自行定义
LV逻辑卷(logical volume)
逻辑卷是建立在卷组之上的,与物理卷无直接关系,对于逻辑卷来说,每一个卷组就是一个整体,
从这个整体中切出一小块空间,作为用户创建文件系统的基础,这一小块空间就称为逻辑卷,使
用mkfs等工具在逻辑卷之上建立文件系统以后,即可挂载到Linux系统中的目录下使用
示意流程如下:
PG > VG > LV > 格式化 》挂载文件系统硬盘(/dev/sda/sdb) > sda1 .sdb1 转换成物理卷(PV)> 创建卷组 > 创建逻辑卷 》格
式化为exit4文件系统,并挂载到根目录系统下/mydate。
LVM逻辑卷的类型为“8e”
创建逻辑卷的流程如下:
1.创建3个逻辑分区,改成LVM逻辑卷的类型为“8e”
[root@lwm ~]# fdisk /dev/sdb (进行对硬盘进行分区)
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
switch off the mode (command ‘c‘) and change display units to
sectors (command ‘u‘).
Command (m for help): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext‘d (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT
1e Hidden W95 FAT1
Command (m for help): t 以下为更改逻辑卷的类型为“8e”
Partition number (1-4): 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@lwm ~]# fdisk -l (查看是否已经更改完毕)
Device Boot Start End Blocks Id System
/dev/sdb1 1 26 208813+ 8e Linux LVM
/dev/sdb2 27 52 208845 8e Linux LVM
/dev/sdb3 53 200 1188810 8e Linux LVM
2.创建PV物理卷:
[root@lwm ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[root@lwm ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created
[root@lwm ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created
使用PVS查看一下:
[root@lwm ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 203.92m 203.92m
/dev/sdb2 lvm2 --- 203.95m 203.95m
/dev/sdb3 lvm2 --- 1.13g 1.13g
3.添加卷组:
[root@lwm ~]# vgcreate magedu /dev/sdb1 /dev/sdb2 /dev/sdb3
Volume group "magedu" successfully created
4.创建逻辑卷:
[root@lwm ~]# lvcreate -L 200M -n magedu-lvm magedu
Logical volume "magedu-lvm" created.
[root@lwm ~]# file /dev/magedu/magedu-lvm
/dev/magedu/magedu-lvm: symbolic link to `../dm-0‘
[root@lwm ~]# mkfs.ext3 /dev/magedu/magedu-lvm
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
正在写入inode表: 完成
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
用LVS命令查看一下状态:
[root@lwm ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
magedu-lvm magedu -wi-a----- 200.00m
5.创建目录,然后进行挂载
[root@lwm ~]# mkdir /data-magedu (创建目录)
[root@lwm ~]# mount /dev/magedu/magedu-lvm /data-magedu/ (然后进行挂载)
[root@lwm ~]# cd /data-magedu/
[root@lwm data-magedu]# ls
lost+found
[root@lwm data-magedu]# touch liwenming 往挂载好的逻辑卷里写东西
[root@lwm data-magedu]# ls 然后查看,确实有文件了
liwenming lost+found
6.用PVS、查看其最终的状态
[root@lwm data-magedu]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 magedu lvm2 a-- 200.00m 0
/dev/sdb2 magedu lvm2 a-- 200.00m 200.00m
/dev/sdb3 magedu lvm2 a-- 1.13g 1.13g
lvs 查看
[root@lwm data-magedu]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
magedu-lvm magedu -wi-ao---- 200.00m
7.查看lvm详细信息
[root@lwm data-magedu]# lvdisplay /dev/magedu/magedu-lvm
--- Logical volume ---
LV Path /dev/magedu/magedu-lvm
LV Name magedu-lvm
VG Name magedu
LV UUID M5r0eS-Q6N6-60l3-3nio-410S-uBKQ-ZT9mMH
LV Write Access read/write
LV Creation host, time lwm, 2016-03-12 17:34:16 +0800
LV Status available
# open 1
LV Size 200.00 MiB
Current LE 50
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
8.现在我想动态扩张200M,添加之
[root@lwm data-magedu]# lvextend -L +200M /dev/magedu/magedu-lvm
Size of logical volume magedu/magedu-lvm changed from 200.00 MiB (50 extents) to 400.00 MiB (100 extents).
Logical volume magedu-lvm successfully resized
[root@lwm data-magedu]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.6G 4.8G 2.5G 67% /
tmpfs 491M 80K 491M 1% /dev/shm
/dev/sr0 7.3G 7.3G 0 100% /media/CentOS 7 x86_64
/dev/mapper/magedu-magedu--lvm
194M 5.6M 179M 4% /data-magedu
[root@lwm data-magedu]# resize2fs /dev/magedu/magedu-lvm 从读文件系统逻辑卷
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/magedu/magedu-lvm is mounted on /data-magedu; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/magedu/magedu-lvm to 409600 (1k) blocks.
The filesystem on /dev/magedu/magedu-lvm is now 409600 blocks long.
[root@lwm data-magedu]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.6G 4.8G 2.5G 67% /
tmpfs 491M 80K 491M 1% /dev/shm
/dev/sr0 7.3G 7.3G 0 100% /media/CentOS 7 x86_64
/dev/mapper/magedu-magedu--lvm
388M 6.3M 362M 2% /data-magedu
8.卸载LVM逻辑卷:然后进行裁剪,将逻辑卷的容量减小。
[root@lwm ~]# umount /data-magedu/
[root@lwm ~]# e2fsck -f /dev/magedu/magedu-lvm 检查一下有没有问题
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/magedu/magedu-lvm: 12/102400 files (0.0% non-contiguous), 19351/409600 blocks
[root@lwm ~]# e2fsck /dev/magedu/magedu-lvm 200M 文件系统级别上减掉。指的是剪到200M
Usage: e2fsck [-panyrcdfvtDFV] [-b superblock] [-B blocksize]
[-I inode_buffer_blocks] [-P process_inode_size]
[-l|-L bad_blocks_file] [-C fd] [-j external_journal]
[-E extended-options] device
Emergency help:
-p Automatic repair (no questions)
-n Make no changes to the filesystem
-y Assume "yes" to all questions
-c Check for bad blocks and add them to the badblock list
-f Force checking even if filesystem is marked clean
-v Be verbose
-b superblock Use alternative superblock
-B blocksize Force blocksize when looking for superblock
-j external_journal Set location of the external journal
-l bad_blocks_file Add to badblocks list
-L bad_blocks_file Set badblocks list
[root@lwm ~]# resize2fs /dev/magedu/magedu-lvm 200M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/magedu/magedu-lvm to 204800 (1k) blocks.
The filesystem on /dev/magedu/magedu-lvm is now 204800 blocks long.
[root@lwm ~]# lvreduce -L -200M /dev/magedu/magedu-lvm
WARNING: Reducing active logical volume to 200.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce magedu-lvm? [y/n]: y
Size of logical volume magedu/magedu-lvm changed from 400.00 MiB (100 extents) to 200.00 MiB (50 extents).
Logical volume magedu-lvm successfully resized
[root@lwm ~]# mount /dev/magedu/magedu-lvm /data-magedu/
[root@lwm ~]# cd /data-magedu/
[root@lwm data-magedu]# ls
liwenming lost+found
[root@lwm data-magedu]# ls
liwenming lost+found
[root@lwm data-magedu]# lvdisplay /dev/magedu/magedu-lvm
--- Logical volume ---
LV Path /dev/magedu/magedu-lvm
LV Name magedu-lvm
VG Name magedu
LV UUID M5r0eS-Q6N6-60l3-3nio-410S-uBKQ-ZT9mMH
LV Write Access read/write
LV Creation host, time lwm, 2016-03-12 17:34:16 +0800
LV Status available
# open 1
LV Size 200.00 MiB
Current LE 50
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
总结:以上就是LVM逻辑卷管理,能够很方便的管理LV逻辑卷。
优点是可以在生产带业务的情况下,进行对逻辑卷的管理工作。大大提高了工作效率。
本文出自 “李文明的博客” 博客,请务必保留此出处http://liwenming18.blog.51cto.com/11068518/1752015
原文地址:http://liwenming18.blog.51cto.com/11068518/1752015