2021-11-08 linux硬盘动态扩容,增加home空间。

需求:生产环境由于原有磁盘home目录的空间不足,希望增加空间存储大小,但又不希望数据丢失,以及业务中断

分析:首先要检查磁盘分区是否磁盘分区格式是否为lvm

1、vgdisplay

如图所显示磁盘分区格式为lvm(逻辑卷)

操作步骤:1、创建pv。

pvcreate /dev/sdb #新增磁盘

即 pvcreate /deb/sdb

2、将pv加入vg。

vgextend 卷组名 /dev/sdb

即vgextend centos /dev/sdb

3、扩容逻辑卷,并制作xfs文件系统

lvextend -r l +100%FREE /dev/mapper/centos/centos-home/

异常处理,创建pv报错,/dev/sda not found

第一种情况:

第一种情况:

①先列出硬盘分区:

[root@centos7 ~]# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x0008e2e1

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 83886046 41941999+ 83 Linux

Disk /dev/vdb: 50.5 GB, 50465865728 bytes, 98566144 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
从列出的信息可以看到,我事先分配的硬盘分区为:/dev/vdb.但是没有sdb,那么我就改用vdb了。虽然有分区了,但是需要经过一系列操作才能生效。

②如下:

[root@centos7 ~]# fdisk /dev/vdb
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 0xec0e9e9e.

Command (m for help): p

Disk /dev/vdb: 50.5 GB, 50465865728 bytes, 98566144 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: 0xec0e9e9e

Device Boot Start End Blocks Id System

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

Command (m for help): p

Disk /dev/vdb: 50.5 GB, 50465865728 bytes, 98566144 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: 0xec0e9e9e

Device Boot Start End Blocks Id System
/dev/vdb1 2048 98566143 49282048 83 Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
其中按提示操作 p打印 n新增 d 删除 w操作生效 q退出。

③查看分区是否完成分配。

[root@centos7 ~]# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x0008e2e1

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 83886046 41941999+ 83 Linux

Disk /dev/vdb: 50.5 GB, 50465865728 bytes, 98566144 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: 0xec0e9e9e

Device Boot Start End Blocks Id System
/dev/vdb1 2048 98566143 49282048 83 Linux
可以看到,出现了/dev/vdb1分区表已成功分配。

④更新分区表,并查看系统是否接收新的分区表。

[root@centos7 ~]# partprobe
[root@centos7 ~]# cat /proc/partitions
major minor #blocks name
253 0 41943040 vda
253 1 41941999 vda1
253 16 49283072 vdb
253 17 49282048 vdb1
上述信息出现vdb的两条信息说明已成功更新分区表。

⑤格式化新加的分区表

[root@centos7 ~]# mkfs.ext3 /dev/vdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3080192 inodes, 12320512 blocks
616025 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
376 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
那么到此就完成了,我的目标是完成硬盘分区来创建物理卷。

⑥查看系统有无PV

[root@centos7 ~]# pvscan
No matching physical volumes found
没有。

⑦这里我将刚才创建的分区 /dev/vdb1作为物理卷。

[root@centos7 ~]# pvcreate /dev/vdb1
WARNING: ext3 signature detected on /dev/vdb1 at offset 1080. Wipe it? [y/n]: y
Wiping ext3 signature on /dev/vdb1.
Physical volume “/dev/vdb1” successfully created.
⑧再次查看

[root@centos7 ~]# pvscan
PV /dev/vdb1 lvm2 [<47.00 GiB]
Total: 1 [<47.00 GiB] / in use: 0 [0 ] / in no VG: 1 [<47.00 GiB]
[root@centos7 ~]# pvdisplay
“/dev/vdb1” is a new physical volume of “<47.00 GiB”
— NEW Physical volume —
PV Name /dev/vdb1
VG Name
PV Size <47.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID p9pkiw-Pd5G-mKFY-eTqw-xYEv-56tJ-SLa4te
成功。

⑨查看物理卷组,发现没有,并创建volume group 为stack-volumes-lvmdriver-1

[root@centos7 ~]# vgs
[root@centos7 ~]# vgcreate stack-volumes-lvmdriver-1 /dev/vdb1
Volume group “stack-volumes-lvmdriver-1” successfully created
[root@centos7 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
stack-volumes-lvmdriver-1 1 0 0 wz–n- <47.00g <47.00g
完成。。。

如果想挂载硬盘到某个目录下,请继续。。。

将home目录全部切换到新硬盘

⑩挂载硬盘到某一目录下,比如是/mnt.

cd /mnt/

mkdir home

挂载到/mnt/home

mount /dev/vdb1 /mnt/home

查看

df -h

把home下的东西拷到挂载的目录下,备份

cp -a /home/* /mnt/home/

把home下的东西删干净

rm -rf /home/*

卸载硬盘

umount /dev/vdb1

查看

df -h

⑪设置开机挂载

vi /etc/fstab

末尾增加一行

/dev/vdb1 home ext3 defaults 1 2

保存退出

查看 /home是否被挂载

df -h

挂载/etc/fstab 中未挂载的分区

mount -a

查看

df -h

猜你喜欢

转载自blog.csdn.net/weixin_47985145/article/details/121212179
今日推荐