LVM创建过程:创建分区格式为8e->PV(物理卷)->VG(卷组)->LV(逻辑卷)
在做逻辑卷之前先做出分区来
Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 2101 16567296 83 Linux /dev/sda3 2101 2611 4096000 82 Linux swap / Solaris /dev/sda4 2611 3916 10483750 5 Extended /dev/sda5 2611 2742 1053563+ 8e Linux LVM /dev/sda6 2743 2874 1060258+ 8e Linux LVM /dev/sda7 2875 3006 1060258+ 8e Linux LVM
1.PV 物理卷
· pvcreate /dev/sda{5,6,7}:创建pv,之前需要将分区类型调整为8e。
[root@localhost ~]# pvcreate /dev/sda{5,6,7} Physical volume "/dev/sda5" successfully created Physical volume "/dev/sda6" successfully created Physical volume "/dev/sda7" successfully created
· pvremove /dev/sda5:删除pv,删除之前需要将pv上的数据移动到别的地方
[root@localhost ~]# pvremove /dev/sda{5,6,7} Labels on physical volume "/dev/sda5" successfully wiped Labels on physical volume "/dev/sda6" successfully wiped Labels on physical volume "/dev/sda7" successfully wiped
· pvmove /dev/sda5:移动/dev/sda5中的数据至别的地方
[root@localhost ~]# pvmove /dev/sda{5,6,7} Physical volume /dev/sda5 not in a volume group Run `pvmove --help' for more information. 由于我的卷本来就是新建的,所以提示空
· pvs;pvscan;pvdisplay:显示当前系统上物理卷的信息
[root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda5 myvg lvm2 a- 1.00g 924.00m /dev/sda6 myvg lvm2 a- 1.01g 1.01g /dev/sda7 myvg lvm2 a- 1.01g 1.01g ------------------------------------------------ [root@localhost ~]# pvscan PV /dev/sda5 VG myvg lvm2 [1.00 GiB / 924.00 MiB free] PV /dev/sda6 VG myvg lvm2 [1.01 GiB / 1.01 GiB free] PV /dev/sda7 VG myvg lvm2 [1.01 GiB / 1.01 GiB free] Total: 3 [3.02 GiB] / in use: 3 [3.02 GiB] / in no VG: 0 [0 ] -------------------------------------------------- [root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sda5 VG Name myvg PV Size 1.00 GiB / not usable 4.87 MiB Allocatable yes PE Size 4.00 MiB Total PE 256 Free PE 231 Allocated PE 25 PV UUID VIpxlf-eka9-D0d1-PvgQ-wCdo-jpbo-7G56a2 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 --- Physical volume --- PV Name /dev/sda7 VG Name myvg PV Size 1.01 GiB / not usable 3.41 MiB Allocatable yes PE Size 4.00 MiB Total PE 258 Free PE 258 Allocated PE 0 PV UUID OFHqZa-SdoL-rEhJ-EN2f-Xa0O-heTB-CLod08
2.VG 卷组
· vgcreate myvg /dev/sda{5,6,7}:将已经创建好的pv加入到vg组中,并给vg组命名
[root@localhost ~]# vgcreate myvg /dev/sda{5,6,7} Volume group "myvg" successfully created
· vgreduce myvg /dev/sda5:将myvg组中的sda5pv移除,需要先迁移sda5中的数据
[root@localhost ~]# pvmove /dev/sda5 No data to move for myvg [root@localhost ~]# vgreduce myvg /dev/sda5 Removed "/dev/sda5" from volume group "myvg"
· vgextend myvg /dev/sda5:向myvg组中添加sda5pv。
[root@localhost ~]# vgextend myvg /dev/sda5 Volume group "myvg" successfully extended
· vgremove myvg:整个移除myvg组
[root@localhost ~]# vgremove myvg Volume group "myvg" successfully removed [root@localhost ~]# vgs No volume groups found [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda5 lvm2 a- 1.00g 1.00g /dev/sda6 lvm2 a- 1.01g 1.01g /dev/sda7 lvm2 a- 1.01g 1.01g
· vgrename myvg yourvg :vg重命名
[root@localhost ~]# vgrename myvg yourvg Volume group "myvg" successfully renamed to "yourvg" [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree yourvg 3 0 0 wz--n- 3.02g 3.02g
· vgs;vgscan;vgdisplay:显示当前系统上卷组信息
[root@localhost ~]# vgscan Reading all physical volumes. This may take a while... Found volume group "myvg" using metadata type lvm2 [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 3 0 0 wz--n- 3.02g 3.02g [root@localhost ~]# vgdisplay --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 3 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 3.02 GiB PE Size 4.00 MiB Total PE 772 Alloc PE / Size 0 / 0 Free PE / Size 772 / 3.02 GiB VG UUID tOavVo-rP7K-F86r-n3a3-qRjL-ahP8-LAA7Hk
3.LV 逻辑卷
· lvcreate -n mylv -L 100M myvg:在myvg组中创建一个大小为100M,名字为mylv的逻辑卷
[root@localhost ~]# lvcreate -n mylv -L 100M myvg Logical volume "mylv" created [root@localhost ~]# lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert mylv myvg -wi-a- 100.00m
创建完逻辑卷后,存在位置为/dev/vg组/lv
[root@localhost ~]# ls /dev/myvg/mylv -l lrwxrwxrwx. 1 root root 7 Mar 7 04:35 /dev/myvg/mylv -> ../dm-0 [root@localhost ~]# ls -l /dev/dm-0 brw-rw----. 1 root disk 253, 0 Mar 7 04:35 /dev/dm-0
· lvresize -l [+|-] 100M /dev/myvg/mylv:调整lv大小,可以增加多少(同等于lvextend),减少多少(同等于lvreduce),或者等于多少;
[root@localhost ~]# lvresize -L -10M /dev/myvg/mylv Rounding up size to full physical extent 8.00 MiB WARNING: Reducing active logical volume to 92.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv? [y/n]: y Reducing logical volume mylv to 92.00 MiB Logical volume mylv successfully resized 减小大小可能会导致数据被损坏 ----------------------------------------------------- [root@localhost ~]# lvresize -L 100M /dev/myvg/mylv Extending logical volume mylv to 100.00 MiB Logical volume mylv successfully resized ------------------------------------------------------ [root@localhost ~]# lvresize -L +100M /dev/myvg/mylv Extending logical volume mylv to 200.00 MiB Logical volume mylv successfully resized
· lvremove /dev/myvg/mylv:直接删除逻辑卷
[root@localhost ~]# lvremove /dev/myvg/mylv Do you really want to remove active logical volume mylv? [y/n]: y Logical volume "mylv" successfully removed
· lvs;lvscan;lvdisplay:显示当前系统上逻辑卷信息
[root@localhost ~]# lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert mylv myvg -wi-a- 100.00m ------------------------------------------------------- [root@localhost ~]# lvscan ACTIVE '/dev/myvg/mylv' [100.00 MiB] inherit ------------------------------------------------------- [root@localhost ~]# lvdisplay --- Logical volume --- LV Name /dev/myvg/mylv VG Name myvg LV UUID UbxtI0-pSLs-luwF-Av4K-JKcw-iVCV-gf4aI3 LV Write Access read/write LV Status available # open 0 LV Size 100.00 MiB Current LE 25 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
4.挂载lvm
在挂载lvm前,需要先给逻辑卷进行分区格式化。
[root@localhost ~]# mke2fs -j /dev/myvg/mylv mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 25688 inodes, 102400 blocks 5120 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67371008 13 block groups 8192 blocks per group, 8192 fragments per group 1976 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 29 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
在格式化完成之后,将逻辑卷挂载至/test下
[root@localhost ~]# mount /dev/myvg/mylv /test [root@localhost ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/sda2 16G 2.2G 13G 15% / tmpfs 996M 88K 996M 1% /dev/shm /dev/sda1 291M 30M 246M 11% /boot /dev/mapper/myvg-mylv 97M 5.6M 87M 7% /test
5.扩容(先调整逻辑卷在调整分区)
resize2fs /dev/myvg/mylv [100M]:重新设定分区大小,不加参数代表重读逻辑卷。
ext3格式支持resize2fs在线扩容,即挂载状态下扩容。需要先将逻辑卷增加容量后再去调整分区
[root@localhost ~]# lvresize -L +100M /dev/myvg/mylv Extending logical volume mylv to 200.00 MiB Logical volume mylv successfully resized 先扩容逻辑卷 --------------------------------------------------------- [root@localhost ~]# resize2fs /dev/myvg/mylv resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv is mounted on /test; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/mylv to 204800 (1k) blocks. The filesystem on /dev/myvg/mylv is now 204800 blocks long. 再去重读分区 ---------------------------------------------------------- [root@localhost ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/sda2 16G 2.2G 13G 15% / tmpfs 996M 88K 996M 1% /dev/shm /dev/sda1 291M 30M 246M 11% /boot /dev/mapper/myvg-mylv 194M 5.6M 179M 4% /test 查看到已经增加值200M容量
6.缩小(先调整分区在调整逻辑卷)
resize2fs /dev/myvg/mylv [100M]:重新设定分区大小,不加参数代表重读逻辑卷。
xfs_growfs 针对逻辑卷的动态调整
注意事项:
a.不能在线缩减,得先卸载;
b.确保缩减后的空间大小可以容下原有的数据;
第一步,卸载分区
[root@localhost ~]# umount /test
第二部,检查文件系统完整性
[root@localhost ~]#e2fsck -f /dev/myvg/mylv
第三部,调整分区大小和逻辑卷大小
[root@localhost ~]#resize2fs /dev/myvg/mylv 100M [root@localhost ~]# lvresize -L 100M /dev/myvg/mylv WARNING: Reducing active and open logical volume to 100.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv? [y/n]: y Reducing logical volume mylv to 100.00 MiB Logical volume mylv successfully resized
第四部,格式化,仅针对xfs文件系统,ext234不用
[root@localhost ~]# mkfs.ext4 /dev/myvg/mylv
第五部,挂载分区
[root@localhost ~]# mount /dev/myvg/mylv /test [root@localhost ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/sda2 16G 2.2G 13G 15% / tmpfs 996M 88K 996M 1% /dev/shm /dev/sda1 291M 30M 246M 11% /boot /dev/mapper/myvg-mylv 97M 5.6M 87M 7% /test 这时看到分区大小已经为100M