Linux下使用LVM管理(扩展/缩小/删除)

LVM的工作原理

  • LVM( Logical Volume Manager)逻辑卷管理,是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。管理员利用LVM可以在磁盘不用重新分区的情况下动态调整文件系统的大小,并且利用LVM管理的文件系统可以跨越磁盘,当服务器添加了新的磁盘后,管理员不必将原有的文件移动到新的磁盘上,而是通过LVM可以直接扩展文件系统跨越磁盘。
  • 它就是通过将底层的物理硬盘封装起来,然后以逻辑卷的方式呈现给上层应用。在LVM中,其通过对底层的硬盘进行封装,当我们对底层的物理硬盘进行操作时,其不再是针对于分区进行操作,而是通过一个叫做逻辑卷的东西来对其进行底层的磁盘管理操作。

LVM常用的术语

  • 物理存储介质(The physical media):LVM存储介质可以是磁盘分区,整个磁盘,RAID阵列或SAN磁盘,设备必须初始化为LVM物理卷,才能与LVM结合使用
  • 物理卷PV(physical volume)  :物理卷就是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数,创建物理卷它可以用硬盘分区,也可以用硬盘本身;
  • 卷组VG(Volume Group)  :一个LVM卷组由一个或多个物理卷组成
  • 逻辑卷LV(logical volume)  :LV建立在VG之上,可以在LV之上建立文件系统
  • PE(physical extents)  :PV物理卷中可以分配的最小存储单元,PE的大小是可以指定的,默认为4MB
  • LE(logical extent)  :    LV逻辑卷中可以分配的最小存储单元,在同一个卷组中,LE的大小和PE是相同的,并且一一对应

最小存储单位总结

  • 名称      最小存储单位      示例
  • 硬盘      扇区(512字节)    
  • 文件系统    block(1K或4K )    #  mkfs.ext4  -b 2048  /dev/sdb1  ,最大支持到4096
  • raid      chunk  (512K)    #mdadm -C -v /dev/md5 -l 5 -n 3 -c 512 -x 1 /dev/sde{1,2,3,5}
  • LVM      PE   (4M)      # vgcreate -s 4M  vg1 /dev/sdb{1,2}

LVM主要元素构成

LVM优点

  • 使用卷组,使多个硬盘空间看起来像是一个大的硬盘
  • 使用逻辑卷,可以跨多个硬盘空间的分区  sdb1 sdb2  sdc1  sdd2  sdf
  • 在使用逻辑卷时,它可以在空间不足时动态调整它的大小
  • 在调整逻辑卷大小时,不需要考虑逻辑卷在硬盘上的位置,不用担心没有可用的连续空间
  • 可以在线对LV,VG 进行创建,删除,调整大小等操作。LVM上的文件系统也需要重新调整大小。
  • 允许创建快照,可以用来保存文件系统的备份。

RAID+LVM一起用:LVM是软件的卷管理方式,而RAID是磁盘管理的方法。对于重要的数据,使用RAID用来保护物理的磁盘不会因为故障而中断业务,再用LVM用来实现对卷的良性的管理,更好的利用磁盘资源。

创建LVM的基本步骤

  • (1) 物理磁盘被格式化为PV,(空间被划分为一个个的PE) #PV包含PE
  • (2) 不同的PV加入到同一个VG中,(不同PV的PE全部进入到了VG的PE池内) #VG包含PV
  • (3) 在VG中创建LV逻辑卷,基于PE创建,(组成LV的PE可能来自不同的物理磁盘) #LV基于PE创建
  • (4) LV直接可以格式化后挂载使用    #格式化挂载使用
  • (5) LV的扩充缩减实际上就是增加或减少组成该LV的PE数量,其过程不会丢失原始数据

 lvm常用的命令

下面的操作会用的一些查看命令:

准备环境

1 [root@wencheng ~] # cat /etc/redhat-release
2 CentOS Linux release 7.5.1804 (Core)
3 
4 [root@wencheng ~] # rpm -qa | grep lvm
5 lvm2-libs-2.02.187-6.el7_9.4.x86_64
6 lvm2-2.02.187-6.el7_9.4.x86_64

 创建并使用LVM逻辑卷

  VMware Workstation添加硬盘

  • 在"我的计算机"中选择该主机--右键--设置--添加--硬盘--下一步--下一步--下一步--完成 添加硬盘步骤。

  •  命令:echo "- - -" > /sys/class/scsi_host/host0/scan
  •  1.新增添磁盘,还不能直接显示出来,要想出现要对磁盘进行扫描(实际生产环境中不需要,都是热插拔技术):
  • 2.假如 fdisk -l 还是未发现新硬盘,则将上面命令中的host0,替换为host1,host2,....看看;
  • 3.我们再查看系统日志/var/log/messages,发现对SCSI设备进行了一次重新扫描,用fdisk -l也看到了新增加的磁盘了。

查看新增的硬盘

创建分区并设置LVM(8e)类型

[root@wencheng ~] # fdisk /dev/sdb
Welcome to  fdisk (util-linux 2.23.2).

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
Building a new DOS disklabel with disk identifier 0x81cc782c.

Command (m  for help): n     // 新建
Partition  type :
   p   primary (0 primary, 0 extended, 4  free )
   e   extended
Select (default p): p     //p 为主分区,e为扩展分区
Partition number (1-4, default 1): 1     // 分区号,回车(默认)
First sector (2048-20971519, default 2048):     // 回车(默认)
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):     // 回车(默认)
Using default value 20971519
Partition 1 of  type Linux and of size 10 GiB is  set

Command (m  for help): p     // 显示信息

Disk  /dev/sdb : 10.7 GB, 10737418240 bytes, 20971520 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
Disk label  type : dos
Disk identifier: 0x81cc782c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1 2048    20971519    10484736   83  Linux

Command (m  for help): l     // 列出所有类型

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS /sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS /sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS /2 hidden C:  c6  DRDOS /sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
 5  Extended        41  PPC PReP Boot   86  NTFS volume  set da  Non-FS data
 6  FAT16           42  SFS             87  NTFS volume  set db  CP /M / CTOS / .
 7  HPFS /NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM        df BootIt
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
 a  OS /2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R /O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD /OS e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP /M a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12 /16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux /PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS /NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 75  PC /IX be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1 80  Old Minix

Command (m  for help): t     // 修改类型
Selected partition 1
Hex code ( type L to list all codes): 8e     // 改为LVM类型
Changed  type of partition  'Linux' to  'Linux LVM'

Command (m  for help): p     // 显示信息

Disk  /dev/sdb : 10.7 GB, 10737418240 bytes, 20971520 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
Disk label  type : dos
Disk identifier: 0x81cc782c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1 2048    20971519    10484736   8e  Linux LVM     // 类型修改成功

Command (m  for help): w     // 保存退出
The partition table has been altered!

Calling ioctl() to re- read partition table.
Syncing disks.

  注: 其他新增硬盘分区类似,不再赘述,如下查看最后分区结果。

[root@wencheng ~] # ls -l /dev/sd*1
brw-rw----. 1 root disk 8,  1 Mar 25 10:54  /dev/sda1      // 系统分区
brw-rw----. 1 root disk 8, 17 Apr 19 16:48  /dev/sdb1
brw-rw----. 1 root disk 8, 33 Apr 19 16:53  /dev/sdc1
brw-rw----. 1 root disk 8, 49 Apr 19 16:53  /dev/sdd1
brw-rw----. 1 root disk 8, 65 Apr 19 16:53  /dev/sde1
[root@wencheng ~] #
[root@localhost ~] # fdisk -l | grep -e "/dev/sd[b-e]1"
/dev/sdb1 2048    41943039    20970496   8e  Linux LVM
/dev/sdc1 2048    41943039    20970496   8e  Linux LVM
/dev/sdd1 2048    41943039    20970496   8e  Linux LVM
/dev/sde1 2048    20971519    10484736   83  Linux     // 备用磁盘

 创建pv,并查看结果

[root@wencheng ~] # pvcreate /dev/sd[b,c,d]1
  Physical volume  "/dev/sdb1" successfully created.
  Physical volume  "/dev/sdc1" successfully created.
  Physical volume  "/dev/sdd1" successfully created.
[root@wencheng ~] #
[root@wencheng ~] # pvdisplay /dev/sd[b,c,d]1
  "/dev/sdb1" is a new physical volume of  "<10.00 GiB"
  --- NEW Physical volume ---
  PV Name                /dev/sdb1
  VG Name
  PV Size               <20.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               kZxjyM-Ora3-io5L-J14w-ljlM-TArl-gYQhqm

  "/dev/sdd1" is a new physical volume of  "<10.00 GiB"
  --- NEW Physical volume ---
  PV Name                /dev/sdd1
  VG Name
  PV Size               <20.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               NN1HSf-JQAA-PiMm-MEmm-WocM-9QrD-zcStUf

  "/dev/sdc1" is a new physical volume of  "<10.00 GiB"
  --- NEW Physical volume ---
  PV Name                /dev/sdc1
  VG Name
  PV Size               <20.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               I92jzU-WN1f-zcrW-Mzm7-HuoZ-3hBP-BjJFkG

创建名为'vg01'的卷组,使用/dev/sdb1, /dev/sdc1和/dev/sdb1创建,并查看结果。

[root@wencheng ~] # vgcreate vg01 /dev/sd[b,c,d]1    //指定PE大小用的参数:-s 16M,  如果存储的数据都是大文件,那么PE尽量调大,读取速度快
  Volume group  "vg01" successfully created
[root@wencheng ~] #
[root@localhost ~] # vgdisplay
  --- Volume group ---
  VG Name               vg01
  System ID
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               &lt;59.99 GiB     ///dev/sd [b,c]1硬盘总和容量
  PE Size               4.00 MiB      // 默认4MB
  Total PE              15357
  Alloc PE / Size       0 / 0
  Free  PE / Size       15357 / &lt;59.99 GiB
  VG UUID               0HRkjd-8ccf-Alpe-vhfJ-eOLi-vtnX-0Vl7sR

创建一个名为'lv01'、大小为30G的逻辑卷。

[root@wencheng ~] # lvcreate -n lv01 -L 30G vg01    //-L指定lv大小的SIZE(M,G) (-l:小l 指定LE的数量)
  Logical volume  "lv01" created.
#或 lvcreate -n lv01 -l "100%FREE" vg01
[root@localhost ~] # lvdisplay
  --- Logical volume ---
  LV Path                 /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                Iwvoxi-Ekrb-8nQ7-QfDt-1jIx-FiXy-srYbbZ
  LV Write Access         read /write
  LV Creation host,  time localhost.localdomain, 2021-04-20 11:17:57 +0800
  LV Status              available
  # open                 0
  LV Size                30.00 GiB
  Current LE             4
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently  set to     8192
  Block device           253:0

 创建文件系统,并挂载。

[root@wencheng ~] # mkdir /mnt/lv01
[root@wencheng ~] # ls -l /dev/vg01/
total 0
lrwxrwxrwx. 1 root root 7 Apr 20 11:17 lv01 -> .. /dm-0

[root@localhost ~] # mkfs.xfs /dev/vg01/lv01
meta-data= /dev/vg01/lv01 isize=512    agcount=1, agsize=4096 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=4096, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@wencheng ~] # mount /dev/vg01/lv01 /mnt/lv01/
[root@wencheng ~] # echo "/dev/vg01/lv01 /mnt/lv01 xfs defaults 0 0" >> /etc/fstab    //添加开机自动挂载
[root@wencheng ~] # df -Th /mnt/lv01/
Filesystem            Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lv01 xfs    31G   61M   30G   1%  /mnt/lv01


字段定义说明
/etc/fstab 文件包含了如下字段,通过空格或 Tab 分隔:

< file system>    < dir >    < type >    <options>    <dump>    <pass>

< file systems> - 要挂载的分区或存储设备.
< dir > - < file systems>的挂载位置。
< type > - 要挂载设备或是分区的文件系统类型,支持许多种不同的文件系统:ext2, ext3, ext4, reiserfs, xfs, jfs, smbfs, iso9660, vfat, ntfs, swap 及 auto。 设置成auto类型, mount 命令会猜测使用的文件系统类型,对 CDROM 和 DVD 等移动设备是非常有用的。
<options> - 挂载时使用的参数,注意有些 mount 参数是特定文件系统才有的。一些比较常用的参数有:
    auto - 在启动时或键入了  mount -a 命令时自动挂载。
    noauto - 只在你的命令下被挂载。
    exec - 允许执行此分区的二进制文件。
    noexec - 不允许执行此文件系统上的二进制文件。
    ro - 以只读模式挂载文件系统。
    rw - 以读写模式挂载文件系统。
    user - 允许任意用户挂载此文件系统,若无显示定义,隐含启用 noexec, nosuid, nodev 参数。
    users - 允许所有  users 组中的用户挂载文件系统.
    nouser - 只能被 root 挂载。
    owner - 允许设备所有者挂载.
    sync - I /O 同步进行。
    async - I /O 异步进行。
    dev - 解析文件系统上的块特殊设备。
    nodev - 不解析文件系统上的块特殊设备。
    suid - 允许 suid 操作和设定 sgid 位。这一参数通常用于一些特殊任务,使一般用户运行程序时临时提升权限。
    nosuid - 禁止 suid 操作和设定 sgid 位。
    noatime - 不更新文件系统上 inode 访问记录,可以提升性能(参见 atime 参数)。
    nodiratime - 不更新文件系统上的目录 inode 访问记录,可以提升性能(参见 atime 参数)。
    relatime - 实时更新 inode access 记录。只有在记录中的访问时间早于当前访问才会被更新。(与 noatime 相似,但不会打断如 mutt 或其它程序探测文件在上次访问后是否被修改的进程。),可以提升性能(参见 atime 参数)。
    flush - vfat 的选项,更频繁的刷新数据,复制对话框或进度条在全部数据都写入后才消失。
    defaults - 使用文件系统的默认挂载参数,例如 ext4 的默认参数为:rw, suid, dev,  exec , auto, nouser, async.
<dump> dump 工具通过它决定何时作备份. dump 会检查其内容,并用数字来决定是否对这个文件系统进行备份。 允许的数字是 0 和 1 。0 表示忽略, 1 则进行备份。大部分的用户是没有安装 dump 的 ,对他们而言 <dump> 应设为 0。
<pass>  fsck 读取 <pass> 的数值来决定需要检查的文件系统的检查顺序。允许的数字是0, 1, 和2。 根目录应当获得最高的优先权 1, 其它所有需要被检查的设备设置为 2. 0 表示设备不会被  fsck 所检查。

 动态调整逻辑卷大小的功能是LVM最有用的功能。

  扩展LV首先,确定一下是否有可用的扩容空间,因为空间是从VG里面创建的,并且LV不能跨VG扩容。

  • 1.如果VG无剩余空间,则添加新硬盘,这里不再赘述(添加硬盘,分区LVM),再如下操作。
[root@wencheng ~] # vgs
  VG    #PV #LV #SN Attr   VSize   VFree
  vg01   3   1   0 wz--n- 30.99g 1012.00m

[root@wencheng ~] # pvcreate /dev/sde1
  Physical volume  "/dev/sde1" successfully created.
[[root@wencheng ~] # vgextend vg01 /dev/sde1    //新增硬盘/dev/sde1为10G
  Volume group  "vg01" successfully extended
[root@wencheng ~] # vgs
  VG    #PV #LV #SN Attr   VSize  VFree
  vg01   4   1   0 wz--n- 30.99g 10.98g
  •  2.如果VG有预留空间,则如下操作。
[root@wencheng ~] # vgs
  VG    #PV #LV #SN Attr   VSize   VFree
  vg01   3   1   0 wz--n- <59.99g 29.97g     // 空闲约29G

 扩容逻辑卷,用的如下命令。

[root@wencheng ~] # lvextend -L 59G /dev/vg01/lv01
  Size of logical volume vg01 /lv01 changed from &lt;30.02 GiB (7684 extents) to 59.00 GiB (15104 extents).
  Logical volume vg01 /lv01 successfully resized.

[root@wencheng ~] # lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv01 vg01 -wi-ao---- 59.00g     //LV 已扩容成功

[root@wencheng ~] # lvdisplay
  --- Logical volume ---
  LV Path                 /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                Iwvoxi-Ekrb-8nQ7-QfDt-1jIx-FiXy-srYbbZ
  LV Write Access         read /write
  LV Creation host,  time localhost.localdomain, 2021-04-20 11:17:57 +0800
  LV Status              available
  # open                 1
  LV Size                59.00 GiB
  Current LE             15104
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently  set to     8192
  Block device           253:0

说明:
  在指定大小的时候,扩容30m和扩容到30m是不一样的写法
  扩容59G ==== -L +59G
  扩容到59G ===== -L 59G

[root@wencheng ~] # df -Th /mnt/lv01/
Filesystem            Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lv01 xfs    31G   61M   30G   1%  /mnt/lv01      // 但仍然看不到扩展的容量

注:可以看到LV虽然扩展了,但是文件系统大小还是原来的,下面开始扩容文件系统
  ext4文件系统扩容使用命令语法: resize2fs  逻辑卷名称
  xfs文件系统扩容使用命令语法:  xfs_growfs  挂载点
  resize2fs和xfs_growfs 两者的区别是传递的参数不一样的,xfs_growfs是采用的挂载点;resize2fs是逻辑卷名称,而且resize2fs命令不能对xfs类型文件系统使用。< /s

[root@wencheng ~] # xfs_growfs /mnt/lv01/
meta-data= /dev/mapper/vg01-lv01 isize=512    agcount=1921, agsize=4096 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=7868416, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 7868416 to 15466496
[root@wencheng ~] #
[root@wencheng ~] # df -Th /mnt/lv01/
Filesystem            Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lv01 xfs    59G  119M   59G   1%  /mnt/lv01      // 扩展完成
注:先扩lv逻辑卷,再扩文件系统。

掌握另一方法:使用lvextend命令加参数-r,它是支持多种不同的文件系统,比如ext3,ext4,xfs,…
 [root@wencheng ~] # lvextend -L 59G -r /dev/vg01/lv01  #直接扩容到59G空间,一步到位,不用再扩文件系统了

缩减LVM卷,注意事项

  • 减少逻辑卷的大小值若小于储存的数据大小,存储在后面的数据会丢失,缩减 LVM 之前,建议先做一个备份。
  •  在 GFS2 或者 XFS 文件系统上不支持在线缩小;btrfs文件系统支持在线缩小。

万不得已需要缩减xfs文件系统的lvm分区空间时候,一定要注意顺序。

  • 第一步,确认现有环境,并目标数据备份;以/mnt/lv01为例。
[root@wencheng ~] # ls -l /mnt/lv01*
/mnt/lv01 :
total 1060
-rw-r--r--.  1 root root     16 Apr 21 10:44 adjtime
-rw-r--r--.  1 root root   1518 Apr 21 10:44 aliases
-rw-r--r--.  1 root root  12288 Apr 21 10:44 aliases.db
-rw-------.  1 root root    541 Apr 21 10:44 anacrontab
-rw-r--r--.  1 root root     55 Apr 21 10:44 asound.conf
-rw-r--r--.  1 root root   2853 Apr 21 10:44 bashrc
-rw-r--r--.  1 root root     38 Apr 21 10:44 centos-release

[root@wencheng ~] # df -Th /mnt/lv01
Filesystem            Type      Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lv01 xfs        50G  170M   50G   1%  /mnt/lv01

[root@wencheng ~] # rpm -qa | grep xfsdump
xfsdump-3.1.7-1.el7.x86_64

[root@wencheng ~] # xfsdump -f /mnt/lv01.xfsdump /mnt/lv01    //xfsdump -f "dump文件存放的绝对路径加文件名" "需要缩减容量的目录"
xfsdump: using  file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump  format 3.0) -  type ^C  for status and control

 ============================= dump label dialog ==============================

please enter label  for this dump session (timeout  in 300 sec)
 lv01
session label entered:  "lv01"

 --------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of wencheng: /mnt/lv01
xfsdump: dump  date : Wed Apr 21 10:59:05 2021
xfsdump: session  id : 619c7222-752a-44af-8b7f-411d33fbef15
xfsdump: session label:  "lv01"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 143172224 bytes
xfsdump:  /var/lib/xfsdump/inventory created

 ============================= media label dialog =============================

please enter label  for media  in drive 0 (timeout  in 300 sec)
 lv01
media label entered:  "lv01"

 --------------------------------- end dialog ---------------------------------

xfsdump: creating dump session media  file 0 (media 0,  file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media  file
xfsdump: media  file size 140934528 bytes
xfsdump: dump size (non- dir files) : 139858712 bytes
xfsdump: dump complete: 32 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0  /mnt/lv01 .xfsdump OK (success)
xfsdump: Dump Status: SUCCESS

[root@wencheng ~] # ls -l /mnt/lv01*
-rw-r--r--. 1 root root 140934528 Apr 21 10:59  /mnt/lv01 .xfsdump     // 生成备份

/mnt/lv01 :
total 1060
-rw-r--r--.  1 root root     16 Apr 21 10:44 adjtime
-rw-r--r--.  1 root root   1518 Apr 21 10:44 aliases
-rw-r--r--.  1 root root  12288 Apr 21 10:44 aliases.db
-rw-------.  1 root root    541 Apr 21 10:44 anacrontab
-rw-r--r--.  1 root root     55 Apr 21 10:44 asound.conf
-rw-r--r--.  1 root root   2853 Apr 21 10:44 bashrc
-rw-r--r--.  1 root root     38 Apr 21 10:44 centos-release
  •  第二步,卸载挂载的目录。
[root@wencheng ~] # umount /mnt/lv01/
  •  第三步,把你原来的逻辑卷变成5G是扩大还是缩小它自己会判断,并格式化以更新到最新大小。
[root@wencheng ~] # lvreduce -L 5G /dev/vg01/lv01
  WARNING: Reducing active logical volume to 5.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg01 /lv01 ? [y /n ]: y
  Size of logical volume vg01 /lv01 changed from 50.00 GiB (12800 extents) to 5.00 GiB (1280 extents).
  Logical volume vg01 /lv01 successfully resized.

[root@wencheng ~] # mkfs.xfs -f /dev/vg01/lv01    //格式化以更新到最新大小(清除所有数据)
meta-data= /dev/vg01/lv01 isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, 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

[root@wencheng ~] # lvs    //lv缩小成功
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv01 vg01 -wi-a----- 5.00g
[root@wencheng ~] # vgs
  VG    #PV #LV #SN Attr   VSize  VFree
  vg01   4   1   0 wz--n- 69.98g 64.98g
  • 第四步,挂载出来空分区 。
[root@wencheng ~] # mount /dev/vg01/lv01 /mnt/lv01
[root@wencheng ~] # df -Th /mnt/lv01
Filesystem            Type      Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lv01 xfs       5.0G   33M  5.0G   1%  /mnt/lv01

[root@wencheng ~] # ls -l /mnt/lv01
total 0
  • 第五步,把dump的数据恢复。
[root@wencheng ~] # xfsrestore -f /mnt/lv01.xfsdump /mnt/lv01
xfsrestore: using  file dump (drive_simple) strategy
xfsrestore: version 3.1.7 (dump  format 3.0) -  type ^C  for status and control
xfsrestore: searching media  for dump
xfsrestore: examining media  file 0
xfsrestore: dump description:
xfsrestore:  hostname : wencheng
xfsrestore:  mount point:  /mnt/lv01
xfsrestore: volume:  /dev/mapper/vg01-lv01
xfsrestore: session  time : Wed Apr 21 10:59:05 2021
xfsrestore: level: 0
xfsrestore: session label:  "lv01"
xfsrestore: media label:  "lv01"
xfsrestore:  file system  id : ded6656d-b154-4c48-bda8-2935c5fa8dd8
xfsrestore: session  id : 619c7222-752a-44af-8b7f-411d33fbef15
xfsrestore: media  id : e4f6c078-14b3-43c3-9ff7-5c847977ca32
xfsrestore: using online session inventory
xfsrestore: searching media  for directory dump
xfsrestore: reading directories
xfsrestore: 533 directories and 3574 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0  /mnt/lv01 .xfsdump OK (success)
xfsrestore: Restore Status: SUCCESS
[root@wencheng ~] #
[root@wencheng ~] # ls -l /mnt/lv01    //查看数据是否恢复完成
total 1060
-rw-r--r--.  1 root root     16 Apr 21 10:44 adjtime
-rw-r--r--.  1 root root   1518 Apr 21 10:44 aliases
-rw-r--r--.  1 root root  12288 Apr 21 10:44 aliases.db
-rw-------.  1 root root    541 Apr 21 10:44 anacrontab
-rw-r--r--.  1 root root     55 Apr 21 10:44 asound.conf
-rw-r--r--.  1 root root   2853 Apr 21 10:44 bashrc
-rw-r--r--.  1 root root     38 Apr 21 10:44 centos-release

 LVM删除操作

创建LVM流程:pvcreate创建pv -> vgcreate创建卷组 -> lvcreate创建逻辑卷 -> mkfs.xfs lv 格式化-> mount挂载
删除LVM流程:umount卸载 -> lvremove lv移出卷组中所有逻辑卷-> vgremove vg移出卷组-> pvremove 移出pv

  • 查看现有lvm状态。
[root@wencheng ~] # df -Th /mnt/lv01
Filesystem            Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lv01 xfs   5.0G  170M  4.9G   4%  /mnt/lv01
[root@wencheng ~] # lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv01 vg01 -wi-ao---- 5.00g
[root@wencheng ~] # vgs
  VG    #PV #LV #SN Attr   VSize  VFree
  vg01   4   1   0 wz--n- 69.98g 64.98g
[root@wencheng ~] # pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sdb1 vg01 lvm2 a--  <20.00g <15.00g
  /dev/sdc1 vg01 lvm2 a--  <20.00g <20.00g
  /dev/sdd1 vg01 lvm2 a--  <20.00g <20.00g
  /dev/sde1 vg01 lvm2 a--  <10.00g <10.00g
  •  卸载lvm挂载目录。
[root@wencheng ~] # umount /mnt/lv01
[root@wencheng ~] # df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda2 xfs        30G  1.2G   29G   4% /
devtmpfs       devtmpfs  981M     0  981M   0%  /dev
tmpfs          tmpfs     992M     0  992M   0%  /dev/shm
tmpfs          tmpfs     992M  9.5M  982M   1%  /run
tmpfs          tmpfs     992M     0  992M   0%  /sys/fs/cgroup
/dev/sda1 xfs       297M  107M  191M  36%  /boot
tmpfs          tmpfs     199M     0  199M   0%  /run/user/0
  •  删除逻辑卷lv。
[root@wencheng ~] # lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv01 vg01 -wi-a----- 5.00g
[root@wencheng ~] #
[root@wencheng ~] # lvremove /dev/vg01/lv01
Do you really want to remove active logical volume vg01 /lv01 ? [y /n ]: y
  Logical volume  "lv01" successfully removed
[root@wencheng ~] # lvs
  •  删除卷组vg。
[root@wencheng ~] # vgs
  VG    #PV #LV #SN Attr   VSize  VFree
  vg01   4   0   0 wz--n- 69.98g 69.98g
[root@wencheng ~] #
[root@wencheng ~] # vgremove vg01
  Volume group  "vg01" successfully removed
[root@wencheng ~] #
[root@wencheng ~] # vgs
  •  删除物理机pv。
[root@wencheng ~] # pvs
  PV         VG Fmt  Attr PSize   PFree
  /dev/sdb1 lvm2 ---  <20.00g <20.00g
  /dev/sdc1 lvm2 ---  <20.00g <20.00g
  /dev/sdd1 lvm2 ---  <20.00g <20.00g
  /dev/sde1 lvm2 ---  <10.00g <10.00g
[root@wencheng ~] #
[root@wencheng ~] # pvremove /dev/sde1
  Labels on physical volume  "/dev/sde1" successfully wiped.
[root@wencheng ~] # pvremove /dev/sdd1
  Labels on physical volume  "/dev/sdd1" successfully wiped.
[root@wencheng ~] #
[root@wencheng ~] # pvs
  PV         VG Fmt  Attr PSize   PFree
  /dev/sdb1 lvm2 ---  <20.00g <20.00g
  /dev/sdc1 lvm2 ---  <20.00g <20.00g
  • 编辑/etc/fstab,删除对应自动挂载信息 。
UUID=57c4afb5-1953-4695-b173-965f1b12500f /                       xfs     defaults        0 0
UUID=a04da5a6-ddd1-41b8-a9d6-ee5f6b62596e  /boot xfs     defaults        0 0
UUID=77e1f34d-8161-4b9c-ba1f-240898a695ef swap                    swap    defaults        0 0
#/dev/vg01/lv01 /mnt/lv01 xfs defaults 0 0
  • 格式化磁盘。
[root@wencheng ~] # mkfs.xfs /dev/sde1
meta-data= /dev/sde1 isize=512    agcount=4, agsize=655296 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621184, 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
[root@wencheng ~] # mkfs.xfs /dev/sdd1
meta-data= /dev/sdd1 isize=512    agcount=4, agsize=1310656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242624, 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

 附:其他LVM 逻辑卷管理器使用及配置详情见【方文档

posted @ 2021-06-07 08:46  讲文张字  阅读(1889)  评论(0编辑  收藏  举报
返回顶部