标签:lvm
LVM相关概念:
LVM(Logical Volume Manager)利用Linux内核的device-mapper来实现存储系统的虚拟化(系统分区独立于底层硬件)。通过LVM,可以实现存储空间的抽象化并在上面建立虚拟分区(virtual partitions),从而可以更简便地扩大和缩小分区,可以在增删分区时无需担心某个硬盘上没有足够的连续空间,LVM只是提供了对硬盘的一个方便的管理机制,并没有提供额外的安全保证。
LVM的相关术语:
物理卷Physical volume(PV):位于逻辑卷管理的最底层,可以是过个硬盘的多个分区。
卷组Volume group(VG):建立在多个物理卷上,它将多个物理卷聚合为一个大的管理单元(卷组),可以动态管理卷组中的物理卷。
逻辑卷Logical volume(LV):虚拟分区,它建立在卷组之上,可以动态的调整逻辑卷大小。
物理块Physical extent(PE):它是物理卷中可供分配的最小存储单元(默认大小4M)。它的大小会在创建卷组的时候被指定,一旦卷组创建完成后,物理区域大小无法再调整;同一卷组中各个物理卷的物理块大小一致。
逻辑块Logical exten(LE):LE是逻辑卷上的最小可存储单元,和PE大小一致。
优点
相对于普通的硬盘分区管理,LVM更富于弹性:
使用卷组(VG),组合多个物理卷形成一个的大的存储池,可动态管理卷组。
使用逻辑卷(LV),可以创建跨越众多硬盘空间的分区(Virtual Partitions),在调整逻辑卷(LV)大小时可以不用考虑逻辑卷在硬盘上的位置,不用担心没有可用的连续空间。
可以在线(online)对逻辑卷(LV)和卷组(VG)进行管理操作,扩展,缩减,删除等。
允许创建快照,可以保存文件系统的备份。
缺点
相对于普通的卷,其创建过程更为复杂。
练习:利用现有磁盘sdb创建两个物理卷,组建一个15G的卷组vg1,并创建一个7G的逻辑卷lv1,挂载到/mnt/lv001
Step1:创建两个分区,并组建成一个15G的卷组
[root@centos6 ~]# fdisk -l /dev/sdb #查看sdb使用情况,磁盘未划分任何分区,接下来创建分区sdb1(5G),sdb2(10G)
Disk /dev/sdb: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x067824c0
Device Boot Start End Blocks Id System
[root@centos6 ~]# fdisk -l /dev/sdb #使用fdisk命令来创建分区sdb1,sdb2
Disk /dev/sdb: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x067824c0
Device Boot Start End Blocks Id System
[root@centos6 ~]# 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): n #创建分区
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-15665, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-15665, default 15665): +5G #指定分区大小5G
Command (m for help): t #改变分区ID号
Selected partition 1
Hex code (type L to list codes): 8e #8e是LVM分区ID号
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
[root@centos6 ~]# partx -a /dev/sdb #通知内核更新分区表
BLKPG: Device or resource busy
error adding partition 1
[root@centos6 ~]# fdisk -l /dev/sdb #验证分区是否创建好
Disk /dev/sdb: 128.8 GB, 128849018880 bytes
....此处略去N行信息
Device Boot Start End Blocks Id System
/dev/sdb1 1 654 5253223+ 8e Linux LVM
同理创建大小为10G的分区sdb2
[root@centos6 ~]# fdisk -l /dev/sdb #创建好的分区sdb1,sdb2如下
Disk /dev/sdb: 128.8 GB, 128849018880 bytes
...此处略去N行信息
Device Boot Start End Blocks Id System
/dev/sdb1 1 654 5253223+ 8e Linux LVM
/dev/sdb2 655 1960 10490445 8e Linux LVM
Step2:将分区转化为物理卷
[root@centos6 ~]# pvcreate /dev/sdb{1,2} #将分区sdb1,sdb2创建为物理卷PV
Physical volume "/dev/sdb1" successfully createdPhysical volume "/dev/sdb2" successfully created
[root@centos6 ~]# vgdisplay vg1 #创建卷组,取名为vg1
--- 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 15.01 GiB
PE Size 4.00 MiB
Total PE 3842
Alloc PE / Size 0 / 0
Free PE / Size 3842 / 15.01 GiBVG UUID QhFxNx-SQz0-62zl-3kOR-hVM5-HucJ-kDuEP6
[root@centos6 ~]# lvcreate -L 7G -n lv1 vg1 #创建逻辑卷,指定大小为7G,卷名为lv1
Logical volume "lv1" created
[root@centos6 ~]# mkfs.ext4 /dev/vg1/lv1 #格式化逻辑卷lv1为ext4文件系统格式
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
458752 inodes, 1835008 blocks
91750 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1879048192
56 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@centos6 ~]# lvdisplay /dev/vg1/lv1 #查看逻辑卷lv1的详细信息
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID XpUB9b-8rjS-AHRo-Dxcs-5Do7-xm76-X3RN5w
LV Write Access read/write
LV Creation host, time centos6, 2015-08-31 05:17:16 +0800
LV Status available
# open 0
LV Size 7.00 GiB
Current LE 1792
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 25
[root@centos6 ~]# mkdir /mnt/lv001 #创建挂载点目录lv001
[root@centos6 ~]# mount /dev/vg1/lv1 /mnt/lv001/ #挂载逻辑卷lv1到/mnt/lv001目录下
[root@centos6 ~]# ll /mnt/lv001/ #验证已挂载成功
total 16
drwx------ 2 root root 16384 Aug 31 05:18 lost+found
至此,逻辑卷lv1已经创建成功,如果要实现自动挂载,修改/etc/fstab文件,为/dev/vg1/lv1添加相应的挂载条目即可。
练习:扩展lv1卷大小,将lv1大小调整到10G (故需要添加3G空间到原有7G空间上去)
[root@centos6 ~]# lvextend --size +3G /dev/vg1/lv1 /dev/sdb2 #为lv1添加3G空间
Size of logical volume vg1/lv1 changed from 7.00 GiB (1792 extents) to 10.00 GiB (2560 extents).
Logical volume lv1 successfully resized
[root@centos6 ~]# lvdisplay /dev/vg1/lv1 #查看lv1的详细信息
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID XpUB9b-8rjS-AHRo-Dxcs-5Do7-xm76-X3RN5w
LV Write Access read/write
LV Creation host, time centos6, 2015-08-31 05:17:16 +0800
LV Status available
# open 1
LV Size 10.00 GiB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:4
[root@centos6 ~]# resize2fs /dev/vg1/lv1 #扩展文件系统大小,扩展完成后,逻辑卷lv1的10G空间就可以正常使用
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg1/lv1 is mounted on /mnt/lv001; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg1/lv1 to 2621440 (4k) blocks.
The filesystem on /dev/vg1/lv1 is now 2621440 blocks long.
练习:缩减逻辑卷大小,将lv1从10G缩减到3G
注意:逻辑卷缩小后的空间不能小于它上面文件现占有的空间,否则可能坏导致数据损坏,缩减逻辑卷的大致步骤是:
卸载文件系统--》使用e2fsck检测逻辑卷--》使用resize2fs调整文件系统大小-》使用lvreduce缩减逻辑卷大小
[root@centos6 ~]# df -lh /dev/vg1/lv1 #验证缩减前实际使用空间不超过3G
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg1-lv1 9.8G 16M 9.3G 1% /mnt/lv001
[root@centos6 ~]# umount /dev/vg1/lv1 #卸载逻辑卷
[root@centos6 ~]# e2fsck -f /dev/vg1/lv1 #强制检测逻辑卷
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg1/lv1: 12/655360 files (0.0% non-contiguous), 77935/2621440 blocks
[root@centos6 ~]# resize2fs /dev/vg1/lv1 3G #调整文件系统大小
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg1/lv1 to 786432 (4k) blocks.
The filesystem on /dev/vg1/lv1 is now 786432 blocks long.
[root@centos6 ~]# lvreduce -L 3G /dev/vg1/lv1 #缩减逻辑卷大小至3G
WARNING: Reducing active logical volume to 3.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv1? [y/n]: y #回答y确认操作
Size of logical volume vg1/lv1 changed from 10.00 GiB (2560 extents) to 3.00 GiB (768 extents).
Logical volume lv1 successfully resized #提示已经调整成功
[root@centos6 ~]# lvdisplay /dev/vg1/lv1 #再度查看lv1详细信息,确认大小已经缩减为3G
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID XpUB9b-8rjS-AHRo-Dxcs-5Do7-xm76-X3RN5w
LV Write Access read/write
LV Creation host, time centos6, 2015-08-31 05:17:16 +0800
LV Status available
# open 0
LV Size 3.00 GiB
Current LE 768
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:4
练习:为卷组vg1添加一个新的10G的PV
...此处省略创建物理卷sb3(大小为10G)的过程。
[root@centos6 ~]# vgextend vg1 /dev/sdb3 #使用vgextend添加新的物理卷sdb3到卷组vg1
Physical volume "/dev/sdb3" successfully created
Volume group "vg1" successfully extended
[root@centos6 ~]# vgdisplay /dev/vg1 #查看vg1的详细信息,卷组大小已经扩展到20G
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 25.01 GiB
PE Size 4.00 MiB
Total PE 6402
Alloc PE / Size 768 / 3.00 GiB
Free PE / Size 5634 / 22.01 GiB
VG UUID QhFxNx-SQz0-62zl-3kOR-hVM5-HucJ-kDuEP6
相关命令如下:
dm: device manager:将一个或多个底层块设备组织成一个逻辑设备的模块
设备文件路径:/dve/dm-#
逻辑卷的文件路径
/dev/mapper/卷组_逻辑卷 或 /dev/卷组/逻辑卷
pv管理工具:
pvs:简要显示系统上的PV的信息
pvdisplay 物理卷:查看指定pv的详细信息
vg管理工具:
vgs 简要显示系统上的卷组信息
vgdisplay 卷组:查看指定卷组的详细信息
lv管理工具:
lvs:简要显示逻辑卷的信息
lvdisplay 逻辑卷: 查看指定逻辑卷的详细信息
lvcreate -L #[mMgGtT] -n NAME VolumeGroup 创建逻辑卷
lvremove /dev/VG_NAME/LV_NAME 删除逻辑卷
扩展逻辑卷:
# lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
# resize2fs /dev/VG_NAME/LV_NAME
缩减逻辑卷:
# umount /dev/VG_NAME/LV_NAME
# e2fsck -f /dev/VG_NAME/LV_NAME
# resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
# lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
# mount
标签:lvm
原文地址:http://siyuan710.blog.51cto.com/10648912/1690587