——————————————————————————————

磁盘原有空间21G,这次试着添加1G空间,并把它扩充到根目录下。

  • 关闭虚拟机,在“设置”中定位磁盘文件位置并记录下来



     
    【转】VirtualBox下扩容磁盘过程记录_centos
    14.png
  • 定位到VirtualBox程序目录,在地址栏输入“cmd"进入命令模式,并输入以下命令:



C:\Program Files\Oracle\VirtualBox>VBoxManage modifyhd F:\vbox\centos7.vdi --resize 22528
# 22528=22*1024


  • 打开虚拟机电源,并SSH登录上去



[root@docker-app ~]# fdisk -l

磁盘 /dev/sda:23.6 GB, 23622320128 字节,46137344 个扇区
......
# 可以看出磁盘空间确实增加了!
[root@docker-app ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 11G 6.7G 61% /
# 根分区为17G
devtmpfs 1.2G 0 1.2G 0% /dev
......


  • 创建新分区



[root@docker-app ~]# fdisk /dev/sda
# 输入命令n,开始创建新分区,采用默认值就好;我这台因为已经有了3个分区,所以先创建了一个扩展分区/dev/sda4 ,再在下面创建一个逻辑分区/dev/sda5;
命令(输入 m 获取帮助):n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e):
Using default response e
已选择分区 4
起始 扇区 (44040192-46137343,默认为 44040192):
将使用默认值 44040192
Last 扇区, +扇区 or +size{K,M,G} (44040192-46137343,默认为 46137343):
将使用默认值 46137343
分区 4 已设置为 Extended 类型,大小设为 1 GiB
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (44042240-46137343,默认为 44042240):
将使用默认值 44042240
Last 扇区, +扇区 or +size{K,M,G} (44042240-46137343,默认为 46137343):
将使用默认值 46137343
分区 5 已设置为 Linux 类型,大小设为 1023 MiB

命令(输入 m 获取帮助):p

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

设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
/dev/sda3 41943040 44040191 1048576 83 Linux
/dev/sda4 44040192 46137343 1048576 5 Extended
/dev/sda5 44042240 46137343 1047552 83 Linux

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。
[root@docker-app ~]# partprobe # 不重启使分区生效


PS: 划分好分区后,一定要用w命令写入!

  • 格式化分区



[root@docker-app ~]#  mkfs.ext4 /dev/sda5
mke2fs 1.42.9 (28-Dec-2013)
......
Allocating group tables: 完成


ps: 其实现在就可以使用mount命令将这个新分区挂载到一个已存在的目录上(但该目录下原有文件会丢失,需要先复制或压缩到其他位置,挂载完成后再复制回来),这里我们要把新加的空间扩充到根目录下,所以继续下面的操作。

  • 将格式后的sda5分区添加为物理卷




[root@docker-app ~]# pvcreate /dev/sda5
WARNING: ext4 signature detected on /dev/sda5 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sda5.
Physical volume "/dev/sda5" successfully created.


  • 查看物理卷及卷组情况



[root@docker-app ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
.......
"/dev/sda5" is a new physical volume of "1023.00 MiB"
--- NEW Physical volume ---
PV Name /dev/sda5
VG Name # 这里为空显示还未加入卷组
.......
[root@docker-app ~]# vgdisplay
--- Volume group ---
VG Name centos
......
VG Size <19.00 GiB # 卷组大小
....


  • 将sda5加入centos组



[root@docker-app ~]# vgextend centos  /dev/sda5
Volume group "centos" successfully extended
[root@docker-app ~]# vgdisplay
--- Volume group ---
VG Name centos
......
VG Size 19.99 GiB # 卷组容量增加了1G


  • 查看逻辑卷情况



[root@docker-app ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
......
--- Logical volume ---
LV Path /dev/centos/root # 记住这个路径名,下面要用
LV Name root # 根目录所在卷
LV Size <17.00 GiB # 还未扩容
......


  • 将新增的逻辑卷全部扩展到“/”分区中



[root@docker-app ~]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to 17.99 GiB (4606 extents).
Logical volume centos/root successfully resized.
[root@docker-app ~]# lvdisplay /dev/centos/root
--- Logical volume ---
LV Path /dev/centos/root
......
LV Size 17.99 GiB # 容量增加了
......


  • 刷新根分区/dev/centos/root的容量



 [root@docker-app ~]# cat /etc/fstab |grep root
/dev/mapper/centos-root / xfs defaults 0 0
# xfs文件系统,要用xfs_growfs命令,而不是resize2fs命令
[root@docker-app ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks
......
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 4455424 to 4716544


  • 最后确认一下根分区容量



[root@docker-app ~]# df -hl /
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 18G 11G 7.7G 58% /


PS: 要查看vg\pv\lv的情况,使用vgs/pvs/lvs也很方便



[root@docker-app ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sda5 centos lvm2 a-- 1020.00m 0
[root@docker-app ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 17.99g
swap centos -wi-ao---- 2.00g
[root@docker-app ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 19.99g 0