Increase Disk Size for a Linux Virtual Machine

 

If your virtual machine is running out of disk space, you can opt to increase the storage accordingly as long as the host machine has enough disk space to accommodate your VM.

Follow the below pre-defined steps to guide you through the storage resizing process. The snapshots provided herein are for Oracle Linux 6 VM running on Windows 10 Operating System. You can also use the same steps to resize VMs running on Windows 7 or Windows 8.

NB: If your virtual machine has snapshots, then, you can opt to clone your VM first, then increase disk space for the cloned machine. Click https://fdba-activities.blogspot.com/2021/06/how-to-clone-virtual-machine-on-windows.html to see a post on how to clone a virtual machine. 

The steps below will only work for a machine without snapshots.


  1.  Before starting the process, ensure your virtual machine is shut down. It’s also recommended to take a copy of the vdi/vhd file to help in recovery just in case things go wrong. You can quickly copy paste the file into a new location.
  2. Then run cmd as an administrator to open a command prompt or ensure your logged-on to a user who has administrator privileges before invoking the command prompt.
  3. On the command prompt, navigate to C:\Program Files\Oracle\VirtualBox>; then, execute VBoxManage.exe modifyhd followed by your actual VirtualBox vdi/vhd path, then --resize option and the new hard disk size (indicated in MBs). In my case, I executed the following to resize my VM to 110GB.

    NB:1GB = 1024mbs. I arrived at 112640 after multiplying 110 by 1024. 

    C:\Program Files\Oracle\VirtualBox>VBoxManage.exe modifyhd “%USERPROFILE%\VirtualBox VMs\oraclelinux6 clone-disk.vdi” -- resize 112640


    4.      After successfully resizing the virtual machine on the host OS, go ahead and start the virtual machine.

       Login as root on the VM. You can also opt to log in as a normal user and then su – root. You will be prompted to enter the root password.

     

    How to create a disk partition in Linux.

Run fdisk –l command to list the available partitions and also to identify the disk definition. After obtaining this information, we’ll have to create a new partition to hold our additional disk space. I will be adding a new partition on my disk /dev/sda.











Run fdisk /dev/sda to launch the fdisk utility. You can invoke (depicts manual) to give you a list of the options available within the utility. Then type n option and hit enter to create a new partition. Enter p for primary partition. Then enter the partition number accordingly.


For my case, the disk /dev/sda has 2 partitions already, i.e. /dev/sda1, and /dev/sda2. So the new partition to be created should be /dev/sda3. Please note that the disk names can differ. Instead of /dev/sda, your disk can be reading another name. e.g. /dev/sdb.

Then, accept the default values offered by just hitting enter until you return to the prompt. 

Finally, type w to write changes to disk and finally exit from the fdisk utility. 

We are going to reboot the VM now so that the new partition can be read and availed after the VM is up.

To reboot the VM, execute the command shutdown -r now.


 Execute fdisk –l to list all the partitions inclusive of the newly added partition. We can see /dev/sda3 which is our new partition.

List Mounted File systems in Linux.

Run df -h to list the currently mounted file systems. Pay attention to the file system we want to resize. In my case, am aiming to increase the size of /dev/mapper/vg_srv1-lv_root.


Creating an LVM Physical Volume in Linux

Execute pvcreate /dev/sda3 to create a physical volume out of our newly created partition.



List Volume Groups in Linux

Execute vgdisplay to list the volume group details in your VM. Note the volume group name we need to extend; i.e. vg_srv1. Locate the correct volume group name in your machine.  Also note that the Free PE/Size is 0/0 as of now.








How to Extend a Volume Group in Linux.

Run vgextend vg_svr1 /dev/sda3 to extend the volume group vg_svr1 to include the physical volume /dev/sda3. Remember to use the volume group name obtained in the previous step.



Type vgdisplay again. Note the Free PE/Size has now changed.









Extending root logical volume to maximum space

Execute lvextend -l +6841 /dev/mapper/vg_srv1-lv_root to extend the logical volume /dev/mapper/vg_srv1-lv_root to the maximum available space.





Run df -h. Note that the size of the file system /dev/mapper/vg_srv1-lv_root is yet to be increased.







Resize the Linux LVM file system

Execute resize2fs /dev/mapper/vg_srv1-lv_root to resize the file system.






You can now happily make use of the increased disk space accordingly J.

Comments

Popular posts from this blog

Clone a Virtual Machine on Windows

Kali Linux Download