将新硬盘加入到根目录

1. 将新硬盘加入到根目录(逻辑卷LVM)

安装系统时采用了LVM分区,根目录只有9.7G

1
2
3
4
5
6
7
8
9
[root@edgex ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 8.0G 8.0G 67M 100% /
/dev/sda1 1014M 275M 740M 28% /boot
tmpfs 379M 0 379M 0% /run/user/0

查看目前虚拟机的磁盘情况,看到新增磁盘:/dev/sdb1 50G

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@edgex ~]# fdisk -l

Disk /dev/sda: 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: 0x000beec6

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 20971519 9436160 8e Linux LVM

Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/centos-root: 8585 MB, 8585740288 bytes, 16769024 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 /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 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

初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@edgex ~]# 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 0x795ab816.

Command (m for help): o
Building a new DOS disklabel with disk identifier 0x51f639a5.

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

Command (m for help): t
Selected partition 1
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.

创建pv ,通过pvcreate命令将磁盘/dev/sdb1创建为一个系统PV

1
2
[root@edgex ~]# pvcreate /dev/sdb1 
Physical volume "/dev/sdb1" successfully created.

查看pv

1
2
3
4
[root@edgex ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<9.00 GiB / 0 free]
PV /dev/sdb1 lvm2 [<50.00 GiB]
Total: 2 [<59.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 1 [<50.00 GiB]

查看vg

1
2
3
[root@edgex ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <9.00g 0

将 PV /dev/sdb1添加到卷组centos中

1
2
[root@edgex ~]# vgextend centos /dev/sdb1
Volume group "centos" successfully extended

查看vg,已经增加为58G

1
2
3
[root@edgex ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 58.99g <50.00g

为根目录添加20G的空间。通过df -h查看根目录的lv名称为 /dev/mapper/centos-root

1
2
3
4
5
6
7
8
9
[root@edgex ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 8.0G 8.0G 67M 100% /
/dev/sda1 1014M 275M 740M 28% /boot
tmpfs 379M 0 379M 0% /run/user/0

增加

1
2
[root@edgex ~]# lvextend -L +50G -n /dev/mapper/centos-root
Insufficient free space: 12800 extents needed, but only 12799 available

发现报错了,空间不够,所以 降低1G输入如下命令:

1
2
3
[root@edgex ~]# lvextend -L +49G -n /dev/mapper/centos-root
Size of logical volume centos/root changed from <8.00 GiB (2047 extents) to <57.00 GiB (14591 extents).
Logical volume centos/root successfully resized.

逻辑卷扩展后并不会马上生效,需要使用“resize2fs”命令重新加载逻辑卷的大小

1
2
3
4
[root@edgex ~]# resize2fs /dev/mapper/centos-root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
Couldn't find valid filesystem superblock.

使用 resize2fs时,提示错误,了解后发现,xfs格式的文件扩充需要使用 xfs_growfs

查看/dev/mapper/centos-root的格式

1
2
3
4
5
6
7
8
9
[root@edgex ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 8.0G 8.0G 67M 100% /
/dev/sda1 xfs 1014M 275M 740M 28% /boot
tmpfs tmpfs 379M 0 379M 0% /run/user/0

查看得知为xfs,使用xfs_growfs扩充

1
2
3
4
5
6
7
8
9
10
11
[root@edgex ~]# xfs_growfs /dev/mapper/centos-root 
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=524032 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=2096128, 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 2096128 to 14941184

至此扩充完毕。

-------------本文结束感谢您的阅读-------------