磁盘管理之LVM
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了磁盘管理之LVM相关的知识,希望对你有一定的参考价值。
1、什么是LVM
LVM(Logical Volume Manager)逻辑卷管理,是linux环境下将一种将一个或多个硬盘的分区在逻辑上集合来呈现给上层应用,对磁盘实现动态管理的机制。相对于普通的磁盘分区有很大的灵活性,使用LVM在一定程度上就可以解决普通磁盘分区带来的问题。
2、专业术语
●物理卷(PhysicalVolume)就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(可 以是RAID),是LVM的基本存储逻辑块
●卷组(VolumeGroup)是物理卷的集合
●LVM(LogicalVolumeManager)的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)
●PE(PhysicalExtent)是每一个逻辑卷和逻辑卷的基本单元,具有唯一编号的PE是可以被LVM寻址 的最小单元。PE的大小是可配置的,默认为4MB
●LE(LogicalExtent)是逻辑卷的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应
3、LVM工作原理图
4、简单实现
<1>创建文件系统
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-419430399, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): +3G Partition 1 of type Linux and of size 3 GiB is set # Command (m for help): T Selected partition 1 Hex code (type L to list all codes): 8e # 调整分区类型为8e Changed type of partition ‘Linux‘ to ‘Linux LVM‘ Command (m for help): w # 保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. # 多创建几个分区,操作方法如上
<2>创建PV
[[email protected]~]#pvcreate /dev/sdc1 # 三个分区创建pv Physical volume "/dev/sdc1" successfully created [[email protected]~]#pvcreate /dev/sdc2 Physical volume "/dev/sdc2" successfully created [[email protected]~]#pvcreate /dev/sdc3 Physical volume "/dev/sdc3" successfully created
<3>创建VG
[[email protected]~]#vgcreate vg1 /dev/sdc{1,2,3} Volume group "vg1" successfully created
<4>创建LV
[[email protected]~]#lvcreate -n lv1 -L 6G /dev/vg1 Logical volume "lv1" created.
<5>为LV创建文件系统
[[email protected]~]#mke2fs -t ext4 /dev/vg1/lv1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 393216 inodes, 1572864 blocks 78643 blocks (5.00%) reserved for the super user .....
<6>挂载逻辑卷
# 创建挂载点 [[email protected]~]#mkdir /mnt/lv1 # 挂载lv1 [[email protected]~]#vim /etc/fstab ... UUID="82ed7610-1975-4433-a741-1db119e4bf0e" /mnt/lv1 ext4 default 0 0 ... # 挂载设备 [[email protected]~]#mount -a [[email protected]~]#
<7>测试逻辑卷大小
# 查看原始数据大小 [[email protected]/mnt/lv1]#df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 100G 6.0G 95G 6% / devtmpfs 474M 0 474M 0% /dev tmpfs 489M 84K 489M 1% /dev/shm tmpfs 489M 7.2M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sdg1 976M 2.6M 907M 1% /mnt/sdg1 /dev/mapper/centos-testdir 20G 33M 20G 1% /testdir /dev/sda1 197M 143M 55M 73% /boot tmpfs 98M 20K 98M 1% /run/user/42 tmpfs 98M 0 98M 0% /run/user/0 /dev/sde1 2.0G 55M 1.8G 3% /home /dev/mapper/vg1-lv1 5.8G 24M 5.5G 1% /mnt/lv1 # 原始大小 # 填充磁盘 [[email protected]/mnt/lv1]#dd if=/dev/zero of=/mnt/lv1/f1 bs=1M count=1024 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 2.58483 s, 415 MB/s # 再此查看磁盘空间 [[email protected]/mnt/lv1]#df -h ... /dev/mapper/vg1-lv1 5.8G 1.1G 4.5G 19% /mnt/lv1 # 已生效,只分配6g
<8>扩展LV,在线扩展(挂载即可扩展)
# 查看VG [[email protected]/mnt/lv1]#pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 122.00g 0 /dev/sdc1 vg1 lvm2 a-- 3.00g 3.00g /dev/sdc2 vg1 lvm2 a-- 5.00g 5.00g /dev/sdc3 vg1 lvm2 a-- 10.00g 4.00g # /dev/sdc一共18g,还有剩余,可扩展 # 扩展LV [[email protected]/mnt/lv1]#lvextend -L 12G /dev/vg1/lv1 # 扩展 Size of logical volume vg1/lv1 changed from 6.00 GiB (1536 extents) to 12.00 GiB (3072 extents). Logical volume lv1 successfully resized. # LV已同步,df -h查看未同步 [[email protected]/mnt/lv1]#lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert ... lv1 vg1 -wi-ao---- 12.00g [[email protected]/mnt/lv1]#df -h ... /dev/mapper/vg1-lv1 5.8G 1.1G 4.5G 19% /mnt/lv1 # 因为新加入的分区尚未创建文件系统,所以df -h查看同步失败,同步文件系统 [[email protected]/mnt/lv1]#resize2fs /dev/vg1/lv1 # resize2fs,ext文件系统专用命令 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/vg1/lv1 is mounted on /mnt/lv1; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/vg1/lv1 is now 3145728 blocks long. # 再此查看 [[email protected]/mnt/lv1]#df -h Filesystem Size Used Avail Use% Mounted on ... /dev/mapper/vg1-lv1 12G 1.1G 11G 10% /mnt/lv1 # 同步完成
<9>如果扩展LV的时候,发现VG空间不足,可继续扩展VG,步骤如下
# 添加一块新的磁盘或者分区,此处以分区为例,与<1>相同 # 再次创建PV [[email protected]~]#pvcreate /dev/sdc5 Physical volume "/dev/sdc5" successfully created [[email protected]~]#pvs PV VG Fmt Attr PSize PFree ... /dev/sdc1 vg1 lvm2 a-- 3.00g 1016.00m /dev/sdc2 vg1 lvm2 a-- 5.00g 5.00g /dev/sdc3 vg1 lvm2 a-- 10.00g 0 /dev/sdc5 lvm2 --- 1.00g 1.00g # 再次将其加入卷组vgextend vg1 /dev/sdc5 # 当然,如果VG空间足够,也就不需要这一步骤了
本文出自 “LINUX_Home” 博客,请务必保留此出处http://dmwing.blog.51cto.com/11607397/1844284
以上是关于磁盘管理之LVM的主要内容,如果未能解决你的问题,请参考以下文章