LVM概念
Logical Volume Manager 逻辑卷管理
|
|
LVM的4个基本概念
|
|
PE逻辑卷空间管理的最基本单位。1个PE默认是4M大小
VG的作用是用来装PV的–空间池
创建LVM
|
|
- 将物理磁盘设备初始化为物理卷12➜ ~ pvcreate /dev/sdbPhysical volume "/dev/sdb" successfully created
查看物理卷信息
123 ➜ ~ pvsPV VG Fmt Attr PSize PFree/dev/sdb lvm2 --- 50.00g 50.00g
- 创建卷组,并将PV加入卷组中123>名字为lvmtest, 可任意➜ ~ vgcreate lvmtest /dev/sdbVolume group "lvmtest" successfully created
|
|
- 基于卷组创建逻辑卷
这里我从lvmtest拿了20G空间来创建了一个名为lv1的逻辑卷
12➜ ~ lvcreate -n lv1 -L 20G lvmtestLogical volume "lv1" created
查看逻辑卷信息
1234567891011121314151617181920 ➜ ~ lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertlv1 lvmtest -wi-a----- 20.00g➜ ~ lvdisplay--- Logical volume ---LV Path /dev/lvmtest/lv1LV Name lv1VG Name lvmtestLV UUID Q4UIbv-yWhL-Bl5A-ywTN-1HJo-TE5r-TKBPfQLV Write Access read/writeLV Creation host, time kali, 2016-08-03 08:44:31 +0800LV Status available# open 0LV Size 20.00 GiBCurrent LE 5120Segments 1Allocation inheritRead ahead sectors auto- currently set to 256Block device 254:0
为创建好的逻辑卷创建文件系统
123456789101112➜ ~ mkfs.ext4 /dev/lvmtest/lv1mke2fs 1.42.12 (29-Aug-2014)Creating filesystem with 5242880 4k blocks and 1310720 inodesFilesystem UUID: 5130e6e1-8293-4fcb-b580-5091f3840f99Superblock backups stored on blocks:32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,4096000Allocating group tables: doneWriting inode tables: doneCreating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: done将格式化好的逻辑卷挂载使用
1➜ ~ mount /dev/lvmtest/lv1 /mnt/
查看
12 ➜ /mnt mount | tail -n 1/dev/mapper/lvmtest-lv1 on /mnt type ext4 (rw,relatime,data=ordered)
删除LVM
先把相应的东西umount掉
1 ➜ ~ umount /mnt
|
|
删除LV
123➜ ~ lvremove /dev/lvmtest/lv1Do you really want to remove active logical volume lv1? [y/n]: yLogical volume "lv1" successfully removed删除VG
12➜ ~ vgremove lvmtestVolume group "lvmtest" successfully removed删除物理卷
12➜ ~ pvremove /dev/sdbLabels on physical volume "/dev/sdb" successfully wiped
扩充逻辑卷容量
逻辑卷的拉伸操作可以在线执行,不需要卸载逻辑卷
- 保证VG中有足够的空闲空间
|
|
扩充逻辑卷
123➜ ~ lvextend -L +1G /dev/vg/lvSize of logical volume vg/lv changed from 20.00 GiB (5120 extents) to 21.00 GiB (5376 extents).Logical volume lv successfully resized查看扩充后LV大小
1234567891011121314151617➜ ~ lvdisplay--- Logical volume ---LV Path /dev/vg/lvLV Name lvVG Name vgLV UUID 5q9mCJ-7Y3f-dEo7-xlDz-lJAW-Oczm-RuCeIELV Write Access read/writeLV Creation host, time kali, 2016-08-03 09:51:18 +0800LV Status available# open 0LV Size 21.00 GiBCurrent LE 5376Segments 1Allocation inheritRead ahead sectors auto- currently set to 256Block device 254:0更新文件系统
12345➜ ~ resize2fs /dev/vg/lvresize2fs 1.42.12 (29-Aug-2014)Filesystem at /dev/vg/lv is mounted on /mnt; on-line resizing requiredold_desc_blocks = 2, new_desc_blocks = 2The filesystem on /dev/vg/lv is now 5505024 (4k) blocks long.查看更新后文件系统
123456789101112➜ ~ df -hFilesystem Size Used Avail Use% Mounted onudev 10M 0 10M 0% /devtmpfs 792M 9.1M 783M 2% /run/dev/sda1 288G 16G 257G 6% /tmpfs 2.0G 208K 2.0G 1% /dev/shmtmpfs 5.0M 0 5.0M 0% /run/locktmpfs 2.0G 0 2.0G 0% /sys/fs/cgrouptmpfs 396M 8.0K 396M 1% /run/user/132tmpfs 396M 12K 396M 1% /run/user/0/dev/sr0 3.1G 3.1G 0 100% /media/cdrom0/dev/mapper/vg-lv 21G 44M 20G 1% /mnt
扩充卷组容量
将要添加到VG的硬盘格式化为PV
12➜ ~ pvcreate /dev/sdcPhysical volume "/dev/sdc" successfully created将新的PV添加到指定卷组中
12➜ ~ vgextend vg /dev/sdcVolume group "vg" successfully extended查看扩充后VG大小
123➜ ~ vgsVG #PV #LV #SN Attr VSize VFreevg 2 1 0 wz--n- 69.99g 48.99g
缩小逻辑卷容量
逻辑卷的缩小操作必须离线执行,要卸载逻辑卷
卸载一键挂载的逻辑卷
1➜ ~ umount /dev/mapper/vg-lv缩小文件系统
缩小为10G
1234567891011121314151617181920➜ ~ resize2fs /dev/vg/lv 10Gresize2fs 1.42.12 (29-Aug-2014)Please run 'e2fsck -f /dev/vg/lv' first.➜ ~ resize2fs /dev/mapper/vg-lv 10Gresize2fs 1.42.12 (29-Aug-2014)Please run 'e2fsck -f /dev/mapper/vg-lv' first.➜ ~ e2fsck -f /dev/mapper/vg-lve2fsck 1.42.12 (29-Aug-2014)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary information/dev/mapper/vg-lv: 11/1376256 files (0.0% non-contiguous), 130401/5505024 blocks➜ ~ resize2fs /dev/mapper/vg-lv 10Gresize2fs 1.42.12 (29-Aug-2014)Resizing the filesystem on /dev/mapper/vg-lv to 2621440 (4k) blocks.The filesystem on /dev/mapper/vg-lv is now 2621440 (4k) blocks long.缩小LV
缩小的容量为之前的容量减去文件系统容量
21-10=11123456➜ ~ lvreduce -L -11G /dev/vg/lvWARNING: Reducing active logical volume to 10.00 GiBTHIS MAY DESTROY YOUR DATA (filesystem etc.)Do you really want to reduce lv? [y/n]: ySize of logical volume vg/lv changed from 21.00 GiB (5376 extents) to 10.00 GiB (2560 extents).Logical volume lv successfully resized查看缩小后的LV
1234567891011121314151617➜ ~ lvdisplay--- Logical volume ---LV Path /dev/vg/lvLV Name lvVG Name vgLV UUID 5q9mCJ-7Y3f-dEo7-xlDz-lJAW-Oczm-RuCeIELV Write Access read/writeLV Creation host, time kali, 2016-08-03 09:51:18 +0800LV Status available# open 0LV Size 10.00 GiBCurrent LE 2560Segments 1Allocation inheritRead ahead sectors auto- currently set to 256Block device 254:0挂载
123➜ ~ mount /dev/vg/lv /mnt➜ ~ mount | tail -n 1/dev/mapper/vg-lv on /mnt type ext4 (rw,relatime,data=ordered)
缩小卷组
将一个PV从制定卷组中移除
12➜ ~ vgreduce vg /dev/sdcRemoved "/dev/sdc" from volume group "vg"查看缩小后的卷组大小
123456789101112131415161718192021~ vgdisplay--- Volume group ---VG Name vgSystem IDFormat lvm2Metadata Areas 1Metadata Sequence No 8VG Access read/writeVG Status resizableMAX LV 0Cur LV 1Open LV 1Max PV 0Cur PV 1Act PV 1VG Size 50.00 GiBPE Size 4.00 MiBTotal PE 12799Alloc PE / Size 2560 / 10.00 GiBFree PE / Size 10239 / 40.00 GiBVG UUID E32ZJW-3NqL-jpr2-1KYN-QWDh-dZhJ-1ZxZ2i移除物理卷
12345678910➜ ~ pvsPV VG Fmt Attr PSize PFree/dev/sdb vg lvm2 a-- 50.00g 40.00g/dev/sdc lvm2 --- 20.00g 20.00g➜ ~ pvremove /dev/sdcLabels on physical volume "/dev/sdc" successfully wiped➜ ~ pvsPV VG Fmt Attr PSize PFree/dev/sdb vg lvm2 a-- 50.00g 40.00g➜ ~