LVM(logical volume manager)逻辑卷管理(创建、挂载、扩容、缩小、删除)
LVM(logical volume manager)逻辑卷管理
PE physical extend 默认每个大小4M
PV physical volume
VG volume group
LV logical volume
1:将物理设备初始化为物理卷
# fdisk -l
/dev/sdc
/dev/sdd
#pvcreate /dev/sdc /dev/sdd
Physical volume "/dev/sdc" successfully created
Physical volume "/dev/sdd" successfully created
( pvcreate /dev/sdd /dev/sde
Device /dev/sdd not found (or ignored by filtering).
Physical volume "/dev/sde" successfully created
/dev/sde 提前使用过fdisk分区
#fdisk -l /dev/sde
Disk /dev/sde: 1098.4 GB, 1098437885952 bytes
255 heads, 63 sectors/track, 133544 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: 0x00000000
Erase the partition table (格式化)
# dd if=/dev/zero of=/dev/sdd bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.009214 s, 55.6 kB/s
#pvcreate /dev/sdd /dev/sde
Physical volume "/dev/sdd" successfully created
Physical volume "/dev/sde" successfully created
#vgextend ark /dev/sdf
Volume group "ark" successfully extended
#vgs
VG #PV #LV #SN Attr VSize VFree
ark 3 0 0 wz--n- 3.00t 3.00t
)
查看物理卷信息
#pvdisplay (详细)
#pvs
2:创建卷组,并将PV加入卷组中
#vgcreate ark /dev/sdc /dev/sdd
Volume group "ark" successfully created
追加新的PV到已存在的VG
#vgextend ark /dev/sdf
Volume group "ark" successfully extended
查看卷组信息
#vgdisplay (详细)
#vgs
3:基于卷组创建逻辑卷(是PE的整数倍)
#lvcreate -n mylv -L 3G ark
Logical volume "mylv" created
查看逻辑卷信息
#lvdisplay (详细)
#lvs
4:为创建好的逻辑卷创建文件系统
(如果逻辑卷较大的话,建议先使用screen
screen -S zhang
screen -r zhang)
#mkfs.ext4 /dev/ark/mylv
5:将格式化好的逻辑卷挂载使用
#mount /dev/ark/mylv /m
6:自动挂载
#vim /etc/fstab
-----------------
删除LVM
1:删除LV
#lvremove /dev/ark/mylv
2:删除VG
#vgremove ark
3:删除物理卷
#pvremove /dev/sdc /dev/sdd
------------------
LVM逻辑卷的拉伸与缩小
逻辑卷的拉伸可以在线执行,不需要卸载逻辑卷
1:保证VG中有足够的空闲空间
#vgs
2:扩充逻辑卷
#lvextend -L +1G /dev/ark/mylv
Extending logical volume mylv to 4.00 GiB
Logical volume mylv successfully resized
3:查看扩充后LV大小
#lvs
4:更新文件系统
#resize2fs /dev/ark/mylv
5:查看更新后文件系统
#df -h
------
拉伸一个卷组
1:将要添加到VG的硬盘格式化为PV
#pvcreate /dev/sde
2:将新的PV添加到指定卷组中
#vgextend ark /dev/sde
3:查看扩充后VG大小
#vgs
---------
缩小一个逻辑卷
逻辑卷的缩小必须离线执行,要卸载逻辑卷
1:卸载已挂载的逻辑卷
#umount /dev/ark/mylv
2:缩小文件系统 (会提示需要运行fsck检查文件系统)
#resize2fs /dev/ark/mylv 2G
Please run 'e2fsck -f /dev/ark/mylv' first.
#e2fsck -f /dev/ark/mylv'
#resize2fs /dev/ark/mylv 2G
3:缩小LV (缩小的LV必须小于等于缩小文件系统的大小,否则会损毁文件)
#lvreduce -L 2G /dev/ark/mylv
WARNING: Reducing active logical volume to 2.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv? [y/n]: y
Reducing logical volume mylv to 2.00 GiB
Logical volume mylv successfully resized
4:查看缩小后的LV
#lvs
5:挂载
#mount /dev/ark/mylv /m
------------
缩小卷组
1:将一个PV从指定卷组中移出 (要保证VG的空间要大于移走的物理卷)
#vgreduce ark /dev/sdc
Removed "/dev/sdc" from volume group "ark"
2:查看缩小后的卷组大小
#vgs
3:#pvs
pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup lvm2 a-- 19.51g 0
/dev/sdc lvm2 a-- 2.00g 2.00g
/dev/sdd ark lvm2 a-- 4.00g 2.00g
#pvremove /dev/sdc
Labels on physical volume "/dev/sdc" successfully wiped
4:可以移出硬盘/dev/sdc