加大VirtualBox Image大小的辦法

轉:https://blog.jyore.com/2013/06/virtualbox-increase-size-of-rhelfedoracentosscientificos-guest-file-system/

VirtualBox: Increase Size of RHEL/Fedora/CentOS/Scientific Guest File System

Tired of running out of space on your VM and running through complicated steps in order to get your VM resized?  This article provides the steps to take without you having to boot live CDs, clone VDI’s, or anything else complicated or time consuming.  We will simply leverage VirtualBox manager commands on our host and system commands on the guest.

E:\Oracle\VirtualBox>VBoxManage modifyhd "D:\VirtualBox VMs\development\development.vdi" --resize 20480
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

First, shutdown the VM you plan to work on.  Then we will run the manager tool. the

1
2
3
cd /path/to/vm
VBoxManage modifyhd vm.vdi --resize <size>
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

The <size> parameter should be the desired size to make your VDI, in MB.  For example, if you wish to resize your 10GB disk to 25GB, then you would use 25600 (1024*25). This should resize the VDI appropriately, however, if you boot up your VM, you will soon see that you are still at your original disk space.

1
2
3
cd /path/to/vm
VBoxManage modifyhd vm.vdi --resize 25600
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

If you open a partition tool, such as gparted, you can easily see that there is the extra available disk available, but it is set to unallocated.  If your guest OS is using LVM (this is all of the Red Hat flavors), then you cannot simply extend the volume as it will be locked.  Other volume managers may let you resize the partition.  Some may let you if you boot from a live CD.

With LVM we can actually make all these changes to the logical volume on a live file system.  It never hurts to backup your filesystem, or at least anything important, but it is not required.

The following steps will allow you to resize your partition.  In a terminal run the following commands in bold (you need root permissions to run fdisk):

  • df
    • Take note of the logical volume mapping (ex. /dev/mapper/fedora-root)
  • fdisk -l
    • Take note of the filesystem partition of your physical volume sits (ex. /dev/sda2)
  • fdisk /dev/sda
    • Run fdisk of this phyisical volume
  • d
    • Delete a partition
  • 2
    • Select your partition (we are using 2 from /dev/sda2)
  • n
    • Create a new partition
  • p
    • Make it a primary partition
  • 2
    • The partition number to make it on (same as we deleted)
  • <return>
    • Set the starting block (keep the default as it is usually correct)
  • <return>
    • Set the ending block (keep the default as it is fine for our use case)
  • w
    • Write the partition (will also exit fdisk shell)
  • reboot
    • We must reboot in order to have the new partition table loaded
  • pvresize /dev/sda2
    • Resizes the physical volume
  • pvscan
    • Use to verify the new size
  • lvextend -l +100%FREE /dev/mapper/fedora-root
    • Extend the logical volume to take all free space
  • resize2fs /dev/mapper/fedora-root
    • Resize the file system
  • df
    • See your newly sized volume

And that is it! With the last df command, you should see that your volume increased! In our example, let’s say you had 9GB of the 10GB disk and you resized it to 25GB.  In the first dfcommand, you would have seen 90% full.  After the resize, you should see the system as only 36% full, thus verifying the resize success!

The commands are reposted below for easier reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
df
fdisk -l
fdisk /dev/sda
d
2
n
p
2
<return>
<return>
w
reboot
pvresize /dev/sda2
pvscan
lvextend -l +100%FREE /dev/mapper/fedora-root
resize2fs /dev/mapper/fedora-root
df
發佈了110 篇原創文章 · 獲贊 14 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章