linux-LVM磁盘扩容
阅读原文时间:2023年07月13日阅读:1

查看磁盘

[ops@stock_kline_database ~]$ sudo fdisk -l

磁盘 /dev/sda:42.9 GB, 字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节
磁盘标签类型:dos
磁盘标识符:0x0006ba9b

设备 Boot Start End Blocks Id System
/dev/sda1 * Linux
/dev/sda2 8e Linux LVM

磁盘 /dev/sdb:493.9 GB, 字节, 个扇区 【已添加到LVM的磁盘,已经格式化】
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节
磁盘标签类型:dos
磁盘标识符:0x8e329555

设备 Boot Start End Blocks Id System
/dev/sdb1 8e Linux LVM

磁盘 /dev/mapper/rhel-root:37.6 GB, 字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节

磁盘 /dev/mapper/rhel-swap: MB, 字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节

磁盘 /dev/mapper/vgdata-lvdata:493.5 GB, 字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节

磁盘 /dev/sdc:214.7 GB, 字节, 个扇区 【未格式化的磁盘】
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节

[ops@stock_kline_database ~]$

注意上面已经格式化与未格式化的磁盘。【未格式化的磁盘就是要添加到LVM的磁盘】

还可以通过vgdisplay和pvdisplay两个命令查看已经添加到lvm的磁盘!

[ops@stock_kline_database ~]$ df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 35G .2G 30G % /
devtmpfs 16G 16G % /dev
tmpfs 16G 16G % /dev/shm
tmpfs 16G 212M 16G % /run
tmpfs 16G 16G % /sys/fs/cgroup
/dev/sda1 976M 112M 798M % /boot
/dev/mapper/vgdata-lvdata 453G 312G 119G % /data
tmpfs .2G .2G % /run/user/
tmpfs .2G .2G % /run/user/

格式化新添加的磁盘为lvm格式

小于2T的磁盘使用fdisk格式化:

:sudo fdisk /dev/sdc 格式化磁盘
进入交互界面
:Command (m for help): n 添加一个分区
Partition type:
p primary ( primary, extended, free)
e extended
Select (default p): p #主分区
Partition number (-, default ): #默认即可
First sector (-, default ): #默认即可
Using default value
Last sector, +sectors or +size{K,M,G} (-, default ):
Using default value
Partition of type Linux and of size GiB is set
Command (m for help): t # 选择格式化分区类型
Selected partition
Hex code (type L to list all codes): L # 会打印所有的类型的代码,选择8e
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

详细过程如下:

sudo fdisk /dev/sdf
Welcome to fdisk (util-linux 2.23.).

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

Command (m for help): p

Disk /dev/sdf: 859.0 GB, bytes, sectors
Units = sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk label type: dos
Disk identifier: 0xa8cd607c

Device Boot Start End Blocks Id System

Command (m for help):
: unknown command
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): n
Partition type:
p primary ( primary, extended, free)
e extended
Select (default p): p
Partition number (-, default ):
First sector (-, default ):
Using default value
Last sector, +sectors or +size{K,M,G} (-, default ):
Using default value
Partition of type Linux and of size GiB is set

Command (m for help): t
Selected partition
Hex code (type L to list all codes): L

Empty NEC DOS Minix / old Lin bf Solaris
FAT12 Hidden NTFS Win Linux swap / So c1 DRDOS/sec (FAT-
XENIX root Plan Linux c4 DRDOS/sec (FAT-
XENIX usr 3c PartitionMagic OS/ hidden C: c6 DRDOS/sec (FAT-
FAT16 <32M Venix Linux extended c7 Syrinx
Extended PPC PReP Boot NTFS volume set da Non-FS data
FAT16 SFS NTFS volume set db CP/M / CTOS / .
HPFS/NTFS/exFAT 4d QNX4.x Linux plaintext de Dell Utility
AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
AIX bootable 4f QNX4.x 3rd part Amoeba e1 DOS access
a OS/ Boot Manag OnTrack DM Amoeba BBT e3 DOS R/O
b W95 FAT32 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
OPUS EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
Hidden FAT12 Golden Bow a8 Darwin UFS f1 SpeedStor
Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
Hidden FAT16 < SpeedStor ab Darwin boot f2 DOS secondary
Hidden FAT16 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
Hidden HPFS/NTF Novell Netware b7 BSDI fs fc VMware VMKCORE
AST SmartSleep Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 Old Minix
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

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

格式化磁盘为LVM格式

格式化后的磁盘如下:

磁盘 /dev/sdc:214.7 GB, 字节, 个扇区
Units = 扇区 of * = bytes
扇区大小(逻辑/物理): 字节 / 字节
I/O 大小(最小/最佳): 字节 / 字节
磁盘标签类型:dos
磁盘标识符:0x6861bee8

设备 Boot Start End Blocks Id System
/dev/sdc1 8e Linux LVM

创建物理卷

[ops@stock_kline_database ~]$ sudo pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created.
[ops@stock_kline_database ~]$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               rhel
  PV Size               <39.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              9983
  Free PE               0
  Allocated PE          9983
  PV UUID               WLW0XL-n3le-1W7N-S6Vd-fYmE-Z1kf-dtkUXW
   
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vgdata
  PV Size               <460.00 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              117759
  Free PE               100
  Allocated PE          117659
  PV UUID               wF3cCs-ecG8-OYTT-kWHk-Vjgm-5WqE-zfZpc9
   
  "/dev/sdc1" is a new physical volume of "<200.00 GiB" #新创建的物理卷
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               <200.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               oq2NDg-GLrY-YMPO-id6F-B6mQ-dpl7-T0C7OT

把新创建的物理卷添加到逻辑卷组中,注意服务器上可能不止一个lvm卷,选择对应的名称!【这里有两个物理卷,新加入的是vgdata这个卷名】

[ops@stock_kline_database ~]$ sudo vgdisplay
--- Volume group ---
VG Name rhel
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size <39.00 GiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size / <39.00 GiB
Free PE / Size /
VG UUID fDS5Qs-jmUa-WTAy-Pojt-zGvt-VtJt-UftWNG

--- Volume group ---
VG Name vgdata
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size <460.00 GiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size / <459.61 GiB
Free PE / Size / 400.00 MiB
VG UUID XIqWiX-jTEm-8xCa-rp2j-TmW3-kmHW-YT8Biw

[ops@stock_kline_database ~]$ sudo vgextend vgdata /dev/sdc1 # 扩展逻辑卷
Volume group "vgdata" successfully extended
[ops@stock_kline_database ~]$ sudo lvextend /dev/mapper/vgdata-lvdata /dev/sdc1 # 扩展lvm卷
Size of logical volume vgdata/lvdata changed from <459.61 GiB ( extents) to 659.60 GiB ( extents).
Logical volume vgdata/lvdata successfully resized.

查看磁盘空间以及文件系统格式:

[ops@stock_kline_database ~]$ df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root ext4 35G .2G 30G % /
devtmpfs devtmpfs 16G 16G % /dev
tmpfs tmpfs 16G 16G % /dev/shm
tmpfs tmpfs 16G 212M 16G % /run
tmpfs tmpfs 16G 16G % /sys/fs/cgroup
/dev/sda1 ext4 976M 112M 798M % /boot
/dev/mapper/vgdata-lvdata ext4 453G 311G 119G % /data
tmpfs tmpfs .2G .2G % /run/user/
tmpfs tmpfs .2G .2G % /run/user/

然后执行最后一步,扩展磁盘:ext4系统和xfs使用不同的命令:

[ops@stock_kline_database ~]$ sudo resize2fs /dev/mapper/vgdata-lvdata
resize2fs 1.42. (-Dec-)
Filesystem at /dev/mapper/vgdata-lvdata is mounted on /data; on-line resizing required
old_desc_blocks = , new_desc_blocks =
The filesystem on /dev/mapper/vgdata-lvdata is now blocks long.

[ops@stock_kline_database ~]$ df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 35G .2G 30G % /
devtmpfs 16G 16G % /dev
tmpfs 16G 16G % /dev/shm
tmpfs 16G 212M 16G % /run
tmpfs 16G 16G % /sys/fs/cgroup
/dev/sda1 976M 112M 798M % /boot
/dev/mapper/vgdata-lvdata 650G 311G 308G % /data
tmpfs .2G .2G % /run/user/
tmpfs .2G .2G % /run/user/

可以看到磁盘空间已经增加!

xfs格式系统扩展如下:

[ops@VM100 ~]$ sudo xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize= agcount=, agsize= blks
= sectsz= attr=, projid32bit=
= crc= finobt= spinodes=
data = bsize= blocks=, imaxpct=
= sunit= swidth= blks
naming =version bsize= ascii-ci= ftype=
log =internal bsize= blocks=, version=
= sectsz= sunit= blks, lazy-count=
realtime =none extsz= blocks=, rtextents=
data blocks changed from to