Linux基础-逻辑卷管理器

2022/2/14 7:18:23

本文主要是介绍Linux基础-逻辑卷管理器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

LVM(逻辑卷管理器)

逻辑卷管理器是Linux系统用于对硬盘分区进行管理的一种机制,理论性较强,其创建初衷是为了解决硬盘设备在创建分区后不易修改分区大小的缺陷。尽管对传统的硬盘分区进行强制扩容或缩容从理论上来讲是可行的,但是却可能造成数据的丢失。而LVM技术是在硬盘分区和文件系统之间添加了一个逻辑层,它提供了一个抽象的卷组,可以把多块硬盘进行卷组合并。这样一来,用户不必关心物理硬盘设备的底层架构和布局,就可以实现对硬盘分区的动态调整。

物理卷处于LVM中的最底层,可以将其理解为物理硬盘、硬盘分区或者RAID磁盘阵列,这都可以。卷组建立在物理卷之上,一个卷组可以包含多个物理卷,而且在卷组创建之后也可以继续向其中添加新的物理卷。逻辑卷是用卷组中空闲的资源建立的,并且逻辑卷在建立后可以动态地扩展或缩小空间。这就是LVM的核心理念。

部署逻辑卷

常用的LVM部署命令

功能/命令

物理卷管理

卷组管理

逻辑卷管理

扫描

pvscan

vgscan

lvscan

建立

pvcreate

vgcreate

lvcreate

显示

pvdisplay

vgdisplay

lvdisplay

删除

pvremove

vgremove

lvremove

扩展

 

vgextend

lvextend

缩小

 

vgreduce

lvreduce

 

我们在日常工作中,对卷组的管理是必须要掌握的

logical  volume manager   

是linux环境下,对磁盘分区进行管理的一个机制,保证零停机前提下,对文件系统的大小进行调整,实现文件跨越不同的磁盘和分区,LVM提供了一个完美的解决方案

 

 

 

专业术语:

PP:物理分区   用来存储数据的块设备,可以是分区,可以是磁盘,RAID,SAN

PV:物理卷    LVM的基础逻辑块,但是和基本的物理介质(磁盘,分区)不同,包含了lvm的管理参数

 

VG:卷组:由一个或多个物理卷组成

LV:逻辑卷,LVM的逻辑卷类似于非LVM中的硬盘分区,lv就可以建立文件系统了,基于lv来建立文件系统文件系统,mkfs来格式化这个/dev/sda 最后变成一个个不同的文件系统

 

 

PE:每一个物理卷被划分为PE基本单元,是lvm最小的存储单元,具有唯一编号,pe大小可以手动制动,默认4MB

LE:逻辑卷也被划分成le,可被寻址的基本单元,在同一个卷组中,PE和LE大小一定是相同的,而且要一一对应。  说白了就是把pe一个一个映射成le

 

逻辑卷管理实验

  1. 普通逻辑卷创建

首先在服务器上添加一块硬盘  

   

lsblk 

NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sr0            11:0    1  6.6G  0 rom  /run/media/root/RHEL-8-0-0-BaseOS-x86_64

nvme0n1       259:0    0   20G  0 disk 

├─nvme0n1p1   259:1    0    1G  0 part /boot

└─nvme0n1p2   259:2    0   19G  0 part 

  ├─rhel-root 253:0    0   17G  0 lvm  /

  └─rhel-swap 253:1    0    2G  0 lvm  [SWAP]

nvme0n2       259:3    0   20G  0 disk

 

接下来把硬盘分区 分成一个5G大小的物理分区

fdisk  /dev/nvme0n2 

 

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 0x25169c5c.

 

Command (m for help): n

Partition type

   p   primary (0 primary, 0 extended, 4 free)

   e   extended (container for logical partitions)

Select (default p): p

Partition number (1-4, default 1): 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): p

Disk /dev/nvme0n2: 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: 0x25169c5c

 

Device         Boot Start      End  Sectors Size Id Type

/dev/nvme0n2p1       2048 10487807 10485760   5G 83 Linux

 

Command (m for help): w

The partition table has been altered.

Calling ioctl() to re-read partition table.

Syncing disks.

 

接下来查看分区结构

[root@example ~]# lsblk 

NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sr0            11:0    1  6.6G  0 rom  /run/media/root/RHEL-8-0-0-BaseOS-x86_64

nvme0n1       259:0    0   20G  0 disk 

├─nvme0n1p1   259:1    0    1G  0 part /boot

└─nvme0n1p2   259:2    0   19G  0 part 

  ├─rhel-root 253:0    0   17G  0 lvm  /

  └─rhel-swap 253:1    0    2G  0 lvm  [SWAP]

nvme0n2       259:3    0   20G  0 disk 

└─nvme0n2p1   259:5    0    5G  0 part 

 

 

[root@rhel8-server ~]# pvcreate /dev/nvme0n2p1 

  Physical volume "/dev/nvme0n2p1" successfully created.

[root@rhel8-server ~]# pvdisplay /dev/nvme0n2p1 

  "/dev/nvme0n2p1" is a new physical volume of "5.00 GiB"

  --- NEW Physical volume ---

  PV Name               /dev/nvme0n2p1

  VG Name               

  PV Size               5.00 GiB

  Allocatable           NO

  PE Size               0   

  Total PE              0

  Free PE               0

  Allocated PE          0

  PV UUID               SnoSyc-5chx-BXiE-5Uj0-Kbv5-qeVN-ojmzbr

 

[root@rhel8-server ~]# vgcreate   -s  4M     testvg   /dev/nvme0n2p1 

  Volume group "testvg" successfully created

[root@rhel8-server ~]# vgdisplay   testvg 

  --- Volume group ---

  VG Name               testvg

  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               <5.00 GiB

  PE Size               4.00 MiB

  Total PE              1279

  Alloc PE / Size       0 / 0   

  Free  PE / Size       1279 / <5.00 GiB

  VG UUID               zNBk30-0L0P-3S1f-AjqM-iPwq-ttAY-pPLAWH

 

 

[root@rhel8-server ~]# lvcreate   -l   1024    -n testlv      testvg

  Logical volume "testlv" created.

[root@rhel8-server ~]# lvdisplay /dev/testvg/testlv 

  --- Logical volume ---

  LV Path                /dev/testvg/testlv

  LV Name                testlv

  VG Name                testvg

  LV UUID                3wtCKQ-BBqQ-lFd3-gCRY-E0aC-tDtC-Imt6pI

  LV Write Access        read/write

  LV Creation host, time rhel8-server, 2022-01-18 00:59:29 -0500

  LV Status              available

  # open                 0

  LV Size                4.00 GiB

  Current LE             1024

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:2

 

做文件系统

[root@rhel8-server ~]# mkfs.ext4   /dev/mapper/testvg-testlv 

mke2fs 1.44.3 (10-July-2018)

Creating filesystem with 1048576 4k blocks and 262144 inodes

Filesystem UUID: b64f2471-3dc5-4e51-892e-ff64571ed986

Superblock backups stored on blocks: 

32768, 98304, 163840, 229376, 294912, 819200, 884736

 

Allocating group tables: done                            

Writing inode tables: done                            

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

 

挂载

[root@rhel8-server ~]# mkdir  /volume1  

[root@rhel8-server ~]# vim   /etc/fstab

编辑内容:

#

# /etc/fstab

# Created by anaconda on Fri Jan  7 22:26:04 2022

#

# 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.

#

# After editing this file, run 'systemctl daemon-reload' to update systemd

# units generated from this file.

#

/dev/mapper/rhel-root   /                       xfs     defaults        0 0

UUID=7002282d-fea0-4380-9a2b-6495aba65ee6 /boot                   xfs     defaults        0 0

/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

/dev/mapper/testvg-testlv     /volume1           ext4    defaults        0 0

[root@rhel8-server ~]# mount -a       这一步如果有问题 开机会进入rescure模式,一定要小心

[root@rhel8-server ~]# df    -hT

Filesystem                Type      Size  Used Avail Use% Mounted on

devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev

tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm

tmpfs                     tmpfs     1.9G   18M  1.9G   1% /run

tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup

/dev/mapper/rhel-root     xfs        17G  4.0G   14G  24% /

/dev/nvme0n1p1            xfs      1014M  169M  846M  17% /boot

tmpfs                     tmpfs     376M   16K  376M   1% /run/user/42

tmpfs                     tmpfs     376M  4.0K  376M   1% /run/user/0

/dev/mapper/testvg-testlv ext4      3.9G   16M  3.7G   1% /volume1

 

看到最后这个逻辑卷挂载成功即实验完成

 

 

实验2 : 逻辑卷扩容

注意点 扩容大小限制是VG的大小限制,最多扩容不能超过vg的总大小

[root@rhel8-server ~]# vgdisplay  testvg 

  --- Volume group ---

  VG Name               testvg

  System ID             

  Format                lvm2

  Metadata Areas        1

  Metadata Sequence No  2

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                1

  Open LV               1

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size               <5.00 GiB

  PE Size               4.00 MiB

  Total PE              1279

  Alloc PE / Size       1024 / 4.00 GiB

  Free  PE / Size       255 / 1020.00 MiB

  VG UUID               zNBk30-0L0P-3S1f-AjqM-iPwq-ttAY-pPLAWH

   

[root@rhel8-server ~]# 

[root@rhel8-server ~]# 

[root@rhel8-server ~]# 

[root@rhel8-server ~]# lvextend    -L   4.8G     /dev/testvg/testlv 

  Rounding size to boundary between physical extents: 4.80 GiB.

  Size of logical volume testvg/testlv changed from 4.00 GiB (1024 extents) to 4.80 GiB (1229 extents).

  Logical volume testvg/testlv successfully resized.

[root@rhel8-server ~]# 

[root@rhel8-server ~]# 

[root@rhel8-server ~]# 

[root@rhel8-server ~]# lvs

  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert

  root   rhel   -wi-ao---- <17.00g                                                    

  swap   rhel   -wi-ao----   2.00g                                                    

  testlv testvg -wi-ao----   4.80g                                                    

[root@rhel8-server ~]# df -hT

Filesystem                Type      Size  Used Avail Use% Mounted on

devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev

tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm

tmpfs                     tmpfs     1.9G   18M  1.9G   1% /run

tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup

/dev/mapper/rhel-root     xfs        17G  4.0G   14G  24% /

/dev/nvme0n1p1            xfs      1014M  169M  846M  17% /boot

tmpfs                     tmpfs     376M   16K  376M   1% /run/user/42

tmpfs                     tmpfs     376M  4.0K  376M   1% /run/user/0

/dev/mapper/testvg-testlv ext4      3.9G   16M  3.7G   1% /volume1

[root@rhel8-server ~]# resize2fs /dev/testvg/testlv 

resize2fs 1.44.3 (10-July-2018)

Filesystem at /dev/testvg/testlv is mounted on /volume1; on-line resizing required

old_desc_blocks = 1, new_desc_blocks = 1

The filesystem on /dev/testvg/testlv is now 1258496 (4k) blocks long.

 

[root@rhel8-server ~]# df  -hT

Filesystem                Type      Size  Used Avail Use% Mounted on

devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev

tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm

tmpfs                     tmpfs     1.9G   18M  1.9G   1% /run

tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup

/dev/mapper/rhel-root     xfs        17G  4.0G   14G  24% /

/dev/nvme0n1p1            xfs      1014M  169M  846M  17% /boot

tmpfs                     tmpfs     376M   16K  376M   1% /run/user/42

tmpfs                     tmpfs     376M  4.0K  376M   1% /run/user/0

/dev/mapper/testvg-testlv ext4      4.7G   16M  4.4G   1% /volume1

 

注意:

# 扩展文件系统,xfs类型的文件系统用xfs_growfs /volume1,后面接的是挂载点的位置

# 如果是ext文件系统刷新要用resize2fs /dev/testvg/testlv 后面接的是逻辑卷的位置

 

 

3.swap分区的创建

注意: swap分区不需要通过lvm来管理,物理分区完成后,可以直接做成swap类型

[root@rhel8-server ~]# fdisk   /dev/nvme0n2dev/testvg1/

 

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.

 

 

Command (m for help): p

Disk /dev/nvme0n2: 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: 0x36c2d278

 

Device         Boot    Start      End  Sectors Size Id Type

/dev/nvme0n2p1          2048 10487807 10485760   5G 83 Linux

/dev/nvme0n2p2      10487808 20973567 10485760   5G 83 Linux

 

Command (m for help): n

Partition type

   p   primary (2 primary, 0 extended, 2 free)

   e   extended (container for logical partitions)

Select (default p): p

Partition number (3,4, default 3): 3

First sector (20973568-41943039, default 20973568): 

Last sector, +sectors or +size{K,M,G,T,P} (20973568-41943039, default 41943039): +2G

 

Created a new partition 3 of type 'Linux' and of size 2 GiB.

 

Command (m for help): p

Disk /dev/nvme0n2: 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: 0x36c2d278

 

Device         Boot    Start      End  Sectors Size Id Type

/dev/nvme0n2p1          2048 10487807 10485760   5G 83 Linux

/dev/nvme0n2p2      10487808 20973567 10485760   5G 83 Linux

/dev/nvme0n2p3      20973568 25167871  4194304   2G 83 Linux

 

Command (m for help): w

The partition table has been altered.

Syncing disks.

 

[root@rhel8-server ~]# mkswap   /dev/nvme0n2p3 

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)

no label, UUID=751d09c5-7404-480e-8b89-be0a55a1d6e7

[root@rhel8-server ~]# blkid    /dev/nvme0n2p3 

/dev/nvme0n2p3: UUID="751d09c5-7404-480e-8b89-be0a55a1d6e7" TYPE="swap" PARTUUID="36c2d278-03"

[root@rhel8-server ~]# vim /etc/fstab 

 

/dev/nvme0n2p3             swap                  swap   defaults    0   0 

 

[root@rhel8-server ~]# swapon    -a        挂载所有swap

[root@rhel8-server ~]# swapon    -s        查看所有swap

Filename Type Size Used Priority

/dev/dm-1                               partition 2097148 0 -2

/dev/nvme0n2p3                          partition 2097148 0 -3

[root@rhel8-server ~]# free               可以查看系统swap总大小

              total        used        free      shared  buff/cache   available

Mem:        3848776      729288     2657132       18220      462356     2860824

Swap:       4194296           0     4194296

 

 

 

 

 

 

4. vdo卷的创建

什么是vdo技术,这是RHEL8中特有的技术   

红帽公司收购了一个公司  叫  permabit

vdo就是 permabit这个公司的技术

vdo也不是使用lvm来管理,是一个单独的技术。

vdo最大的作用就是节省硬盘空间,可以做到一个1TB的硬盘,直接存放3TB的数据甚至更多

vdo是怎么实现这个功能的? 靠的就是删除和压缩解压缩技术    重删就是把硬盘里相同的数据以前要存多份,现在会把多余的删掉,只留一份。,压缩算法来节省空间。

 

我们要使用vdo技术 将一块20 GD的硬盘,直接做成60G,并且挂载到系统上直接使用

 

yum install -y  vdo     kmod-kvdo 

安装时间较长,大约需要几分钟,安装完成以后需要重启设备  

 

[root@rhel8-server ~]# vdo create   --name=testvdo   --device=/dev/nvme0n3  --vdoLogicalSize=60G

Creating VDO testvdo

      The VDO volume can address 16 GB in 8 data slabs, each 2 GB.

      It can grow to address at most 16 TB of physical storage in 8192 slabs.

      If a larger maximum size might be needed, use bigger slabs.

Starting VDO testvdo

Starting compression on VDO testvdo

VDO instance 0 volume is ready at /dev/mapper/testvdo

[root@rhel8-server ~]# lsblk 

NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sr0                11:0    1  6.6G  0 rom  /run/media/root/RHEL-8-0-0-BaseOS-x86_64

nvme0n1           259:0    0   20G  0 disk 

├─nvme0n1p1       259:1    0    1G  0 part /boot

└─nvme0n1p2       259:2    0   19G  0 part 

  ├─rhel-root     253:0    0   17G  0 lvm  /

  └─rhel-swap     253:1    0    2G  0 lvm  [SWAP]

nvme0n2           259:3    0   20G  0 disk 

├─nvme0n2p1       259:4    0    5G  0 part 

│ └─testvg-testlv 253:2    0  4.8G  0 lvm  /volume1

├─nvme0n2p2       259:5    0    5G  0 part 

│ └─myvg-mylv     253:3    0    4G  0 lvm  /new-volume

└─nvme0n2p3       259:6    0    2G  0 part [SWAP]

nvme0n3           259:7    0   20G  0 disk 

└─testvdo         253:4    0   60G  0 vdo  

[root@rhel8-server ~]# mkfs.xfs    /dev/mapper/testvdo 

meta-data=/dev/mapper/testvdo    isize=512    agcount=4, agsize=3932160 blks

         =                       sectsz=4096  attr=2, projid32bit=1

         =                       crc=1        finobt=1, sparse=1, rmapbt=0

         =                       reflink=1

data     =                       bsize=4096   blocks=15728640, imaxpct=25

         =                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0, ftype=1

log      =internal log           bsize=4096   blocks=7680, version=2

         =                       sectsz=4096  sunit=1 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

 

[root@rhel8-server ~]# mkdir   /vdoblock

 

[root@rhel8-server /]# vim   /etc/fstab 

 

/dev/mapper/testvdo     /vdoblock    xfs  defaults,x-systemd.requires=vdo.service  0  0 

[root@rhel8-server /]# mount -a

[root@rhel8-server /]# df  -hT

Filesystem                Type      Size  Used Avail Use% Mounted on

devtmpfs                  devtmpfs  1.8G     0  1.8G   0% /dev

tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm

tmpfs                     tmpfs     1.9G   10M  1.9G   1% /run

tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup

/dev/mapper/rhel-root     xfs        17G  4.2G   13G  25% /

/dev/mapper/myvg-mylv     xfs       4.0G   61M  4.0G   2% /new-volume

/dev/mapper/testvg-testlv ext4      4.7G   16M  4.4G   1% /volume1

/dev/nvme0n1p1            xfs      1014M  243M  772M  24% /boot

tmpfs                     tmpfs     371M   16K  371M   1% /run/user/42

tmpfs                     tmpfs     371M  2.3M  369M   1% /run/user/0

/dev/sr0                  iso9660   6.7G  6.7G     0 100% /run/media/root/RHEL-8-0-0-BaseOS-x86_64

/dev/mapper/testvdo       xfs        60G  461M   60G   1% /vdoblock

 

 

实验5:关于逻辑卷的缩小 (考试不一定考)

[root@rhel8-server /]# df  -hT     查看一下目前的大小

Filesystem                Type      Size  Used Avail Use% Mounted on

devtmpfs                  devtmpfs  1.8G     0  1.8G   0% /dev

tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm

tmpfs                     tmpfs     1.9G   10M  1.9G   1% /run

tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup

/dev/mapper/rhel-root     xfs        17G  4.2G   13G  25% /

/dev/mapper/myvg-mylv     xfs       4.0G   61M  4.0G   2% /new-volume

/dev/mapper/testvg-testlv ext4      4.7G   16M  4.4G   1% /volume1

/dev/nvme0n1p1            xfs      1014M  243M  772M  24% /boot

tmpfs                     tmpfs     371M   16K  371M   1% /run/user/42

tmpfs                     tmpfs     371M  2.3M  369M   1% /run/user/0

/dev/sr0                  iso9660   6.7G  6.7G     0 100% /run/media/root/RHEL-8-0-0-BaseOS-x86_64

/dev/mapper/testvdo       xfs        60G  463M   60G   1% /vdoblock

 

在做磁盘裁剪的时候,首先必须要把文件系统给取消挂载

[root@rhel8-server /]# umount  /dev/mapper/testvg-testlv 

[root@rhel8-server /]# df -hT

Filesystem            Type      Size  Used Avail Use% Mounted on

devtmpfs              devtmpfs  1.8G     0  1.8G   0% /dev

tmpfs                 tmpfs     1.9G     0  1.9G   0% /dev/shm

tmpfs                 tmpfs     1.9G   10M  1.9G   1% /run

tmpfs                 tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup

/dev/mapper/rhel-root xfs        17G  4.2G   13G  25% /

/dev/mapper/myvg-mylv xfs       4.0G   61M  4.0G   2% /new-volume

/dev/nvme0n1p1        xfs      1014M  243M  772M  24% /boot

tmpfs                 tmpfs     371M   16K  371M   1% /run/user/42

tmpfs                 tmpfs     371M  2.3M  369M   1% /run/user/0

/dev/sr0              iso9660   6.7G  6.7G     0 100% /run/media/root/RHEL-8-0-0-BaseOS-x86_64

/dev/mapper/testvdo   xfs        60G  463M   60G   1% /vdoblock

 

裁剪前必须要检测文件系统,避免把文件系统的错误扩大化导致文件系统奔溃

 

[root@rhel8-server /]# e2fsck -f   /dev/testvg/testlv 

e2fsck 1.44.3 (10-July-2018)

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/testvg/testlv: 11/319488 files (0.0% non-contiguous), 40540/1258496 blocks

[root@rhel8-server /]# 

[root@rhel8-server /]# resize2fs  /dev/testvg/testlv     2G

resize2fs 1.44.3 (10-July-2018)

Resizing the filesystem on /dev/testvg/testlv to 524288 (4k) blocks.

The filesystem on /dev/testvg/testlv is now 524288 (4k) blocks long.

 

[root@rhel8-server /]# lvreduce   -L   2G   /dev/testvg/testlv 

  WARNING: Reducing active logical volume to 2.00 GiB.

  THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce testvg/testlv? [y/n]: y

  Size of logical volume testvg/testlv changed from 4.80 GiB (1229 extents) to 2.00 GiB (512 extents).

  Logical volume testvg/testlv successfully resized.

[root@rhel8-server /]# mount -a

[root@rhel8-server /]# df  -hT

Filesystem                Type      Size  Used Avail Use% Mounted on

devtmpfs                  devtmpfs  1.8G     0  1.8G   0% /dev

tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm

tmpfs                     tmpfs     1.9G   10M  1.9G   1% /run

tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup

/dev/mapper/rhel-root     xfs        17G  4.2G   13G  25% /

/dev/mapper/myvg-mylv     xfs       4.0G   61M  4.0G   2% /new-volume

/dev/nvme0n1p1            xfs      1014M  243M  772M  24% /boot

tmpfs                     tmpfs     371M   16K  371M   1% /run/user/42

tmpfs                     tmpfs     371M  2.3M  369M   1% /run/user/0

/dev/sr0                  iso9660   6.7G  6.7G     0 100% /run/media/root/RHEL-8-0-0-BaseOS-x86_64

/dev/mapper/testvdo       xfs        60G  463M   60G   1% /vdoblock

/dev/mapper/testvg-testlv ext4      2.0G   12M  1.8G   1% /volume1

 

 

 

实验6 逻辑卷的删除

 

[root@rhel8-server /]# umount   /dev/mapper/testvg-testlv 

[root@rhel8-server /]# 

[root@rhel8-server /]# vim /etc/fstab      配置文件需要清除这一行命令

[root@rhel8-server /]# 

[root@rhel8-server /]# 

[root@rhel8-server /]# lvremove   /dev/testvg/testlv 

Do you really want to remove active logical volume testvg/testlv? [y/n]: y

  Logical volume "testlv" successfully removed

[root@rhel8-server /]# vgremove   testvg 

  Volume group "testvg" successfully removed

[root@rhel8-server /]# pvremove    /dev/nvme0n2p1 

  Labels on physical volume "/dev/nvme0n2p1" successfully wiped.

 

 

 

 

 

 

 

 

 

实验7 : 逻辑卷快照技术  

LVM还具备有“快照卷”功能,该功能类似于虚拟机软件的还原时间点功能。例如,可以对某一个逻辑卷设备做一次快照,如果日后发现数据被改错了,就可以利用之前做好的快照卷进行覆盖还原。LVM的快照卷功能有两个特点:

快照卷的容量必须等同于逻辑卷的容量;

快照卷仅一次有效,一旦执行还原操作后则会被立即自动删除。

[root@rhel8-server /]# vgdisplay 

  --- 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                1

  Open LV               1

  Max PV                0

  Cur PV                1

  Act PV                1

  VG Size               <5.00 GiB

  PE Size               4.00 MiB

  Total PE              1279

  Alloc PE / Size       1024 / 4.00 GiB

  Free  PE / Size       255 / 1020.00 MiB

  VG UUID               6DqrkB-18SV-uf0r-AvzU-QgJY-8Zib-lc5OEe

   

[root@rhel8-server /]# echo "hello,world"    >   /new-volume/readme.txt     //在还原时间点快照前写入一个文件,验证恢复快照不会丢失之前的文件  

[root@rhel8-server /]# ls  -l   /new-volume/      检查刚刚写入的文件是否存在

total 4

-rw-r--r--. 1 root root 12 Jan 18 03:43 readme.txt

[root@rhel8-server /]# lvcreate   -L   120M   -s     -n  SNAP   /dev/myvg/mylv       //给这个lv卷打一个快照

  Logical volume "SNAP" created.

[root@rhel8-server /]# lvdisplay            //查看lv所有卷组,会发现快照卷已经存在了

  --- Logical volume ---

  LV Path                /dev/myvg/mylv

  LV Name                mylv

  VG Name                myvg

  LV UUID                LzpESZ-2eF4-j1hf-ZJAj-DYKA-dl1y-BsGTF0

  LV Write Access        read/write

  LV Creation host, time rhel8-server, 2022-01-18 01:51:32 -0500

  LV snapshot status     source of

                         SNAP [active]

  LV Status              available

  # open                 1

  LV Size                4.00 GiB

  Current LE             1024

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:3

   

  --- Logical volume ---

  LV Path                /dev/myvg/SNAP

  LV Name                SNAP

  VG Name                myvg

  LV UUID                uw7iP9-ahPA-W4b1-fgto-0UlM-p7Zi-ts3vxz

  LV Write Access        read/write

  LV Creation host, time rhel8-server, 2022-01-18 03:44:13 -0500

  LV snapshot status     active destination for mylv

  LV Status              available

  # open                 0

  LV Size                4.00 GiB

  Current LE             1024

  COW-table size         120.00 MiB

  COW-table LE           30

  Allocated to snapshot  0.00%

  Snapshot chunk size    4.00 KiB

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:6

在逻辑卷所挂载的目录中创建一个100MB的垃圾文件,然后再查看快照卷的状态。可以发现存储空间占的用量上升了

[root@rhel8-server /]# dd  if=/dev/zero    of=/new-volume/files   count=1  bs=100M

1+0 records in

1+0 records out

104857600 bytes (105 MB, 100 MiB) copied, 0.138176 s, 759 MB/s

[root@rhel8-server /]# lvdisplay 

  --- Logical volume ---

  LV Path                /dev/myvg/mylv

  LV Name                mylv

  VG Name                myvg

  LV UUID                LzpESZ-2eF4-j1hf-ZJAj-DYKA-dl1y-BsGTF0

  LV Write Access        read/write

  LV Creation host, time rhel8-server, 2022-01-18 01:51:32 -0500

  LV snapshot status     source of

                         SNAP [active]

  LV Status              available

  # open                 1

  LV Size                4.00 GiB

  Current LE             1024

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:3

   

  --- Logical volume ---

  LV Path                /dev/myvg/SNAP

  LV Name                SNAP

  VG Name                myvg

  LV UUID                uw7iP9-ahPA-W4b1-fgto-0UlM-p7Zi-ts3vxz

  LV Write Access        read/write

  LV Creation host, time rhel8-server, 2022-01-18 03:44:13 -0500

  LV snapshot status     active destination for mylv

  LV Status              available

  # open                 0

  LV Size                4.00 GiB

  Current LE             1024

  COW-table size         120.00 MiB

  COW-table LE           30

  Allocated to snapshot  83.67%

  Snapshot chunk size    4.00 KiB

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:6

  恢复快照之前必须要取消挂载

[root@rhel8-server /]# umount   /new-volume 

[root@rhel8-server /]# lvconvert --merge   /dev/myvg/SNAP        通过lvconvert --merge 来指定快照SNAP恢复

  Merging of volume myvg/SNAP started.

  myvg/mylv: Merged: 35.58%

  myvg/mylv: Merged: 100.00%

[root@rhel8-server /]# mount -a         //挂载所有卷组

[root@rhel8-server /]# cd /new-volume/

[root@rhel8-server new-volume]# ll       //此时刚刚的100M垃圾随着快照技术已经删除了

total 4

-rw-r--r--. 1 root root 12 Jan 18 03:43 readme.txt



这篇关于Linux基础-逻辑卷管理器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程