安装完virtualbox的虚拟机,一开始全部按默认设置的,往往还没有开始爽起来,就发现磁盘不够用了。怎么办?重装又得重新来过,而且太没有技术含量了。按照本文的流程,将帮助你化解这一个尴尬的境地,让virtualbox虚拟机的硬盘扩容一点都不麻烦。

1.VBoxManager扩展VDI大小

这个很简单,在虚拟机关闭后通过命令行即可迅速搞定。

cd C:\Program Files\Oracle\VirtualBox
VBoxManage.exe list hdds
UUID: e18a527c-57eb-488d-8953-e8e6f2b56f34
Parent UUID: base
State: created
Type: normal (base)
Location: D:\vm\centos6\centos.vdi
Storage format: VDI
Capacity: 8192 MBytes
Encryption: disabled

找到对应vdi的UUID,将其扩展到40G

VBoxManage.exe modifyhd e18a527c-57eb-488d-8953-e8e6f2b56f34 --resize 40906
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

这个很快搞定,通过虚拟机的设置查看,硬盘的确变成了40G。但是启动虚拟机后,通过df -h一看,会发现硬盘还是只有8G。所以,还得想办法,让centos自己知道硬盘发生了变化。

1.1 磁盘格式不是vdi的情况

如果Storate format格式是vmdk格式,则会报错。

VboxManage: error: Resize hard disk operation for this format is not implemented yet!

需要将该磁盘先转换为vdi格式

VBoxManage clonedhd "source.vmdk" "clone.vdi" --format vdi

然后关闭虚拟机,将磁盘从之前的vmdk磁盘更换为vdi磁盘。再对新的vdi磁盘进行扩容操作。

2. centos扩展硬盘

2.1 先查看一下硬盘情况,发现硬盘的确有变大,只不过还没有扩展上。

fdisk -l /dev/sda
Disk /dev/sda: 42.9 GB, 42893049856 bytes
255 heads, 63 sectors/track, 5214 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00043c35
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 1045 7875584 8e Linux LVM

2.2 接下来将使新增加的空间变得可用

# fdisk /dev/sda
n {new partition}
p {primary partition}
3 {partition number}
#这时会提示修改大小,选择默认直接回车即可
t {change partition id}
3 {partition number}
8e {Linux LVM partition}
w

完成后会有警告的提示,不用管,重启系统即可。

2.3 重启后,重新运行fdisk命令,发现第3块硬盘已经加载成功。

fdisk -l
Disk /dev/sda: 42.9 GB, 42893049856 bytes
255 heads, 63 sectors/track, 5214 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00043c35
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 1045 7875584 8e Linux LVM
/dev/sda3 1045 5214 33492847 8e Linux LVM

2.4 把新分配的空间创建一个新的物理卷

pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created

2.5 看一下Volume Group名称

vgdisplay
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
...

VolGroup是我的VolumeGroup的名称。

2.6 然后使用新的物理卷来扩展 LVM 的 VolGroup,

vgextend VolGroup /dev/sda3
Volume group "VolGroup" successfully extended

2.7 使用df -h可以知道逻辑卷名称是/dev/mapper/VolGroup-lv_root

df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root

2.8 然后扩展 LVM 的逻辑卷

lvextend /dev/mapper/VolGroup-lv_root /dev/sda3
Size of logical volume VolGroup/lv_root changed from 6.71 GiB (1718 extents) to 38.65 GiB (9894 extents).
Logical volume lv_root successfully resized

2.9 最后调整逻辑卷的大小:

resize2fs /dev/mapper/VolGroup-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 10131456 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_root is now 10131456 blocks long.

如果这里报错,还需要继续执行一个命令

resize2fs: Bad magic number in super-block while trying to open /dev/mapper/VolGroup-lv_root
Couldn't find valid filesystem superblock.
xfs_growfs /dev/mapper/VolGroup-lv_root
...

2.10 就这样,虚拟机的硬盘正式宣告被扩展成功了。

df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
38G 6.4G 30G 18% /
tmpfs 372M 0 372M 0% /dev/shm
/dev/sda1 477M 30M 422M 7% /boot

标签: virtualbox

添加新评论