为什么要用LVM?
想象一个示例,如果我们给定的分区是50G,过段时间发现空间不够用,此时我们就要重新加一块新硬盘,重新分区格式化,将原来分区上的数据复制过来,再重新挂载,整个过程会非常麻烦,LVM可以很有效的解决这个问题
LVM简介
及逻辑卷(Logical Volume Manager)管理,它的突出重点在于可以弹性收缩filesystem的容量,可以将多块物理硬盘整合成一个整体,这样看就像是一块磁盘一样,而且还可以添加或删除硬盘到这个整体中,实现弹性磁盘空间。
PV:物理卷(Physical Volume)
PV物理卷就是指硬盘区分或从逻辑上与磁盘分区具有同样功能的设备,是LVM的基本存储逻辑块。
VG:卷组(Volume Group)
其内部是由多个物理卷组合而成的一个整体空间,可有一个或多个物理卷组成
LV:逻辑卷(Logical Volume)
逻辑卷类似于系统中的硬盘分区,在逻辑卷上可以创建文件系统并使用
dm:Device Mapper,将一个或多个底层块设备组织成一个逻辑设备的模块
/dev/dm#
/dev/mapper/VG_NAME-LV_NAME
/dev/mapper/vo10-root
/dev/VG_NAME/LV_NAME
/dev/vo10/root
pv管理工具:
pvs:查看pv简要信息
pvdisplay:查看pv详细信息
pvcreate /dev/DEVICE:创建pv
pvmove /dev/DEVICEE:将指定pv的数据移至其他pv
pvremove /dev/DEVICE:移除指定的pv
vg管理工具:
vgs:查看vg简要信息
vgdisplay:查看vg详细信息
vgcreate VolumeGroupName PhysicalDevicePath… 创建卷组
vgextend VolumeGroupName PhysicalDevicePath… 扩展卷组
vgreduce VolumeGroupName PhysicalDevicePath… 缩减卷组
缩减前先做pvmove
vgremove VG_NAME:移除卷组
lv管理工具:
lvs:查看lv简要信息
lvdisplay:查看lv详细信息
lvcreate -L #[mMgGtT] -n LV_NAME VolumeGroup 创建lv
lvremove /dev/VG_NAME/LV_NAME
扩展逻辑卷:
lvextend -L [+]#[mGgGtT] /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 /dev/VG_NAME/LV_NAME /point
创建LVM
1.创建pv物理卷
[root@CentOS6 ~]# pvcreate /dev/sd{b,c} Physical volume "/dev/sdb" successfully created Physical volume "/dev/sdc" successfully created [root@CentOS6 ~]# pvs #此时pv还不属于任何卷组 PV VG Fmt Attr PSize PFree /dev/sdb lvm2 ---- 20.00g 20.00g /dev/sdc lvm2 ---- 20.00g 20.00g [root@CentOS6 ~]#
2.创建vg卷组
[root@CentOS6 ~]# vgcreate myvg /dev/sd{b,c} #创建名字为myvg的卷组 Volume group "myvg" successfully created [root@CentOS6 ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 2 0 0 wz--n- 39.99g 39.99g [root@CentOS6 ~]# pvs #两个pv属于卷组myvg PV VG Fmt Attr PSize PFree /dev/sdb myvg lvm2 a--u 20.00g 20.00g /dev/sdc myvg lvm2 a--u 20.00g 20.00g [root@CentOS6 ~]#
3.创建lv逻辑卷
[root@CentOS6 ~]# lvcreate -L 20G -n mylv myvg Logical volume "mylv" created. [root@CentOS6 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-a----- 20.00g [root@CentOS6 ~]#
4.创建文件系统
[root@CentOS6 ~]# mke2fs -t ext4 -b 2048 -m 3 -L MYLV /dev/myvg/mylv mke2fs 1.41.12 (17-May-2010) Filesystem label=MYLV OS type: Linux Block size=2048 (log=1) Fragment size=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 1310720 inodes, 10485760 blocks 314572 blocks (3.00%) reserved for the super user First data block=0 Maximum filesystem blocks=547356672 640 block groups 16384 blocks per group, 16384 fragments per group 2048 inodes per group Superblock backups stored on blocks: 16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 2048000, 3981312, 5619712, 10240000 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 23 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@CentOS6 ~]# blkid /dev/myvg/mylv /dev/myvg/mylv: LABEL="MYLV" UUID="26fd05b4-b59e-4c65-b9f5-03bb4749912a" TYPE="ext4" [root@CentOS6 ~]#
5.挂载使用
[root@CentOS6 ~]# mount -a [root@CentOS6 ~]# mount | tail -1 /dev/mapper/myvg-mylv on /mnt/lv type ext4 (rw) [root@CentOS6 ~]# cd /mnt/lv/ [root@CentOS6 lv]# cp /etc/fstab ./ [root@CentOS6 lv]# cat fstab # # /etc/fstab # Created by anaconda on Thu Jul 21 15:34:03 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=f4d977bb-af76-4617-b07f-9c70ae42a056 / ext4 defaults 1 1 UUID=7907abdd-41cd-4609-8392-80e4a88c5b4c /boot ext4 defaults 1 2 UUID=50054489-14c8-4e49-a912-2ff368371653 /testdir ext4 defaults 1 2 UUID=d9f41da6-9df2-410f-8074-500425b26a3d swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 //10.1.250.60/share /root/mount cifs username=share,password=zhaizong 0 0 /dev/sr0 /media/cdrom iso9660 defaults 0 0 LABEL=MYLV /mnt/lv ext4 defaults 0 0 [root@CentOS6 lv]#
扩展卷组
[root@CentOS6 ~]# pvcreate /dev/sdd #添加一个pv Physical volume "/dev/sdd" successfully created [root@CentOS6 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb myvg lvm2 a--u 20.00g 0 /dev/sdc myvg lvm2 a--u 20.00g 19.99g /dev/sdd lvm2 ---- 20.00g 20.00g [root@CentOS6 ~]# vgextend myvg /dev/sdd #扩展卷组 Volume group "myvg" successfully extended [root@CentOS6 ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 3 1 0 wz--n- 59.99g 39.99g [root@CentOS6 ~]#
缩减卷组
[root@CentOS6 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb myvg lvm2 a--u 20.00g 0 /dev/sdc myvg lvm2 a--u 20.00g 19.99g /dev/sdd myvg lvm2 a--u 20.00g 20.00g [root@CentOS6 ~]# pvmove /dev/sdb #先将指定pv上的空间移至其他pv /dev/sdb: Moved: 0.1% /dev/sdb: Moved: 85.2% /dev/sdb: Moved: 100.0% [root@CentOS6 ~]# vgreduce myvg /dev/sdb #从卷组中移除指定pv Removed "/dev/sdb" from volume group "myvg" [root@CentOS6 ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 2 1 0 wz--n- 39.99g 19.99g [root@CentOS6 ~]# pvremove /dev/sdb Labels on physical volume "/dev/sdb" successfully wiped [root@CentOS6 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdc myvg lvm2 a--u 20.00g 19.99g /dev/sdd myvg lvm2 a--u 20.00g 0 [root@CentOS6 ~]#
扩展lv
[root@CentOS6 ~]# lvs #当前lv为20G空间大小 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-ao---- 20.00g [root@CentOS6 ~]# lvextend -L 30G /dev/myvg/mylv #扩展lv Size of logical volume myvg/mylv changed from 20.00 GiB (5120 extents) to 30.00 GiB (7680 extents). Logical volume mylv successfully resized. [root@CentOS6 ~]# resize2fs /dev/myvg/mylv 30G #扩展文件系统至30G resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv is mounted on /mnt/lv; on-line resizing required old desc_blocks = 10, new_desc_blocks = 15 Performing an on-line resize of /dev/myvg/mylv to 15728640 (2k) blocks. The filesystem on /dev/myvg/mylv is now 15728640 blocks long. [root@CentOS6 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-ao---- 30.00g [root@CentOS6 ~]# df -hP |tail -1 #扩展成功 /dev/mapper/myvg-mylv 30G 15M 29G 1% /mnt/lv 扩展可以不用卸载文件系统
缩减lv
[root@CentOS6 ~]# umount /dev/myvg/mylv #第一步先卸载文件系统 [root@CentOS6 ~]# fsck -f /dev/myvg/mylv #强制检测文件系统 fsck from util-linux-ng 2.17.2 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 MYLV: 12/1966080 files (0.0% non-contiguous), 288304/15728640 blocks [root@CentOS6 ~]# resize2fs /dev/myvg/mylv 10G #缩减文件系统至10G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv to 5242880 (2k) blocks. The filesystem on /dev/myvg/mylv is now 5242880 blocks long. [root@CentOS6 ~]# lvreduce -L 10G /dev/ #缩减物理卷 Display all 215 possibilities? (y or n) [root@CentOS6 ~]# lvreduce -L 10G /dev/myvg/mylv WARNING: Reducing active logical volume to 10.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvg/mylv? [y/n]: y Size of logical volume myvg/mylv changed from 30.00 GiB (7680 extents) to 10.00 GiB (2560 extents). Logical volume mylv successfully resized. [root@CentOS6 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-a----- 10.00g [root@CentOS6 ~]# mount /dev/myvg/mylv /mnt/lv/ #重新挂载逻辑卷,大小已缩减为10G [root@CentOS6 ~]# df -hP | tail -1 /dev/mapper/myvg-mylv 9.8G 13M 9.5G 1% /mnt/lv
删除逻辑卷
[root@CentOS6 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-a----- 10.00g [root@CentOS6 ~]# lvremove /dev/myvg/mylv #删除逻辑卷mylv Do you really want to remove active logical volume mylv? [y/n]: y Logical volume "mylv" successfully removed [root@CentOS6 ~]# lvs [root@CentOS6 ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 2 0 0 wz--n- 39.99g 39.99g [root@CentOS6 ~]# vgremove myvg #删除卷组myvg Volume group "myvg" successfully removed [root@CentOS6 ~]# vgs [root@CentOS6 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdc lvm2 ---- 20.00g 20.00g /dev/sdd lvm2 ---- 20.00g 20.00g [root@CentOS6 ~]# pvremove /dev/sd{c,d} #删除所有pv Labels on physical volume "/dev/sdc" successfully wiped Labels on physical volume "/dev/sdd" successfully wiped [root@CentOS6 ~]# pvs [root@CentOS6 ~]#
快照卷
快照就是将当时的系统信息记录下来,就好像照相一般,若将来有任何数据改动了,则原始数据会被移动到快照区,没有改动的区域则由快照区和文件系统共享
如何创建快照卷
lvcreate -L #[mMgGtT] -p r -n snapshot_lv_name -s original_lv_name
1.创建快照卷
[root@CentOS6 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-ao---- 10.00g [root@CentOS6 ~]# lvcreate -L 2G -p r -n mulv_snap -s /dev/myvg/mylv #为mylv逻辑卷创建快照卷 Logical volume "mulv_snap" created. [root@CentOS6 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mulv_snap myvg sri-a-s--- 2.00g mylv 0.00 mylv myvg owi-aos--- 10.00g [root@CentOS6 ~]# mkdir /mnt/snap [root@CentOS6 ~]# mount /dev/myvg/mulv_snap /mnt/snap/ #挂载快照卷 mount: block device /dev/mapper/myvg-mulv_snap is write-protected, mounting read-only [root@CentOS6 ~]# cd /mnt/snap/ [root@CentOS6 snap]# ls fstab lost+found
2.恢复快照卷
[root@CentOS6 lv]# ls fstab lost+found [root@CentOS6 lv]# rm -rf fstab #原有的mylv中的数据丢失 [root@CentOS6 lv]# ls lost+found [root@CentOS6 lv]# cp -r /mnt/snap/* ./ #把快照卷当中数据复制回来即可 cp: overwrite `./fstab'? y [root@CentOS6 lv]# ls fstab lost+found [root@CentOS6 lv]#
3.删除快照卷
[root@CentOS6 lv]# umount /mnt/snap/ #先卸载快照卷 [root@CentOS6 lv]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mulv_snap myvg sri-a-s--- 2.00g mylv 0.00 mylv myvg owi-aos--- 10.00g [root@CentOS6 lv]# lvremove /dev/myvg/mulv_snap #删除快照卷 Do you really want to remove active logical volume mulv_snap? [y/n]: y Logical volume "mulv_snap" successfully removed [root@CentOS6 lv]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-ao---- 10.00g [root@CentOS6 lv]#
原创文章,作者:zhai796898,如若转载,请注明出处:http://www.178linux.com/42720