虚拟机扩充磁盘亲测 (centos 7 )

首先确认我用的centos 7 ,不过centos 6原理应该一样。

**第一步:

就是在VM 上把磁盘进行扩展,打开VM->编辑虚拟机–>硬件—>磁盘---->扩展.

Centos7添加新磁盘 centos7扩展磁盘_文件系统

第二步

打开虚拟机进入;用root用户查看,并且进行添加分区。

进入后首先查看:df -h

Centos7添加新磁盘 centos7扩展磁盘_Centos7添加新磁盘_02


可以看到我的可用只剩了3.6G,

再查看分区情况:fdisk -l

Centos7添加新磁盘 centos7扩展磁盘_硬盘分区_03


可以看到 /dev/sda 下的空间变成了100多G ,这里一般没做过分区的应该只有2个 /dev/sda1 /dev/sad2

,那个/dev/sda3 是我弄的别管 如果你们只到2 那后面就新建2即可。

下面开始分区

输入:fdisk /dev/sda

The number of cylinders for this disk is set to 5221. 
There is nothing wrong with that, but this is larger than 1024, 
and could in certain setups cause problems with: 
1) software that runs at boot time (e.g., old versions of LILO) 
2) booting and partitioning software from other OSs 
(e.g., DOS FDISK, OS/2 FDISK) 
Command (m for help): m //查看帮助

Command action 
a toggle a bootable flag 
b edit bsd disklabel 
c toggle the dos compatibility flag 
d delete a partition 
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 // n 是增加分区

Command action 
e extended 
p primary partition (1-4) 
p   //" 选择创建主分区"此时

Partition number (1-4): 4  //已经就1,2,3两个分区了,这里分为第四个区

First cylinder (2611-5221, default 2611):   //此时,fdisk又会让你选择该分区的开始值这个就是分区的Start 值(start cylinder);这里最好直接按回车
Using default value 2611 
Last cylinder or +size or +sizeM or +sizeK (2611-5221, default 5221):   //此时,fdisk又会让你选择该分区的开始值这个就是分区的End 值这里最好直接按回车
Using default value 5221 

Command (m for help):  w  //w "保存所有并退出,分区划分完毕"
The partition table has been altered! 

Calling ioctl() to re-read partition table. 

WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 
The kernel still uses the old table. 
The new table will be used at the next reboot. 
Syncing disks. 
[root@rac2 ~]# fdisk -l 

Disk /dev/sda: 42.9 GB, 42949672960 bytes 
255 heads, 63 sectors/track, 5221 cylinders 
Units = cylinders of 16065 * 512 = 8225280 bytes 

Device Boot Start End Blocks Id System 
/dev/sda1 * 1 13 104391 83 Linux 
/dev/sda2 14 2610 20860402+ 8e Linux LVM 
/dev/sda3 2611 5221 20972857+ 83 Linux

Centos7添加新磁盘 centos7扩展磁盘_centos_04


看好我的是第4块了,如果之前没添加过的就是3.

下一步:

使用fdisk将其改成LVM的;
[root@xue01 xue]# fdisk /dev/sda

The number of cylinders for this disk is set to 5221. 
There is nothing wrong with that, but this is larger than 1024, 
and could in certain setups cause problems with: 
1) software that runs at boot time (e.g., old versions of LILO) 
2) booting and partitioning software from other OSs 
(e.g., DOS FDISK, OS/2 FDISK) 

Command (m for help): m 
Command action 
a toggle a bootable flag 
b edit bsd disklabel 
c toggle the dos compatibility flag 
d delete a partition 
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): t //改变分区系统id
Partition number (1-4): 4 //指定分区号,选择分区3

Hex code (type L to list codes): 8e //指定要改成的id号,8e代表LVM
Changed system type of partition 3 to 8e (Linux LVM) 

Command (m for help): w    //w "保存所有并退出,分区划分完毕"
The partition table has been altered! 

Calling ioctl() to re-read partition table. 

WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 
The kernel still uses the old table. 
The new table will be used at the next reboot. 
Syncing disks.

然后:fdisk -l

你会看到你添加的分区变为了LVM

Centos7添加新磁盘 centos7扩展磁盘_文件系统_05


第三步:

已经增加了一个分区,格式也改为LVM

#mkfs -t ext3 /dev/sda4 //在硬盘分区“/dev/sda4”上创建“ext3”文件系统

然后重启系统,准备格式化该新添加的分区

[root@xue01 xue]# mkfs -t ext3 /dev/sda4

Centos7添加新磁盘 centos7扩展磁盘_硬盘分区_06


然后开始扩展文件系统:

创建PV

[root@xue01 xue]# pvcreate /dev/sda4

Centos7添加新磁盘 centos7扩展磁盘_硬盘分区_07


//pvcreate指令用于将物理硬盘分区初始化为物理卷,以便被LVM使用。要创建物理卷必须首先对硬盘进行分区,并且将硬盘分区的类型设置为“8e”后,才能使用pvcreat指令将分区初始化为物理卷。扩展VG

[root@xue01 xue]# vgextend contes /dev/sda4

(其中是当前需要扩充的lvm组名,可以通过vgdisplay 查看,例如我的是: contes) //vgextend指令用于动态的扩展卷组,它通过向卷组中添加物理卷来增加卷组的容量。

Centos7添加新磁盘 centos7扩展磁盘_硬盘分区_08

[root@xue01 xue]# vgextend contes(写的是上面的VG name) /dev/sda4

Centos7添加新磁盘 centos7扩展磁盘_Centos7添加新磁盘_09


可以再次vgdisplay 查看;你会发现 Free PE / Size 640 / 20.00 GB 这行会变为你扩展的磁盘大小。下一步:

(主要查看Free PE / Size 640 / 20.00 GB,说明我们最多可以有20GB的扩充空间。)

扩展LV,这里的扩展空间要小于VG的Free PE

我是写了48G,看自己添加了的大小。

其中/dev/mapper/centos-root 是你df -h 中显示的路径

Centos7添加新磁盘 centos7扩展磁盘_硬盘分区_10


[root@xue01 xue]#lvextend -L+48G /dev/mapper/centos-root /dev/sda4

Centos7添加新磁盘 centos7扩展磁盘_centos_11


再查看VG的信息,你会发现空闲的空间就没了。

vgdisplay最后一步了:

//resize2fs指令被用来增大或者收缩未加载的“ext2/ext3”文件系统的大小。

这个执行前得看你的文件系统

确认文件系统是xfs:

[root@xue01 xue]# cat /etc/fstab | grep centos-root

/dev/mapper/centos-root / xfs defaults 0 0

如果是那就执行:

[root@xue01 xue] # resize2fs /dev/mapper/centos-root --这个目录还是你df -h那看到的那个

Centos7添加新磁盘 centos7扩展磁盘_硬盘分区_12


最后再df -h 可以看到成功了。