来自 操作系统 2019-11-07 08:05 的文章
当前位置: 网上澳门金莎娱乐 > 操作系统 > 正文

Centos 中的LVM详解整理

有关逻辑卷的其他操作,请看:

大纲

  Linux中对逻辑卷的建立

一、简介

二、版本

三、LVM 模块

四、具体操作

  • 对添加的硬盘进行分区(fdisk /dev/[hs]d[a-z])

  • 对创建的分区创建物理卷(pvcreate)

  • 给逻辑卷创建逻辑容器(卷组)

  • 在卷组创建大小不同的逻辑卷(lvcreate)

  • 给以存在的卷组扩大容量

  • 实现在线扩大LVM容量

  • 实现缩减LVM容量(不支持在线缩减)

  • 减小卷组容量

  • 利用给LVM创建快照,并完成备份并还原数据

一、简介

LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,于1998年发布到Linux社区中,它允许你在Linux系统上用简单的命令行管理一个完整的逻辑卷管理环境。

  Linux中对逻辑卷进行扩容与缩小

二、版本

  Linux中对逻辑卷的移除

LVM1 最初的LVM与1998年发布,只在Linux内核2.4版本上可用,它提供最基本的逻辑卷管理。

  

LVM2 LVM-1的更新版本,在Linux内核2.6中才可用,它在标准的LVM-1功能外还提供了额外的功能。

  LVM还有快照的功能,类似windows的系统还原点。其特点:

查看:(测试机CentOS 5.5 X86_64)

    1.快照卷的容量必须与逻辑卷的容量相同

1234567[[email protected] ~]``# uname -a ``Linux localhost.localdomain 2.6.18-194.el5 ``#1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux ``[[email protected] ~]``# uname -r ``2.6.18-194.el5 ``[[email protected] ~]``# rpm -qa | grep lvm ``lvm2-2.02.56-8.el5 ``[[email protected] ~]``#

    2.快照卷仅一次有效,在执行还原后就自动删除了

三、LVM 模块

 首先查看卷组的信息:

Physical volume (PV)、Volume group (VG)、Logical volume(LV)、 Physical extent (PE),下面我们用一个简单的图来说明下物理卷、卷组、逻辑卷他们之间的关系(此图只是个人理解,仅供参考)

  图片 1

图片 2

 可以看到,这里的分配空间为120M,所以新创建的快照(zhi_back)的大小应该也是120M

简而言之:
逻辑卷的创建,就是将多块硬盘创建物理卷,而将这些物理卷以逻辑的形式总成一个容器,然后从这个容器里面创建大小不同的分区文件,而这个容器就是所谓的逻辑卷,而从这个容器里创建大小不同的分区文件,这个分区文件就叫做逻辑卷。嘿嘿,你懂了吗? ^_^ ……

 1.给原始的卷组中创建一个文件lv-zhi.info(其中保存的是vg的信息):

四、具体操作

图片 3

  1. 分区

1234567891011121314151617181920212223242526272829303132333435363738[[email protected] ~]``# fdisk –l #查看已存在的分区与硬盘``Disk ``/dev/sda``: 21.4 GB, 21474836480 bytes ``255 heads, 63 sectors``/track``, 2610 cylinders ``Units = cylinders of 16065 * 512 = 8225280 bytes````Device Boot Start End Blocks Id System ``/dev/sda1``* 1 33 265041 83 Linux ``/dev/sda2``34 1308 10241437+ 83 Linux ``/dev/sda3``1309 1945 5116702+ 83 Linux ``/dev/sda4``1946 2610 5341612+ 5 Extended ``/dev/sda5``1946 2072 1020096 82 Linux swap / Solaris``Disk ``/dev/sdb``: 21.4 GB, 21474836480 bytes ``255 heads, 63 sectors``/track``, 2610 cylinders ``Units = cylinders of 16065 * 512 = 8225280 bytes``Disk ``/dev/sdb``doesn't contain a valid partition table``Disk ``/dev/sdc``: 21.4 GB, 21474836480 bytes ``255 heads, 63 sectors``/track``, 2610 cylinders ``Units = cylinders of 16065 * 512 = 8225280 bytes``Disk ``/dev/sdc``doesn't contain a valid partition table``[[email protected] ~]``# fdisk /dev/sdb #给/sdb分区,效果如下``Command (m ``for``help): p``Disk ``/dev/sdb``: 21.4 GB, 21474836480 bytes ``255 heads, 63 sectors``/track``, 2610 cylinders ``Units = cylinders of 16065 * 512 = 8225280 bytes````Device Boot Start End Blocks Id System ``/dev/sdb1``1 244 1959898+ 8e Linux LVM ``#注意修改分区类型为8e ``/dev/sdb2``245 488 1959930 8e Linux LVM ``/dev/sdb3``489 732 1959930 8e Linux LVM``Command (m ``for``help):``[[email protected] ~]``# partprobe #让内核重新读取一下硬盘``[[email protected] ~]``# fdisk -l /dev/sdb``Disk ``/dev/sdb``: 21.4 GB, 21474836480 bytes ``255 heads, 63 sectors``/track``, 2610 cylinders ``Units = cylinders of 16065 * 512 = 8225280 bytes````Device Boot Start End Blocks Id System ``/dev/sdb1``1 244 1959898+ 8e Linux LVM ``/dev/sdb2``245 488 1959930 8e Linux LVM ``/dev/sdb3``489 732 1959930 8e Linux LVM ``[[email protected] ~]``#

  1. 将物理分区与硬盘创建为物理卷(pvcreate)

12345678910111213[[email protected] ~]``# pvcreate /dev/sdb[1-3] #将分区创建为物理卷 ````Physical volume ``"/dev/sdb1"``successfully created ````Physical volume ``"/dev/sdb2"``successfully created ````Physical volume ``"/dev/sdb3"``successfully created ``[[email protected] ~]``#``[[email protected] ~]``# pvcreate /dev/sdc #将硬盘创建为物理卷 ````Physical volume ``"/dev/sdc"``successfully created``[[email protected] ~]``# pvs #查看创建的物理卷 ````PV VG Fmt Attr PSize PFree ````/dev/sdb1``myvg lvm2 a- 1.87G 1.87G ````/dev/sdb2``myvg lvm2 a- 1.87G 1.87G ````/dev/sdb3``myvg lvm2 a- 1.87G 1.87G ````/dev/sdc``myvg lvm2 a- 20.00G 20.00G

  1. 将物理卷(pv)创建为卷组(vgcreate),名为myvg

12345678910111213141516171819202122232425262728[[email protected] ~]``# vgcreate myvg /dev/sdb[1-3] /dev/sdc````Volume group ``"myvg"``successfully created ``[[email protected] ~]``# vgs ````VG ``#PV #LV #SN Attr VSize VFree ````myvg 4 0 0 wz--n- 25.60G 25.60G ``[[email protected] ~]``#``[[email protected] ~]``# vgdisplay ````--- Volume group --- ````VG Name myvg ``#卷组名 ````System ID ````Format lvm2 ````Metadata Areas 4 ````Metadata Sequence No 1 ````VG Access ``read``/write````VG Status resizable ````MAX LV 0 ````Cur LV 0 ````Open LV 0 ````Max PV 0 ````Cur PV 4 ````Act PV 4 ````VG Size 25.60 GB ````PE Size 4.00 MB ``#物理盘的基本单位:默认4MB ````Total PE 6553 ````Alloc PE / Size 0 / 0 ````Free PE / Size 6553 / 25.60 GB ````VG UUID wuNBgb-tP95-pVoX-ehHw-cMfN-hyem-PNMqwe ``[[email protected] ~]``#

  1. 在卷组里创建逻辑卷并格式化、挂载使用

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950[[email protected] ~]``# lvcreate -L 2G -n mylv1 myvg #创建逻辑卷 ````Logical volume ``"mylv1"``created ``[[email protected] ~]``# lvs #查看逻辑卷 ````LV VG Attr LSize Origin Snap% Move Log Copy% Convert ````mylv1 myvg -wi-a- 2.00G ``[[email protected] ~]``# mke2fs -j /dev/myvg/mylv1 #格式化逻辑卷 ``mke2fs 1.39 (29-May-2006) ``Filesystem label= ``OS ``type``: Linux ``Block size=4096 (log=2) ``Fragment size=4096 (log=2) ``262144 inodes, 524288 blocks ``26214 blocks (5.00%) reserved ``for``the super user ``First data block=0 ``Maximum filesystem blocks=536870912 ``16 block ``groups``32768 blocks per group, 32768 fragments per group ``16384 inodes per group ``Superblock backups stored on blocks: ````32768, 98304, 163840, 229376, 294912``Writing inode tables: ``done``Creating journal (16384 blocks): ``done``Writing superblocks and filesystem accounting information: ``done``This filesystem will be automatically checked every 26 mounts or ``180 days, whichever comes first. Use tune2fs -c or -i to override. ``[[email protected] ~]``# mkdir /mydata #创建挂载目录 ``[[email protected] ~]``# mount /dev/myvg/mylv1 /mydata/ #挂载``[[email protected] ~]``# mount #查看是否挂载成功 ``/dev/sda2``on / ``type``ext3 (rw) ``proc on ``/proc``type``proc (rw) ``sysfs on ``/sys``type``sysfs (rw) ``devpts on ``/dev/pts``type``devpts (rw,gid=5,mode=620) ``/dev/sda3``on ``/data``type``ext3 (rw) ``/dev/sda1``on ``/boot``type``ext3 (rw) ``tmpfs on ``/dev/shm``type``tmpfs (rw) ``none on ``/proc/sys/fs/binfmt_misc``type``binfmt_misc (rw) ``sunrpc on ``/var/lib/nfs/rpc_pipefs``type``rpc_pipefs (rw) ``/dev/mapper/myvg-mylv1``on ``/mydata``type``ext3 (rw) ``[[email protected] ~]``#``[[email protected] ~]``# ls /mydata/ #查看 ``lost+found ``[[email protected] ~]``# df –h #查看硬盘 ``Filesystem Size Used Avail Use% Mounted on ``/dev/sda2``9.5G 1.8G 7.2G 20% / ``/dev/sda3``4.8G 138M 4.4G 4% ``/data``/dev/sda1``251M 17M 222M 7% ``/boot``tmpfs 60M 0 60M 0% ``/dev/shm``/dev/mapper/myvg-mylv1````2.0G 68M 1.9G 4% ``/mydata``[[email protected] ~]``#

  1. 发现卷组空间不够,我们扩大卷组空间

1234567891011121314151617[[email protected] ~]``# pvcreate /dev/sdd #将新硬盘/sdd加入物理卷中 ````Physical volume ``"/dev/sdd"``successfully created``[[email protected] ~]``# pvs #查看物理卷 ````/dev/cdrom``: ``open``failed: No medium found ````PV VG Fmt Attr PSize PFree ````/dev/sdb1``myvg lvm2 a- 1.87G 1.87G ````/dev/sdb2``myvg lvm2 a- 1.87G 1.87G ````/dev/sdb3``myvg lvm2 a- 1.87G 1.87G ````/dev/sdc``myvg lvm2 a- 20.00G 18.00G ````/dev/sdd``myvg lvm2 a- 20.00G 20.00G ``[[email protected] ~]``#``[[email protected] ~]``# vgextend myvg /dev/sdd #扩展卷组 ````Volume group ``"myvg"``successfully extended ``[[email protected] ~]``# vgs #查看新增加的卷组 ````VG ``#PV #LV #SN Attr VSize VFree ````myvg 5 1 0 wz--n- 45.59G 43.59G ``[[email protected] ~]``#

  1. 扩展逻辑卷 (支持在线扩展)

123456789101112131415161718192021222324252627282930313233将``/dev/myvg/mylv1``扩展到4G,并且要求数据可以正常访问``[[email protected] ~]``# cd /mydata/ ``[[email protected] mydata]``# touch index.html ``[[email protected] mydata]``# echo "test" > index.html ``[[email protected] mydata]``# cat index.html ``test``[[email protected] mydata]``# vgs ````/dev/cdrom``: ``open``failed: No medium found ````VG ``#PV #LV #SN Attr VSize VFree ````myvg 5 1 0 wz--n- 45.59G 43.59G ``[[email protected] mydata]``# lvextend -L +2G /dev/myvg/mylv1````Extending logical volume mylv1 to 4.00 GB ````Logical volume mylv1 successfully resized``[[email protected] mydata]``# resize2fs -p /dev/myvg/mylv1 #通过 resize2fs 将文件系统的容量确实添加``resize2fs 1.39 (29-May-2006) ``Filesystem at ``/dev/myvg/mylv1``is mounted on ``/mydata``; on-line resizing required ``Performing an on-line resize of ``/dev/myvg/mylv1``to 1048576 (4k) blocks. ``The filesystem on ``/dev/myvg/mylv1``is now 1048576 blocks long.``[[email protected] mydata]``# lvs ````LV VG Attr LSize Origin Snap% Move Log Copy% Convert ````mylv1 myvg -wi-ao 4.00G ``[[email protected] mydata]``#``[[email protected] mydata]``# cat index.html ``test``[[email protected] mydata]``# df -h ``Filesystem Size Used Avail Use% Mounted on ``/dev/sda2``9.5G 1.8G 7.2G 20% / ``/dev/sda3``4.8G 138M 4.4G 4% ``/data``/dev/sda1``251M 17M 222M 7% ``/boot``tmpfs 60M 0 60M 0% ``/dev/shm``/dev/mapper/myvg-mylv1````4.0G 69M 3.7G 2% ``/mydata``#成功增加了2G ``[[email protected] mydata]``#

  1. 缩减逻辑卷
  • 查看逻辑卷使用空间状况

  • 不能在线缩减,得先卸载

  • 确保缩减后的空间大小依然能存储原有的所有数据

  • 在缩减之前应该先强行检查文件,以确保文件系统处于一至性状态

12345678910111213141516将``/dev/myvg/mylv1``缩减到1G,并且要求数据可以正常访问 (所以我们就按上面的提示在操作)``[[email protected] ~]``#df –lh #查看已用空间大小``[[email protected] ~]``# umount /dev/myvg/mylv1 #卸载分区``[[email protected] ~]``# e2fsck -f /dev/myvg/mylv1 #强制检查文件系统``[[email protected] ~]``# resize2fs /dev/myvg/mylv1 1G #缩减逻辑大小 ``[[email protected] ~]``# lvreduce -L 1G /dev/myvg/mylv1 #缩减物理边界大小``[[email protected] ~]``# lvs #查看逻辑卷 ````/dev/cdrom``: ``open``failed: No medium found ````LV VG Attr LSize Origin Snap% Move Log Copy% Convert ````mylv1 myvg -wi-ao 1.00G ``[[email protected] ~]``# mount /dev/myvg/mylv1 /mydata``[[email protected] ~]``# ls /mydata/ ``index.html lost+found ``[[email protected] ~]``# cat /mydata/index.html ``test``[[email protected] ~]``#

  1. 缩减磁盘空间

发现物理磁盘空间使用不足,将其中一块硬盘或分区拿掉

  • pvmove /dev/sdb1 #将/dev/sdb1上存储的数据移到其它物理卷中

  • vgreduce myvg /dev/sdb1 #将/dev/sdb1从myvg卷组中移除

  • pvremove /dev/sdb1 #将/dev/sdb1从物理卷上移除

1234567891011121314151617181920212223[[email protected] ~]``# pvs ````/dev/cdrom``: ``open``failed: No medium found ````PV VG Fmt Attr PSize PFree ````/dev/sdb1``myvg lvm2 a- 1.87G 1.87G ````/dev/sdb2``myvg lvm2 a- 1.87G 1.87G ````/dev/sdb3``myvg lvm2 a- 1.87G 1.87G ````/dev/sdc``myvg lvm2 a- 20.00G 19.00G ````/dev/sdd``myvg lvm2 a- 20.00G 20.00G ``[[email protected] ~]``#``[[email protected] ~]``# pvmove /dev/sdb1 ````No data to move ``for``myvg``[[email protected] ~]``# vgreduce myvg /dev/sdb1 ````Removed ``"/dev/sdb1"``from volume group ``"myvg"``[email protected] ~]``# pvremove /dev/sdb1 ````Labels on physical volume ``"/dev/sdb1"``successfully wiped ``[[email protected] ~]``# pvs ````/dev/cdrom``: ``open``failed: No medium found ````PV VG Fmt Attr PSize PFree ````/dev/sdb2``myvg lvm2 a- 1.87G 1.87G ````/dev/sdb3``myvg lvm2 a- 1.87G 1.87G ````/dev/sdc``myvg lvm2 a- 20.00G 19.00G ````/dev/sdd``myvg lvm2 a- 20.00G 20.00G ``[[email protected] ~]``#

  1. 实现快照,进行备份还原

在/mnt/lvm目录上,我们将原始的目录文件进行快照,然后将/mydata目录中的内容清空,并进行还原

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950[[email protected] ~]``# cd /mydata/ ``[[email protected] mydata]``# ls ``index.html lost+found ``[[email protected] mydata]``# cat index.html ``test``[[email protected] mydata]``# lvcreate -L 30M -n backup -s -p r /dev/myvg/mylv1 #-L 快照大小 –n:快照名称 –p 权限只读 –s 创建快照 ````Rounding up size to full physical extent 32.00 MB ````Logical volume ``"backup"``created ``[[email protected] mydata]``# ll ``total 20 ``-rw-r--r-- 1 root root 5 Jun 28 01:04 index.html ``drwx------ 2 root root 16384 Jun 28 00:47 lost+found ``[[email protected] mydata]``# mkdir /tmp/backup #创建挂载目录 ``[[email protected] mydata]``# mount /dev/myvg/backup /tmp/backup/ #挂载``mount``: block device ``/dev/myvg/backup``is write-protected, mounting ``read``-only ``[[email protected] mydata]``# cd /tmp/backup/ ``[[email protected] backup]``# ls ``index.html lost+found ``[[email protected] backup]``# mkdir /tmp/lvmbackup #创建备份目录 ``[[email protected] backup]``# tar jcf /tmp/lvmbackup/sandy.tar.bz2 index.html #打包并压缩文件 ``[[email protected] backup]``# cd .. ``[[email protected] tmp]``# cd lvmbackup/ ``[[email protected] lvmbackup]``# ls #查看备份 ``sandy.``tar``.bz2 ``[[email protected] lvmbackup]``# cd /mydata/ ``[[email protected] mydata]``# ll ``total 20 ``-rw-r--r-- 1 root root 5 Jun 28 01:04 index.html ``drwx------ 2 root root 16384 Jun 28 00:47 lost+found ``[[email protected] mydata]``# rm -rf * #删除所有文件 ``[[email protected] mydata]``# ``[[email protected] mydata]``# ll ``total 0 ``[[email protected] mydata]``# tar xf /tmp/lvmbackup/sandy.tar.bz2 #还原备份数据 ``[[email protected] mydata]``# ll ``total 4 ``-rw-r--r-- 1 root root 5 Jun 28 01:04 index.html ``[[email protected] mydata]``# cat index.html ``test``[[email protected] mydata]``# df -h ``Filesystem Size Used Avail Use% Mounted on ``/dev/sda2``9.5G 1.8G 7.2G 21% / ``/dev/sda3``4.8G 138M 4.4G 4% ``/data``/dev/sda1``251M 17M 222M 7% ``/boot``tmpfs 60M 0 60M 0% ``/dev/shm``/dev/mapper/myvg-mylv1````1008M 67M 901M 7% ``/mydata``/dev/mapper/myvg-mylv1````1008M 67M 901M 7% ``/tmp/backup``[[email protected] mydata]``#

了解以上步骤操作的含义,我相信大家应该对LVM有更深的认识,嘿嘿 ^_^……

  2.创建快照zhi-back

本文出自 “Share your knowledge …” 博客,请务必保留此出处http://freeloda.blog.51cto.com/2033581/1231089

lvcreate -s -L 120M -n zhi-back /dev/zhi/lv-zhi

http://www.bkjia.com/Linuxjc/772900.htmlwww.bkjia.comtruehttp://www.bkjia.com/Linuxjc/772900.htmlTechArticle大纲 一、简介 二、版本 三、LVM 模块 四、具体操作 对添加的硬盘进行分区(fdisk /dev/[hs]d[a-z]) 对创建的分区创建物理卷(pvcreate) 给逻辑卷创...

  -L  要创建的大小哦

  -s 创建snapshot (快照)

  -n 快照的名字

  最后是要备份的硬盘

本文由网上澳门金莎娱乐发布于操作系统,转载请注明出处:Centos 中的LVM详解整理

关键词: