8月11日上课 逻辑卷管理

1、逻辑卷简要说明

将设备指定为物理卷
用一个或者多个物理卷来创建一个卷组
物理卷是用固定大小的物理区域(Physical Extent,PE)来定义的
在物理卷上创建的逻辑卷是由物理区域(PE)组成
可以在逻辑卷上创建文件系统


8月11日上课 逻辑卷管理_第1张图片
Paste_Image.png

2、创建逻辑卷

[root@centos7 ~]#pvcreate /dev/sdb /dev/sdc1   ---创建物理卷
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc1" successfully created.
[root@centos7 ~]#pvs  ---查看
  PV         VG Fmt  Attr PSize  PFree 
  /dev/sdb      lvm2 ---  20.00g 20.00g
  /dev/sdc1     lvm2 ---  15.00g 15.00g
[root@centos7 ~]#pvdisplay  ---详细查看
  "/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   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
  "/dev/sdc1" is a new physical volume of "15.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               15.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
[root@centos7 ~]#vgcreate vg0 /dev/sd{b,c1} ----创建卷组,可以用-s选项指定PE的大小
  Volume group "vg0" successfully created
[root@centos7 ~]#pvdisplay  ---创建完卷组后再来看物理卷
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               vg0   ---卷组名
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB  ---PE大小
  Total PE              5119  ---PE个数逻辑卷,分逻辑卷时是把PE分给
  Free PE               5119
  Allocated PE          0
  PV UUID               72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               vg0
  PV Size               15.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              3839
  Free PE               3839
  Allocated PE          0
  PV UUID               j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
[root@centos7 ~]#vgs  ---查看卷组
  VG  #PV #LV #SN Attr   VSize  VFree 
  vg0   2   0   0 wz--n- 34.99g 34.99g
[root@centos7 ~]#vgdisplay ---详细查看
  --- Volume group ---
  VG Name               vg0
  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               34.99 GiB
  PE Size               4.00 MiB
  Total PE              8958
  Alloc PE / Size       0 / 0   
  Free  PE / Size       8958 / 34.99 GiB
  VG UUID               HMIGkc-kO3K-sd6j-YuPc-i6ez-IOqY-KIReKJ
   [root@centos7 ~]#lvcreate -n lv0 -L 15G vg0   ---创建逻辑卷
  Logical volume "lv0" created.
[root@centos7 ~]#lvs  ---查看逻辑卷
  LV   VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vg0 -wi-a----- 15.00g                                                    
[root@centos7 ~]#lvdisplay ---详细查看
  --- Logical volume ---
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                HH3DXN-bMUd-ebeu-89Vs-ifnw-XYE5-aEBotX
  LV Write Access        read/write
  LV Creation host, time centos7.magedu.com, 2017-08-12 11:49:32 +0800
  LV Status              available
  # open                 0
  LV Size                15.00 GiB
  Current LE             3840
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
[root@centos7 ~]#ll /dev/vg0/lv0  ---可以看到逻辑卷的设备名为一个软连接
lrwxrwxrwx. 1 root root 7 Aug 12 11:49 /dev/vg0/lv0 -> ../dm-0
[root@centos7 ~]#lvcreate -n lv1 -L 10G vg0  ---再创建一个逻辑卷
  Logical volume "lv1" created.
[root@centos7 ~]#lvdisplay ---此时有两个逻辑卷
  --- Logical volume ---
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                HH3DXN-bMUd-ebeu-89Vs-ifnw-XYE5-aEBotX
  LV Write Access        read/write
  LV Creation host, time centos7.magedu.com, 2017-08-12 11:49:32 +0800
  LV Status              available
  # open                 0
  LV Size                15.00 GiB
  Current LE             3840
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/vg0/lv1
  LV Name                lv1
  VG Name                vg0
  LV UUID                RAIOfB-sJ1U-0wGj-FPjn-53SZ-rBeE-AvsvBp
  LV Write Access        read/write
  LV Creation host, time centos7.magedu.com, 2017-08-12 12:00:14 +0800
  LV Status              available
  # open                 0
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
[root@centos7 ~]#mkfs.ext4 /dev/vg0/lv0  ---创建文件系统
[root@centos7 ~]#mkdir /mnt/lv0
[root@centos7 ~]#mount /dev/vg0/lv0 /mnt/lv0/  ---挂载
[root@centos7 ~]#df
Filesystem          1K-blocks     Used Available Use% Mounted on
/dev/sda2            52403200 15338660  37064540  30% /
devtmpfs               485268        0    485268   0% /dev
tmpfs                  499980       88    499892   1% /dev/shm
tmpfs                  499980    13544    486436   3% /run
tmpfs                  499980        0    499980   0% /sys/fs/cgroup
/dev/sda3            52403200   540228  51862972   2% /app
/dev/sda1             1038336   172284    866052  17% /boot
tmpfs                  100000       16     99984   1% /run/user/0
/dev/sr0              8086368  8086368         0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0  15350728    40984  14506928   1% /mnt/lv0

总结:创建逻辑卷的过程是在磁盘或者分区上先创建物理卷,如果是分区,创建分区时要指定id号为LVM,之后再创建卷组,通过卷组再创建逻辑卷,因为逻辑卷是通过卷组创建的,所以卷组中创建了PE
,默认PE大小为4MiB,用它来指定逻辑卷的最小块的单位,在创建卷组时可以通过-s来指定PE的大小来指定逻辑卷中块的大小,在没有创建卷组时物理卷中的PE是没有的,创建之后物理卷中才能看到。

3、扩展逻辑卷

[root@centos7 app]#vgdisplay ---首先要看卷组里是否有剩余空间
  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               34.99 GiB
  PE Size               4.00 MiB
  Total PE              8958
  Alloc PE / Size       6400 / 25.00 GiB
  Free  PE / Size       2558 / 9.99 GiB  ----卷组剩下空间
  VG UUID               HMIGkc-kO3K-sd6j-YuPc-i6ez-IOqY-KIReKJ
 ①没有剩余空间
  [root@centos7 app]#pvcreate /dev/sdc2
WARNING: dos signature detected on /dev/sdc2 at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sdc2.
  Physical volume "/dev/sdc2" successfully created.  ---创建物理卷
[root@centos7 app]#vgextend vg0 /dev/sdc2  ---扩展卷组,将物理卷加到卷组里
  Volume group "vg0" successfully extended
[root@centos7 app]#vgdisplay 
  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               54.99 GiB
  PE Size               4.00 MiB
  Total PE              14077
  Alloc PE / Size       6400 / 25.00 GiB
  Free  PE / Size       7677 / 29.99 GiB  ---卷组的空间扩大
  VG UUID               HMIGkc-kO3K-sd6j-YuPc-i6ez-IOqY-KIReKJ
   [root@centos7 app]#lvs
  LV   VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vg0 -wi-ao---- 15.00g                                                    
  lv1  vg0 -wi-a----- 10.00g                                                    
[root@centos7 app]#lvextend -L 20g /dev/vg0/lv0  ---扩展逻辑卷到20G
  Size of logical volume vg0/lv0 changed from 15.00 GiB (3840 extents) to 20.00 GiB (5120 extents).
  Logical volume vg0/lv0 successfully resized.
[root@centos7 app]#lvs
  LV   VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vg0 -wi-ao---- 20.00g                                                    
  lv1  vg0 -wi-a----- 10.00g       
[root@centos7 app]#df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             50G   15G   36G  30% /
devtmpfs             474M     0  474M   0% /dev
tmpfs                489M   88K  489M   1% /dev/shm
tmpfs                489M   14M  476M   3% /run
tmpfs                489M     0  489M   0% /sys/fs/cgroup
/dev/sda3             50G  1.1G   49G   3% /app
/dev/sda1           1014M  169M  846M  17% /boot
tmpfs                 98M   16K   98M   1% /run/user/0
/dev/sr0             7.8G  7.8G     0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0   15G  541M   14G   4% /mnt/lv0   ---此时文件系统显示的大小还是15G,说明文件系统没有同步
如果是xfs文件系统:xfs_growfs /mnt/lv0
[root@centos7 app]#resize2fs /dev/vg0/lv0 20G  ---让文件系统同步
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/vg0/lv0 is now 5242880 blocks long.
[root@centos7 app]#df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/sda2           xfs        50G   15G   36G  30% /
devtmpfs            devtmpfs  474M     0  474M   0% /dev
tmpfs               tmpfs     489M   88K  489M   1% /dev/shm
tmpfs               tmpfs     489M   14M  476M   3% /run
tmpfs               tmpfs     489M     0  489M   0% /sys/fs/cgroup
/dev/sda3           xfs        50G  1.1G   49G   3% /app
/dev/sda1           xfs      1014M  169M  846M  17% /boot
tmpfs               tmpfs      98M   16K   98M   1% /run/user/0
/dev/sr0            iso9660   7.8G  7.8G     0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0 ext4       20G  544M   19G   3% /mnt/lv0  ---已经同步
②有剩余空间
[root@centos7 app]#mkfs.xfs /dev/vg0/lv1  ---对逻辑卷lv1创建文件系统
[root@centos7 app]#mount /dev/vg0/lv1 /mnt/lv1  ---挂载
[root@centos7 app]#df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             50G   15G   36G  30% /
devtmpfs             474M     0  474M   0% /dev
tmpfs                489M   88K  489M   1% /dev/shm
tmpfs                489M   14M  476M   3% /run
tmpfs                489M     0  489M   0% /sys/fs/cgroup
/dev/sda3             50G  1.1G   49G   3% /app
/dev/sda1           1014M  169M  846M  17% /boot
tmpfs                 98M   16K   98M   1% /run/user/0
/dev/sr0             7.8G  7.8G     0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0   20G  544M   19G   3% /mnt/lv0
/dev/mapper/vg0-lv1   10G   33M   10G   1% /mnt/lv1
[root@centos7 app]#lvs
  LV   VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vg0 -wi-ao---- 20.00g                                                    
  lv1  vg0 -wi-ao---- 10.00g                                                    
[root@centos7 app]#lvextend -r -L +5G /dev/vg0/lv1 ---将lv1逻辑卷增加5G,-r选项可以实现自动同步文件系统。
  Size of logical volume vg0/lv1 changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
  Logical volume vg0/lv1 successfully resized.
meta-data=/dev/mapper/vg0-lv1    isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 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@centos7 app]#lvs
  LV   VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vg0 -wi-ao---- 20.00g                                                    
  lv1  vg0 -wi-ao---- 15.00g      ---已增加
[root@centos7 app]#df -h ---我们看到已经自动同步文件系统
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             50G   15G   36G  30% /
devtmpfs             474M     0  474M   0% /dev
tmpfs                489M   88K  489M   1% /dev/shm
tmpfs                489M   14M  476M   3% /run
tmpfs                489M     0  489M   0% /sys/fs/cgroup
/dev/sda3             50G  1.1G   49G   3% /app
/dev/sda1           1014M  169M  846M  17% /boot
tmpfs                 98M   16K   98M   1% /run/user/0
/dev/sr0             7.8G  7.8G     0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0   20G  544M   19G   3% /mnt/lv0
/dev/mapper/vg0-lv1   15G   33M   15G   1% /mnt/lv1

4、缩减逻辑卷

只有逻辑卷是ext4文件系统的才能缩减逻辑卷,生产中一般不会缩减逻辑卷,有可能会破坏数据

[root@centos7 lv0]#df -T  --这里我们缩减lv0逻辑卷
Filesystem          Type     1K-blocks     Used Available Use% Mounted on
/dev/sda2           xfs       52403200 15338912  37064288  30% /
devtmpfs            devtmpfs    485268        0    485268   0% /dev
tmpfs               tmpfs       499980       88    499892   1% /dev/shm
tmpfs               tmpfs       499980    13548    486432   3% /run
tmpfs               tmpfs       499980        0    499980   0% /sys/fs/cgroup
/dev/sda3           xfs       52403200  1052228  51350972   3% /app
/dev/sda1           xfs        1038336   172284    866052  17% /boot
tmpfs               tmpfs       100000       16     99984   1% /run/user/0
/dev/sr0            iso9660    8086368  8086368         0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0 ext4      20511312   557040  18931260   3% /mnt/lv0
/dev/mapper/vg0-lv1 xfs       15718400    71272  15647128   1% /mnt/lv1
[root@centos7 lv0]#umount /mnt/lv0   ---第一步取消挂载
[root@centos7 ~]#fsck -f /dev/vg0/lv0   ---第二步检查文件系统
fsck from util-linux 2.23.2
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/mapper/vg0-lv0: 12/1310720 files (0.0% non-contiguous), 126323/5242880 blocks
[root@centos7 ~]#resize2fs /dev/vg0/lv0 18G   ---第三步缩减文件系统到18G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 4718592 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 4718592 blocks long.
[root@centos7 ~]#lvreduce -L 18G /dev/vg0/lv0   ---第四步缩减逻辑卷到18G
  WARNING: Reducing active logical volume to 18.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: y
  Size of logical volume vg0/lv0 changed from 20.00 GiB (5120 extents) to 18.00 GiB (4608 extents).
  Logical volume vg0/lv0 successfully resized.
[root@centos7 ~]#lvs
  LV   VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vg0 -wi-ao---- 18.00g      ---已经缩减                                            
  lv1  vg0 -wi-ao---- 15.00g     
 [root@centos7 ~]#mount -a
[root@centos7 ~]#df -Th
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/sda2           xfs        50G   15G   36G  30% /
devtmpfs            devtmpfs  474M     0  474M   0% /dev
tmpfs               tmpfs     489M   88K  489M   1% /dev/shm
tmpfs               tmpfs     489M   20M  469M   4% /run
tmpfs               tmpfs     489M     0  489M   0% /sys/fs/cgroup
/dev/sda3           xfs        50G  1.1G   49G   3% /app
/dev/sda1           xfs      1014M  169M  846M  17% /boot
tmpfs               tmpfs      98M   16K   98M   1% /run/user/0
/dev/sr0            iso9660   7.8G  7.8G     0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv1 xfs        15G   70M   15G   1% /mnt/lv1
/dev/mapper/vg0-lv0 ext4       18G   45M   17G   1% /mnt/lv0

总结:缩减逻辑卷的过程中是先取消挂载,这一步很重要,不要忘记,然后检查文件系统,缩减文件系统,缩减逻辑卷,要按照这个过程一步步的来。

5、迁移卷组

[root@centos6 ~]#umount /mnt/lv0/  ---第一步取消挂载
[root@centos6 ~]#df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       50264772 4786024  42918748  11% /
tmpfs             502056      76    501980   1% /dev/shm
/dev/sda3       40185208  561020  37576188   2% /app
/dev/sda1         991512   34904    905408   4% /boot
/dev/sr0         6049912 6049912         0 100% /media/CDROM
[root@centos6 ~]#vgrename vg0 newvg0  ---第二步为了和其他卷组的名字冲突,重新定义卷组和逻辑卷的名字
  Volume group "vg0" successfully renamed to "newvg0"
[root@centos6 ~]#lvrename /dev/newvg0/lv0 newlv0   ---逻辑卷的名字改不改都可以,卷组的名字变了,逻辑卷的名字也跟着变了
  Renamed "lv0" to "newlv0" in volume group "newvg0"
[root@centos6 ~]#vgchange -an newvg0    ---第三步把卷组设为非活动状态
  0 logical volume(s) in volume group "newvg0" now active
[root@centos6 ~]#lvdisplay 
  --- Logical volume ---
  LV Path                /dev/newvg0/newlv0
  LV Name                newlv0
  VG Name                newvg0
  LV UUID                ua7BV0-2Zem-qtzq-Zeiv-DaM4-hA4K-2UKux0
  LV Write Access        read/write
  LV Creation host, time centos6.magedu.com, 2017-08-09 16:46:39 +0800
  LV Status              NOT available   ---查看已经处于不活动状态
  LV Size                8.00 GiB
  Current LE             2048
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  [root@centos6 ~]#vgexport newvg0
  Volume group "newvg0" successfully exported  ---第四步导出卷组
halt   ---第五步关机,将硬盘装入centos7
[root@centos7 ~]#echo '- - -'> /sys/class/scsi_host/host0/scan  ---激活一下硬盘
[root@centos7 ~]#lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0  200G  0 disk 
├─sda1              8:1    0    1G  0 part /boot
├─sda2              8:2    0   50G  0 part /
├─sda3              8:3    0   50G  0 part /app
├─sda4              8:4    0  512B  0 part 
└─sda5              8:5    0    2G  0 part [SWAP]
sdb                 8:16   0   20G  0 disk 
└─vg0-lv0         253:0    0   18G  0 lvm  /mnt/lv0
sdc                 8:32   0  150G  0 disk 
├─sdc1              8:33   0   15G  0 part 
│ └─vg0-lv1       253:1    0   15G  0 lvm  /mnt/lv1
└─sdc2              8:34   0   20G  0 part 
  └─vg0-lv1       253:1    0   15G  0 lvm  /mnt/lv1
sdd                 8:48   0  120G  0 disk 
sde                 8:64   0   60G  0 disk   ---可以看到这个磁盘了
├─sde1              8:65   0    5G  0 part 
│ └─newvg0-newlv0 253:2    0    8G  0 lvm  
└─sde2              8:66   0    5G  0 part 
  └─newvg0-newlv0 253:2    0    8G  0 lvm  
sr0                11:0    1  7.7G  0 rom  /run/media/root/CentOS 7 x86_64
[root@centos7 ~]#pvscan ---第六步可以看到新的卷组处于导出状态
  PV /dev/sdb    VG vg0             lvm2 [20.00 GiB / 2.00 GiB free]
  PV /dev/sdc1   VG vg0             lvm2 [15.00 GiB / 4.00 MiB free]
  PV /dev/sdc2   VG vg0             lvm2 [20.00 GiB / 19.99 GiB free]
  PV /dev/sde1    is in exported VG newvg0 [5.01 GiB / 0    free]
  PV /dev/sde2    is in exported VG newvg0 [5.01 GiB / 2.02 GiB free]
  Total: 5 [65.00 GiB] / in use: 5 [65.00 GiB] / in no VG: 0 [0   ]
root@centos7 ~]#vgimport newvg0 ---第七步导入卷组
  Volume group "newvg0" successfully imported
root@centos7 ~]#vgchange -ay newvg0   ---第八步激活卷组
  1 logical volume(s) in volume group "newvg0" now active
[root@centos7 ~]#mkdir /mnt/newlv0
[root@centos7 ~]#mount /dev/newvg0/newlv0 /mnt/newlv0/
[root@centos7 ~]#df
Filesystem                1K-blocks     Used Available Use% Mounted on
/dev/sda2                  52403200 15341732  37061468  30% /
devtmpfs                     485268        0    485268   0% /dev
tmpfs                        499980      128    499852   1% /dev/shm
tmpfs                        499980    19816    480164   4% /run
tmpfs                        499980        0    499980   0% /sys/fs/cgroup
/dev/sda3                  52403200  1052228  51350972   3% /app
/dev/sda1                   1038336   172284    866052  17% /boot
tmpfs                        100000       20     99980   1% /run/user/0
/dev/sr0                    8086368  8086368         0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv1        15718400    71272  15647128   1% /mnt/lv1
/dev/mapper/vg0-lv0        18447056    45084  17441872   1% /mnt/lv0
/dev/mapper/newvg0-newlv0   8125880   530424   7159644   7% /mnt/newlv0
[root@centos7 ~]#cd /mnt/newlv0/
[root@centos7 newlv0]#ls
f1  lost+found
可以看到文件没有丢失

6、从逻辑卷中删除正在使用的物理分区(硬盘)

[root@centos7 ~]#vgdisplay   ---先看一下卷组
  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  10
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               54.99 GiB
  PE Size               4.00 MiB
  Total PE              14077
  Alloc PE / Size       8448 / 33.00 GiB
  Free  PE / Size       5629 / 21.99 GiB  ---卷组的剩余空间是否满足将硬盘或分期的空间转移到其他物理卷
  VG UUID               HMIGkc-kO3K-sd6j-YuPc-i6ez-IOqY-KIReKJ
[root@centos7 ~]#pvdisplay   ---这里我们可以把/dev/sdc2分区中已经分配出去的2个PE转移到其他物理卷
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               vg0
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               511
  Allocated PE          4608
  PV UUID               72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
   
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               vg0
  PV Size               15.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              3839
  Free PE               1
  Allocated PE          3838
  PV UUID               j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
   
  --- Physical volume ---
  PV Name               /dev/sdc2
  VG Name               vg0
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               5117
  Allocated PE          2
  PV UUID               DTnJdv-pV0n-p8Dz-Ku8b-gtQv-hNDI-A56LPK
[root@centos7 ~]#pvmove /dev/sdc2  ---移动/dev/sdc2中已经分配的PE空间给其他物理卷
  /dev/sdc2: Moved: 100.00%
[root@centos7 ~]#pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               vg0
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               509
  Allocated PE          4610
  PV UUID               72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
   
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               vg0
  PV Size               15.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              3839
  Free PE               1
  Allocated PE          3838
  PV UUID               j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
   
  --- Physical volume ---
  PV Name               /dev/sdc2
  VG Name               vg0
  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    ---此时/dev/sdc2分区一个PE也没有分配给逻辑卷
  PV UUID               DTnJdv-pV0n-p8Dz-Ku8b-gtQv-hNDI-A56LPK
[root@centos7 ~]#vgreduce vg0 /dev/sdc2  ---缩减卷组
  Removed "/dev/sdc2" from volume group "vg0"
[root@centos7 ~]#pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               vg0
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               509
  Allocated PE          4610
  PV UUID               72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
   
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               vg0
  PV Size               15.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              3839
  Free PE               1
  Allocated PE          3838
  PV UUID               j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
   
  "/dev/sdc2" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc2
  VG Name                        ---可以看到已经移除,不属于任何卷组
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               DTnJdv-pV0n-p8Dz-Ku8b-gtQv-hNDI-A56LPK
[root@centos7 ~]#pvremove /dev/sdc2  ---删除物理卷
  Labels on physical volume "/dev/sdc2" successfully wiped.
[root@centos7 ~]#pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               vg0
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               509
  Allocated PE          4610
  PV UUID               72dJR7-GCfL-GOHH-Tg9a-IIbl-EgTD-YLrRGz
   
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               vg0
  PV Size               15.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              3839
  Free PE               1
  Allocated PE          3838
  PV UUID               j8i7QQ-sSR2-bsJn-UlJj-a1jY-Ubum-KRYgWx
  之后就可以将这个分区删除

7、逻辑卷快照管理

逻辑卷的快照就相当于在同一个卷组中再创建一个逻辑卷快照的空间,这个空间的大小要保证修改的文件能够存入,刚开始时逻辑卷的快照里没有数据,只是一个空间,当在逻辑卷中修改一个文件时,会把一个文件的原始状态推送到这个快照空间中保存起来。

[root@centos7 lv0]#lvcreate -L 500M -s -n lv0-snapshot -p r /dev/vg0/lv0   ---对逻辑卷lv0创建一个500M的快照,-L指定大小,-s指定是快照,-n指定名字,-p指定是只读
  Using default stripesize 64.00 KiB.
  Logical volume "lv0-snapshot" created.
[root@centos7 lv0]#mkdir /mnt/lv0-snapshot
[root@centos7 lv0]#mount /dev/vg0/lv0-snapshot /mnt/lv0-snapshot/  ---挂载快照
mount: /dev/mapper/vg0-lv0--snapshot is write-protected, mounting read-only
[root@centos7 lv0]#df
Filesystem                    1K-blocks     Used Available Use% Mounted on
/dev/sda2                      52403200 15341232  37061968  30% /
devtmpfs                         918932        0    918932   0% /dev
tmpfs                            933644       88    933556   1% /dev/shm
tmpfs                            933644     9260    924384   1% /run
tmpfs                            933644        0    933644   0% /sys/fs/cgroup
/dev/sda3                      52403200  1052228  51350972   3% /app
/dev/mapper/vg0-lv0            18447056    45084  17441872   1% /mnt/lv0
/dev/sda1                       1038336   172284    866052  17% /boot
tmpfs                            186732       12    186720   1% /run/user/0
/dev/sr0                        8086368  8086368         0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg0-lv0--snapshot  18447056    45084  17441872   1% /mnt/lv0-snapshot
[root@centos7 lv0-snapshot]#cd /mnt/lv0  ---进入原来的lv0逻辑卷目录
[root@centos7 lv0]#ls
fstab  lost+found
[root@centos7 lv0]#vim fstab ---修改一下文件内容
  1 UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 /                       xfs     defaults
           0 0
  2 UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app                    xfs     defaults
           0 0
  3 UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot                   xfs     defaults
           0 0
  4 UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap                    swap    defaults
           0 0
                                                                          
"fstab" 4L, 376C written
[root@centos7 lv0]#
[root@centos7 lv0]#
[root@centos7 lv0]#
[root@centos7 lv0]#
[root@centos7 lv0]#cat fstab ---修改之后的内容
UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 /                       xfs     defaults        0 0
UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app                    xfs     defaults        0 0
UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot                   xfs     defaults        0 0
UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap                    swap    defaults        0 0
[root@centos7 lv0]#
   0 0
[root@centos7 lv0]#cd /mnt/lv0-snapshot/  ---进入快照的目录
[root@centos7 lv0-snapshot]#ls 
fstab  lost+found
[root@centos7 lv0-snapshot]#cat fstab ---可以看到已经将原始的内容保存到其中

#
# /etc/fstab
# Created by anaconda on Fri Jul 14 11:16:04 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 /                       xfs     defaults        0 0
UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app                    xfs     defaults        0 0
UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot                   xfs     defaults        0 0
UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap                    swap    defaults        0 0
[root@centos6 ~]#lvconvert --merge /dev/vg0/lv0-snapshot ---恢复快照
  Merging of volume vg0/lv0-snapshot started.
  lv1: Merged: 100.00%
[root@centos6 lv1]#cd /mnt/lv0
[root@centos6 lv1]#ls
etc  fstab
[root@centos6 lv0]#cat fstab ---可以看到文件又恢复到原来的状态,没有丢失
总结:在恢复快照之前一定要先取消挂载,包括逻辑卷的挂载和快照的挂载都要取消,否则无法恢复快照。这个是在练习中经常出现的错误。
#
# /etc/fstab
# Created by anaconda on Fri Jul 14 11:16:04 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=e7e1738b-58ae-49d3-9f7e-c62988ead392 /                       xfs     defaults        0 0
UUID=348930fe-f95e-40e6-90ff-83c66b3f2e9a /app                    xfs     defaults        0 0
UUID=dfbab032-681e-4e34-a16e-2f5e1eda5f32 /boot                   xfs     defaults        0 0
UUID=6145ae5c-6ebd-4655-a52f-e90f9b5a9e58 swap                    swap    defaults        0 0

UUID=fca04a06-6e33-41bb-8902-cc89b908ae22    /mnt/lv0             ext4     defaults       0  0 

总结:在修改逻辑卷上的文件时会将原文件数据推送至快照中,在生产中一般不会使用快照。因为这样会影响服务器的性能。相当于在修改的时候会把原数据输出到快照。

8、删除逻辑卷的步骤

从上往下删除,跟创建的时候相反

[root@centos7 lv1]#lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg0/lv1
  LV Name                lv1
  VG Name                vg0
  LV UUID                RAIOfB-sJ1U-0wGj-FPjn-53SZ-rBeE-AvsvBp
  LV Write Access        read/write
  LV Creation host, time centos7.magedu.com, 2017-08-12 12:00:14 +0800
  LV Status              available
  # open                 1
  LV Size                15.00 GiB
  Current LE             3840
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
[root@centos7 ~]#umount /mnt/lv1  ---先取消挂载
[root@centos7 ~]#lvremove /dev/vg0/lv1  ---删除逻辑卷
Do you really want to remove active logical volume vg0/lv1? [y/n]: y
  Logical volume "lv1" successfully removed
[root@centos7 ~]#vgremove vg0
  Volume group "vg0" successfully removed
[root@centos7 ~]#pvs
  PV         VG Fmt  Attr PSize  PFree 
  /dev/sdb      lvm2 ---  20.00g 20.00g
  /dev/sdc1     lvm2 ---  15.00g 15.00g
[root@centos7 ~]#pvremove /dev/sd{b,c1}  ---删除物理卷
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc1" successfully wiped.
之后就可以删除分区或者拔除硬盘了。

你可能感兴趣的:(8月11日上课 逻辑卷管理)