首页 > 代码库 > Linux命令:pv,vg,lg,快照

Linux命令:pv,vg,lg,快照


PV: physical volume

  pvcreate, pvdisplay, pvs, pvmove, pvremove

VG: volume group

  vgcreate, vgdisplay, vgs, vgreduce, vgextend, vgremove

lV: logical volume

  lvcreate, lvdisplay, lvs, 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
  No data to move for myvg
[root@localhost ~]# vgreduce myvg /dev/sdb2
  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
  Labels on physical volume "/dev/sdb2" successfully wiped   ***删除PV

[root@localhost ~]# pvs
  PV         VG         Fmt  Attr PSize   PFree  
  /dev/sdb1  myvg       lvm2 a-   488.00M 488.00M

[root@localhost ~]# pvcreate /dev/sdb2   ***创建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 --   980.53M 980.53M

[root@localhost ~]# vgextend myvg /dev/sdb2    ***将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 a-   976.00M 976.00M

[root@localhost ~]# vgs
  VG         #PV #LV #SN Attr   VSize VFree
  myvg         2   0   0 wz--n- 1.43G 1.43G


[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 -j /dev/myvg/testlv

[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名称中含有‘ - ’,故LV的名称不可使用-


[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


  A、新建200M的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  6.1M  274M   3% /users

[root@localhost ~]#


  B、将空间为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   295M  5.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


  C、创建快照卷,用于备份

     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


---end---

Linux命令:pv,vg,lg,快照