【Linux】Linux LVM --- 硬盘扩容

admin / Linux / / 1118 字 · 约 37 分钟 / 470 次阅读 /

简介

LVM 是逻辑盘卷管理(LogicalVolumeManager)的简称,在 Linux 环境下对磁盘分区进行管理的一种机制,LVM 是建立在硬盘和 分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过 LVM 系统管理员可以轻松管理磁盘分区,如:将若干个磁盘分区连接为一个整块的卷(volumegroup),形成一个存储池。管理员可以在卷组上随意创建逻辑卷组(logicalvolumes),并进一步在逻辑卷组上创建文件系统。

LVM 组成

Logical Volume Manager(逻辑卷管理)

PV:是物理的磁盘分区

VG:LVM 中的物理的磁盘分区,也就是 PV,必须加入 VG,可以将 VG 理解为一个仓库统一管理了几个大的硬盘,形成了一个统一虚拟的存储资源池。

LV:也就是从 VG 中划分的逻辑分区

LVM 模型

操作环境介绍

1,在线扩容根分区,根分区 100G,新增一块 20G 硬盘,将根分区扩容至 120G。

2,新建逻辑卷并扩容和缩减,新增 2 块 20G 磁盘,划分 10GLV,再扩容 10G,随后缩减 5G.

扩容根分区

查看系统可用块设备以及依赖关系

[root@zabbix-server ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0   99G  0 part
  ├─centos-root 253:0    0   91G  0 lvm  /
  └─centos-swap 253:1    0    8G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk
sr0              11:0    1  4.3G  0 rom

查看系统初始硬盘

[root@zabbix-server ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        91G  1.6G   90G   2% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               xfs       497M  139M  359M  28% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0

查看当前物理卷

[root@zabbix-server ~]# pvs
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sda2  centos lvm2 a--  99.00g    0

创建硬盘分区

[root@zabbix-server ~]# 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 0x6488a7cc.

Command (m for help): n    //添加新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p   // 回车,默认p,主分区
Partition number (1-4, default 1):   // 回车,默认1
First sector (2048-41943039, default 2048):  // 回车,默认起始位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):  // 回车,默认最后位置
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): t // 更改分区格式
Selected partition 1
Hex code (type L to list all codes): 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
Hex code (type L to list all codes): 8e // 更改分区格式为LVM
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p  //打印,Blocks为分区大小 单位为kb

Disk /dev/sdb: 21.5 GB, 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
Disk label type: dos
Disk identifier: 0x6488a7cc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   8e  Linux LVM

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

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

将物理硬盘格式化成 PV(物理卷) 使用的是 pvcreate 命令

[root@zabbix-server ~]# pvcreate  /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

查看当前 vg 信息

[root@zabbix-server ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               99.00 GiB
  PE Size               4.00 MiB
  Total PE              25344
  Alloc PE / Size       25344 / 99.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               280QHe-8dkW-t34f-9JKh-QhMk-1rZE-cejqal

把转换好的物理卷并到 vg(卷组)中, 通过 vgcreate 命令

[root@zabbix-server ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended
[root@zabbix-server ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  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               <119.00 GiB
  PE Size               4.00 MiB
  Total PE              30463
  Alloc PE / Size       30463 / <119.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               280QHe-8dkW-t34f-9JKh-QhMk-1rZE-cejqal

查看逻辑卷,默认系统分了 2 个,选择对应逻辑卷路径

[root@zabbix-server ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                vhJWup-CgKn-C9xH-dmOv-3iLW-TM4g-ezKp3R
  LV Write Access        read/write
  LV Creation host, time zabbix-server, 2019-07-29 18:48:17 +0800
  LV Status              available
  # open                 1
  LV Size                <111.00 GiB
  Current LE             28415
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                813MLJ-AbCR-hdvv-85hd-L289-GHEH-DDC1Lj
  LV Write Access        read/write
  LV Creation host, time zabbix-server, 2019-07-29 18:48:17 +0800
  LV Status              available
  # open                 2
  LV Size                8.00 GiB
  Current LE             2048
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

扩容当前逻辑卷空间

[root@zabbix-server ~]# lvextend /dev/centos/root /dev/sdb1
  Size of logical volume centos/root changed from 91.00 GiB (23296 extents) to <111.00 GiB (28415 extents).
  Logical volume centos/root successfully resized.

写入文件系统,使扩容生效

[root@zabbix-server ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=5963776 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=23855104, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=11648, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 23855104 to 29096960

使用 ext 文件系统,可以使用 resize2fs 命令替换 xfs_growfs 命令。

查看扩容后的物理卷

[root@zabbix-server ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--   99.00g    0
  /dev/sdb1  centos lvm2 a--  <20.00g    0

查看根分区扩容效果

[root@zabbix-server ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       111G  1.6G  110G   2% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               xfs       497M  139M  359M  28% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0

新建逻辑分区扩容

查看硬盘信息

查看系统可用块设备以及依赖关系

将新增磁盘格式化为物理卷 PV

[root@zabbix-server ~]# pvcreate  /dev/sdb /dev/sdc
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.

查看 pv 信息

创建卷组(diskvg),并将 PV 加入到卷组中 通过 vgcreate 命令

[root@zabbix-server ~]# vgcreate diskvg /dev/sdb /dev/sdc
Volume group "diskvg" successfully created

通过 vgdisplay 或 vgs 命令查看 vg 的信息

[root@zabbix-server ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   1   2   0 wz--n- 99.00g     0
  diskvg   2   0   0 wz--n- 19.99g 19.99g
[root@zabbix-server ~]# vgdisplay
  --- Volume group ---
  VG Name               diskvg
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       0 / 0
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               itBEsX-bKdl-lqdf-qd8y-07AG-W12i-lFIvL0

  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               99.00 GiB
  PE Size               4.00 MiB
  Total PE              25344
  Alloc PE / Size       25344 / 99.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               280QHe-8dkW-t34f-9JKh-QhMk-1rZE-cejqal

基于卷组(VG)创建逻辑卷(LV) 通过 lvcreate 命令

lvcreate -n app1 -L 5G diskvg (基于 diskvg 创建逻辑卷 lv,名字为 app,大小为 5G)

[root@zabbix-server ~]# lvcreate -n app1 -L 5G diskvg
  Logical volume "app1" created.

用 lvdisplay 或 lvs 命令查看创建好的逻辑卷。可以看到名字为 app1 的逻辑卷 lv 已经创建好了,它是基于 diskvg 创建的,大小为 5G

[root@zabbix-server ~]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 91.00g
  swap centos -wi-ao----  8.00g
  app1 diskvg -wi-a-----  5.00g

格式化逻辑卷

[root@zabbix-server ~]# mkfs -t xfs  /dev/diskvg/app1
meta-data=/dev/diskvg/app1       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@zabbix-server ~]# mkdir /app1
[root@zabbix-server ~]# mount  /dev/diskvg/app1 /app1
[root@zabbix-server ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        91G  1.2G   90G   2% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               xfs       497M  139M  359M  28% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0

添加开机启动

echo "/dev/diskvg/app1 /app1 xfs defaults 0 0" >>/etc/fstab

扩容逻辑卷

查看 vg 信息

[root@zabbix-server app1]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   1   2   0 wz--n- 99.00g     0
  diskvg   2   1   0 wz--n- 19.99g 14.99g

卸载文件系统与挂载点关联

umount /app1

扩展逻辑卷至 10G

[root@zabbix-server /]# lvextend  -L 10G /dev/diskvg/app1
  Size of logical volume diskvg/app1 changed from 5.00 GiB (1280 extents) to 10.00 GiB (2560 extents).
  Logical volume diskvg/app1 successfully resized.

挂载文件系统

mount /dev/diskvg/app1 /app1 或 mount -a

重置 LV 容量

[root@zabbix-server /]# xfs_growfs /dev/diskvg/app1
meta-data=/dev/mapper/diskvg-app1 isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=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               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 1310720 to 2621440

查看扩容硬盘信息

admin

admin

云原生架构 · 全栈开发

感谢阅读本文!记录系统架构思考与实战复盘。专注于 Python、Django、PostgreSQL 与云原生容器化工程实践。