逻辑卷LVM

一、基本概念

逻辑卷的概念:逻辑卷是Linux环境中对磁盘分区进行管理的一种机制,是建立在硬盘和分区之上,文件系统之下的一个逻辑层,可以提高磁盘分区管理的灵活性。

单点故障:单点故障,是指引发系统失效的单项故障,会给整个系统带来灾难性的破坏。 和单个故障是看似相似实为不同的两个词语。 单个故障则是不会引发整个系统失效的单项故障。 打个比方,一部正在放映的电影,如果出现放映机短路而导致观影结束,放映机短路就是单点故障。

二、创建逻辑卷

1 添加新磁盘

SATA 不需要重启

SCSI 需要重启

制作lvm的时候可以是一整块硬盘也可以是硬盘分好的区并且是还没有用的。必须是没有做文件系统的

[root@zuoye ~]# ll -d /dev/sd*
brw-rw---- 1 root disk 8,  0 9月   6 10:41 /dev/sda
brw-rw---- 1 root disk 8,  1 9月   6 10:41 /dev/sda1
brw-rw---- 1 root disk 8,  2 9月   6 10:41 /dev/sda2
brw-rw---- 1 root disk 8, 16 9月   6 14:51 /dev/sdb
brw-rw---- 1 root disk 8, 32 9月   6 14:51 /dev/sdc

2 创建物理卷pv

  2.1 创建pv

pvcreate  磁盘

[root@zuoye ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

2.2查看pv

[root@zuoye ~]# pvs   #方法一
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdb          lvm2 ---   10.00g 10.00g
[root@zuoye ~]# pvscan  #方法二
  PV /dev/sda2   VG centos          lvm2 [<19.00 GiB / 0    free]
  PV /dev/sdb                       lvm2 [10.00 GiB]
  Total: 2 [<29.00 GiB] / in use: 1 [<19.00 GiB] / in no VG: 1 [10.00 GiB]
[root@zuoye ~]# pvdisplay  #方法三
  "/dev/sdb" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               8LsEDw-v8gU-cDkP-hpx1-nY39-T8Ic-a3vcRN
   

3 创建卷组vg

  3.1 创建vg

[root@zuoye ~]# vgcreate vg1 /dev/sdb
  Volume group "vg1" successfully created

  3.2查看vg

[root@zuoye ~]# vgs       方法一
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <19.00g      0 
  vg1      1   0   0 wz--n- <10.00g <10.00g
[root@zuoye ~]# vgscan    方法二
  Reading volume groups from cache.
  Found volume group "vg1" using metadata type lvm2
  Found volume group "centos" using metadata type lvm2
[root@zuoye ~]# vgdisplay 方法三
  --- Volume group ---
  VG Name               vg1
  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               <10.00 GiB
  PE Size               4.00 MiB
  Total PE              2559
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2559 / <10.00 GiB
  VG UUID               EXLlqg-pEwQ-WQ7y-nu9w-beSK-Xv75-vW1yYt

4创建逻辑卷

4.1 创建逻辑卷

lvcreate -L 大小 -n lv1 vg1

[root@zuoye ~]# lvcreate -L +2G -n lv1 vg1
  Logical volume "lv1" created.
[root@zuoye ~]# lvcreate -L +2G -n lv2 vg1
 Logical volume "lv2" created.

2.4.2查看逻辑卷

[root@zuoye ~]# lvs     方法一
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <17.00g                                                    
  swap centos -wi-ao----   2.00g                                                    
  lv1  vg1    -wi-a-----   2.00g                                                    
  lv2  vg1    -wi-a-----   2.00g                                                    
[root@zuoye ~]# lvscan        方法二
  ACTIVE            '/dev/vg1/lv1' [2.00 GiB] inherit
  ACTIVE            '/dev/vg1/lv2' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/root' [<17.00 GiB] inherit
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
[root@zuoye ~]# lvdisplay     方法三
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                0SQpPI-h4vU-gDoY-HWJu-o89h-W2A1-FWOsfR
  LV Write Access        read/write
  LV Creation host, time zuoye, 2023-09-06 15:20:16 +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
   
  --- Logical volume ---
  LV Path                /dev/vg1/lv2
  LV Name                lv2
  VG Name                vg1
  LV UUID                8Q1YHH-Lqn4-vWku-PM3W-CVka-NSzQ-cohPje
  LV Write Access        read/write
  LV Creation host, time zuoye, 2023-09-06 15:23:23 +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:3

5.制作系统文件

  5.1 制作xfs的系统文件

[root@zuoye ~]# mkfs.xfs /dev/vg1/lv1
meta-data=/dev/vg1/lv1           isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=524288, 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

  5.2 制作ext4的系统文件

[root@zuoye ~]# mkfs.ext4 /dev/vg1/lv2
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成 

 6 挂载

  6.1 创建挂载点

[root@zuoye ~]# mkdir /mnt/lv1
[root@zuoye ~]# mkdir /mnt/lv2

   6.2挂载

[root@zuoye ~]# mount /dev/vg1/lv1  /mnt/lv1
[root@zuoye ~]# mount /dev/vg1/lv2  /mnt/lv2
[root@zuoye ~]# df -Th
文件系统                类型      容量  已用  可用 已用% 挂载点
devtmpfs                devtmpfs  475M     0  475M    0% /dev
tmpfs                   tmpfs     487M     0  487M    0% /dev/shm
tmpfs                   tmpfs     487M  7.7M  479M    2% /run
tmpfs                   tmpfs     487M     0  487M    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        17G  1.7G   16G   10% /
/dev/sda1               xfs      1014M  137M  878M   14% /boot
tmpfs                   tmpfs      98M     0   98M    0% /run/user/0
/dev/mapper/vg1-lv1     xfs       2.0G   33M  2.0G    2% /mnt/lv1
/dev/mapper/vg1-lv2     ext4      2.0G  6.0M  1.8G    1% /mnt/lv2

6.3 自动挂载

[root@zuoye ~]# vim /etc/fstab
/dev/vg1/lv1    /mnt/lv1    xfs     defaults    0 0
/dev/vg1/lv2    /mnt/lv2    ext4    defaults    0 0
[root@zuoye ~]# mount -a

三、扩容:

3.1 给lv扩容

若vg有足够的容量,直接从vg扩容

[root@zuoye ~]# lvextend -L +1G  /dev/vg1/lv1
  Size of logical volume vg1/lv1 changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents).
  Logical volume vg1/lv1 successfully resized.
[root@zuoye ~]# lvextend -L +2G  /dev/vg1/lv2
  Size of logical volume vg1/lv2 changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
  Logical volume vg1/lv2 successfully resized.

扩容后查看

[root@zuoye ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <17.00g                                                    
  swap centos -wi-ao----   2.00g                                                    
  lv1  vg1    -wi-ao----   3.00g                                                    
  lv2  vg1    -wi-ao----   4.00g      

xfs的文件系统扩容

[root@zuoye ~]# xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1    isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=524288, 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 524288 to 786432

ext4的文件系统扩容

[root@zuoye ~]# resize2fs /dev/vg1/lv2
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg1/lv2 is mounted on /mnt/lv2; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg1/lv2 is now 1048576 blocks long.

文件系统扩容后查看

[root@zuoye ~]# df -Th
文件系统                类型      容量  已用  可用 已用% 挂载点
devtmpfs                devtmpfs  475M     0  475M    0% /dev
tmpfs                   tmpfs     487M     0  487M    0% /dev/shm
tmpfs                   tmpfs     487M  7.7M  479M    2% /run
tmpfs                   tmpfs     487M     0  487M    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        17G  1.7G   16G   10% /
/dev/sda1               xfs      1014M  137M  878M   14% /boot
tmpfs                   tmpfs      98M     0   98M    0% /run/user/0
/dev/mapper/vg1-lv1     xfs       3.0G   33M  3.0G    2% /mnt/lv1
/dev/mapper/vg1-lv2     ext4      3.9G  8.0M  3.7G    1% /mnt/lv2

3.2给物理卷pv和卷组gv扩容

[root@zuoye ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created.
[root@zuoye ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdb   vg1    lvm2 a--  <10.00g <3.00g
  /dev/sdc          lvm2 ---    8.00g  8.00g
[root@zuoye ~]# vgextend vg1  /dev/sdc
  Volume group "vg1" successfully extended
[root@zuoye ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree 
  centos   1   2   0 wz--n- <19.00g     0 
  vg1      2   2   0 wz--n-  17.99g 10.99g

四、给ext4文件缩容

  1. 备份数据: 在开始缩小逻辑卷之前,首先备份其中的所有数据,以防止数据丢失。

  2. 卸载逻辑卷: 如果逻辑卷包含操作系统的根目录或者已挂载的其他重要目录,需要在进入单用户模式下或使用 Live CD 环境下卸载该逻辑卷。这是因为无法在线缩小当前正在使用的逻辑卷。

  3. 卸载文件系统: 在缩小逻辑卷之前,确保已卸载文件系统。如果是 ext2/ext3/ext4 文件系统,可以使用以下命令:

[root@zuoye ~]# umount /mnt/lv2

    4.检查文件系统: 在缩小逻辑卷之前,最好使用文件系统检查工具检查文件系统是否有错误。          对于 ext2/ext3/ext4 文件系统,可以运行

[root@zuoye ~]# e2fsck -f /dev/vg1/lv2
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv2: 11/262144 files (0.0% non-contiguous), 34894/1048576 blocks

5.  缩小逻辑卷: 使用 lvresize 命令来缩小逻辑卷。例如,假设你要将逻辑卷 /dev/vg1/lv12缩小到 2G,可以运行:lvresize --resizefs --size 2G /dev/vg1/lv2

[root@zuoye ~]# lvresize --resizefs --size 2G /dev/vg1/lv2
fsck,来自 util-linux 2.23.2
/dev/mapper/vg1-lv2: clean, 11/262144 files, 34894/1048576 blocks
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vg1-lv2 to 524288 (4k) blocks.
The filesystem on /dev/mapper/vg1-lv2 is now 524288 blocks long.

  Size of logical volume vg1/lv2 changed from 4.00 GiB (1024 extents) to 2.00 GiB (512 extents).
  Logical volume vg1/lv2 successfully resized.

6.重新挂载逻辑卷: 如果你在步骤2中卸载了逻辑卷,现在可以重新挂载它

[root@zuoye ~]# mount /dev/vg1/lv2 /mnt/lv2

7.验证操作: 确认文件系统和逻辑卷已成功缩小到所需大小

[root@zuoye ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 475M     0  475M    0% /dev
tmpfs                    487M     0  487M    0% /dev/shm
tmpfs                    487M  7.7M  479M    2% /run
tmpfs                    487M     0  487M    0% /sys/fs/cgroup
/dev/mapper/centos-root   17G  1.7G   16G   10% /
/dev/sda1               1014M  137M  878M   14% /boot
tmpfs                     98M     0   98M    0% /run/user/0
/dev/mapper/vg1-lv1      3.0G   33M  3.0G    2% /mnt/lv1
/dev/mapper/vg1-lv2      2.0G  6.0M  1.8G    1% /mnt/lv2

五、删除逻辑卷

1、取消挂载

[root@zuoye ~]# umount /mnt/lv1
[root@zuoye ~]# umount /mnt/lv2

2、删除配置文件

vim /etc/fstab

3、删除逻辑卷lv

[root@zuoye ~]# lvremove /dev/vg1/lv1
Do you really want to remove active logical volume vg1/lv1? [y/n]: y
  Logical volume "lv1" successfully removed
[root@zuoye ~]# lvremove /dev/vg1/lv2
Do you really want to remove active logical volume vg1/lv2? [y/n]: y
  Logical volume "lv2" successfully removed
[root@zuoye ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <17.00g                                                    
  swap centos -wi-ao----   2.00g     

4.删除卷组

[root@zuoye ~]# vgremove /dev/vg1
  Volume group "vg1" successfully removed
[root@zuoye ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <19.00g    0 

5.删除物理卷

[root@zuoye ~]# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped.
[root@zuoye ~]# pvremove /dev/sdc
  Labels on physical volume "/dev/sdc" successfully wiped.
[root@zuoye ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <19.00g    0 

你可能感兴趣的:(数据库)