Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VirtualBox: extend partition

I have virtualbox-4.1.0 with centos-5.6 installed in. Now I would like to extend the VM image, which I previously allocated for virtual machine installation, it was 8Gb, that's not enough now. Is there a way to extend the partition without loosing information?

Actually in centos I have one root fs with my home dir etc. so this partition eventually would need to be resized.

Thanks in advance for suggestions!

like image 399
Mark Avatar asked Nov 15 '11 18:11

Mark


People also ask

How do I add more disk space to VirtualBox?

VirtualBox 6 added a graphical option for enlarging and resizing virtual disks. To access it, click File > Virtual Media Manager in the main VirtualBox window. Select a virtual hard disk in the list and use the “Size” slider at the bottom of the window to change its size. Click “Apply” when you're done.

How do I extend a VM partition?

Right-click the partition of the disk that you want to extend, and click Extend Volume in the context menu. Configure the volume parameters, and set the needed volume size. When done, close the Disk Management snap-in, and shut down the Windows 7 VM. Open virtual machine settings of the Windows 7 VM.

How do I increase hard disk space in VirtualBox vmware?

Select Window > Virtual Machine Library. Select a virtual machine in the Virtual Machine Library window and click Settings. In the Settings window, click the hard disk you want to resize. Use the Disk size slider to set the new size.


2 Answers

It can be done in 4 steps :

  1. Clone your VMDK to VDI format with VBoxManage Tool.
  2. Resize the disk to create free space with VBoxManage Tool.
  3. Modify the filesystem to allocate free space for your drive with GParted.
  4. Add created disk space to Linux FileSystem.

The detailed steps are below (tested with "Virtual Machine CentOS 6.4" and "VirtualBox 4.2.18");

  1. Observe disk format of the virtualbox file, if it is not *.vdi, convert disk format from *.wmdk to *.vdi. Open windows terminal:
    $ VBoxManage clonehd --format VDI "path_of_wmdk_file" "path_of_vdi_file"

  2. Resize disk size of vdi file. Open windows terminal. For example to do disk size ~500 GB => 512000;
    $ VBoxManage modifymedium "path_of_vdi_file" --resize 512000

  3. Choose *.vdi file instead of *.wmdk file as disk
    Virtual Machine -> Settings -> Storage -> Controller : SATA (Right Click on *.wmdk file) -> Remove Attachment -> Add HardDisk and choose newly created *.vdi file

  4. Download "gparted-live-x.xx.x-x-ixxx.iso" file from http://gparted.sourceforge.net/download.php. Mount this iso file as CD.
    Virtual Machine -> Settings -> Storage -> Controller IDE (Right Click) -> Add CD/DVD -> Select gparted-live-x.xx.x-x-ixxx.iso file

  5. Run virtual machine, Virtual Machine will boot from this CD. Choose default values with pressing "Enter", "Enter" ... until Gpart ISO GUI starts. Select tool gpart program and start.

  6. Extend disk size as below;

    • Right click on partitions and if "possible" click on "Disable Active Partion".
    • Extend Partition as much as possible from GUI (for this case 500GB).
    • Right click the partition which is disabled and select "Enable Active Partion".
    • Apply and wait until the operations finished.
    • Shut down virtual machine.
    • Unmount gparted-live-x.xx.x-x-ixxx.iso.
      Virtual Machine -> Settings -> Storage-> Controller IDE (Right Click on gparted-live-x.xx.x-x-ixxx.iso) -> Remove Attachement
    • Start the virtual machine.

  7. Open linux terminal and login as root. Run commands below;

  lvm vgdisplay        => Free  PE / Size       122880 / 480.00 GiB 
  lvm lvdisplay /dev/VolGroup/lv_root        => Current LE             3978 
  • Calculate the sum of the values above. In this case : 122880 + 3978 = 126858 <- will be used in the next command
  lvm lvresize -l 126858 /dev/VolGroup/lv_root 
  resize2fs  /dev/VolGroup/lv_root    
  lsblk     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++     NAME                        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT     sr0                          11:0    1  1024M  0 rom       sda                           8:0    0   500G  0 disk          +¦sda1                        8:1    0   500M  0 part /boot         L¦sda2                        8:2    0 499.5G  0 part              +¦VolGroup-lv_root (dm-0) 253:0    0   480G  0 lvm  /             L¦VolGroup-lv_swap (dm-1) 253:1    0     4G  0 lvm  [SWAP]     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
  • Check whether the filesystem extended or not with creating a huge file:
  fallocate -l 480G /test_file 
  • Remove the test file of course:
  rm -rif /test_file 
like image 75
19 revs Avatar answered Oct 02 '22 10:10

19 revs


I'm using a CentOS7 virtualbox, and I finally enlarged my partition /dev/mapper/centos-root - gparted doesn't work for me because I do not have a desktop on CentOS7 virtualbox.

  1. Power off your CentOS virtual machine
  2. Go into the directory of your *.vdi image. If you don't know where it is, look at your Virtualbox Manager GUI virtualbox -> settings -> storage -> *.vdi -> location e.g. mine is located under ~/VirtualBox VMs/CentOS7/CentOS.vdi
  3. Back up your image just in case anything goes wrong

    $ cp CentOS7.vdi CentOS7.backup.vdi

  4. Resize your virtual storage size, e.g. 200 GB

    $ VBoxManage modifyhd CentOS7.vdi --resize 204800

  5. Power on your CentOS virtual machine, issue

    $ sudo fdisk -l

   Device Boot      Start         End      Blocks   Id  System     /dev/sda1   *        2048     1026047      512000   83  Linux     /dev/sda2         1026048   209715199   104344576   8e  Linux LVM 
  1. Use fdisk utility to delete/create partitions

    $ sudo fdisk /dev/sda

  2. You are in the fdisk utility interactive mode, issue following commands: (mostly just follow the default recommendation)

    d - delete a partition

    2 - select a partition to delete (/dev/sda2 here)

    n - create a new partition

    p - make it a primary partition

    2 - make it on the same partition number as we deleted

    <return> - set the starting block (by default)

    <return> - set end ending block (by default)

    w - write the partition and leave the fdisk interactive mode

  3. Reboot your CentOS machine

    $ sudo reboot

  4. Resize the physical volume and verify the new size

    $ sudo pvresize /dev/sda2

    $ sudo pvscan

  5. Take a look at your logical mapping volume to see what volume you want to enlarge, in my case, /dev/mapper/centos-root

  6. Resize the file system by adding -r option, it will take care of resizing for you

    $lvextend -r -l +100%FREE /dev/mapper/centos-root

  7. Resize the file system:

    $resize2fs /dev/mapper/fedora-root

    For CentOS 7: $xfs_growfs /dev/mapper/fedora-root

  8. Last check:

    $df -h.

Reference:https://blog.jyore.com/2013/06/virtualbox-increase-size-of-rhelfedoracentosscientificos-guest-file-system/#comment-2879

like image 31
Nicole Finnie Avatar answered Oct 02 '22 10:10

Nicole Finnie