zoukankan      html  css  js  c++  java
  • Linux-LVM管理

    Linux-LVM管理

    LVM简介

    LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制

    应用场景:
    随着公司的发展,数据增长较快,最初规划的磁盘容量不够用了

    弊端:
    数据不是直接存放在硬盘上,而是在硬盘的上面又虚拟出来一层逻辑卷存放数据,故而增加了磁盘数据恢复的难度

    物理卷、卷组、逻辑卷

    物理卷(PV):把常规的块设备(硬盘,分区等可以读写数据的设备)通过pvcreate命令对其进行初始化,就成了物理卷

    卷组(VG):把多个物理卷的容量组成一个逻辑整体,可以从里面灵活分配容量

    逻辑卷(LV):从卷组中划分部分空间成为一个可以读写数据的逻辑单元,需要对其格式化然后挂载使用

    部署LVM

    第一步:准备物理磁盘(这里创建了3个20G的硬盘)

    [root@localhost ~]# lsblk
    NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda             8:0    0  100G  0 disk 
    ├─sda1          8:1    0    1G  0 part /boot
    └─sda2          8:2    0   99G  0 part 
      ├─rhel-root 253:0    0   50G  0 lvm  /
      ├─rhel-swap 253:1    0    2G  0 lvm  [SWAP]
      └─rhel-home 253:2    0   47G  0 lvm  /home
    sdb             8:16   0   20G  0 disk 
    sdc             8:32   0   20G  0 disk 
    sdd             8:48   0   20G  0 disk 
    sr0            11:0    1 1024M  0 rom  
    
    //给sdb分3个分区,大小分别是5G、7G、8G
    [root@localhost ~]# fdisk /dev/sdb
    
    Welcome to fdisk (util-linux 2.32.1).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Device does not contain a recognized partition table.
    Created a new DOS disklabel with disk identifier 0x15bdf929.
    
    Command (m for help): n
    Partition type
       p   primary (0 primary, 0 extended, 4 free)
       e   extended (container for logical partitions)
    Select (default p): 
    
    Using default response p.
    Partition number (1-4, default 1): 
    First sector (2048-41943039, default 2048): 
    Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +5G
    
    Created a new partition 1 of type 'Linux' and of size 5 GiB.
    
    Command (m for help): n
    Partition type
       p   primary (1 primary, 0 extended, 3 free)
       e   extended (container for logical partitions)
    Select (default p): 
    
    Using default response p.
    Partition number (2-4, default 2): 
    First sector (10487808-41943039, default 10487808): 
    Last sector, +sectors or +size{K,M,G,T,P} (10487808-41943039, default 41943039): +7G
    
    Created a new partition 2 of type 'Linux' and of size 7 GiB.
    
    Command (m for help): n
    Partition type
       p   primary (2 primary, 0 extended, 2 free)
       e   extended (container for logical partitions)
    Select (default p): 
    
    Using default response p.
    Partition number (3,4, default 3): 
    First sector (25167872-41943039, default 25167872): 
    Last sector, +sectors or +size{K,M,G,T,P} (25167872-41943039, default 41943039): 
    
    Created a new partition 3 of type 'Linux' and of size 8 GiB.
    
    Command (m for help): p
    Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0x15bdf929
    
    Device     Boot    Start      End  Sectors Size Id Type
    /dev/sdb1           2048 10487807 10485760   5G 83 Linux
    /dev/sdb2       10487808 25167871 14680064   7G 83 Linux
    /dev/sdb3       25167872 41943039 16775168   8G 83 Linux
    
    Command (m for help): w
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
    //刷新分区表
    [root@localhost ~]# partprobe 
    [root@localhost ~]# lsblk
    NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda             8:0    0  100G  0 disk 
    ├─sda1          8:1    0    1G  0 part /boot
    └─sda2          8:2    0   99G  0 part 
      ├─rhel-root 253:0    0   50G  0 lvm  /
      ├─rhel-swap 253:1    0    2G  0 lvm  [SWAP]
      └─rhel-home 253:2    0   47G  0 lvm  /home
    sdb             8:16   0   20G  0 disk 
    ├─sdb1          8:17   0    5G  0 part 
    ├─sdb2          8:18   0    7G  0 part 
    └─sdb3          8:19   0    8G  0 part 
    sdc             8:32   0   20G  0 disk 
    sdd             8:48   0   20G  0 disk 
    sr0            11:0    1 1024M  0 rom  
    

    第二步:创建物理卷(这里我们用分区sdb1和磁盘sdc举例)

    //将分区sdb1初始化为物理卷
    [root@localhost ~]# pvcreate /dev/sdb1
      Physical volume "/dev/sdb1" successfully created.
    
    //查看物理卷的概要信息
    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree
      /dev/sda2  rhel lvm2 a--  <99.00g    0 
      /dev/sdb1       lvm2 ---    5.00g 5.00g
    
    //将磁盘sdc初始化为物理卷
    [root@localhost ~]# pvcreate /dev/sdc
      Physical volume "/dev/sdc" successfully created.
    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree 
      /dev/sda2  rhel lvm2 a--  <99.00g     0 
      /dev/sdb1       lvm2 ---    5.00g  5.00g
      /dev/sdc        lvm2 ---   20.00g 20.00g
    

    第三步:创建卷组,将物理卷加入卷组

    //创建一个名为vg0的卷组,并将分区sdb1加入vg0卷组
    [root@localhost ~]# vgcreate vg0 /dev/sdb1
      Volume group "vg0" successfully created
    
    //查看卷组的概要信息
    [root@localhost ~]# vgs
      VG   #PV #LV #SN Attr   VSize   VFree 
      rhel   1   3   0 wz--n- <99.00g     0 
      vg0    1   0   0 wz--n-  <5.00g <5.00g
    

    第四步:创建逻辑卷, 在卷组中划分逻辑卷

    ////创建一个4G的逻辑卷lv0,卷组为vg0
    [root@localhost ~]# lvcreate -n lv0 -L 4G vg0
      Logical volume "lv0" created.
    
    //查看逻辑卷的概要信息
    [root@localhost ~]# lvs
      LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      home rhel -wi-ao---- 46.95g                                                    
      root rhel -wi-ao---- 50.00g                                                    
      swap rhel -wi-ao----  2.04g                                                    
      lv0  vg0  -wi-a-----  4.00g   
    
    //检查逻辑卷
    [root@localhost ~]# lvscan
      ACTIVE            '/dev/vg0/lv0' [4.00 GiB] inherit
      ACTIVE            '/dev/rhel/swap' [2.04 GiB] inherit
      ACTIVE            '/dev/rhel/home' [46.95 GiB] inherit
      ACTIVE            '/dev/rhel/root' [50.00 GiB] inherit
    

    第五步:格式化逻辑卷

    //将逻辑卷lv0格式化成xfs格式
    [root@localhost ~]# mkfs.xfs /dev/vg0/lv0 
    meta-data=/dev/vg0/lv0           isize=512    agcount=4, agsize=262144 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=1, sparse=1, rmapbt=0
             =                       reflink=1
    data     =                       bsize=4096   blocks=1048576, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
    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
    
    //查看逻辑卷lv0的UUID
    [root@localhost ~]# blkid /dev/vg0/lv0
    /dev/mapper/vg0-lv0: UUID="a18169e8-6d51-450c-9b70-6329f7742936" TYPE="xfs"
    

    第六步:挂载并使用

    [root@localhost ~]# mkdir /lvm
    [root@localhost ~]# mount UUID="a18169e8-6d51-450c-9b70-6329f7742936" /lvm
    [root@localhost ~]# df -hT /dev/vg0/lv1
    Filesystem            Type      Size  Used Avail Use% Mounted on
    /dev/mapper/vg0-lv0   xfs       4.0G   61M  4.0G   2% /lvm
    [root@localhost ~]# cd /lvm
    [root@localhost lvm]# touch yqh
    [root@localhost lvm]# ll
    total 0
    -rw-r--r--. 1 root root 0 Dec  4 19:16 yqh
    
    //写配置文件,将lv0永久挂载
    UUID="a18169e8-6d51-450c-9b70-6329f7742936" /lvm xfs defaults 0 0
    
    //取消挂载
    [root@localhost ~]# umount /lvm
    
    //重新读取配置文件,检查是否成功
    [root@localhost ~]# mount -a
    [root@localhost ~]# df -hT /dev/vg0/lv1
    Filesystem            Type      Size  Used Avail Use% Mounted on
    /dev/mapper/vg0-lv0   xfs       4.0G   61M  4.0G   2% /lvm
    

    卷组扩展

    扩展卷组,将新磁盘加入卷组

    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree   
      /dev/sda2  rhel lvm2 a--  <99.00g       0 
      /dev/sdb1  vg0  lvm2 a--   <5.00g 1020.00m
      /dev/sdc        lvm2 ---   20.00g   20.00g
      
    //把磁盘sdc加入卷组vg0中
    [root@localhost ~]# vgextend vg0 /dev/sdc
      Volume group "vg0" successfully extended
    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree   
      /dev/sda2  rhel lvm2 a--  <99.00g       0 
      /dev/sdb1  vg0  lvm2 a--   <5.00g 1020.00m
      /dev/sdc   vg0  lvm2 a--  <20.00g  <20.00g
    
    //vg0从20G扩展到了25G
    [root@localhost ~]# vgs
      VG   #PV #LV #SN Attr   VSize   VFree 
      rhel   1   3   0 wz--n- <99.00g     0 
      vg0    2   1   0 wz--n-  24.99g 20.99g
    

    逻辑卷扩展

    逻辑卷扩展,逻辑卷的扩展取决于卷组中的容量,逻辑卷扩展的容量不能超过卷组的容量

    xfs扩展文件系统的逻辑卷扩展

    [root@localhost ~]# lvs
      LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      home rhel -wi-ao---- 46.95g                                                    
      root rhel -wi-ao---- 50.00g                                                    
      swap rhel -wi-ao----  2.04g                                                    
      lv0  vg0  -wi-a-----  4.00g   
    
    //将lv0扩展到10G(-L后可以用+6G也可以用10G)
    [root@localhost ~]# lvextend -L +6G /dev/vg0/lv0
      Size of logical volume vg0/lv0 changed from 4.00 GiB (1024 extents) to 10.00 GiB (2560 extents).
      Logical volume vg0/lv0 successfully resized.
    [root@localhost ~]# lvs
      LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      home rhel -wi-ao---- 46.95g                                                    
      root rhel -wi-ao---- 50.00g                                                    
      swap rhel -wi-ao----  2.04g                                                    
      lv0  vg0  -wi-ao---- 10.00g    
    
    //查看系统磁盘发现lv0还是4G没有变化
    [root@localhost ~]# df -hT /dev/vg0/lv0
    Filesystem            Type      Size  Used Avail Use% Mounted on
    /dev/mapper/vg0-lv0   xfs       4.0G   61M  4.0G   2% /lvm
    
    //xfs拉伸扩展
    [root@localhost ~]# xfs_growfs /dev/vg0/lv0
    meta-data=/dev/mapper/vg0-lv0    isize=512    agcount=4, agsize=262144 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=1, sparse=1, rmapbt=0
             =                       reflink=1
    data     =                       bsize=4096   blocks=1048576, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
    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
    data blocks changed from 1048576 to 2621440
    [root@localhost ~]# df -hT /dev/vg0/lv0
    Filesystem            Type      Size  Used Avail Use% Mounted on
    /dev/mapper/vg0-lv0   xfs        10G  105M  9.9G   2% /lvm
    

    分配磁盘池中百分之50给逻辑卷

    [root@localhost ~]# vgs
      VG   #PV #LV #SN Attr   VSize   VFree 
      rhel   1   3   0 wz--n- <99.00g     0 
      vg0    2   1   0 wz--n-  24.99g 14.99g
    [root@localhost ~]# lvextend -l +50%free /dev/vg0/lv0
      Size of logical volume vg0/lv0 changed from 10.00 GiB (2560 extents) to <17.50 GiB (4479 extents).
      Logical volume vg0/lv0 successfully resized.
    [root@localhost ~]# lvs
      LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      home rhel -wi-ao----  46.95g                                                    
      root rhel -wi-ao----  50.00g                                                    
      swap rhel -wi-ao----   2.04g                                                    
      lv0  vg0  -wi-ao---- <17.50g    
    
    //xfs拉伸扩展
    [root@localhost ~]# xfs_growfs /dev/vg0/lv0
    meta-data=/dev/mapper/vg0-lv0    isize=512    agcount=10, agsize=262144 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=1, sparse=1, rmapbt=0
             =                       reflink=1
    data     =                       bsize=4096   blocks=2621440, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
    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
    data blocks changed from 2621440 to 4586496
    [root@localhost ~]# df -hT /dev/vg0/lv0
    Filesystem            Type      Size  Used Avail Use% Mounted on
    /dev/mapper/vg0-lv0   xfs        18G  159M   18G   1% /lvm
    [root@localhost ~]# lsblk
    NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda             8:0    0  100G  0 disk 
    ├─sda1          8:1    0    1G  0 part /boot
    └─sda2          8:2    0   99G  0 part 
      ├─rhel-root 253:0    0   50G  0 lvm  /
      ├─rhel-swap 253:1    0    2G  0 lvm  [SWAP]
      └─rhel-home 253:2    0   47G  0 lvm  /home
    sdb             8:16   0   20G  0 disk 
    ├─sdb1          8:17   0    5G  0 part 
    │ └─vg0-lv0   253:3    0 17.5G  0 lvm  /lvm
    ├─sdb2          8:18   0    7G  0 part 
    └─sdb3          8:19   0    8G  0 part 
    sdc             8:32   0   20G  0 disk 
    └─vg0-lv0     253:3    0 17.5G  0 lvm  /lvm
    sdd             8:48   0   20G  0 disk 
    sr0            11:0    1 1024M  0 rom  
    

    ext4文件系统的逻辑卷扩展

    [root@localhost ~]# vgs
      VG   #PV #LV #SN Attr   VSize   VFree 
      rhel   1   3   0 wz--n- <99.00g     0 
      vg0    2   1   0 wz--n-  24.99g <7.50g
    
    //创建一个3G的逻辑卷lv1,卷组为vg0
    [root@localhost ~]# lvcreate -n lv1 -L 3G vg0
      Logical volume "lv1" created.
    [root@localhost ~]# lvs
      LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      home rhel -wi-ao----  46.95g                                                    
      root rhel -wi-ao----  50.00g                                                    
      swap rhel -wi-ao----   2.04g                                                    
      lv0  vg0  -wi-ao---- <17.50g                                                    
      lv1  vg0  -wi-a-----   3.00g    
      
    //将逻辑卷lv1格式化成xfs格式
    [root@localhost ~]# mkfs.ext4 /dev/vg0/lv1
    mke2fs 1.45.4 (23-Sep-2019)
    Creating filesystem with 786432 4k blocks and 196608 inodes
    Filesystem UUID: cb9d74f0-f461-4302-8597-edd39bd2d9d2
    Superblock backups stored on blocks: 
    	32768, 98304, 163840, 229376, 294912
    
    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (16384 blocks): done
    Writing superblocks and filesystem accounting information: done 
    
    [root@localhost ~]# lsblk
    NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda             8:0    0  100G  0 disk 
    ├─sda1          8:1    0    1G  0 part /boot
    └─sda2          8:2    0   99G  0 part 
      ├─rhel-root 253:0    0   50G  0 lvm  /
      ├─rhel-swap 253:1    0    2G  0 lvm  [SWAP]
      └─rhel-home 253:2    0   47G  0 lvm  /home
    sdb             8:16   0   20G  0 disk 
    ├─sdb1          8:17   0    5G  0 part 
    │ └─vg0-lv0   253:3    0 17.5G  0 lvm  /lvm
    ├─sdb2          8:18   0    7G  0 part 
    └─sdb3          8:19   0    8G  0 part 
    sdc             8:32   0   20G  0 disk 
    ├─vg0-lv0     253:3    0 17.5G  0 lvm  /lvm
    └─vg0-lv1     253:4    0    3G  0 lvm  
    sdd             8:48   0   20G  0 disk 
    sr0            11:0    1 1024M  0 rom 
    [root@localhost ~]# mkdir /ext4
    
    //查看逻辑卷lv1的UUID
    [root@localhost ~]# blkid /dev/vg0/lv1
    /dev/vg0/lv1: UUID="cb9d74f0-f461-4302-8597-edd39bd2d9d2" TYPE="ext4"
    
    //写永久挂载配置文件
    [root@localhost ~]# vi /etc/fstab 
    UUID="cb9d74f0-f461-4302-8597-edd39bd2d9d2" /ext4 ext4 defaults 0 0
    
    //读取挂载配置文件
    [root@localhost ~]# mount -a
    
    //将lv1扩容到6G
    [root@localhost ~]# lvextend -L 6G /dev/vg0/lv1
      Size of logical volume vg0/lv1 changed from 3.00 GiB (768 extents) to 6.00 GiB (1536 extents).
      Logical volume vg0/lv1 successfully resized.
    [root@localhost ~]# df -hT /dev/vg0/lv1
    Filesystem            Type      Size  Used Avail Use% Mounted on
    /dev/mapper/vg0-lv1   ext4      2.9G  9.0M  2.8G   1% /ext4
    
    //拉伸扩容
    [root@localhost ~]# resize2fs /dev/vg0/lv1
    resize2fs 1.45.4 (23-Sep-2019)
    Filesystem at /dev/vg0/lv1 is mounted on /ext4; on-line resizing required
    old_desc_blocks = 1, new_desc_blocks = 1
    The filesystem on /dev/vg0/lv1 is now 1572864 (4k) blocks long.
    
    [root@localhost ~]# df -hT /dev/vg0/lv1
    Filesystem            Type      Size  Used Avail Use% Mounted on
    /dev/mapper/vg0-lv1   ext4      5.9G   12M  5.6G   1% /ext4
    

    数据迁移卷组

    同一卷组的磁盘才可以进行在线迁移

    小拓展:请问什么情况下需要迁移数据?

    答:当硬盘坏了不能写入数据,要拿去修的时候,需要迁移数据。

    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree 
      /dev/sda2  rhel lvm2 a--  <99.00g     0 
      /dev/sdb1  vg0  lvm2 a--   <5.00g     0 
      /dev/sdc   vg0  lvm2 a--  <20.00g <1.50g
    
    //将sdd创建成物理卷
    [root@localhost ~]# pvcreate /dev/sdd
      Physical volume "/dev/sdd" successfully created.
    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree 
      /dev/sda2  rhel lvm2 a--  <99.00g     0 
      /dev/sdb1  vg0  lvm2 a--   <5.00g     0 
      /dev/sdc   vg0  lvm2 a--  <20.00g <1.50g
      /dev/sdd        lvm2 ---   20.00g 20.00g
    
    //将sdd加入卷组vg0中
    [root@localhost ~]# vgextend vg0 /dev/sdd
      Volume group "vg0" successfully extended
    [root@localhost ~]# vgs
      VG   #PV #LV #SN Attr   VSize   VFree 
      rhel   1   3   0 wz--n- <99.00g     0 
      vg0    3   2   0 wz--n- <44.99g 21.49g
    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree  
      /dev/sda2  rhel lvm2 a--  <99.00g      0 
      /dev/sdb1  vg0  lvm2 a--   <5.00g      0 
      /dev/sdc   vg0  lvm2 a--  <20.00g  <1.50g
      /dev/sdd   vg0  lvm2 a--  <20.00g <20.00g
    
    //pvmove在线数据将sdb1迁移至其他pv(这里由于只有sdd有空间,所以将数据默认移到sdd里去了)
    [root@localhost ~]# pvmove /dev/sdb1
      /dev/sdb1: Moved: 0.23%
      /dev/sdb1: Moved: 100.00%
    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree 
      /dev/sda2  rhel lvm2 a--  <99.00g     0 
      /dev/sdb1  vg0  lvm2 a--   <5.00g <5.00g
      /dev/sdc   vg0  lvm2 a--  <20.00g <1.50g
      /dev/sdd   vg0  lvm2 a--  <20.00g 15.00g
    
    //将sdb1从卷组vg0中移除
    [root@localhost ~]# vgreduce vg0 /dev/sdb1
      Removed "/dev/sdb1" from volume group "vg0"
    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree 
      /dev/sda2  rhel lvm2 a--  <99.00g     0 
      /dev/sdb1       lvm2 ---    5.00g  5.00g
      /dev/sdc   vg0  lvm2 a--  <20.00g <1.50g
      /dev/sdd   vg0  lvm2 a--  <20.00g 15.00g
    
    //将sdb1从物理卷中移除
    [root@localhost ~]# pvremove /dev/sdb1
      Labels on physical volume "/dev/sdb1" successfully wiped.
    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree 
      /dev/sda2  rhel lvm2 a--  <99.00g     0 
      /dev/sdc   vg0  lvm2 a--  <20.00g <1.50g
      /dev/sdd   vg0  lvm2 a--  <20.00g 15.00g
    [root@localhost ~]# lsblk
    NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda             8:0    0  100G  0 disk 
    ├─sda1          8:1    0    1G  0 part /boot
    └─sda2          8:2    0   99G  0 part 
      ├─rhel-root 253:0    0   50G  0 lvm  /
      ├─rhel-swap 253:1    0    2G  0 lvm  [SWAP]
      └─rhel-home 253:2    0   47G  0 lvm  /home
    sdb             8:16   0   20G  0 disk 
    ├─sdb1          8:17   0    5G  0 part 
    ├─sdb2          8:18   0    7G  0 part 
    └─sdb3          8:19   0    8G  0 part 
    sdc             8:32   0   20G  0 disk 
    ├─vg0-lv0     253:3    0 17.5G  0 lvm  /lvm
    └─vg0-lv1     253:4    0    6G  0 lvm  /ext4
    sdd             8:48   0   20G  0 disk 
    └─vg0-lv0     253:3    0 17.5G  0 lvm  /lvm
    sr0            11:0    1 1024M  0 rom  
    

    小扩展:如何把sdb3中的7G空间扩容到root中?

    //将sdb3创建成为物理卷
    [root@localhost ~]# pvcreate /dev/sdb3
      Physical volume "/dev/sdb3" successfully created.
    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree 
      /dev/sda2  rhel lvm2 a--  <99.00g     0 
      /dev/sdb3       lvm2 ---   <8.00g <8.00g
      /dev/sdc   vg0  lvm2 a--  <20.00g <1.50g
      /dev/sdd   vg0  lvm2 a--  <20.00g 15.00g
    
    //将物理卷sdb3加入到rhel卷组中
    [root@localhost ~]# vgextend rhel /dev/sdb3
      Volume group "rhel" successfully extended
    [root@localhost ~]# pvs
      PV         VG   Fmt  Attr PSize   PFree 
      /dev/sda2  rhel lvm2 a--  <99.00g     0 
      /dev/sdb3  rhel lvm2 a--   <8.00g <8.00g
      /dev/sdc   vg0  lvm2 a--  <20.00g <1.50g
      /dev/sdd   vg0  lvm2 a--  <20.00g 15.00g
    [root@localhost ~]# vgs
      VG   #PV #LV #SN Attr   VSize   VFree  
      rhel   2   3   0 wz--n- 106.99g  <8.00g
      vg0    2   2   0 wz--n-  39.99g <16.50g
    [root@localhost ~]# lvs
      LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      home rhel -wi-ao----  46.95g                                                    
      root rhel -wi-ao----  50.00g                                                    
      swap rhel -wi-ao----   2.04g                                                    
      lv0  vg0  -wi-ao---- <17.50g                                                    
      lv1  vg0  -wi-ao----   6.00g                                                    
    
    //将root扩容加7G
    [root@localhost ~]# lvextend -L +7G /dev/rhel/root
      Size of logical volume rhel/root changed from 50.00 GiB (12800 extents) to 57.00 GiB (14592 extents).
      Logical volume rhel/root successfully resized.
    [root@localhost ~]# lvs
      LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      home rhel -wi-ao----  46.95g                                                    
      root rhel -wi-ao----  57.00g                                                    
      swap rhel -wi-ao----   2.04g                                                    
      lv0  vg0  -wi-ao---- <17.50g                                                    
      lv1  vg0  -wi-ao----   6.00g  
    [root@localhost ~]# df -hT /dev/rhel/root 
    Filesystem            Type  Size  Used Avail Use% Mounted on
    /dev/mapper/rhel-root xfs    50G  1.7G   49G   4% /
    
    //xfs拉伸扩容
    [root@localhost ~]# xfs_growfs /dev/rhel/root 
    meta-data=/dev/mapper/rhel-root  isize=512    agcount=4, agsize=3276800 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=1, sparse=1, rmapbt=0
             =                       reflink=1
    data     =                       bsize=4096   blocks=13107200, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
    log      =internal log           bsize=4096   blocks=6400, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 13107200 to 14942208
    [root@localhost ~]# df -hT /dev/rhel/root 
    Filesystem            Type  Size  Used Avail Use% Mounted on
    /dev/mapper/rhel-root xfs    57G  1.7G   56G   3% /
    

    逻辑卷裁剪

    只能对ext4文件系统的逻辑卷进行裁剪容量

    xfs文件系统的逻辑卷不能进行裁剪

    准备工作:

    //这里用lv1进行演示,添加点文件看剪裁后会不会损坏
    [root@localhost ~]# df -hT /dev/vg0/lv1
    Filesystem            Type      Size  Used Avail Use% Mounted on
    /dev/mapper/vg0-lv1   ext4      5.9G   12M  5.6G   1% /ext4
    [root@localhost ~]# cd /ext4/
    [root@localhost ext4]# echo 'hello yqh' > abc
    [root@localhost ext4]# cp /etc/passwd .
    [root@localhost ext4]# ls
    abc  lost+found  passwd
    [root@localhost ext4]# cd
    [root@localhost ~]# du -sh /ext4/
    28K	/ext4/
    

    第一步:如果已经挂载,必须先卸载

    [root@localhost ~]# umount /ext4
    

    第二步:检测文件系统

    [root@localhost ~]# e2fsck -f /dev/vg0/lv1
    e2fsck 1.45.4 (23-Sep-2019)
    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/vg0/lv1: 13/393216 files (0.0% non-contiguous), 44144/1572864 blocks
    
    //调整裁剪大小,检测是否能够裁剪
    [root@localhost ~]# resize2fs /dev/vg0/lv1 200M
    resize2fs 1.45.4 (23-Sep-2019)
    Resizing the filesystem on /dev/vg0/lv1 to 51200 (4k) blocks.
    The filesystem on /dev/vg0/lv1 is now 51200 (4k) blocks long.
    

    第三步:裁剪逻辑卷空间

    [root@localhost ~]# lvreduce -L 200M /dev/vg0/lv1
      WARNING: Reducing active logical volume to 200.00 MiB.
      THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce vg0/lv1? [y/n]: y
      Size of logical volume vg0/lv1 changed from 6.00 GiB (1536 extents) to 200.00 MiB (50 extents).
      Logical volume vg0/lv1 successfully resized.
    [root@localhost ~]# lvs
      LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      home rhel -wi-ao----  46.95g                                                    
      root rhel -wi-ao----  57.00g                                                    
      swap rhel -wi-ao----   2.04g                                                    
      lv0  vg0  -wi-ao---- <17.50g                                                    
      lv1  vg0  -wi-a----- 200.00m           
    

    第四步:再次检测文件系统

    [root@localhost ~]# e2fsck -f /dev/vg0/lv1
    e2fsck 1.45.4 (23-Sep-2019)
    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/vg0/lv1: 13/16384 files (0.0% non-contiguous), 18190/51200 blocks
    

    第五步:挂载测试

    [root@localhost ~]# blkid /dev/vg0/lv1
    /dev/vg0/lv1: UUID="cb9d74f0-f461-4302-8597-edd39bd2d9d2" TYPE="ext4"
    [root@localhost ~]# vi /etc/fstab 
    UUID="cb9d74f0-f461-4302-8597-edd39bd2d9d2" /ext4 ext4 defaults 0 0
    [root@localhost ~]# mount -a
    [root@localhost ~]# df -hT /dev/vg0/lv1
    Filesystem          Type  Size  Used Avail Use% Mounted on
    /dev/mapper/vg0-lv1 ext4  132M  3.1M  116M   3% /ext4
    [root@localhost ~]# cd /ext4/
    [root@localhost ext4]# ls
    abc  lost+found  passwd
    [root@localhost ext4]# cat abc 
    hello yqh
    [root@localhost ext4]# ll passwd /etc/passwd
    -rw-r--r--. 1 root root 1106 Dec  4 15:11 /etc/passwd
    -rw-r--r--. 1 root root 1106 Dec  4 21:43 passwd
    

    缩减逻辑卷注意事项:

    1. 不能在线缩减,得先卸载;
    2. 确保缩减后的空间大小依然能存储原有的所有数据;
    3. 在缩减之前应该先强行检查文件,以确保文件系统处于一致性状态。

    查看详细信息

    pvdisplay 查看物理卷详细信息

    [root@localhost ~]# pvdisplay 
      --- Physical volume ---
      PV Name               /dev/sdc
      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               1869
      Allocated PE          3250
      PV UUID               XIfvPh-uen9-C4NP-JNIg-n24Q-Ufbo-CpN0M0
       
      --- Physical volume ---
      PV Name               /dev/sdd
      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               3840
      Allocated PE          1279
      PV UUID               77Qm2h-80g9-YAG0-AG0i-PSuJ-BfLV-d8AHaJ
       
      --- Physical volume ---
      PV Name               /dev/sda2
      VG Name               rhel
      PV Size               <99.00 GiB / not usable 3.00 MiB
      Allocatable           yes (but full)
      PE Size               4.00 MiB
      Total PE              25343
      Free PE               0
      Allocated PE          25343
      PV UUID               KOdE0D-yQe5-Sgg0-R6j0-eO82-orSM-1uRLU0
       
      --- Physical volume ---
      PV Name               /dev/sdb3
      VG Name               rhel
      PV Size               <8.00 GiB / not usable 3.00 MiB
      Allocatable           yes 
      PE Size               4.00 MiB
      Total PE              2047
      Free PE               255
      Allocated PE          1792
      PV UUID               s4hjIw-A6lo-w47q-KWut-8uHb-CuRr-2mqigB
    

    vgdisplay 查看卷组详细信息

    [root@localhost ~]# vgdisplay
      --- Volume group ---
      VG Name               vg0
      System ID             
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  13
      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               39.99 GiB
      PE Size               4.00 MiB
      Total PE              10238
      Alloc PE / Size       4529 / 17.69 GiB
      Free  PE / Size       5709 / 22.30 GiB
      VG UUID               0iMBSz-enXW-41Ht-l0Uw-s0FJ-IHKd-huxaI5
       
      --- Volume group ---
      VG Name               rhel
      System ID             
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  6
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                3
      Open LV               3
      Max PV                0
      Cur PV                2
      Act PV                2
      VG Size               106.99 GiB
      PE Size               4.00 MiB
      Total PE              27390
      Alloc PE / Size       27135 / <106.00 GiB
      Free  PE / Size       255 / 1020.00 MiB
      VG UUID               tWcoeV-G0iX-sog6-uncL-LDQf-MuDp-hMiMmv
    

    lvdisplay 查看逻辑卷详细信息

    [root@localhost ~]# lvdisplay
      --- Logical volume ---
      LV Path                /dev/vg0/lv0
      LV Name                lv0
      VG Name                vg0
      LV UUID                djEzeX-uBe4-GmcL-7Fo1-PkDP-7qei-nSXjL4
      LV Write Access        read/write
      LV Creation host, time localhost.localdomain, 2020-12-04 19:15:16 +0800
      LV Status              available
      # open                 1
      LV Size                <17.50 GiB
      Current LE             4479
      Segments               2
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:3
       
      --- Logical volume ---
      LV Path                /dev/vg0/lv1
      LV Name                lv1
      VG Name                vg0
      LV UUID                q7smcB-UJdz-MtQ5-AVao-Snrv-jCH9-JirHNO
      LV Write Access        read/write
      LV Creation host, time localhost.localdomain, 2020-12-04 19:23:23 +0800
      LV Status              available
      # open                 1
      LV Size                200.00 MiB
      Current LE             50
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:4
       
      --- Logical volume ---
      LV Path                /dev/rhel/swap
      LV Name                swap
      VG Name                rhel
      LV UUID                snKxjg-XAyh-k024-MUYE-3oGN-690L-AlsVcK
      LV Write Access        read/write
      LV Creation host, time localhost.localdomain, 2020-12-04 15:09:59 +0800
      LV Status              available
      # open                 2
      LV Size                2.04 GiB
      Current LE             523
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:1
       
      --- Logical volume ---
      LV Path                /dev/rhel/home
      LV Name                home
      VG Name                rhel
      LV UUID                QSobf6-pvbi-044D-7EK4-FCix-ReLr-3TscQE
      LV Write Access        read/write
      LV Creation host, time localhost.localdomain, 2020-12-04 15:09:59 +0800
      LV Status              available
      # open                 1
      LV Size                46.95 GiB
      Current LE             12020
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:2
       
      --- Logical volume ---
      LV Path                /dev/rhel/root
      LV Name                root
      VG Name                rhel
      LV UUID                CybXr1-7or4-fHo8-hhtg-eSIx-0rOO-AVl3kO
      LV Write Access        read/write
      LV Creation host, time localhost.localdomain, 2020-12-04 15:10:00 +0800
      LV Status              available
      # open                 1
      LV Size                57.00 GiB
      Current LE             14592
      Segments               2
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:0
    
  • 相关阅读:
    声明
    Random类——获取随机数
    JDK_API的使用方法
    Ajax?有谁开始学习了吗?
    用xslt循环xml同一节点的不同子节点
    在Repeater控件中嵌套一个Repeater控件
    html+css的一些技巧.收集中...
    记录一下: onbeforeunload()方法, 避免未保存而关闭页面.
    简单的C# Socket编程
    不实用的UriBuilder类
  • 原文地址:https://www.cnblogs.com/yuqinghao/p/14091425.html
Copyright © 2011-2022 走看看