操作过程-CentOS7下添加新硬盘扩充已经存在的逻辑卷分区的存储空间

时间:2022-05-27 23:06:52
操作过程-CentOS7下添加新硬盘扩充已经存在的逻辑卷分区的存储空间

Linux添加硬盘扩充已有分区存储空间方式

总体步骤

  • 磁盘初始化分区

  • 创建物理卷

  • 扩展卷组

  • 扩展逻辑卷

  • 通知文件系统生效

磁盘初始化分区

[root@oracledb ~]# fdisk -l

磁盘 /dev/sda:214.7 GB, 214748364800 字节,419430400 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0000373a 设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 419430399 208665600 8e Linux LVM
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. 磁盘 /dev/sdb:1099.5 GB, 1099511627776 字节,2147483648 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt
Disk identifier: 0416CD57-7203-48B1-8D1C-6604E73EAFE5 # Start End Size Type Name
1 2048 2147483614 1024G Linux filesyste 磁盘 /dev/mapper/centos-root:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘 /dev/mapper/centos-swap:8455 MB, 8455716864 字节,16515072 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘 /dev/mapper/centos-home:151.5 GB, 151523426304 字节,295944192 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

已有磁盘200G,添加一块1T的磁盘

[root@oracledb ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。 命令(输入 m 获取帮助):p 磁盘 /dev/sdb:1099.5 GB, 1099511627776 字节,2147483648 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x55c66695 设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2147483647 1073740800 83 Linux 命令(输入 m 获取帮助):m
命令操作
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) 命令(输入 m 获取帮助):g
Building a new GPT disklabel (GUID: 0416CD57-7203-48B1-8D1C-6604E73EAFE5) 命令(输入 m 获取帮助):p 磁盘 /dev/sdb:1099.5 GB, 1099511627776 字节,2147483648 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt
Disk identifier: 0416CD57-7203-48B1-8D1C-6604E73EAFE5 # Start End Size Type Name
命令(输入 m 获取帮助):n
分区号 (1-128,默认 1):
第一个扇区 (2048-2147483614,默认 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-2147483614,默认 2147483614):
已创建分区 1 命令(输入 m 获取帮助):p 磁盘 /dev/sdb:1099.5 GB, 1099511627776 字节,2147483648 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt
Disk identifier: 0416CD57-7203-48B1-8D1C-6604E73EAFE5 # Start End Size Type Name
1 2048 2147483614 1024G Linux filesyste
命令(输入 m 获取帮助):w
The partition table has been altered! Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@oracledb ~]#

执行结果

[root@oracledb ~]# fdisk -l

磁盘 /dev/sda:214.7 GB, 214748364800 字节,419430400 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0000373a 设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 419430399 208665600 8e Linux LVM
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. 磁盘 /dev/sdb:1099.5 GB, 1099511627776 字节,2147483648 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt
Disk identifier: 0416CD57-7203-48B1-8D1C-6604E73EAFE5 # Start End Size Type Name
1 2048 2147483614 1024G Linux filesyste 磁盘 /dev/mapper/centos-root:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘 /dev/mapper/centos-swap:8455 MB, 8455716864 字节,16515072 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘 /dev/mapper/centos-home:151.5 GB, 151523426304 字节,295944192 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

第二块磁盘分区已经创建,分配空间1T(全部空间)

创建物理卷

现有物理卷如下:

[root@oracledb ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <199.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 50943
Free PE 1
Allocated PE 50942
PV UUID mBYtSA-2Hzk-QXQd-cDxv-P4Mu-6tX4-UVsrUd

创建物理卷

[root@oracledb dev]# pvcreate /dev/sdb1
WARNING: ext3 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
Wiping ext3 signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created. [root@oracledb dev]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <199.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 50943
Free PE 1
Allocated PE 50942
PV UUID mBYtSA-2Hzk-QXQd-cDxv-P4Mu-6tX4-UVsrUd "/dev/sdb1" is a new physical volume of "<1024.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <1024.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 1aGhki-as5k-8Eqh-Hraf-HS9x-SgZT-mij9Hz

扩展卷组

现有卷组

[root@oracledb dev]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size <199.00 GiB
PE Size 4.00 MiB
Total PE 50943
Alloc PE / Size 50942 / 198.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID 5BZGt4-mAIC-EAS1-o2T8-hyAD-1gXU-sG7Pwt

执行扩展

[root@oracledb dev]# vgextend centos /dev/sdb1
Volume group "centos" successfully extended
[root@oracledb dev]# 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 3
Open LV 3
Max PV 0
Cur PV 2
Act PV 2
VG Size 1.19 TiB
PE Size 4.00 MiB
Total PE 313086
Alloc PE / Size 50942 / 198.99 GiB
Free PE / Size 262144 / 1.00 TiB
VG UUID 5BZGt4-mAIC-EAS1-o2T8-hyAD-1gXU-sG7Pwt

扩展逻辑卷

现有逻辑卷

[root@oracledb dev]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID VnspUh-5yWi-xLcu-LWeK-d3Bx-YfqO-i5opYp
LV Write Access read/write
LV Creation host, time localhost, 2017-10-17 19:44:08 +0800
LV Status available
# open 2
LV Size <7.88 GiB
Current LE 2016
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1 --- Logical volume ---
LV Path /dev/centos/home
LV Name home
VG Name centos
LV UUID 0vTVTH-bxb8-02kQ-QWvd-yLdY-olDQ-dXacRh
LV Write Access read/write
LV Creation host, time localhost, 2017-10-17 19:44:09 +0800
LV Status available
# open 1
LV Size <141.12 GiB
Current LE 36126
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2 --- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID HzVl83-V4BV-V6OA-32IE-fnPR-NPwF-4KPvk5
LV Write Access read/write
LV Creation host, time localhost, 2017-10-17 19:44:11 +0800
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12800
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

扩展/dev/centos/home卷(/home分区)

[root@oracledb dev]# lvextend -l +100%FREE /dev/centos/home
Size of logical volume centos/home changed from <141.12 GiB (36126 extents) to <1.14 TiB (298270 extents).
Logical volume centos/home successfully resized. [root@oracledb dev]#
[root@oracledb dev]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID VnspUh-5yWi-xLcu-LWeK-d3Bx-YfqO-i5opYp
LV Write Access read/write
LV Creation host, time localhost, 2017-10-17 19:44:08 +0800
LV Status available
# open 2
LV Size <7.88 GiB
Current LE 2016
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1 --- Logical volume ---
LV Path /dev/centos/home
LV Name home
VG Name centos
LV UUID 0vTVTH-bxb8-02kQ-QWvd-yLdY-olDQ-dXacRh
LV Write Access read/write
LV Creation host, time localhost, 2017-10-17 19:44:09 +0800
LV Status available
# open 1
LV Size <1.14 TiB
Current LE 298270
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2 --- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID HzVl83-V4BV-V6OA-32IE-fnPR-NPwF-4KPvk5
LV Write Access read/write
LV Creation host, time localhost, 2017-10-17 19:44:11 +0800
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12800
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

通知文件系统生效

检查文件系统类型

[root@oracledb dev]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Oct 17 19:44:14 2017
#
# 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
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=d3ec0225-abeb-47ef-a209-9c1ba481bb02 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0

注意,xfs文件系统使用 xfs_growfs,ext文件系统使用 resize2fs;xfs文件系统只支持增大不支持缩小

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

调整成功

[root@oracledb dev]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 7.8G 43G 16% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 4.0K 7.8G 1% /dev/shm
tmpfs 7.8G 9.1M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 1014M 234M 781M 24% /boot
/dev/mapper/centos-home 1.2T 2.3G 1.2T 1% /home
tmpfs 1.6G 0 1.6G 0% /run/user/1001
tmpfs 1.6G 12K 1.6G 1% /run/user/42
tmpfs 1.6G 0 1.6G 0% /run/user/0
tmpfs 1.6G 0 1.6G 0% /run/user/1000