VirtualBox虚拟机在使用的过程中,有时会遇到磁盘大小不够用,如果设置了”动态分配存储“,可以通过下面的方法对磁盘的空间进行手动扩展。此处以Linux为例。
①启动cmd 进入VBox的安装主目录
C:\Users\QY>D:
D:\>cd D:\Program Files\Oracle\VirtualBox
②使用VBoxManage.exe list hdds查看需要修改的虚拟机的VDI的UUID
D:\Program Files\Oracle\VirtualBox>VBoxManage.exe list hdds
UUID: 465ab746-4bac-4b07-b3ca-53a29ea75fd2
Parent UUID: base
State: created
Type: normal (base)
Location: F:\System\Virtual\centos演示\centos演示.vdi
Storage format: VDI
Capacity: 20480 MBytes
Encryption: disabled
③使用modifyhd及其resize命令修改vdi大小
D:\Program Files\Oracle\VirtualBox> VBoxManage.exe modifyhd 465ab746-4bac-4b07-b3ca-53a29ea75fd2 --resize 25000%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
或者使用如下方式
D:\Program Files\Oracle\VirtualBox> VBoxManage.exe modifyhd F:\System\Virtual\centos演示\centos演示.vdi --resize 25000%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
再次查看可以看到vdi的大小已经被修改
D:\Program Files\Oracle\VirtualBox>VBoxManage.exe list hdds UUID: 465ab746-4bac-4b07-b3ca-53a29ea75fd2 Parent UUID: base State: created Type: normal (base) Location: F:\System\Virtual\centos演示\centos演示.vdi Storage format: VDI Capacity: 25000 MBytes Encryption: disabled
④启动新增空间
启动虚拟机查看磁盘情况
[root@localhost ~]# fdisk -l /dev/sda Disk /dev/sda: 26.2 GB, 26214400000 bytes 255 heads, 63 sectors/track, 3187 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: 0x0001b6a2 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 2611 20458496 8e Linux LVM
可以看到磁盘的大小已经改变了,但这是还不可以用,需要将空闲空间创建出一个新的分区。
[root@localhost ~]# fdisk /dev/sda WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (2611-3187, default 2611): Using default value 2611 Last cylinder, +cylinders or +size{K,M,G} (2611-3187, default 3187): Using default value 3187 Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): 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 or after you run partprobe(8) or kpartx(8) Syncing disks.
具体操作步奏:
# fdisk /dev/sda n {new partition} p {primary partition} 3 {partition number} [这时会提示修改大小,选择默认直接回车即可] t {change partition id} 3 {partition number} 8e {Linux LVM partition} w
如果中间有设置大小之类的提示,就直接回车。
完成分区的提示如下
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 or after you run partprobe(8) or kpartx(8)
将系统重启查看新增加的sda3是否标记为LVM。
[root@localhost ~]# fdisk -l /dev/sda Disk /dev/sda: 26.2 GB, 26214400000 bytes 255 heads, 63 sectors/track, 3187 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: 0x0001b6a2 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 2611 20458496 8e Linux LVM /dev/sda3 2611 3187 4628057+ 8e Linux LVM
可以看到将增加的5G空间加到了系统里面。
⑤调整LVM
Ø 虽然将新增加的空间加入了分区,但是当查看磁盘空间时,磁盘可使用的空间大小并没有改变。
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 18G 1.9G 16G 11% / tmpfs 372M 0 372M 0% /dev/shm /dev/sda1 485M 34M 426M 8% /boot
Ø 查看Volume Group的名称
[root@localhost ~]# vgdisplay --- Volume group --- VG Name VolGroup System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable …… VG Size 19.51 GiB PE Size 4.00 MiB Total PE 4994 Alloc PE / Size 4994 / 19.51 GiB Free PE / Size 0 / 0 VG UUID eSiqBn-m1gp-zaxS-FMqO-LLNE-0jOR-t5xRMW
实际操作时使用VG Name操作
Ø 新分配的空间创建一个物理卷,并扩展到VolGroup
[root@localhost ~]# pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created [root@localhost ~]# vgextend VolGroup /dev/sda3 Volume group "VolGroup" successfully extended
扩展 LVM 的逻辑卷 VolGroup/lv_root,并调整逻辑卷的大小
[root@localhost VolGroup]# lvextend /dev/VolGroup/lv_root /dev/sda3 Extending logical volume lv_root to 22.45 GiB Logical volume lv_root successfully resized [root@localhost VolGroup]# resize2fs /dev/VolGroup/lv_root resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required old desc_blocks = 2, new_desc_blocks = 2 Performing an on-line resize of /dev/VolGroup/lv_root to 5884928 (4k) blocks. The filesystem on /dev/VolGroup/lv_root is now 5884928 blocks long.
查看可用空间
[root@localhost VolGroup]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 23G 1.9G 20G 9% / tmpfs 372M 0 372M 0% /dev/shm /dev/sda1 485M 34M 426M 8% /boot
至此虚拟机空间扩展完成。