linux学习33 运维高级技能-在Linux实现LVM逻辑卷及快照

一、LVM2

  1、LVM:Logical Volume Manager,Version:2

    LVM有点类似于我们的软raid,不过他们的驱动完全相反,我们的软raid用的是内核中的md模块,而LVM用的是dm模块,也就是device mapper(设备映射组件)。

  2、dm:device mapper,将一个或多个底层块设备组织成一个逻辑设备的模块

  3、我们首先将我们的物理硬盘设备可用创建为PV,也就是可用记录元数据的设备,然后再将我们的多个pv组成VG,在组成VG之前我们的PV是不会划分成PE的,当我们的PV组成VG以后我们的PV就会被划分成多个块(PE[physics Extent])组成VG。因为我们不同的VG有可能PE大小是不一样的,比如有些VG的单个PE是8M,有些VG的单个PE是16M,因此我们的PE是PV加入至VG之后才开始分配的。在我们VG之上创建LV存储卷的过程就是指定特定数量的PE来组成一个逻辑存储空间的过程。比如我给其三个PE,一个PE为4M那么一共就是12M,将来这个空间不够用时再加一个PE进去就成16M了,然后我们发现分配过多我们还可以减掉一个,然后又回到了12M了,但是这个过程是不会损坏LV中本来存放的数据的,所以逻辑卷的大小可以动态扩展或收缩而不损坏原来的数据的

    

 

  4、我们的PE一旦分配给这个逻辑卷使用后他就不再叫做PE而叫做LE [Logic Extent]

    

 

 

  5、对于VG来讲,他的每一个设备文件组织完以后这个设备文件都在 /dev/mapper/VG_NAME-LV_NAME下,比如/dev/mapper/vol0-root表示vol0这个卷组上的一个叫做root的lv逻辑卷。因为这样不怎么好记因此他有一个简洁的符号链接指向他,也就是/dev/VG_NAME/LV_NAME,即

    /dev/mapper/VG_NAME-LV_NAME  <--- /dev/VG_NAME/LV_NAME

[root@localhost ~]# ls -ld /dev/mapper/*
lrwxrwxrwx 1 root root       7 Jan  7 05:09 /dev/mapper/centos-root -> ../dm-0
lrwxrwxrwx 1 root root       7 Jan  7 05:09 /dev/mapper/centos-swap -> ../dm-1
crw------- 1 root root 10, 236 Jan  7 05:09 /dev/mapper/control
[root@localhost ~]# ls -ld /dev/centos/*
lrwxrwxrwx 1 root root 7 Jan  7 05:09 /dev/centos/root -> ../dm-0
lrwxrwxrwx 1 root root 7 Jan  7 05:09 /dev/centos/swap -> ../dm-1

二、LVM讲解

  1、LVM:Logical Volume Manager,Version:2

  2、dm:device mapper,将一个或多个底层块设备组织成一个逻辑设备的模块

      /dev/dm-#

      /dev/mapper/VG_NAME-LV_NAME

        /dev/mapper/vol0-root

      /dev/VG_NAME/LV_NAME

        /dev/vol0/root

三、创建和使用逻辑卷

  1、PV管理工具

    a、pvs:简要显示pv信息

    b、pvdisplay:显示pv的详细信息

[root@localhost ~]# pvs #简要查看PV
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <39.00g 4.00m
[root@localhost ~]# pvdisplay  #查看PV详细信息
  --- Physical volume ---
  PV Name               /dev/sda2  #PV名称
  VG Name               centos  #属于哪个卷组
  PV Size               <39.00 GiB / not usable 3.00 MiB #PV大小为39G
  Allocatable           yes   #可以分配
  PE Size               4.00 MiB #PE大小为4M
  Total PE              9983 #一共有9983个PE
  Free PE               1  #还剩一个PE可用
  Allocated PE          9982 #已经分配的PE
  PV UUID               Lr6vmk-mbi2-Idqu-0Ie2-55VM-V8w9-R1WNRF #PV的UUID

    显示指定的PV

[root@localhost ~]# pvdisplay /dev/sda2 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <39.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              9983
  Free PE               1
  Allocated PE          9982
  PV UUID               Lr6vmk-mbi2-Idqu-0Ie2-55VM-V8w9-R1WNRF

  2、创建物理卷 PV,他可以创建在任何块设备上,硬盘,分区,raid等块设备都可以

    a、pvcreate /dev/DEVICE :创建pv

[root@localhost ~]# pvcreate /dev/sdb
WARNING: dos signature detected on /dev/sdb at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sdb.
  Physical volume "/dev/sdb" successfully created.
[root@localhost ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <39.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              9983
  Free PE               1
  Allocated PE          9982
  PV UUID               Lr6vmk-mbi2-Idqu-0Ie2-55VM-V8w9-R1WNRF
   
  "/dev/sdb" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0   #创建pv卷后还不会划分PE,只有将pv卷加入至vg卷组才会分配,默认为4M 
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               9TN3GU-AXJL-rN25-eXe9-oAFb-Mngp-tmo4eU

    b、删除pv

      pvremove /dev/DEVICE

[root@localhost ~]# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped.
[root@localhost ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <39.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              9983
  Free PE               1
  Allocated PE          9982
  PV UUID               Lr6vmk-mbi2-Idqu-0Ie2-55VM-V8w9-R1WNRF

  3、VG卷组操作

    a、简要显示所有vg卷组

    b、显示vg卷组详细信息

[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <39.00g 4.00m
[root@localhost ~]# vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <39.00 GiB
  PE Size               4.00 MiB
  Total PE              9983
  Alloc PE / Size       9982 / 38.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               SItSMP-eeve-WFUf-Jf8R-3d69-TOea-MrXNdo

  4、创建VG卷

    a、vgcreate

[root@localhost ~]# vgcreate myvg /dev/sdb  #创建myvg vg卷组,包含pv卷/dev/sdb
  Volume group "myvg" successfully created
[root@localhost ~]# vgdisplay myvg
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               <20.00 GiB
  PE Size               4.00 MiB #默认的PE大小为4M,我们可用使用-s选项指定大小
  Total PE              5119
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5119 / <20.00 GiB
  VG UUID               E0gbL2-Yfyu-5dzP-UL0c-S1ft-zwZ9-ahkbhH

      此时我们来看我们的pv的信息

[root@localhost ~]# pvdisplay /dev/sdb
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               myvg
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               5119
  Allocated PE          0
  PV UUID               HJFz6y-SGc3-Qhg6-tBIt-x8Lt-504H-Lu1zls

    b、扩展vg卷组

[root@localhost ~]# pvcreate /dev/sdc #首先再创建一个pv卷/dev/sdc
  Physical volume "/dev/sdc" successfully created.
[root@localhost ~]# vgextend myvg /dev/sdc #将/dev/sdc扩展至vg卷组myvg
  Volume group "myvg" successfully extended
[root@localhost ~]# vgdisplay myvg 
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  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               39.99 GiB #可以看到我们的vg卷组大小变成了40G
  PE Size               4.00 MiB
  Total PE              10238
  Alloc PE / Size       0 / 0   
  Free  PE / Size       10238 / 39.99 GiB
  VG UUID               E0gbL2-Yfyu-5dzP-UL0c-S1ft-zwZ9-ahkbhH

    c、现在我们要将某个pv挪出去,也就是缩减vg卷组的大小

[root@localhost ~]# pvmove /dev/sdc  #首先我们移除/dev/sdc这个pv上已经存在的数据PE,因此此处没有数据所以很快就移除了
  No data to move for myvg.
[root@localhost ~]# vgreduce myvg /dev/sdc  #然后我们从vg卷组中移除pv /dev/sdc
  Removed "/dev/sdc" from volume group "myvg"
[root@localhost ~]# vgdisplay myvg 
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <20.00 GiB #可以看到我们的vg大小又变成了20G
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5119 / <20.00 GiB
  VG UUID               E0gbL2-Yfyu-5dzP-UL0c-S1ft-zwZ9-ahkbhH

  5、删除VG卷组

    a、vgremove  :删除卷组这个卷组上的逻辑卷都要先删除,如果逻辑卷不删除那么删除卷组后其上的所有逻辑卷都会被删除。

    b、接下来演示一波从lv 到 vg再到pv的删除

[root@localhost /]# 
[root@localhost /]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 36.99g                                                    
  swap centos -wi-ao----  2.00g                                                    
  mylv myvg   -wi-a-----  3.00g                                                    
[root@localhost /]# lvremove /dev/myvg/mylv 
Do you really want to remove active logical volume myvg/mylv? [y/n]: y
  Logical volume "mylv" successfully removed
[root@localhost /]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <39.00g   4.00m
  myvg     1   0   0 wz--n- <20.00g <20.00g
[root@localhost /]# vgremove myvg 
  Volume group "myvg" successfully removed
[root@localhost /]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <39.00g  4.00m
  /dev/sdb          lvm2 ---   20.00g 20.00g
  /dev/sdc          lvm2 ---   20.00g 20.00g
[root@localhost /]# pvremove /dev/sdb /dev/sdc
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.

 

      总结:

 

 四、LV的管理

  1、查看我们的逻辑卷

    a、简单查看逻辑卷

    b、查看逻辑卷的详细信息

[root@localhost ~]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 36.99g                                                    
  swap centos -wi-ao----  2.00g                                                    
[root@localhost ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                r7JTIz-yP0P-Qd02-w80O-z8bf-K6ZS-s80s43
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-10-24 05:18:53 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                EjWwzA-53e6-dXoN-2MUP-MVux-d2qf-zEirhw
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-10-24 05:18:53 +0800
  LV Status              available
  # open                 1
  LV Size                36.99 GiB
  Current LE             9470
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
[root@localhost ~]# lvdisplay /dev/centos/root  #查看单个lv逻辑卷
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                EjWwzA-53e6-dXoN-2MUP-MVux-d2qf-zEirhw
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-10-24 05:18:53 +0800
  LV Status              available
  # open                 1
  LV Size                36.99 GiB
  Current LE             9470
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  2、创建逻辑卷

    a、lvcreate -L #[mMgGtT] -n NAME VolumeGroup  #即在vg卷组VolumeGroup中创建一个名称为NAME大小为#的lv逻辑卷。

      注意我们指定的逻辑卷大小一定要在对应的卷组中有足够的空间分配给他,否则会有问题

[root@localhost ~]# lvcreate -L 2G -n mylv myvg  #从卷组myvg中创建一个大小为2G名称为mylv的逻辑卷
WARNING: ext4 signature detected on /dev/myvg/mylv at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/myvg/mylv.
  Logical volume "mylv" created.
[root@localhost ~]# ls /dev/mapper/
centos-root  centos-swap  control  myvg-mylv
[root@localhost ~]# lvdisplay /dev/myvg/mylv 
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                EBTL0m-g18W-NTbH-jk2o-hiGI-9cXW-it8BXw
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-01-08 04:54:47 +0800
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

    b、现在我们将我们创建的逻辑卷进行格式化并挂载至/backup目录中

[root@localhost ~]# mke2fs -t ext4 -b 1024 -L MYLV /dev/myvg/mylv 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=MYLV
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 2097152 blocks
104857 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=35651584
256 block groups
8192 blocks per group, 8192 fragments per group
512 inodes per group
Superblock backups stored on blocks: 
    8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553, 
    1024001, 1990657

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   
[root@localhost ~]# mkdir /backup
[root@localhost ~]# mount /dev/myvg/mylv /backup/
[root@localhost ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        37G  1.3G   36G   4% /
devtmpfs                devtmpfs  984M     0  984M   0% /dev
tmpfs                   tmpfs     996M     0  996M   0% /dev/shm
tmpfs                   tmpfs     996M  9.4M  987M   1% /run
tmpfs                   tmpfs     996M     0  996M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  132M  883M  14% /boot
tmpfs                   tmpfs     200M     0  200M   0% /run/user/0
/dev/mapper/myvg-mylv   ext4      2.0G  3.1M  1.9G   1% /backup

  3、扩展逻辑卷

    a、lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME  :其中的+表示再增加多少,如果不用+表示增加至多少。比如我们现在卷为2G,我们要扩展至5G可以使用+3G或5G的参数方式

[root@localhost ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        37G  1.3G   36G   4% /
devtmpfs                devtmpfs  984M     0  984M   0% /dev
tmpfs                   tmpfs     996M     0  996M   0% /dev/shm
tmpfs                   tmpfs     996M  9.4M  987M   1% /run
tmpfs                   tmpfs     996M     0  996M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  132M  883M  14% /boot
tmpfs                   tmpfs     200M     0  200M   0% /run/user/0
/dev/mapper/myvg-mylv   ext4      2.0G  3.1M  1.9G   1% /backup
[root@localhost ~]# lvextend -L +5G /dev/myvg/mylv #表示将mylv逻辑卷再扩大5G
  Size of logical volume myvg/mylv changed from 2.00 GiB (512 extents) to 7.00 GiB (1792 extents).
  Logical volume myvg/mylv successfully resized.

      此时我们可以发现我们扩展完成以后这个逻辑卷大小并没有变大

[root@localhost ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        37G  1.3G   36G   4% /
devtmpfs                devtmpfs  984M     0  984M   0% /dev
tmpfs                   tmpfs     996M     0  996M   0% /dev/shm
tmpfs                   tmpfs     996M  9.4M  987M   1% /run
tmpfs                   tmpfs     996M     0  996M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  132M  883M  14% /boot
tmpfs                   tmpfs     200M     0  200M   0% /run/user/0
/dev/mapper/myvg-mylv   ext4      2.0G  3.1M  1.9G   1% /backup

      对于整个逻辑卷来讲,其实我们创建完一个逻辑卷以后他的物理空间就决定了大小,此时我们还需要在物理空间上创建文件系统,因此我们扩展时我们确实是把外面的边界拉大了但是文件系统却没有被拉大,因此我们的文件系统也需要扩展至整个物理空间的可用大小。此时我们可用使用 resize2fs来进行物理空间的扩展(此命令只适用于ext系列的文件系统,如果是btrfs或xfs文件系统则需要其它工具)

[root@localhost ~]# resize2fs /dev/myvg/mylv 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/mylv is mounted on /backup; on-line resizing required
old_desc_blocks = 16, new_desc_blocks = 56
The filesystem on /dev/myvg/mylv is now 7340032 blocks long.

[root@localhost ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        37G  1.3G   36G   4% /
devtmpfs                devtmpfs  984M     0  984M   0% /dev
tmpfs                   tmpfs     996M     0  996M   0% /dev/shm
tmpfs                   tmpfs     996M  9.4M  987M   1% /run
tmpfs                   tmpfs     996M     0  996M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  132M  883M  14% /boot
tmpfs                   tmpfs     200M     0  200M   0% /run/user/0
/dev/mapper/myvg-mylv   ext4      6.9G  3.2M  6.6G   1% /backup

    b、因此我们扩展lv逻辑卷有两步。

      (1)、修改物理边界:lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME

      (2)、修改逻辑边界:resize2fs /dev/VG_NAME/LV_NAME

    c、扩展逻辑卷一般没什么太大的风险,一般安全问题不大

  4、缩减逻辑卷(应该先缩减内部文件系统大小,也就是逻辑边界。然后再去缩减物理空间到指定大小,也就是物理边界)

    a、先卸载文件系统。缩减是不允许联机进行的,因为联机进行风险非常大

      umount /dev/VG_NAME/LV_NAME

    b、做文件系统的强制检测和修复

      e2fsck -f /dev/VG_NAME/LV_NAME

    c、缩减逻辑卷的逻辑边界

      resize2fs /dev/VG_NAME/LV_NAME  #[mMgGtT] #将对应的逻辑卷缩减为指定大小。

    d、缩减对应物理边界

      lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME  #其中的 - 号和上面的+号描述意思一样,就是减去指定大小的空间,如果不加  - 表示减少到这个值。

      现在我们来对刚刚的mylv逻辑卷进行缩容

[root@localhost ~]# umount /backup/
[root@localhost ~]# e2fsck -f /dev/myvg/mylv 
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
MYLV: 13/458752 files (0.0% non-contiguous), 153364/7340032 blocks
[root@localhost ~]# resize2fs /dev/myvg/mylv 3G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylv to 3145728 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 3145728 blocks long.

[root@localhost ~]# lvreduce -L 3G /dev/myvg/mylv 
  WARNING: Reducing active logical volume to 3.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myvg/mylv? [y/n]: y
  Size of logical volume myvg/mylv changed from 7.00 GiB (1792 extents) to 3.00 GiB (768 extents).
  Logical volume myvg/mylv successfully resized.
[root@localhost ~]# mount /dev/myvg/mylv /backup/
[root@localhost ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        37G  1.3G   36G   4% /
devtmpfs                devtmpfs  984M     0  984M   0% /dev
tmpfs                   tmpfs     996M     0  996M   0% /dev/shm
tmpfs                   tmpfs     996M  9.4M  987M   1% /run
tmpfs                   tmpfs     996M     0  996M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  132M  883M  14% /boot
tmpfs                   tmpfs     200M     0  200M   0% /run/user/0
/dev/mapper/myvg-mylv   ext4      3.0G  3.2M  2.8G   1% /backup
[root@localhost ~]# ls /backup/
grub2.cfg  issue  lost+found

  5、删除逻辑卷

    lvremove /dev/VG_NAME/LV_NAME

五、快照:snapshot

  1、创建快照命令:lvcreate -L #[mMgGtT](快照大小,即快照最多能保存的原逻辑卷增长的数据量大小)  -p r (表示只读权限)-s(表示创建的是快照) -n snaphshot_lv_name (快照名称)original_lv_name(原卷名称)

[root@localhost ~]# lvcreate -s -L 512M -n mylv-snap -p r /dev/myvg/mylv
  Logical volume "mylv-snap" created.
[root@localhost ~]# mount /dev/myvg/mylv-snap /mnt/
mount: /dev/mapper/myvg-mylv--snap is write-protected, mounting read-only
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
grub2.cfg  issue  lost+found
[root@localhost mnt]# cat issue 
\S
Kernel \r on an \m

    现在我们编辑原卷添加内容然后对比我们快照卷中的内容

[root@localhost mnt]# cat /backup/issue  #编辑原卷后的内容
\S
Kernel \r on an \m

NEW LINE
[root@localhost mnt]# cat /mnt/issue  #快照卷中的内容
\S
Kernel \r on an \m

  2、删除快照卷(如果删除原逻辑卷快照卷也会被一起删除)

[root@localhost /]# umount /mnt/
[root@localhost /]# lvremove /dev/myvg/mylv-snap 
Do you really want to remove active logical volume myvg/mylv-snap? [y/n]: y
  Logical volume "mylv-snap" successfully removed

六、练习题

  1、创建一个至少有两个PV组成的大小为20G的名为testvg的VG:要求PE大小为16MB,而后在卷组中创建大小为5G的逻辑卷testlv,挂载至/users目录

  2、新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制/etc/pam.d目录至自己的家目录

  3、扩展testlv至7G,要求archlinux用户的文件不能丢失

  4、收缩testlv至3G,要求archlinux用户的文件不能丢失

  5、对testlv创建快照,并尝试基于快照备份数据,验证快照的功能

七、文件系统挂载使用

  1、挂载光盘设备

    a、光盘设备文件:

      IDE接口: /dev/hdc

      SATA:/dev/sro

      符号链接文件:

        /dev/cdrom

        /dev/cdrw

        /dev/dvd

        /dev/dvdrw

    b、挂载或卸载光盘文件

      mount -r /dev/cdrom /media/cdrom

      umount /dev/cdrom

八、dd命令的应用

  1、dd命令:    - convert and copy a file (转换并且复制一个文件),它是通过块形式进行复制的。

  2、用法

    dd if=/PATH/FROM/SRC  of=/PATH/TO/DEST

[root@node1 ~]# dd if=/etc/fstab of=/tmp/fstab
0+1 records in
0+1 records out
465 bytes (465 B) copied, 0.000476404 s, 976 kB/s

    bs=#  :block size,即复制单元大小

    count=#:复制多少个bs

      只复制/etc/fstab 100个字节

[root@node1 ~]# dd if=/etc/fstab of=/tmp/fstab.2 bs=1 count=100
100+0 records in
100+0 records out
100 bytes (100 B) copied, 0.000481648 s, 208 kB/s
[root@node1 ~]# cat /tmp/fstab.2 

#
# /etc/fstab
# Created by anaconda on Tue Jun  4 22:30:32 2019
#

  3、磁盘复制

    dd if=/dev/sda of=/dev/sdb

  4、备份MBR

    dd if=/dev/sda of=/tmp/mbr.bak bs=512 count=1 

    破坏MBR中的bootloader:

    dd if=/dev/zero of=/dev/sda bs=256 count=1

  5、linux中的两个特殊设备

    /dev/null :数据黑洞

    /dev/zero:吐零机

    

    

      

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

posted @ 2020-01-06 22:51  Presley  阅读(627)  评论(0编辑  收藏  举报