8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Vm » Here

VirtualBox : Extend Virtual Disk and File System

Related articles.

Extend Virtual Disk

If the virtual disk is defined as dynamically allocated (Dynamically allocated differencing storage), the existing disk is extended using the VBoxManage modifyhd command. The approach is similar on Linux and Windows. The --resize option assumes a size is specified in megabytes. If you want to specify the size in bytes, use the --resizebytes option.

The examples below show how to resize a virtual disk to 40G on Linux and Windows.

$ cd /u01/VirtualBox/ol6-112/
$ VBoxManage modifyhd ol6-112.vdi --resize 40960
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

$


C:\>cd "C:\Users\myuser\VirtualBox VMs\ol6-112"
C:\Users\myuser\VirtualBox VMs>"C:\Program Files\Oracle\VirtualBox\VBoxManage" modifyhd ol6-112.vdi --resize 40960
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

C:\Users\myuser\VirtualBox VMs>

If this command gives an error, saying the disk is already registered, use the UUID presented in the error in place of the file name, as shown below.

$ VBoxManage modifyhd ./ol6-112.vdi --resize 40960
VBoxManage: error: Cannot register the hard disk '/u01/VirtualBox/ol6-112/ol6-112.vdi' {fc63137b-c779-41a2-b0a3-5fb5788e77cc} because a hard disk '/u01/VirtualBox/ol6-112/ol6-112.vdi' with UUID {fc63137b-c779-41a2-b0a3-5fb5788e77cc} already exists
VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component VirtualBox, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 178 of file VBoxManageDisk.cpp
$

$ VBoxManage modifyhd fc63137b-c779-41a2-b0a3-5fb5788e77cc --resize 40960
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
$ 

Create Virtual Disk

Rather than extending an existing virtual disk, you can create a new virtual disk and associate it with the virtual machine to provide the extra space. The following text shows how this can be done in Linux and Windows.

$ cd /u01/VirtualBox/ol6-112/
$ VBoxManage createhd --filename ol6-112b.vdi --resize --size 10240
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Disk image created. UUID: 34d7532e-cd4c-4bf8-88e3-5207aca1e343

$ VBoxManage storageattach ol6-112 --storagectl "SATA" --port 1 --device 0 \
    --type hdd --medium ol6-112b.vdi --mtype normal

$


C:\>cd "C:\Users\myuser\VirtualBox VMs\ol6-112"
C:\Users\myuser\VirtualBox VMs>"C:\Program Files\Oracle\VirtualBox\VBoxManage" createhd ^
     --filename ol6-112b.vdi --size 10240
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Disk image created. UUID: 532fa35b-a437-4a07-8f4a-5503e9049cb6

C:\Users\myuser\VirtualBox VMs>"C:\Program Files\Oracle\VirtualBox\VBoxManage" storageattach ol6-112 ^
    --storagectl "SATA" --port 1 --device 0 ^
    --type hdd --medium ol6-112b.vdi --mtype normal

C:\Users\myuser\VirtualBox VMs>

When you start the virtual machine the disk will be visible to the OS. If you can't see it, you will need to force a rescan for SCSI devices. There are two methods to achieve this. The first involves echoing wildcards to the individual SCSI host(s).

# ls /sys/class/scsi_host/
host0  host1  host2
#

# echo "- - -" > /sys/class/scsi_host/host0/scan
# echo "- - -" > /sys/class/scsi_host/host1/scan
# echo "- - -" > /sys/class/scsi_host/host2/scan

The second involves echoing the value "1" to the SCSI device rescan file.

# ls /sys/class/scsi_device
1:0:0:0  2:0:0:0
#

# echo 1 > /sys/class/scsi_device/1:0:0:0/device/rescan
# echo 1 > /sys/class/scsi_device/2:0:0:0/device/rescan

Within a few seconds the device should be visible to the Linux OS. If in doubt, reboot the VM.

Partition the New Space

Start the virtual machine and check the current file system sizes. We plan to add space to the root file system.

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_ol6112-lv_root
                       26G   19G  5.3G  78% /
tmpfs                1004M   88K 1004M   1% /dev/shm
/dev/sda1             485M  144M  316M  32% /boot
//192.168.0.4/u02     907G  746G  115G  87% /host
#

In this example, we are adding the extra space by extending the existing virtual disk. If we were adding the space using an extra disk, we would follow a similar approach, but we would partition the new disk (/dev/sdb) instead. Keep this in mind when any device paths are used in this example.

Check the new space is visible from the operating system.

# 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): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x00076f69

   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        3917    30944256   8e  Linux LVM

Command (m for help):

Create a new partition using the remaining space on the disk. In this case, it was the 3rd partition on the disk, so the partition was created with the "n, p, 3, (return), (return)" sequence of entries.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (3917-5221, default 3917): 
Using default value 3917
Last cylinder, +cylinders or +size{K,M,G} (3917-5221, default 5221): 
Using default value 5221

Command (m for help):

Change the partition type to "Linux LVM". In this case, the sequence of entries was, "t, 3, 8e".

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):

Write the changes to the partition table using the "w" command.

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.
#

Due to the final warning, the VM must be rebooted before you can continue.

If we had used a new virtual disk, rather than extending the existing one, we would not have needed to reboot after the partitioning step.

Add Partition to LVM

Add the new partition to the volume group. Create a physical volume from the "/dev/sda3" partition using the pvcreate command.

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

Add the physical volume to the existing volume group. Display the volume group name using the vgdisplay command, then use the volume group name and partition name with the vgextend command.

# vgdisplay
  --- Volume group ---
  VG Name               vg_ol6112
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               29.51 GiB
  PE Size               4.00 MiB
  Total PE              7554
  Alloc PE / Size       7554 / 29.51 GiB
  Free  PE / Size       0 / 0   
  VG UUID               M3LcFJ-etHj-u7zh-I4em-4Js0-SczK-8JDj93
   
# vgextend vg_ol6112 /dev/sda3
  Volume group "vg_ol6112" successfully extended
#

Use lvdisplay command to get the root logical volume name. Use the vgdisplay command to get the free space in the volume group. Use this information in the lvextend command to extend the logical volume.

# lvdisplay | grep "LV Path"
  LV Path                /dev/vg_ol6112/lv_root
  LV Path                /dev/vg_ol6112/lv_swap
# vgdisplay vg_ol6112 | grep Free
  Free  PE / Size       2558 / 9.99 GiB
# lvextend --size +9.99G --resizefs /dev/vg_ol6112/lv_root
  Rounding size to boundary between physical extents: 9.99 GiB
  Extending logical volume lv_root to 35.56 GiB
  Logical volume lv_root successfully resized
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_ol6112-lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/vg_ol6112-lv_root to 9322496 (4k) blocks.
The filesystem on /dev/mapper/vg_ol6112-lv_root is now 9322496 blocks long.

#

If we had specified a size of 10G, we might have got an error message saying something like, "could not extend by 2560 extents", due to a rounding error. In the above example, the size of 9.99G was used to allow it to fit, but a better alternative would be to specify the size using extents, using one less extent than the number reported in the error, as shown below. Thanks to Julian Dyke for suggesting the extents approach.

# lvextend --extents +2559 --resizefs  /dev/vg_ol6112/lv_root

The lvextend command used the --resizefs option. If we had not used this, we would have to run the resize2fs command separately.

# resize2fs /dev/vg_ol6112/lv_root

In the comments Juan suggested the following command if you are using BTRFS.

# btrfs filesystem resize max /

Checking the size of the file systems reveals the root file system has grown by approximately 10G.

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_ol6112-lv_root
                       36G   19G   15G  56% /
tmpfs                1004M   88K 1004M   1% /dev/shm
/dev/sda1             485M  144M  316M  32% /boot
//192.168.0.4/u02     907G  746G  115G  87% /host
#

If you need more information about the Linux Logical Volume Manager (LVM), you can read about it here.

Resize Fixed Size Disks

The VBoxManage modifyhd command doesn't work on fixed disks, so to get around this, simply clone the existing disk to a dynamically allocated disk, then resize that.

$ VBoxManage clonehd ol6-112.vdi ol6-112-b.vdi --format VDI --variant Standard
$ VBoxManage modifyhd ol6-112-b.vdi --resize 40960

Once that is complete, use the new virtual disk for the virtual machine and perform the OS resize operations as before.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.