Resize Virtualbox disk
Resizing Linux guest disk running on a Windows host.
This guest OS has a second drive which I’m expanding. To expand the guests main OS drive you would need to boot a linux live CD or use a GParted live cd.
I’ll create another post later on how to do that.
- Shutdown the VM
- Locate the disk’s vdi Virtualbox Manager -> Right click on the machine -> Settings -> Storage -> Controller: SATA and select the diskOn the right under “Information”, “Location” will give you the vdi’s location on your computer
- Open the command prompt Start -> type “cmd” in the search box for Windows 7 or 8.
- Take a backup of your disk
1cp "E:\Virtual Machines\Fedora_storage.vdi" "E:\Virtual Machines\Fedora_storage_backup.vdi" - CD to the VirtualBox program directory so that we can use the VBoxmanage command
1cd "C:\Program Files\Oracle\VirtualBox\" - Resize the disk to your required size, here i’m resizing to 300GB (format is in megabytes so 300000)
123C:\Program Files\Oracle\VirtualBox>VBoxManage modifyhd "E:\Virtual Machines\Fedora_storage.vdi" --resize 3000000%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%C:\Program Files\Oracle\VirtualBox> - Boot the virtual machine
- Delete the partition and recreate a new partition utilising the entire disk (Include the new space)
12345678910111213141516171819202122➜ ~ sudo fdisk -lDisk /dev/sda: 80 GiB, 85899345920 bytes, 167772160 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x000baec9Device Boot Start End Sectors Size Id Type/dev/sda1 * 2048 1026047 1024000 500M 83 Linux/dev/sda2 1026048 167763967 166737920 79.5G 8e Linux LVMDisk /dev/sdb: 293 GiB, 314572800000 bytes, 614400000 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x997fb42aDevice Boot Start End Sectors Size Id Type/dev/sdb1 2048 419430399 419428352 200G 83 Linux
You can see the partition is 200G we need to increase that;To do that using fdisk we would use the following commands.
p (print partition table to make sure this is the only partition)
d – Delete partition
n – New partition
p – primary partition
enter – Select the first sector as default
enter – Select the last sector as default
p – Check everything is right
w – Write changes to diskSee example below;
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950➜ ~ sudo fdisk /dev/sdbWelcome to fdisk (util-linux 2.25.2).Changes will remain in memory only, until you decide to write them.Be careful before using the write command.Command (m for help): pDisk /dev/sdb: 293 GiB, 314572800000 bytes, 614400000 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x997fb42aDevice Boot Start End Sectors Size Id Type/dev/sdb1 2048 419430399 419428352 200G 83 LinuxCommand (m for help): dSelected partition 1Partition 1 has been deleted.Command (m for help): nPartition typep primary (0 primary, 0 extended, 4 free)e extended (container for logical partitions)Select (default p): pPartition number (1-4, default 1):First sector (2048-614399999, default 2048):Last sector, +sectors or +size{K,M,G,T,P} (2048-614399999, default 614399999):Created a new partition 1 of type 'Linux' and of size 293 GiB.Command (m for help): pDisk /dev/sdb: 293 GiB, 314572800000 bytes, 614400000 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x997fb42aDevice Boot Start End Sectors Size Id Type/dev/sdb1 2048 614399999 614397952 293G 83 LinuxCommand (m for help): wThe partition table has been altered.Calling ioctl() to re-read partition table.Syncing disks.
Using the following steps to delete recreate and then write. Note that because this is the only partition on the disk the partition as standard starts at sector 2048 so all I need to do is delete the existing and create a new parition (which will start at 2048) and extend it to the end of the drive. If there are other paritions before this one you will have to remember where the partition starts. You can get this information by typing p for print. - Once the partition is resized using fdisk we need to resize the file system on the partition which is easy.
123456789101112➜ ~ sudo e2fsck -f /dev/sdb1e2fsck 1.42.12 (29-Aug-2014)Pass 1: Checking inodes, blocks, and sizesPass 2: Checking directory structurePass 3: Checking directory connectivityPass 4: Checking reference countsPass 5: Checking group summary information/dev/sdb1: 173618/13107200 files (0.3% non-contiguous), 49803021/52428544 blocks➜ ~ sudo resize2fs /dev/sdb1resize2fs 1.42.12 (29-Aug-2014)Resizing the filesystem on /dev/sdb1 to 76799744 (4k) blocks.The filesystem on /dev/sdb1 is now 76799744 (4k) blocks long.
- Done!