Centos 中的LVM详解
时间:2015-01-10 23:52 来源:www.it.net.cn 作者:IT网
一、简介
二、版本
三、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系统上用简单的命令行管理一个完整的逻辑卷管理环境。
二、版本
LVM1 最初的LVM与1998年发布,只在Linux内核2.4版本上可用,它提供最基本的逻辑卷管理。
LVM2 LVM-1的更新版本,在Linux内核2.6中才可用,它在标准的LVM-1功能外还提供了额外的功能。
查看:(测试机CentOS 5.5 X86_64)
[root@localhost ~]# 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
[root@localhost ~]# uname -r
2.6.18-194.el5
[root@localhost ~]# rpm -qa | grep lvm
lvm2-2.02.56-8.el5
[root@localhost ~]#
三、LVM 模块
Physical volume (PV)、Volume group (VG)、Logical volume(LV)、 Physical extent (PE),下面我们用一个简单的图来说明下物理卷、卷组、逻辑卷他们之间的关系(此图只是个人理解,仅供参考)
简而言之:
逻辑卷的创建,就是将多块硬盘创建物理卷,而将这些物理卷以逻辑的形式总成一个容器,然后从这个容器里面创建大小不同的分区文件,而这个容器就是所谓的逻辑卷,而从这个容器里创建大小不同的分区文件,这个分区文件就叫做逻辑卷。嘿嘿,你懂了吗? ^_^ ……
四、具体操作
1. 分区
root@localhost ~]# 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/sda234 1308 10241437+ 83 Linux
/dev/sda31309 1945 5116702+ 83 Linux
/dev/sda41946 2610 5341612+ 5 Extended
/dev/sda51946 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/sdbdoesn'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/sdcdoesn't contain a valid partition table
[root@localhost ~]# fdisk /dev/sdb #给/sdb分区,效果如下
Command (m forhelp): 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/sdb11 244 1959898+ 8e Linux LVM #注意修改分区类型为8e
/dev/sdb2245 488 1959930 8e Linux LVM
/dev/sdb3489 732 1959930 8e Linux LVM
Command (m forhelp):
[root@localhost ~]# partprobe #让内核重新读取一下硬盘
[root@localhost ~]# 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/sdb11 244 1959898+ 8e Linux LVM
/dev/sdb2245 488 1959930 8e Linux LVM
/dev/sdb3489 732 1959930 8e Linux LVM
[root@localhost ~]#
2. 将物理分区与硬盘创建为物理卷(pvcreate)
[root@localhost ~]# pvcreate /dev/sdb[1-3] #将分区创建为物理卷
Physical volume "/dev/sdb1"successfully created
Physical volume "/dev/sdb2"successfully created
Physical volume "/dev/sdb3"successfully created
[root@localhost ~]#
[root@localhost ~]# pvcreate /dev/sdc #将硬盘创建为物理卷
Physical volume "/dev/sdc"successfully created
[root@localhost ~]# pvs #查看创建的物理卷
PV VG Fmt Attr PSize PFree
/dev/sdb1myvg lvm2 a- 1.87G 1.87G
/dev/sdb2myvg lvm2 a- 1.87G 1.87G
/dev/sdb3myvg lvm2 a- 1.87G 1.87G
/dev/sdcmyvg lvm2 a- 20.00G 20.00G
3. 将物理卷(pv)创建为卷组(vgcreate),名为myvg
[root@localhost ~]# vgcreate myvg /dev/sdb[1-3] /dev/sdc
Volume group "myvg"successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 4 0 0 wz--n- 25.60G 25.60G
[root@localhost ~]#
[root@localhost ~]# 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
[root@localhost ~]#
4. 在卷组里创建逻辑卷并格式化、挂载使用
[root@localhost ~]# lvcreate -L 2G -n mylv1 myvg #创建逻辑卷
Logical volume "mylv1"created
[root@localhost ~]# lvs #查看逻辑卷
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
mylv1 myvg -wi-a- 2.00G
[root@localhost ~]# 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 forthe 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.
[root@localhost ~]# mkdir /mydata #创建挂载目录
[root@localhost ~]# mount /dev/myvg/mylv1 /mydata/ #挂载
[root@localhost ~]# mount #查看是否挂载成功
/dev/sda2on / typeext3 (rw)
proc on /proctypeproc (rw)
sysfs on /systypesysfs (rw)
devpts on /dev/ptstypedevpts (rw,gid=5,mode=620)
/dev/sda3on /datatypeext3 (rw)
/dev/sda1on /boottypeext3 (rw)
tmpfs on /dev/shmtypetmpfs (rw)
none on /proc/sys/fs/binfmt_misctypebinfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefstyperpc_pipefs (rw)
/dev/mapper/myvg-mylv1on /mydatatypeext3 (rw)
[root@localhost ~]#
[root@localhost ~]# ls /mydata/ #查看
lost+found
[root@localhost ~]# df –h #查看硬盘
Filesystem Size Used Avail Use% Mounted on
/dev/sda29.5G 1.8G 7.2G 20% /
/dev/sda34.8G 138M 4.4G 4% /data
/dev/sda1251M 17M 222M 7% /boot
tmpfs 60M 0 60M 0% /dev/shm
/dev/mapper/myvg-mylv1
2.0G 68M 1.9G 4% /mydata
[root@localhost ~]#
5. 发现卷组空间不够,我们扩大卷组空间
[root@localhost ~]# pvcreate /dev/sdd #将新硬盘/sdd加入物理卷中
Physical volume "/dev/sdd"successfully created
[root@localhost ~]# pvs #查看物理卷
/dev/cdrom: openfailed: No medium found
PV VG Fmt Attr PSize PFree
/dev/sdb1myvg lvm2 a- 1.87G 1.87G
/dev/sdb2myvg lvm2 a- 1.87G 1.87G
/dev/sdb3myvg lvm2 a- 1.87G 1.87G
/dev/sdcmyvg lvm2 a- 20.00G 18.00G
/dev/sddmyvg lvm2 a- 20.00G 20.00G
[root@localhost ~]#
[root@localhost ~]# vgextend myvg /dev/sdd #扩展卷组
Volume group "myvg"successfully extended
[root@localhost ~]# vgs #查看新增加的卷组
VG #PV #LV #SN Attr VSize VFree
myvg 5 1 0 wz--n- 45.59G 43.59G
[root@localhost ~]#
6. 扩展逻辑卷 (支持在线扩展)
将/dev/myvg/mylv1扩展到4G,并且要求数据可以正常访问
[root@localhost ~]# cd /mydata/
[root@localhost mydata]# touch index.html
[root@localhost mydata]# echo "test" > index.html
[root@localhost mydata]# cat index.html
test
[root@localhost mydata]# vgs
/dev/cdrom: openfailed: No medium found
VG #PV #LV #SN Attr VSize VFree
myvg 5 1 0 wz--n- 45.59G 43.59G
[root@localhost mydata]# lvextend -L +2G /dev/myvg/mylv1
Extending logical volume mylv1 to 4.00 GB
Logical volume mylv1 successfully resized
[root@localhost mydata]# resize2fs -p /dev/myvg/mylv1 #通过 resize2fs 将文件系统的容量确实添加
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/myvg/mylv1is mounted on /mydata; on-line resizing required
Performing an on-line resize of /dev/myvg/mylv1to 1048576 (4k) blocks.
The filesystem on /dev/myvg/mylv1is now 1048576 blocks long.
[root@localhost mydata]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
mylv1 myvg -wi-ao 4.00G
[root@localhost mydata]#
[root@localhost mydata]# cat index.html
test
[root@localhost mydata]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda29.5G 1.8G 7.2G 20% /
/dev/sda34.8G 138M 4.4G 4% /data
/dev/sda1251M 17M 222M 7% /boot
tmpfs 60M 0 60M 0% /dev/shm
/dev/mapper/myvg-mylv1
4.0G 69M 3.7G 2% /mydata#成功增加了2G
[root@localhost mydata]#
7. 缩减逻辑卷
查看逻辑卷使用空间状况
不能在线缩减,得先卸载
确保缩减后的空间大小依然能存储原有的所有数据
在缩减之前应该先强行检查文件,以确保文件系统处于一至性状态
将/dev/myvg/mylv1缩减到1G,并且要求数据可以正常访问 (所以我们就按上面的提示在操作)
[root@localhost ~]#df –lh #查看已用空间大小
[root@localhost ~]# umount /dev/myvg/mylv1 #卸载分区
[root@localhost ~]# e2fsck -f /dev/myvg/mylv1 #强制检查文件系统
[root@localhost ~]# resize2fs /dev/myvg/mylv1 1G #缩减逻辑大小
[root@localhost ~]# lvreduce -L 1G /dev/myvg/mylv1 #缩减物理边界大小
[root@localhost ~]# lvs #查看逻辑卷
/dev/cdrom: openfailed: No medium found
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
mylv1 myvg -wi-ao 1.00G
[root@localhost ~]# mount /dev/myvg/mylv1 /mydata
[root@localhost ~]# ls /mydata/
index.html lost+found
[root@localhost ~]# cat /mydata/index.html
test
[root@localhost ~]#
8. 缩减磁盘空间
发现物理磁盘空间使用不足,将其中一块硬盘或分区拿掉
pvmove /dev/sdb1 #将/dev/sdb1上存储的数据移到其它物理卷中
vgreduce myvg /dev/sdb1 #将/dev/sdb1从myvg卷组中移除
pvremove /dev/sdb1 #将/dev/sdb1从物理卷上移除
[root@localhost ~]# pvs
/dev/cdrom: openfailed: No medium found
PV VG Fmt Attr PSize PFree
/dev/sdb1myvg lvm2 a- 1.87G 1.87G
/dev/sdb2myvg lvm2 a- 1.87G 1.87G
/dev/sdb3myvg lvm2 a- 1.87G 1.87G
/dev/sdcmyvg lvm2 a- 20.00G 19.00G
/dev/sddmyvg lvm2 a- 20.00G 20.00G
[root@localhost ~]#
[root@localhost ~]# pvmove /dev/sdb1
No data to move formyvg
[root@localhost ~]# vgreduce myvg /dev/sdb1
Removed "/dev/sdb1"from volume group "myvg"
root@localhost ~]# pvremove /dev/sdb1
Labels on physical volume "/dev/sdb1"successfully wiped
[root@localhost ~]# pvs
/dev/cdrom: openfailed: No medium found
PV VG Fmt Attr PSize PFree
/dev/sdb2myvg lvm2 a- 1.87G 1.87G
/dev/sdb3myvg lvm2 a- 1.87G 1.87G
/dev/sdcmyvg lvm2 a- 20.00G 19.00G
/dev/sddmyvg lvm2 a- 20.00G 20.00G
[root@localhost ~]#
9. 实现快照,进行备份还原
在/mnt/lvm目录上,我们将原始的目录文件进行快照,然后将/mydata目录中的内容清空,并进行还原
[root@localhost ~]# cd /mydata/
[root@localhost mydata]# ls
index.html lost+found
[root@localhost mydata]# cat index.html
test
[root@localhost 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
[root@localhost 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
[root@localhost mydata]# mkdir /tmp/backup #创建挂载目录
[root@localhost mydata]# mount /dev/myvg/backup /tmp/backup/ #挂载
mount: block device /dev/myvg/backupis write-protected, mounting read-only
[root@localhost mydata]# cd /tmp/backup/
[root@localhost backup]# ls
index.html lost+found
[root@localhost backup]# mkdir /tmp/lvmbackup #创建备份目录
[root@localhost backup]# tar jcf /tmp/lvmbackup/sandy.tar.bz2 index.html #打包并压缩文件
[root@localhost backup]# cd ..
[root@localhost tmp]# cd lvmbackup/
[root@localhost lvmbackup]# ls #查看备份
sandy.tar.bz2
[root@localhost lvmbackup]# cd /mydata/
[root@localhost 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
[root@localhost mydata]# rm -rf * #删除所有文件
[root@localhost mydata]#
[root@localhost mydata]# ll
total 0
[root@localhost mydata]# tar xf /tmp/lvmbackup/sandy.tar.bz2 #还原备份数据
[root@localhost mydata]# ll
total 4
-rw-r--r-- 1 root root 5 Jun 28 01:04 index.html
[root@localhost mydata]# cat index.html
test
[root@localhost mydata]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda29.5G 1.8G 7.2G 21% /
/dev/sda34.8G 138M 4.4G 4% /data
/dev/sda1251M 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
[root@localhost mydata]#
了解以上步骤操作的含义,我相信大家应该对LVM有更深的认识,嘿嘿 ^_^……
本文出自 “Share your knowledge …” 博客,请务必保留此出处http://freeloda.blog.51cto.com/2033581/1231089
(责任编辑:IT)
一、简介 对创建的分区创建物理卷(pvcreate) 给逻辑卷创建逻辑容器(卷组) 在卷组创建大小不同的逻辑卷(lvcreate) 给以存在的卷组扩大容量 实现在线扩大LVM容量 实现缩减LVM容量(不支持在线缩减) 减小卷组容量 利用给LVM创建快照,并完成备份并还原数据
一、简介
简而言之: 不能在线缩减,得先卸载 确保缩减后的空间大小依然能存储原有的所有数据 在缩减之前应该先强行检查文件,以确保文件系统处于一至性状态
将/dev/myvg/mylv1缩减到1G,并且要求数据可以正常访问 (所以我们就按上面的提示在操作) vgreduce myvg /dev/sdb1 #将/dev/sdb1从myvg卷组中移除 pvremove /dev/sdb1 #将/dev/sdb1从物理卷上移除
[root@localhost ~]# pvs 本文出自 “Share your knowledge …” 博客,请务必保留此出处http://freeloda.blog.51cto.com/2033581/1231089 (责任编辑:IT) |