LVM创建管理快照

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVM创建管理快照相关的知识,希望对你有一定的参考价值。

创建分区

[root@cloud ~]# echo -e "n\n\n\n\n+10G\nt\n8e\nw\n" | fdisk /dev/sdd &> /dev/null 
[root@cloud ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk 
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0  200M  0 part /boot
├─sda2   8:2    0  4.9G  0 part [SWAP]
└─sda3   8:3    0   30G  0 part /
sdb      8:16   0   20G  0 disk 
└─sdb1   8:17   0   10G  0 part 
sdc      8:32   0   30G  0 disk 
└─sdc1   8:33   0   10G  0 part 
sdd      8:48   0   40G  0 disk 
└─sdd1   8:49   0   10G  0 part 
sr0     11:0    1    4G  0 rom  

创建物理卷

[root@cloud ~]# pvcreate /dev/sdb,c,d1
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdc1" successfully created.
  Physical volume "/dev/sdd1" successfully created.
[root@cloud ~]# pvs
  PV         VG Fmt  Attr PSize  PFree 
  /dev/sdb1     lvm2 ---  10.00g 10.00g
  /dev/sdc1     lvm2 ---  10.00g 10.00g
  /dev/sdd1     lvm2 ---  10.00g 10.00g

创建逻辑卷

[root@cloud ~]# vgcreate vg01 -s 16M  /dev/sdb,c,d1
  Volume group "vg01" successfully created
[root@cloud ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg01
  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               29.95 GiB
  PE Size               16.00 MiB
  Total PE              1917
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1917 / 29.95 GiB
  VG UUID               d4S78Z-00S8-cnoN-2VPb-usOw-mXIn-WdEELS
[root@cloud ~]# lvcreate -n lvm01 vg01 -l 100 
  Logical volume "lvm01" created.
[root@cloud ~]# lvcreate -L 5G -n lvm02 vg01
  Logical volume "lvm02" created.
[root@cloud ~]# lvs
  LV    VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvm01 vg01 -wi-a----- 1.56g                                                    
  lvm02 vg01 -wi-a----- 5.00g

创建文件系统

[root@cloud ~]# mkfs.ext4 /dev/vg/lvm01
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@cloud ~]# mkfs.xfs /dev/vg/lvm02
meta-data=/dev/vg/lvm02          isize=256    agcount=4, agsize=102400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=409600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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@cloud ~]# mount /dev/vg/lvm01 /opt/lvm01
[root@cloud ~]# mount /dev/vg/lvm02 /opt/lvm02
[root@cloud ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              30G  1.2G   29G   4% /
devtmpfs              1.9G     0  1.9G   0% /dev
tmpfs                 1.9G     0  1.9G   0% /dev/shm
tmpfs                 1.9G  8.5M  1.9G   1% /run
tmpfs                 1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1             197M  103M   94M  53% /boot
tmpfs                 379M     0  379M   0% /run/user/0
/dev/mapper/vg-lvm01  4.8G   20M  4.6G   1% /opt/lvm01
/dev/mapper/vg-lvm02  1.6G   33M  1.6G   3% /opt/lvm02

自动挂载

[root@cloud ~]# blkid | grep lvm01
/dev/mapper/vg-lvm01: UUID="e3af4ba6-a917-4905-ae03-7e2356de6107" TYPE="ext4" 
[root@cloud ~]# sed -i "11a UUID=e3af4ba6-a917-4905-ae03-7e2356de6107 /opt/lvm01              ext4     defaults        0 0" /etc/fstab 
[root@cloud ~]# mount -a
[root@cloud ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              30G  1.2G   29G   4% /
devtmpfs              1.9G     0  1.9G   0% /dev
tmpfs                 1.9G     0  1.9G   0% /dev/shm
tmpfs                 1.9G  8.5M  1.9G   1% /run
tmpfs                 1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1             197M  103M   94M  53% /boot
tmpfs                 379M     0  379M   0% /run/user/0
/dev/mapper/vg-lvm01  2.9G  2.9G     0 100% /opt/lvm01
/dev/mapper/vg-lvm02  3.0G  3.0G   20K 100% /opt/lvm02

在线扩容

[root@cloud ~]# lvs
  LV    VG Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvm01 vg -wi-ao---- 5.00g                                                    
  lvm02 vg -wi-ao---- 1.56g                                                    
[root@cloud ~]# lvextend -L +1G /dev/vg/lvm01 
  Size of logical volume vg/lvm01 changed from 5.00 GiB (320 extents) to 6.00 GiB (384 extents).
  Logical volume vg/lvm01 successfully resized.
[root@cloud ~]# lvs
  LV    VG Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvm01 vg -wi-ao---- 6.00g                                                    
  lvm02 vg -wi-ao---- 1.56g                                                    
[root@cloud ~]# df -h                       容量没有增加到
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              30G  1.2G   29G   4% /
devtmpfs              1.9G     0  1.9G   0% /dev
tmpfs                 1.9G     0  1.9G   0% /dev/shm
tmpfs                 1.9G  8.5M  1.9G   1% /run
tmpfs                 1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1             197M  103M   94M  53% /boot
tmpfs                 379M     0  379M   0% /run/user/0
/dev/mapper/vg-lvm01  4.8G   20M  4.6G   1% /opt/lvm01
/dev/mapper/vg-lvm02  1.6G   33M  1.6G   3% /opt/lvm02
[root@cloud ~]# resize2fs /dev/vg/lvm01                 #调整ext文件系统的大小
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg/lvm01 is mounted on /opt/lvm01; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg/lvm01 is now 1572864 blocks long.
[root@cloud ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              30G  1.2G   29G   4% /
devtmpfs              1.9G     0  1.9G   0% /dev
tmpfs                 1.9G     0  1.9G   0% /dev/shm
tmpfs                 1.9G  8.5M  1.9G   1% /run
tmpfs                 1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1             197M  103M   94M  53% /boot
tmpfs                 379M     0  379M   0% /run/user/0
/dev/mapper/vg-lvm01  5.8G   20M  5.5G   1% /opt/lvm01
/dev/mapper/vg-lvm02  1.6G   33M  1.6G   3% /opt/lvm02
[root@cloud ~]# lvextend -L +1G /dev/vg/lvm02
  Size of logical volume vg/lvm02 changed from 1.56 GiB (100 extents) to 2.56 GiB (164 extents).
  Logical volume vg/lvm02 successfully resized.
[root@cloud ~]# xfs_growfs /dev/vg/lvm02        调整xfs文件系统的大小
meta-data=/dev/mapper/vg-lvm02   isize=256    agcount=4, agsize=102400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=409600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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 409600 to 671744
[root@cloud ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              30G  1.2G   29G   4% /
devtmpfs              1.9G     0  1.9G   0% /dev
tmpfs                 1.9G     0  1.9G   0% /dev/shm
tmpfs                 1.9G  8.5M  1.9G   1% /run
tmpfs                 1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1             197M  103M   94M  53% /boot
tmpfs                 379M     0  379M   0% /run/user/0
/dev/mapper/vg-lvm01  5.8G   20M  5.5G   1% /opt/lvm01
/dev/mapper/vg-lvm02  2.6G   33M  2.6G   2% /opt/lvm02

或者
lvextend -L +1G -r /dev/vg/lvm01    #加了-r自动调整文件系统大小

压缩逻辑卷(危险) xfs不能压缩

[root@cloud ~]# umount /opt/lvm01
[root@cloud ~]# e2fsck -f /dev/vg/lvm01
e2fsck 1.42.9 (28-Dec-2013)
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/vg/lvm01: 11/393216 files (0.0% non-contiguous), 63598/1572864 blocks
[root@cloud ~]# resize2fs /dev/vg/lvm01 3G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg/lvm01 to 786432 (4k) blocks.
The filesystem on /dev/vg/lvm01 is now 786432 blocks long.

[root@cloud ~]# mount /dev/vg/lvm01 /opt/lvm01
[root@cloud ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              30G  1.2G   29G   4% /
devtmpfs              1.9G     0  1.9G   0% /dev
tmpfs                 1.9G     0  1.9G   0% /dev/shm
tmpfs                 1.9G  8.5M  1.9G   1% /run
tmpfs                 1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1             197M  103M   94M  53% /boot
tmpfs                 379M     0  379M   0% /run/user/0
/dev/mapper/vg-lvm02  3.0G   33M  3.0G   2% /opt/lvm02
/dev/mapper/vg-lvm01  2.9G   18M  2.7G   1% /opt/lvm01

速度比较

[root@cloud ~]# dd if=/dev/zero of=/root/f1 bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 2.76954 s, 388 MB/s
[root@cloud ~]# dd if=/dev/zero of=/opt/lvm02/f1 bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 0.611213 s, 1.8 GB/s

卷组扩展

[root@cloud lvm02]# vgextend vg /dev/sdc1
  Volume group "vg" successfully extended
[root@cloud lvm02]# vgextend vg /dev/sdd1
  Volume group "vg" successfully extended
[root@cloud lvm02]# vgdisplay 
  --- Volume group ---
  VG Name               vg
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  24
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               2
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               29.95 GiB
  PE Size               16.00 MiB
  Total PE              1917
  Alloc PE / Size       608 / 9.50 GiB
  Free  PE / Size       1309 / 20.45 GiB
  VG UUID               CSCN9I-Y7q9-KoN9-xiWK-htU6-XOd4-M2JjEe

卷组压缩

情况一:没有数据
    [root@cloud ~]# pvdisplay 
      --- Physical volume ---
      PV Name               /dev/sdb1
      VG Name               vg
      PV Size               10.00 GiB / not usable 16.00 MiB
      Allocatable           yes 
      PE Size               16.00 MiB
      Total PE              639
      Free PE               63
      Allocated PE          576
      PV UUID               5IwieR-HXxC-6txr-hUOo-zSUE-vela-Rg5ZSJ

      --- Physical volume ---
      PV Name               /dev/sdc1
      VG Name               vg
      PV Size               10.00 GiB / not usable 16.00 MiB
      Allocatable           yes 
      PE Size               16.00 MiB
      Total PE              639
      Free PE               639
      Allocated PE          0
      PV UUID               7nleyP-4EUB-7FME-2sDo-75Fr-YPfV-3wp9vM

      --- Physical volume ---
      PV Name               /dev/sdd1
      VG Name               vg
      PV Size               10.00 GiB / not usable 16.00 MiB
      Allocatable           yes 
      PE Size               16.00 MiB
      Total PE              639
      Free PE               639
      Allocated PE          0
      PV UUID               EcEfTe-Eaxq-X8GU-qiXe-3YuS-gxwI-ln6Clb
    [root@cloud ~]# vgreduce vg /dev/sdd1
      Removed "/dev/sdd1" from volume group "vg"
    [root@cloud ~]# pvdisplay /dev/sdd1
      "/dev/sdd1" is a new physical volume of "10.00 GiB"
      --- NEW Physical volume ---
      PV Name               /dev/sdd1
      VG Name               
      PV Size               10.00 GiB
      Allocatable           NO
      PE Size               0   
      Total PE              0
      Free PE               0
      Allocated PE          0
      PV UUID               EcEfTe-Eaxq-X8GU-qiXe-3YuS-gxwI-ln6Clb

    [root@cloud ~]# pvremove /dev/sdd1
      Labels on physical volume "/dev/sdd1" successfully wiped.
    [root@cloud ~]# pvdisplay 
      --- Physical volume ---
      PV Name               /dev/sdb1
      VG Name               vg
      PV Size               10.00 GiB / not usable 16.00 MiB
      Allocatable           yes 
      PE Size               16.00 MiB
      Total PE              639
      Free PE               63
      Allocated PE          576
      PV UUID               5IwieR-HXxC-6txr-hUOo-zSUE-vela-Rg5ZSJ

      --- Physical volume ---
      PV Name               /dev/sdc1
      VG Name               vg
      PV Size               10.00 GiB / not usable 16.00 MiB
      Allocatable           yes 
      PE Size               16.00 MiB
      Total PE              639
      Free PE               639
      Allocated PE          0
      PV UUID               7nleyP-4EUB-7FME-2sDo-75Fr-YPfV-3wp9vM

    [root@cloud ~]# vgdisplay 
      --- Volume group ---
      VG Name               vg
      System ID             
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  8
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                2
      Open LV               2
      Max PV                0
      Cur PV                2
      Act PV                2
      VG Size               <19.97 GiB
      PE Size               16.00 MiB
      Total PE              1278
      Alloc PE / Size       576 / 9.00 GiB
      Free  PE / Size       702 / <10.97 GiB
      VG UUID               CSCN9I-Y7q9-KoN9-xiWK-htU6-XOd4-M2JjEe
情况二:有数据,但是硬盘快坏了
    [root@cloud lvm02]# pvdisplay 
      --- Physical volume ---
      PV Name               /dev/sdb1
      VG Name               vg
      PV Size               10.00 GiB / not usable 16.00 MiB
      Allocatable           yes 
      PE Size               16.00 MiB
      Total PE              639
      Free PE               607
      Allocated PE          32
      PV UUID               5IwieR-HXxC-6txr-hUOo-zSUE-vela-Rg5ZSJ

      --- Physical volume ---
      PV Name               /dev/sdc1
      VG Name               vg
      PV Size               10.00 GiB / not usable 16.00 MiB
      Allocatable           yes 
      PE Size               16.00 MiB
      Total PE              639
      Free PE               63
      Allocated PE          576
      PV UUID               7nleyP-4EUB-7FME-2sDo-75Fr-YPfV-3wp9vM

    [root@cloud lvm02]# pvmove /dev/sdc1
      /dev/sdc1: Moved: 0.00%
      /dev/sdc1: Moved: 66.67%
      /dev/sdc1: Moved: 100.00%
    [root@cloud lvm02]# pvdisplay 
      --- Physical volume ---
      PV Name               /dev/sdb1
      VG Name               vg
      PV Size               10.00 GiB / not usable 16.00 MiB
      Allocatable           yes 
      PE Size               16.00 MiB
      Total PE              639
      Free PE               31
      Allocated PE          608
      PV UUID               5IwieR-HXxC-6txr-hUOo-zSUE-vela-Rg5ZSJ

      --- Physical volume ---
      PV Name               /dev/sdc1
      VG Name               vg
      PV Size               10.00 GiB / not usable 16.00 MiB
      Allocatable           yes 
      PE Size               16.00 MiB
      Total PE              639
      Free PE               639
      Allocated PE          0
      PV UUID               7nleyP-4EUB-7FME-2sDo-75Fr-YPfV-3wp9vM

    [root@cloud lvm02]# vgreduce vg /dev/sdc1
      Removed "/dev/sdc1" from volume group "vg"
    [root@cloud lvm02]# pvremove /dev/sdc1
      Labels on physical volume "/dev/sdc1" successfully wiped.
    [root@cloud lvm02]# vgdisplay 
      --- Volume group ---
      VG Name               vg
      System ID             
      Format                lvm2
      Metadata Areas        1
      Metadata Sequence No  22
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                3
      Open LV               2
      Max PV                0
      Cur PV                1
      Act PV                1
      VG Size               9.98 GiB
      PE Size               16.00 MiB
      Total PE              639
      Alloc PE / Size       608 / 9.50 GiB
      Free  PE / Size       31 / 496.00 MiB
      VG UUID               CSCN9I-Y7q9-KoN9-xiWK-htU6-XOd4-M2JjEe

lvm创建快照

[root@cloud lvm02]# ll /opt/lvm02
total 3102540
-rw-r--r-- 1 root root 1073741824 Aug 21 00:18 f1
-rw-r--r-- 1 root root 1073741824 Aug 21 00:19 f2
-rw-r--r-- 1 root root 1029517312 Aug 21 00:19 f3
-rw-r--r-- 1 root root          0 Aug 21 00:19 f4
[root@cloud lvm02]# lvcreate -n lvm_snap02 -L 1.5G -s /
[root@cloud lvm02]# lvcreate -n lvm_snap02 -L 1.5G -s /dev/vg/lvm02 
  Logical volume "lvm_snap02" created.
[root@cloud lvm02]# lvs
  LV         VG Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvm01      vg -wi-ao----   6.00g                                                    
  lvm02      vg owi-aos---   3.00g                                                    
  lvm03      vg -wi-a----- 512.00m                                                    
  lvm_snap02 vg swi-a-s---   1.50g      lvm02  0.00
[root@cloud lvm02]# \rm f1
[root@cloud lvm02]# > f2
[root@cloud lvm02]# ll
total 1005388
-rw-r--r-- 1 root root          0 Aug 21 00:48 f2
-rw-r--r-- 1 root root 1029517312 Aug 21 00:19 f3
-rw-r--r-- 1 root root          0 Aug 21 00:19 f4
[root@cloud ~]# lvconvert --merge /dev/vg/lvm_snap02 
  Merging of volume vg/lvm_snap02 started.
  vg/lvm02: Merged: 99.87%
  vg/lvm02: Merged: 100.00%
[root@cloud ~]# lvs
  LV    VG Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvm01 vg -wi-ao----   6.00g                                                    
  lvm02 vg -wi-ao----   3.00g                                                    
  lvm03 vg -wi-a----- 512.00m
[root@cloud ~]# mount /dev/vg/lvm02 /opt/lvm02
[root@cloud ~]# ll /opt/lvm02
total 3102540
-rw-r--r-- 1 root root 1073741824 Aug 21 00:18 f1
-rw-r--r-- 1 root root 1073741824 Aug 21 00:19 f2
-rw-r--r-- 1 root root 1029517312 Aug 21 00:19 f3
-rw-r--r-- 1 root root          0 Aug 21 00:19 f4

以上是关于LVM创建管理快照的主要内容,如果未能解决你的问题,请参考以下文章

LVM创建管理快照

LVM逻辑卷管理--在线扩容逻辑卷与卷组容量缩减逻辑卷快照

LVM逻辑卷-创建扩容缩减迁移快照

LVM逻辑卷创建管理

lvm之创建/扩容/缩容/快照及关闭的全部流程操作记录

Linux LVM详解及创建