首页 > 代码库 > (转载) Linux LVM分区之VG扩容、LV扩容、LV缩减、LVM快照

(转载) Linux LVM分区之VG扩容、LV扩容、LV缩减、LVM快照

http://www.dwhd.org/20150521_225146.html

Linux LVM分区之VG扩容、LV扩容、LV缩减、LVM快照

摘要

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

 

一、简介
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 6.6 X86_64)

01
02
03
04
05
06
07
08
09
10
[root@ZhongH100 ~]# rpm -qa | grep lvm
mesa-private-llvm-3.4-3.el6.x86_64
lvm2-libs-2.02.111-2.el6_6.2.x86_64
lvm2-2.02.111-2.el6_6.2.x86_64
[root@ZhongH100 ~]# cat /etc/centos-release
CentOS release 6.6 (Final)
[root@ZhongH100 ~]# uname -a
Linux ZhongH100.wxjr.com.cn 2.6.32-504.16.2.el6.centos.plus.x86_64 #1 SMP Wed Apr 22 00:59:31 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@ZhongH100 ~]# getconf LONG_BIT
64

技术分享


三、LVM 模块
Physical volume (PV)、Volume group (VG)、Logical volume(LV)、 Physical extent (PE),下面我们用一个简单的图来说明下物理卷、卷组、逻辑卷他们之间的关系(此图只是个人理解,仅供参考)
LVM 详解技术分享
简而言之:
逻辑卷的创建,就是将多块硬盘创建物理卷,而将这些物理卷以逻辑的形式总成一个容器,然后从这个容器里面创建大小不同的分区文件,而这个容器就是所谓的逻辑卷,而从这个容器里创建大小不同的分区文件,这个分区文件就叫做逻辑卷。嘿嘿,你懂了吗? ^_^ ……


四、具体操作
1. 分区 (本实验环境使用的是一块新磁盘/dev/sdb)

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@ZhongH100 ~]# fdisk -l /dev/sd[a-z]
 
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x0006c656
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        6591    52428800   8e  Linux LVM
 
Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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
 
[root@ZhongH100 ~]#
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
[root@ZhongH100 ~]# fdisk /dev/sdb  #试用fdisk命令来管理磁盘分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xfb1f25cf.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won‘t be recoverable.
 
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
         switch off the mode (command ‘c‘) and change display units to
         sectors (command ‘u‘).
 
Command (m for help): p                      #输入p来打印当前磁盘上的分区
 
Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0xfb1f25cf
 
   Device Boot      Start         End      Blocks   Id  System
 
Command (m for help): n                      #输入n 新建分区
Command action
   e   extended
   p   primary partition (1-4)
p                                            #输入p 选择分区类型为主分区
Partition number (1-4): 1                    #输入1 选择为第一个主分区
First cylinder (1-7832, default 1):          #直接回车 选择分区起始块为1
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-7832, default 7832): +10G        #输入+10G 为新分区大小为10G
 
Command (m for help): n                      #输入n 在当前磁盘上再次新建一个分区
Command action
   e   extended
   p   primary partition (1-4)
p                                            #输入p 选择分区类型为主分区
Partition number (1-4): 2                    #输入2 选择为第二个主分区
First cylinder (1307-7832, default 1307):    #直接回车 选择分区起始块为1307
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-7832, default 7832): +10G      #输入+10G 为新分区大小为10G
 
Command (m for help): n                      #输入n 在当前磁盘上再次新建一个分区
Command action
   e   extended
   p   primary partition (1-4)
p                                            #输入p 选择分区类型为主分区
Partition number (1-4): 3                    #输入3 选择为第三个主分区
First cylinder (2613-7832, default 2613):    #直接回车 选择分区起始块为2613
Using default value 2613
Last cylinder, +cylinders or +size{K,M,G} (2613-7832, default 7832): +10G      #输入+10G 为新分区大小为10G
 
Command (m for help): p                      #输入p来打印当前磁盘上的分区
 
Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0xfb1f25cf
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2612    10490445   83  Linux
/dev/sdb3            2613        3918    10490445   83  Linux
 
Command (m for help): t                      #输入t 来改变分区类型
Partition number (1-4): 1                    #输入1 来选择改变分区类型的分区号为1
Hex code (type L to list codes): 8e          #输入8e 改变分区类型为LVM
Changed system type of partition 1 to 8e (Linux LVM)
 
Command (m for help): t                      #输入t 来改变分区类型
Partition number (1-4): 2                    #输入2 来选择改变分区类型的分区号为2
Hex code (type L to list codes): 8e          #输入8e 改变分区类型为LVM
Changed system type of partition 2 to 8e (Linux LVM)
 
Command (m for help): t                      #输入t 来改变分区类型
Partition number (1-4): 3                    #输入3 来选择改变分区类型的分区号为3
Hex code (type L to list codes): 8e          #输入8e 改变分区类型为LVM
Changed system type of partition 3 to 8e (Linux LVM)
 
Command (m for help): p                      #输入p来打印当前磁盘上的分区
 
Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0xfb1f25cf
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  8e  Linux LVM
/dev/sdb2            1307        2612    10490445   8e  Linux LVM
/dev/sdb3            2613        3918    10490445   8e  Linux LVM
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@ZhongH100 ~]#

新分区完毕后我们需要让内核重新载入,如果执行一次不能载入所有分区那么就多执行几次,直至全部能识别到,我们的sdb上有3个分区,下面的命令已经显示全部识别了

1
2
3
4
5
6
7
[root@ZhongH100 ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
2. 将物理分区与硬盘创建为物理卷(pvcreate)
01
02
03
04
05
06
07
08
09
10
11
[root@ZhongH100 ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3 #这是正常的命令写法 也可以使用下面那种扩展写法^C
[root@ZhongH100 ~]# pvcreate /dev/sdb{1,2,3}
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
  Physical volume "/dev/sdb3" successfully created
[root@ZhongH100 ~]# pvs                                          #使用pvs来查看当前系统上所有的pv
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sdb1           lvm2 ---  10.00g 10.00g
  /dev/sdb2           lvm2 ---  10.00g 10.00g
  /dev/sdb3           lvm2 ---  10.00g 10.00g
[root@ZhongH100 ~]#
3. 将物理卷(pv)创建为卷组(vgcreate),名为VGtest
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@ZhongH100 ~]# vgcreate VGtest /dev/sdb{1,2,3}
  Volume group "VGtest" successfully created
[root@ZhongH100 ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  VGtest     3   0   0 wz--n- 30.00g 30.00g
[root@ZhongH100 ~]# vgdisplay
  --- Volume group ---
  VG Name               VGtest     #卷组名是VGtest
  System ID
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               30.00 GiB  #新的VG大小是30G 3个10G分区组成的
  PE Size               4.00 MiB   #物理盘的基本单位:默认4MB
  Total PE              7680
  Alloc PE / Size       0 / 0
  Free  PE / Size       7680 / 30.00 GiB
  VG UUID               W8fYiw-Zh46-53lr-qWuf-hqLR-Rqla-x1mFQH
[root@ZhongH100 ~]#
4. 在卷组里创建逻辑卷并格式化、挂载使用
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[root@ZhongH100 ~]# lvcreate  -L 2G -n LVtest1 VGtest   #在名为VGtest的VG上创建一个名为LVtest1 大小为2G的逻辑卷
  Logical volume "LVtest1" created
[root@ZhongH100 ~]# lvs                                 #查看系统上的LV逻辑卷
  LV      VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LVtest1 VGtest   -wi-a-----  2.00g
[root@ZhongH100 ~]# mke2fs -t ext4 /dev/VGtest/LVtest1  #格式化新建的LVtest1逻辑卷为ext4格式
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912
 
正在写入inode表: 完成
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
 
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@ZhongH100 ~]# mkdir /LVtest1                      #创建一个LVtest1的目录
[root@ZhongH100 ~]# mount /dev/VGtest/LVtest1 /LVtest1  #将/dev/VGtest/LVtest1这个逻辑卷挂载到 /LVtest1目录上
[root@ZhongH100 ~]# mount                               #查看挂载情况
/dev/mapper/vgzhongH-root on / type ext4 (rw,acl)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vgzhongH-data on /data type ext4 (rw,acl)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/VGtest-LVtest1 on /LVtest1 type ext4 (rw)   #挂载成功 分区格式是ext4 可读写
[root@ZhongH100 ~]# df -hP                              #查看系统上的分区情况
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vgzhongH-root    30G  3.3G   25G  12% /
tmpfs                       932M     0  932M   0% /dev/shm
/dev/sda1                   477M   34M  418M   8% /boot
/dev/mapper/vgzhongH-data   4.8G   10M  4.6G   1% /data
/dev/mapper/VGtest-LVtest1  2.0G  3.0M  1.9G   1% /LVtest1  #LVtest1逻辑卷分区正常
[root@ZhongH100 ~]#
5. 发现卷组pv空间不够,我们需要扩大卷组空间
 现在系统上新增了一块20G的硬盘/dev/sdc
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@ZhongH100 ~]# fdisk -l /dev/sd[a-z]
 
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x0006c656
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        6591    52428800   8e  Linux LVM
 
Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0xfb1f25cf
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  8e  Linux LVM
/dev/sdb2            1307        2612    10490445   8e  Linux LVM
/dev/sdb3            2613        3918    10490445   8e  Linux LVM
 
Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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
01
02
03
04
05
06
07
08
09
10
11
12
13
[root@ZhongH100 ~]# pvcreate /dev/sdc   #将新硬盘/sdc加入物理卷上
  Physical volume "/dev/sdc" successfully created
[root@ZhongH100 ~]# pvs                       #查看物理卷
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sdb1  VGtest   lvm2 a--  10.00g  8.00g
  /dev/sdb2  VGtest   lvm2 a--  10.00g 10.00g
  /dev/sdb3  VGtest   lvm2 a--  10.00g 10.00g
  /dev/sdc            lvm2 ---  20.00g 20.00g
[root@ZhongH100 ~]# vgextend VGtest /dev/sdc  #扩展卷组
  Volume group "VGtest" successfully extended
[root@ZhongH100 ~]# vgs                       #查看卷组
  VG       #PV #LV #SN Attr   VSize  VFree
  VGtest     4   1   0 wz--n- 50.00g 48.00g   #从大小可以看出我们已经扩容成功
6. 扩展逻辑卷 (支持在线扩展)
 在线将/dev/VGtest/LVtest1 扩展到4G,并且要求数据可以正常访问
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@ZhongH100 ~]# cd /LVtest1/
[root@ZhongH100 LVtest1]# echo "this is a test for LVM" > lvtest #穿件个lvtest的文件并写入内容
[root@ZhongH100 LVtest1]# cat lvtest
this is a test for LVM
[root@ZhongH100 LVtest1]# lvs
  LV      VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LVtest1 VGtest   -wi-ao----  2.00g
[root@ZhongH100 LVtest1]# lvextend -L +2G /dev/VGtest/LVtest1
  Size of logical volume VGtest/LVtest1 changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
  Logical volume LVtest1 successfully resized
[root@ZhongH100 LVtest1]# lvs
  LV      VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LVtest1 VGtest   -wi-ao----  4.00g                         #逻辑卷空间已经增加
[root@ZhongH100 LVtest1]# e2fsck -f /dev/VGtest/LVtest1
[root@ZhongH100 LVtest1]# resize2fs -p /dev/VGtest/LVtest1   #通过 resize2fs 将文件系统的容量确实添加
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VGtest/LVtest1 is mounted on /LVtest1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VGtest/LVtest1 to 1048576 (4k) blocks.
The filesystem on /dev/VGtest/LVtest1 is now 1048576 blocks long.
 
[root@ZhongH100 LVtest1]# cat l
lost+found/ lvtest
[root@ZhongH100 LVtest1]# cat lvtest   #文件没有受损
this is a test for LVM
[root@ZhongH100 LVtest1]# df -hP
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vgzhongH-root    30G  3.3G   25G  12% /
tmpfs                       932M     0  932M   0% /dev/shm
/dev/sda1                   477M   34M  418M   8% /boot
/dev/mapper/vgzhongH-data   4.8G   10M  4.6G   1% /data
/dev/mapper/VGtest-LVtest1  3.9G  4.0M  3.7G   1% /LVtest1  #挂载的分区空间已经增加
[root@ZhongH100 LVtest1]#

如果是xfs文件系统话上面这种扩容方法就不行了,需要用下面的方法,
参考:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Storage_Administration_Guide/xfsgrow.html
http://oss.sgi.com/archives/xfs/2001-05/msg03189.html

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
[root@www ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/xvda2 LBVG lvm2 a--   14.51g     0
  /dev/xvda3 LBVG lvm2 a--  135.00g 85.01g
[root@www ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  LBVG   2   2   0 wz--n- 149.51g 85.01g
[root@www ~]# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root LBVG -wi-ao---- 62.00g
  swap LBVG -wi-ao----  2.50g
[root@www ~]# lvcreate -L 10G -n data LBVG
  Logical volume "data" created.
[root@www ~]# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data LBVG -wi-a----- 10.00g
  root LBVG -wi-ao---- 62.00g
  swap LBVG -wi-ao----  2.50g
[root@www ~]# mkfs.xfs /dev/LBVG/data
meta-data=http://www.mamicode.com//dev/LBVG/data         isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@www ~]# lvextend -L +5G /dev/LBVG/data
  Size of logical volume LBVG/data changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
  Logical volume data successfully resized.
[root@www ~]# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data LBVG -wi-a----- 15.00g
  root LBVG -wi-ao---- 62.00g
  swap LBVG -wi-ao----  2.50g
[root@www ~]# e2fsck -f /dev/LBVG/data
e2fsck 1.42.13 (17-May-2015)
ext2fs_open2: Bad magic number in super-block
e2fsck: 超级块无效, trying backup blocks...
e2fsck: Bad magic number in super-block 当尝试打开 /dev/LBVG/data
 
The 超级块 could not be read or does not describe a valid ext2/ext3/ext4
文件系统.  If the 设备 is valid and it really contains an ext2/ext3/ext4
文件系统 (and not swap or ufs or something else), then the 超级块
is corrupt, and you might try running e2fsck with an alternate 超级块:
    e2fsck -b 8193 <设备>
 or
    e2fsck -b 32768 <设备>
 
[root@www ~]# mkdir /data
[root@www ~]# mount /dev/LBVG/data /data
[root@www ~]# df -hP|grep /data
文件系统               容量  已用  可用 已用% 挂载点
/dev/mapper/LBVG-data   10G   33M   10G    1% /data
[root@www ~]# xfs_growfs /dev/LBVG/data
meta-data=http://www.mamicode.com//dev/mapper/LBVG-data  isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1 spinodes=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2621440 to 3932160
[root@www ~]# df -hP|grep /data
文件系统               容量  已用  可用 已用% 挂载点
/dev/mapper/LBVG-data   15G   33M   15G    1% /data
[root@www ~]#
7. 缩减逻辑卷
 查看逻辑卷使用空间状况
 不能在线缩减,得先卸载 切记
 确保缩减后的空间大小依然能存储原有的所有数据
 在缩减之前应该先强行检查文件,以确保文件系统处于一至性状态
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@ZhongH100 ~]# umount /dev/VGtest/LVtest1       #卸载/dev/VGtest/LVtest1
[root@ZhongH100 ~]# e2fsck -f /dev/VGtest/LVtest1    #强制检查文件系统
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/VGtest/LVtest1: 12/262144 files (0.0% non-contiguous), 33871/1048576 blocks
[root@ZhongH100 ~]# resize2fs /dev/VGtest/LVtest1 1G  #缩减逻辑大小到1G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/VGtest/LVtest1 to 262144 (4k) blocks.
The filesystem on /dev/VGtest/LVtest1 is now 262144 blocks long.
 
[root@ZhongH100 ~]# lvreduce -L 1G /dev/VGtest/LVtest1
  WARNING: Reducing active logical volume to 1.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LVtest1? [y/n]: y         #输入y 同意裁剪
  Size of logical volume VGtest/LVtest1 changed from 4.00 GiB (1024 extents) to 1.00 GiB (256 extents).
  Logical volume LVtest1 successfully resized
[root@ZhongH100 ~]# lvs                                #查看逻辑卷
  LV      VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LVtest1 VGtest   -wi-a-----  1.00g
[root@ZhongH100 ~]# mount /dev/VGtest/LVtest1 /LVtest1/ #挂载逻辑卷/dev/VGtest/LVtest1
[root@ZhongH100 ~]# df -hP                              #查看系统分区详情
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vgzhongH-root    30G  3.3G   25G  12% /
tmpfs                       932M     0  932M   0% /dev/shm
/dev/sda1                   477M   34M  418M   8% /boot
/dev/mapper/vgzhongH-data   4.8G   10M  4.6G   1% /data
/dev/mapper/VGtest-LVtest1  944M  2.6M  891M   1% /LVtest1 #已经缩减成功
[root@ZhongH100 ~]# cat /LVtest1/lvtest                  #查看缩减前文件是否受损
this is a test for LVM
[root@ZhongH100 ~]#
8. 缩减磁盘空间
 发现物理磁盘空间使用不足,将其中一块硬盘或分区拿掉
 pvmove /dev/sdb1 #将/dev/sdb1上存储的数据移到其它物理卷中
 vgreduce VGtest /dev/sdb1 #将/dev/sdb1从VGtest卷组中移除
 pvremove /dev/sdb1 #将/dev/sdb1从物理卷上移除
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
[root@ZhongH100 ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sdb1  VGtest   lvm2 a--  10.00g  9.00g
  /dev/sdb2  VGtest   lvm2 a--  10.00g 10.00g
  /dev/sdb3  VGtest   lvm2 a--  10.00g 10.00g
  /dev/sdc   VGtest   lvm2 a--  20.00g 20.00g
[root@ZhongH100 ~]# pvmove /dev/sdb1
  /dev/sdb1: Moved: 2.3%
  /dev/sdb1: Moved: 86.3%
  /dev/sdb1: Moved: 100.0%
[root@ZhongH100 ~]# vgreduce VGtest /dev/sdb1
  Removed "/dev/sdb1" from volume group "VGtest"
[root@ZhongH100 ~]# pvremove /dev/sdb1
  Labels on physical volume "/dev/sdb1" successfully wiped
[root@ZhongH100 ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sdb2  VGtest   lvm2 a--  10.00g  9.00g
  /dev/sdb3  VGtest   lvm2 a--  10.00g 10.00g
  /dev/sdc   VGtest   lvm2 a--  20.00g 20.00g
[root@ZhongH100 ~]#
9. 实现快照,进行备份还原
 在/mnt/lvm目录上,我们将原始的目录文件进行快照,然后将/LVtets1目录中的内容清空,并进行还原
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@ZhongH100 ~]# cat /LVtest1/lvtest
this is a test for LVM
[root@ZhongH100 ~]# lvcreate -L 30M -n backup -s -p r /dev/VGtest/LVtest1
  Rounding up size to full physical extent 32.00 MiB
  Logical volume "backup" created
[root@ZhongH100 ~]# mkdir /tmp/backup/
[root@ZhongH100 ~]# mount /dev/VGtest/backup /tmp/backup/
mount: block device /dev/mapper/VGtest-backup is write-protected, mounting read-only
[root@ZhongH100 ~]# cat /tmp/backup/lvtest
this is a test for LVM
[root@ZhongH100 ~]# rm -rf /LVtest1/*
You are going to execute "/bin/rm -rf /LVtest1/lost+found /LVtest1/lvtest",please confirm (yes or no):yes
[root@ZhongH100 ~]# cd /LVtest1/
[root@ZhongH100 LVtest1]# ls -l
总用量 0
[root@ZhongH100 LVtest1]# tar xf /tmp/sandy.tar.bz2
[root@ZhongH100 LVtest1]# ls -l
总用量 8
drwx------ 2 root root 4096 5月  21 23:33 lost+found
-rw-r--r-- 1 root root   23 5月  21 23:53 lvtest
[root@ZhongH100 LVtest1]# cat lvtest
this is a test for LVM
[root@ZhongH100 LVtest1]# df -hP
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vgzhongH-root    30G  3.3G   25G  12% /
tmpfs                       932M     0  932M   0% /dev/shm
/dev/sda1                   477M   34M  418M   8% /boot
/dev/mapper/vgzhongH-data   4.8G   10M  4.6G   1% /data
/dev/mapper/VGtest-LVtest1  944M  2.5M  891M   1% /LVtest1
/dev/mapper/VGtest-backup   944M  2.6M  891M   1% /tmp/backup
[root@ZhongH100 LVtest1]#

(转载) Linux LVM分区之VG扩容、LV扩容、LV缩减、LVM快照