PV: physical volume
  pvcreate, pvdisplay详单, pvs属性, pvmove, pvremove删除
VG: volume group
vgreduce挪出, vgextend延伸, vgremove

LV: logical volume
lvreduce, lvextend, lvresize, lvremove

一、PV新增、删除、查看、扫描  
[root@localhost ~]# pvscan
  PV /dev/sda10   VG VolGroup00   lvm2 [1.41 GB / 1.41 GB free]
  Total: 1 [1.41 GB] / in use: 1 [1.41 GB] / in no VG: 0 [0   ]
[root@localhost ~]# vgs
  VG    #PV #LV #SN Attr   VSize VFree
  VolGroup00   1   0   0 wz--n- 1.41G 1.41G
[root@localhost ~]# vgcreate myvg /dev/sdb{1,2}   ***未建pv时,无法直接建vg,会出错
  No physical volume label read from /dev/sdb1
  /dev/sdb1 not identified as an existing physical volume
  Unable to add physical volume '/dev/sdb1' to volume group 'myvg'.
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize VFree
  VolGroup00   1   0   0 wz--n- 1.41G 1.41G
[root@localhost ~]# pvcreate /dev/sdb{1,2}
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
[root@localhost ~]# pvs
  PV    VG         Fmt  Attr PSize   PFree  
  /dev/sda10 VolGroup00 lvm2 a-     1.41G   1.41G
  /dev/sdb1        lvm2 --   494.16M 494.16M
  /dev/sdb2        lvm2 --   980.53M 980.53M
[root@localhost ~]# pvdisplay 
  "/dev/sdb1" is a new physical volume of "494.16 MB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               494.16 MB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE             0
  Free PE             0
  Allocated PE        0
  PV UUID           0eFqhA-maz7-mgxp-YG7A-UU8N-yE7b-zUMKwa
   
  "/dev/sdb2" is a new physical volume of "980.53 MB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               980.53 MB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID             WwfLj0-FxRD-JzrI-F9K5-T352-f3ho-4r2lVx[root@localhost ~]# pvscan         ***新硬盘中扫描后方可识别
  PV /dev/sda10   VG VolGroup00      lvm2 [1.41 GB / 1.41 GB free]
  PV /dev/sdb1                       lvm2 [494.16 MB]
  PV /dev/sdb2                       lvm2 [980.53 MB]
  Total: 3 [2.85 GB] / in use: 1 [1.41 GB] / in no VG: 2 [1.44 GB]
二、VG新增、删除、查看、扫描
[root@localhost ~]# vgcreate myvg /dev/sdb{1,2}
  Volume group "myvg" successfully created[root@localhost ~]# vgdisplay myvg
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               1.44 GB
  PE Size               4.00 MB
  Total PE              368
  Alloc PE / Size       0 / 0   
  Free  PE / Size       368 / 1.44 GB
  VG UUID               iqm9Zu-qHSZ-Evnw-PNxq-FcbI-AShe-nhN5Oj   
[root@localhost ~]# pvdisplay /dev/sdb1
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               myvg
  PV Size               494.16 MB / not usable 2.16 MB
  Allocatable           yes 
  PE Size (KByte)       4096
  Total PE              123
  Free PE               123
  Allocated PE        0
  PV UUID            0eFqhA-maz7-mgxp-YG7A-UU8N-yE7b-zUMKwa[root@localhost ~]# vgremove myvg     ***删除vg
  Volume group "myvg" successfully removed
[root@localhost ~]# vgs
  VG    #PV #LV #SN Attr   VSize VFree
  VolGroup00   1   0   0 wz--n- 1.41G 1.41G  ***VG 1.41G***[root@localhost ~]# vgcreate -s 8M myvg /dev/sdb{1,2}
  Volume group "myvg" successfully created
[root@localhost ~]# vgdisplay myvg
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               1.43 GB
  PE Size               8.00 MB
  Total PE              183
  Alloc PE / Size       0 / 0   
  Free  PE / Size       183 / 1.43 GB
  VG UUID               TC2ZaN-P1IW-KFGe-CPU3-wZcx-eSJi-qbSi6C   
[root@localhost ~]# pvs
  PV     VG         Fmt  Attr PSize   PFree      /dev/sdb1  myvg       lvm2 a-   488.00M 488.00M
  /dev/sdb2  myvg       lvm2 a-   976.00M 976.00M   ***/dev/sdb2属于组myvg[root@localhost ~]# pvmove /dev/sdb2     **1*将PV上数据移走
  No data to move for myvg
[root@localhost ~]# vgreduce myvg /dev/sdb2   **2*在VG上挪出此PV   Removed "/dev/sdb2" from volume group "myvg"
[root@localhost ~]# vgs
  VG         #PV #LV #SN Attr   VSize   VFree  
  myvg     1   0   0 wz--n- 488.00M 488.00M***VG 1.41G减少为488.00M***[root@localhost ~]# pvs
  PV     VG      Fmt  Attr PSize   PFree  
  /dev/sdb1  myvg     lvm2 a-   488.00M 488.00M
  /dev/sdb2        lvm2 --   980.53M 980.53M    ***/dev/sdb2不属于任何组[root@localhost ~]# pvremove /dev/sdb2       **3* 删除PV
***删除PV
[root@localhost ~]# pvs
  PV      VG         Fmt  Attr PSize   PFree  
  /dev/sdb1  myvg       lvm2 a-   488.00M 488.00M
[root@localhost ~]# pvcreate /dev/sdb21*创建PV 
  Physical volume "/dev/sdb2" successfully created
[root@localhost ~]# pvs
  PV      VG     Fmt  Attr PSize   PFree  
  /dev/sdb1  myvg    lvm2 a-   488.00M 488.00M
  /dev/sdb2             lvm2[root@localhost ~]# vgextend myvg /dev/sdb2 2*将PV加入,延伸VG空间 
  Volume group "myvg" successfully extended
[root@localhost ~]# pvs
  PV         VG         Fmt  Attr PSize   PFree  
  /dev/sdb1  myvg       lvm2 a-   488.00M 488.00M
  /dev/sdb2  myvg       lvm2[root@localhost ~]# vgs
  VG         #PV #LV #SN Attr   VSize VFree
  myvg     2
[root@localhost ~]# pvs
  PV    VG      Fmt  Attr PSize   PFree  
  /dev/sda10 VolGroup00 lvm2 a-     1.41G   1.41G
  /dev/sdb1  myvg       lvm2 a-   488.00M 488.00M
  /dev/sdb2  myvg       lvm2 a-   976.00M 976.00M
三、LV新增、删除、查看、扫描
[root@localhost ~]# lvcreate -L 50M -n testlv myvg
  Rounding up size to full physical extent 56.00 MB
  Logical volume "testlv" created
[root@localhost ~]# lvs
  LV     VG   Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  testlv myvg -wi-a- 56.00M                                      
[root@localhost ~]# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/myvg/testlv
  VG Name                myvg
  LV UUID                zXwoKM-OYnU-EWAC-EBoD-dFTh-l4sE-ip1m3q
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                56.00 MB
  Current LE             7
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0[root@localhost ~]# mke2fs
[root@localhost ~]# mount /dev/myvg/testlv /mnt
[root@localhost ~]# ls /mnt/
lost+found
[root@localhost ~]# mount
/dev/mapper/myvg-testlv on /mnt type ext3 (rw) ***myvg-testlv名称中含有‘ - ’,故尽量不用‘ - ’
[root@localhost ~]# lvremove /dev/mapper/myvg-testlv 
  Can't remove open logical volume "testlv"   ***挂载中的LV不能正常删除
[root@localhost ~]# umount /mnt
[root@localhost ~]# lvremove /dev/mapper/myvg-testlv 
Do you really want to remove active logical volume testlv? [y/n]: y
  Logical volume "testlv" successfully removed
[root@localhost ~]# lvs
的LV,并将空间扩展到300M,可在线扩展
[root@localhost ~]# lvcreate -L 200M -n testlv myvg  ***新建200M的LV
  Logical volume "testlv" created
[root@localhost ~]# mke2fs -j /dev/myvg/testlv 
[root@localhost ~]# vim /etc/fstab /dev/myvg/testlv    /users            ext3    defaults,acl    0 0
[root@localhost ~]# mount -a
[root@localhost ~]# lvextend -L 300M /dev/myvg/testlv   ***将LV扩展到300M
  Rounding up size to full physical extent 304.00 MB
  Extending logical volume testlv to 304.00 MB
  Logical volume testlv successfully resized
[root@localhost ~]# lvs
  LV     VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  testlv myvg -wi-ao 304.00M                                      
/dev/mapper/myvg-testlv     198337      5666    182431   4% /users
[root@localhost ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             996M   40M  905M   5% /boot
tmpfs                 3.9G     0  3.9G   0% /dev/shm
/dev/mapper/myvg-testlv
   194M  5.6M  179M   4% /users  ***LV扩展到300M后空间未变[root@localhost ~]# lvs
  LV     VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  testlv myvg -wi-ao 304.00M                                      
[root@localhost ~]# resize2fs -p /dev/myvg/testlv   ***文件系统重定义大小resize2fs 1.39 (29-May-2006)
Filesystem at /dev/myvg/testlv is mounted on /users; on-line resizing required
Performing an on-line resize of /dev/myvg/testlv to 311296 (1k) blocks.
The filesystem on /dev/myvg/testlv is now 311296 blocks long.[root@localhost ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             996M   40M  905M   5% /boot
tmpfs                 3.9G     0  3.9G   0% /dev/shm
/dev/mapper/myvg-testlv
  295M[root@localhost ~]#

将空间为300M的LV,缩减到200M,必须卸载后方可缩减
      1、不能在线缩减,得先卸载;
      2、确保缩减后的空间大小依然能存储原有的所有数据;
      3、在缩减之前应该先强行检查文件,以确保文件系统处于一至性状态;

[root@localhost ~]# mount   1***查看文件系统是否正在使用LV
[root@localhost /]# fuser -cu /users    ***查看是否正在使用LV对应的挂载文件进程
/mnt:         15060c(root)
[root@localhost /]# fuser -ck /users    ***清除正在使用LV对应挂载文件进程
/mnt:         15060c
[root@localhost ~]# umount /users/    2***卸载文件
[root@localhost ~]# e2fsck -f /dev/myvg/testlv     3***检查文件,确保文件系统一致
e2fsck 1.39 (29-May-2006)
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
/dev/myvg/testlv: 12/77824 files (8.3% non-contiguous), 15999/311296 blocks
[root@localhost ~]# df -lh  4***确保缩减后的空间大小依然能存储原有的所有数据
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/myvg-testlv   295M5.6M  181M   3% /users
[root@localhost ~]# resize2fs /dev/myvg/testlv 200M   5***缩减到200M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/myvg/testlv to 204800 (1k) blocks.
The filesystem on /dev/myvg/testlv is now 204800 blocks long.
[root@localhost ~]# lvreduce -L 200M /dev/myvg/testlv 
  WARNING: Reducing active logical volume to 200.00 MB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testlv? [y/n]: y
  Reducing logical volume testlv to 200.00 MB
  Logical volume testlv successfully resized
[root@localhost ~]# mount -a    6***重新挂载
[root@localhost ~]# df -lh
Filesystem        Size  Used Avail Use% Mounted on
/dev/sda1          996M   40M  905M   5% /boot
tmpfs               3.9G     0  3.9G   0% /dev/shm
/dev/mapper/myvg-testlv 194M  5.6M  181M   3% /users
创建快照卷,用于备份(复制报头,写时复制)
复制 Origin 原始卷的元数据(metadata)来创建一个逻辑卷,并没有复制物理卷上的任何数据, 因此它的创建过程是实时瞬间完成的。快照是特殊类型的逻辑卷,它含有创建时刻所指定的原始逻辑卷的完整数据,您可以操作快照而无需担心数据的变更令备份失效。 
写时复制(COW --- Copy-On-Write)”的技术来跟踪和维持其数据的一致性。它的原理比较简单,就是跟踪原始卷上块的改变, 在这些数据被改变之前将其复制到快照自己的预留空间里(顾名思义称为写时复制)。 当对快照进行读取的时候,被修改的数据从快照的预留空间中读取,未修改的数据则重定向到原始卷上去读取,因此在快照的文件系统与设备之间多了一层COW设备。


     1、生命周期为整个数据时长;在这段时长内,数据的增长量不能超出快照卷大小;
     2、快照卷应该是只读的;
     3、跟原卷在同一卷组内;


lvcreate
    -s        创建快照
    -p r|w    LV权限,一般为r
    -n       创建快照的逻辑卷名
lvcreate -L # -n SLV_NAME -p r  /PATH/TO/LV

[root@localhost ~]# lvs
  LV     VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  testlv myvg -wi-ao 200.00M                                      
[root@localhost ~]# lvcreate -L 20M -n TestSnap -s -p r /dev/myvg/testlv ***创建快照  Rounding up size to full physical extent 24.00 MB
  Logical volume "TestSnap" created
[root@localhost ~]# lvs
  LV       VG   Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  TestSnap myvg sri-a-  24.00M testlv   0.05                        
  testlv   myvg owi-ao 200.00M      [root@localhost ~]# mount /dev/myvg/TestSnap /mnt/
mount: block device /dev/myvg/TestSnap is write-protected, mounting read-only
[root@localhost ~]# ls /mnt/
inittab  lost+found
[root@localhost ~]# cat /mnt/inittab | wc -l
53
[root@localhost ~]# mount
/dev/sda1 on /boot type ext3 (rw)
/dev/mapper/myvg-testlv on /users type ext3 (rw,acl)
/dev/mapper/myvg-TestSnap on /mnt type ext3 (ro)
[root@localhost ~]# cat /mnt/inittab | wc -l
53[root@localhost ~]# vi /users/inittab      ***创建快照后删除文件中8行文字,修改文件
[root@localhost ~]# cat /users/inittab | wc -l
41[root@localhost ~]# cat /mnt/inittab | wc -l    ***文件行数不变
53
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# tar -jcf /tmp/users.tar.bz2 inittab  ***将snap中文件压缩归档 [root@localhost mnt]# cd
[root@localhost ~]# umount /mnt/     ***将snap卸载[root@localhost ~]# lvremove /dev/myvg/TestSnap     ***将snap对应的LV删除
Do you really want to remove active logical volume TestSnap? [y/n]: y
  Logical volume "TestSnap" successfully removed[root@localhost users]# mv inittab inittab.bad    ***将修改过的文件改名
[root@localhost users]# ll
total 14
-rw-r--r-- 1 root root  1356 Apr 25 11:04 inittab.bad
drwx------ 2 root root 12288 Apr 25 09:49 lost+found
[root@localhost users]# tar -xf /tmp/users.tar.bz2 -C ./  ***将归档的文件解压到原路径[root@localhost users]# ll
total 16
-rw-r--r-- 1 root root  1666 Apr 25 09:52 inittab
-rw-r--r-- 1 root root  1356 Apr 25 11:04 inittab.bad
drwx------ 2 root root 12288 Apr 25 09:49 lost+found
[root@localhost users]# cat /users/inittab |wc -l   ***归档的文件行数53
[root@localhost users]# cat /users/inittab.bad |wc -l   ***修改的文件数
41

  D、实验

1.创建VG

[root@fox ~]# vgcreate myvg /dev/sda1
  Volume group "myvg" successfully created
[root@fox ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  myvg     1   0   0 wz--n- 20.01g 20.01g

2.创建LV

[root@fox ~]# lvcreate -L 20G -n mydata myvg
  Logical volume "mydata" created
[root@fox ~]# lvs
LV    VG   Attr   LSize  Origin Snap%  Move Log Copy%  Convert
mydata  myvg   -wi-a- 20.00g  

3.挂载LV

[root@fox ~]# mount /dev/myvg/mydata /mydata/
mount: you must specify the filesystem type
[root@fox ~]#
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
...
This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@fox ~]# cd /mydata/
[root@fox mydata]# touch 1.txt
[root@fox mydata]# vim 
1.txt       lost+found/ 
[root@fox mydata]# vim 1.txt 
[root@fox ~]# cat /mydata/1.txt 
dkjfakljfadfjaldfjajdfjald

4.扩大容量  (增加20G:lvextend  -L +20G )

[root@k ~]# vgextend myvg /dev/sda2
[root@k ~]# lvextend  -L +20G /dev/myvg/mydata   
[root@k ~]# resize2fs /dev/myvg/mydata 


[root@fox mydata]# cd 
[root@fox ~]# vgextend myvg /dev/sda2 
Volume group "myvg" successfully extended
[root@fox ~]# vgs
VG   #PV #LV #SN Attr   VSize  VFree 
myvg  2   1   0 wz--n- 40.02g 20.02g
[root@fox ~]# lvs
LV   VG   Attr   LSize  Origin Snap%  Move Log Copy%  Convert
mydata  myvg   -wi-ao 20.00g

[root@fox ~]#
  Extending logical volume mydata to 40.00 GiB
  Logical volume mydata successfully resized
[root@fox ~]# lvs
LV   VG   Attr   LSize  Origin Snap%  Move Log Copy%  Convert
mydata myvg   -wi-ao 40.00g 
[root@fox ~]#
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mydata is mounted on /mydata; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/myvg/mydata to 10485760 (4k) blocks.
The filesystem on /dev/myvg/mydata is now 10485760 blocks long.
[root@fox ~]# ls /mydata/
1.txt  lost+found
[root@fox ~]# cat /mydata/1.txt 
dkjfakljfadfjaldfjajdfjald
[root@fox ~]# lvs
LV   VG  Attr   LSize  Origin Snap%  Move Log Copy%  Convert
mydata  myvg   -wi-ao 40.00g 
[root@fox ~]# cd /mydata/
[root@fox mydata]# df -lah
Filesystem     Size  Used Avail Use% Mounted on
...
/dev/mapper/myvg-mydata  40G  176M   38G   1% /mydata

5.缩小容量

[root@k ~]# umount /mnt/
[root@k ~]# resize2fs -f /dev/vg0/data0 2G   #调整分区大小
[root@k ~]# lvreduce -L 2G /dev/vg0/data0   #减小到指定的2G大小
[root@k ~]# mount


[root@fox mydata]#
umount: /mydata: device is busy.
   (In some cases useful info about processes that use
    the device is found by lsof(8) or fuser(1))
[root@fox mydata]# cd
[root@fox ~]# umount /mydata/
[root@fox ~]# resize2fs /dev/myvg/mydata 20G   #无-f, 出错
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/myvg/mydata' first.
[root@fox ~]# resize2fs -f /dev/myvg/mydata 20G  #调整分区大小
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mydata to 5242880 (4k) blocks.
The filesystem on /dev/myvg/mydata is now 5242880 blocks long.
[root@fox ~]# lvs
  LV   VG   Attr LSize  Origin Snap%  Move Log Copy%  Convert
  mydata  myvg   -wi-a- 40.00g 
  lv_home vg_fox -wi-ao  5.57g 
  lv_root vg_fox -wi-ao 50.00g
  lv_swap vg_fox -wi-ao  3.94g
[root@fox ~]# lvreduce -L 20G /dev/myvg/mydata    #挪出LV
  WARNING: Reducing active logical volume to 20.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mydata? [y/n]: y
  Reducing logical volume mydata to 20.00 GiB
  Logical volume mydata successfully resized
[root@fox ~]# lvs
  LV      VG     Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  mydata  myvg   -wi-a- 20.00g    
[root@fox ~]# mount /dev/myvg/mydata /mydata/
[root@fox ~]# ls /mydata/
1.txt  lost+found
[root@fox ~]# 
[root@fox ~]# lvs
  LV   VG  Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  mydata  myvg   -wi-ao 20.00g 
[root@fox ~]# pvs
  PV    VG   Fmt  Attr PSize  PFree 
  /dev/sda1  myvg   lvm2 a-   20.01g  8.00m
  /dev/sda2  myvg   lvm2 a-   20.01g 20.01g
[root@fox ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree 
  myvg     2   1   0 wz--n- 40.02g 20.02g
  vg_fox   1   3   0 wz--n- 59.51g     0 

5.再次扩大容量  (增加到40G:lvextend -L 40G)

[root@fox ~]# lvextend -L 40G /dev/myvg/mydata 
  Extending logical volume mydata to 40.00 GiB
  Logical volume mydata successfully resized
[root@fox ~]# resize2fs /dev/myvg/mydata 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mydata is mounted on /mydata; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/myvg/mydata to 10485760 (4k) blocks.
The filesystem on /dev/myvg/mydata is now 10485760 blocks long.
[root@fox ~]# lvs
  LV      VG     Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  mydata  myvg   -wi-ao 40.00g                                      
  lv_home vg_fox -wi-ao  5.57g                                      
  lv_root vg_fox -wi-ao 50.00g                                      
  lv_swap vg_fox -wi-ao  3.94g                                      
[root@fox ~]# mount
...
/dev/mapper/myvg-mydata on /mydata type ext4 (rw)
[root@fox ~]# ls /mydata/
1.txt  lost+found
[root@fox ~]# cat /mydata/1.txt 
dkjfakljfadfjaldfjajdfjald

---end---

https://blog.51cto.com/wangfx/1869542