标签:使用逻辑卷管理灵活储存
第五单元
使用逻辑卷管理灵活储存
一 lvm 定义
Lvm是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。将磁盘分区处理成物理卷(pv),再将物理卷变成一个整块的卷组(voilmegroup,vg)形成一个储存池。然后就可以在卷组随意创建lvm(逻辑卷组),并进一步在lvm上创建文件系统。
Lvm可拉伸也可压缩,当系统添加了新的磁盘,通过lvm,就不用将磁盘的文件移动到新的磁盘上以充分利用新的存储空间。而是直接扩展文件系统跨越磁盘即可
二 lvm 建立
如果出现以下问题:
[root@localhost ~]# pvcreate /dev/vdb1
Physical volume /dev/vdb1 not found
Can‘t open /dev/vdb1 exclusively. Mounted filesystem?
说明/dev/md0没有停止
解决:[root@localhost ~]# mdadm -S /dev/md0
mdadm: stopped /dev/md0
1 fdisk /dev/vdb ###建立两个分区####
图
注意:在wq保存前,要先将分区类型选成lvm的类型
监控pv,vg,lvm的信息:watch -n 1 ‘echo ===pvmessaage=== ; pvs; echo ===vgmessage=== ; vgs; echo ===lvmessage=== ; lvs; df /weixindata’
2 pvcreate /dev/vdb1 ###将物理分区创建成物理卷(pv)###
3 vgcreate vg0 /dev/vdb1 ##将物理卷创建成卷组,卷组名为vg0###
4 lvcreate -L 300M -n lv0 vg0 ###在vg0上创建lvm ,-L 值大小为300M,-n指取名为lv0###
5 Mkfs.xfs /dev/vg0/lv0 ###将lvm格式化###
6 mount /dev/vg0/lv0 /weixindata/ ###将lvm挂载到/weixindata目录下
Lvm创建过程如下:
[root@localhost ~]# pvcreate /dev/vdb1
Physical volume /dev/vdb1 not found
WARNING: linux_raid_member signature detected on /dev/vdb1 at offset 4096. Wipe it? [y/n] y
Wiping linux_raid_member signature on /dev/vdb1.
WARNING: xfs signature detected on /dev/vdb1 at offset 0. Wipe it? [y/n] y
Wiping xfs signature on /dev/vdb1.
Physical volume "/dev/vdb1" successfully created
[root@localhost ~]# vgcreate vg0 /dev/vdb1
Volume group "vg0" successfully created
[root@localhost ~]# lvcreate -L 300M -n lv0 vg0
Logical volume "lv0" created
[root@localhost ~]# mkfs.xfs /dev/vg0/lv0
meta-data=/dev/vg0/lv0 isize=256 agcount=4, agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mount /dev/vg0/lv0 /weixindata/
注: /dev/mapper/vg0-lv0 和 /dev/vg0/lv0都是指你所创建的lvm即lv0
如下:
[root@localhost ~]# ll /dev/mapper/vg0-lv0
lrwxrwxrwx. 1 root root 7 Apr 22 03:47 /dev/mapper/vg0-lv0 -> ../dm-0
[root@localhost ~]# ll /dev/vg0/lv0
lrwxrwxrwx. 1 root root 7 Apr 22 03:47 /dev/vg0/lv0 -> ../dm-0
[root@localhost ~]#
三 lvm拉伸
分两步:1 拉伸设备
2 拉伸文件系统
Lvextend -L 900M /dev/vg0/lv0 ###拉伸lvm到900M###
xfs_growfs /dev/vg0/lv0 ###拉伸文件系统###
过程如下:
[root@localhost ~]# lvextend -L 900M /dev/vg0/lv0
Extending logical volume lv0 to 900.00 MiB
Logical volume lv0 successfully resized
[root@localhost ~]# xfs_growfs /dev/vg0/lv0
meta-data=/dev/mapper/vg0-lv0 isize=256 agcount=4, agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 76800 to 230400
注意:如果要拉伸的大小太大,lvm不够时,只需扩大pv即可,pv不够就再加一块硬盘。
例:将大小为1024M的lvm拉伸到1500M,由于lvm不够,因此要扩大pv
Pvcreate /dev/vdb2 ###增加pv###
vgextend vg0 /dev/vdb2 ###增加vg0###
Lvextend -L 1500M /dev/vg0/lv0 ###拉伸lvm到1500M###
xfs_growfs /dev/vg0/lv0 ###拉伸文件系统###
过程如下:
[root@localhost ~]# lvextend -L 1500M /dev/vg0/lv0
Extending logical volume lv0 to 1.46 GiB
Insufficient free space: 150 extents needed, but only 30 available
[root@localhost ~]# pvcreate /dev/vdb2
Physical volume /dev/vdb2 not found
WARNING: linux_raid_member signature detected on /dev/vdb2 at offset 4096. Wipe it? [y/n] y
Wiping linux_raid_member signature on /dev/vdb2.
Physical volume "/dev/vdb2" successfully created
[root@localhost ~]# vgextend vg0 /dev/vdb2
Volume group "vg0" successfully extended
[root@localhost ~]# lvextend -L 1500M /dev/vg0/lv0
Extending logical volume lv0 to 1.46 GiB
Logical volume lv0 successfully resized
[root@localhost ~]# xfs_growfs /dev/vg0/lv0
meta-data=/dev/mapper/vg0-lv0 isize=256 agcount=12, agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=230400, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 230400 to 384000
四 lvm 压缩
注意:有的文件系统不能压缩,像xfs的文件系统就不能压缩,ext4文件系统可以压缩
所以如果是xfs的文件系统要先改变文件系统
Umount /weixindata ###卸载###
Mkfs.ext4 /dev/vg0/lv0 ###格式化###
Mount /dev/vg0/lv0 /weixindata/ ###挂载###
过程如下:
[root@localhost ~]# umount /weixindata/
[root@localhost ~]# mkfs.ext4 /dev/vg0/lv0
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
96000 inodes, 384000 blocks
19200 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=394264576
12 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mount /dev/vg0/lv0 /weixindata/
开始压缩:
为了防止压缩过小将文件损坏,就要先卸载,然后扫描,如果压缩过小就会提示
umount /weixindata/ ###卸载###
e2fsck -f /dev/vg0/lv0 ###扫描###
resize2fs /dev/vg0/lv0 300M ###压缩文件系统到300M###
如果没有提示,则说明压缩在允许范围内
mount /dev/vg0/lv0 /weixindata/ ##挂载###
lvreduce -L 300 /dev/vg0/lv0 ###压缩设备到300M###
过程如下:
[root@localhost ~]# umount /weixindata/
[root@localhost ~]# mkfs.ext4 /dev/vg0/lv0
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
96000 inodes, 384000 blocks
19200 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=394264576
12 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mount /dev/vg0/lv0 /weixindata/
五 移除物理卷
要从一个卷组中移除一个物理卷,首先要确认要移除的物理卷没有被任何逻辑卷正在使用,可通过“pvdisplay”命令查看
Pvdisplay /dev/vdb2
如果total PE 和Free PE 相等,则没有被使用
如果正被使用,就需要将该物理卷的数据转移到别的物理卷,然后才能移除。
pvmove /dev/vdb1 /dev/vdb2 ###将物理卷/dev/vdb1的数据移到/dev/vdb2###
vgreduce vg0 /dev/vdb1 ###把vdb1从vg0里移走###
pvremove /dev/vdb1 ###删除物理卷/dev/vdb1 ###
过程如下:
root@localhost ~]# pvmove /dev/vdb1 /dev/vdb2
/dev/vdb1: Moved: 17.3%
/dev/vdb1: Moved: 100.0%
[root@localhost ~]# vgreduce vg0 /dev/vdb1
Removed "/dev/vdb1" from volume group "vg0"
[root@localhost ~]# pvremove /dev/vdb1
Labels on physical volume "/dev/vdb1" successfully wiped
六 lvm 快照
目的 :为保护原始数据
lvcreate -L 50M -n lv0backup -s /dev/vg0/lv0 ###-s指对/dev/vg0/lv0进行快照,-L指快照大小为50M,-n指快照名字为 lv0backup
mount /dev/vg0/lv0backup /weixindata/ ###将快照挂载到/weixindata/
例:在lvm挂载的目录/weixindata/ 下建立文件,然后对lvm进行快照,将其挂载到/weixindata/ 下,查看里面的文件,将文件全部删除,再删除快照,重新建立新的快照,再次查看/weixindata/ 下文件,发现文件还在,所以起到保护原始数据的作用。
过程如下:
[root@localhost ~]# touch /weixindata/file{1..10}
[root@localhost ~]# cd /weixindata/
[root@localhost weixindata]# ls
file1 file2 file4 file6 file8 lost+found
file10 file3 file5 file7 file9
[root@localhost weixindata]# lvcreate -L 50M -n lv0backup -s /dev/vg0/lv0
Rounding up size to full physical extent 52.00 MiB
Logical volume "lv0backup" created
[root@localhost weixindata]# mount /dev/vg0/lv0backup /weixindata/
[root@localhost weixindata]# cd /weixindata/
[root@localhost weixindata]# ls
file1 file2 file4 file6 file8 lost+found
file10 file3 file5 file7 file9
[root@localhost weixindata]# rm -fr *
[root@localhost weixindata]# ls
[root@localhost weixindata]# cd
[root@localhost ~]# umount /weixindata/
[root@localhost ~]# lvremove /dev/vg0/lv0backup ###删除快照###
Do you really want to remove active logical volume lv0backup? [y/n]: y
Logical volume "lv0backup" successfully removed
[root@localhost ~]# lvcreate -L 50M -n lv0backup -s /dev/vg0/lv0 ###建立快照##
Rounding up size to full physical extent 52.00 MiB
Logical volume "lv0backup" created
[root@localhost ~]# mount /dev/vg0/lv0backup /weixindata/
[root@localhost ~]# cd /weixindata/
[root@localhost weixindata]# ls
file1 file2 file4 file6 file8 lost+found
file10 file3 file5 file7 file9
七 卸载
umount /weixindata/ ###卸载###
lvremove /dev/vg0/lvbackup ###删除快照###
lvremove /dev/vg0/lv0 ###删除lv0###
vgremove vg0 ###删除vg0###
pvremove /dev/vdb2 ###删除物理卷/dev/vdb2###
标签:使用逻辑卷管理灵活储存
原文地址:http://12774272.blog.51cto.com/12764272/1919997