Increasing Disk Space In VM
Published: 2024-01-29 08:45:11

## Index
- [Checking Current Storage Status](#checking-current-storage-status)
- [Checking Size of Connected Storage Device](#checking-current-storage-status)
- [Increasing Image Size](#increasing-image-size)
- [Increasing Partition Size](#increasing-partition-size)
- [Conclusion](#conclusion)
- [References](#references)

In this guide, I have mentioned the solution that worked for me and that I found to be the most simplest for beginners based on the resources mentioned in the [References](#references) section. **If you would like to quickly increase the disk space, you can directly visit the [Conclusion](#conclusion) section for a quick go-through**.
     
## Checking Current Storage Status
From this [StackExchange Comment](https://unix.stackexchange.com/a/666387):
> df displays the amount of disk space available on the file system containing each file name argument.

Run `df -h` inside the VM, where `df = disk free` and `-h = human format`. Running `df -h` for me currently shows that the storage of my VM is at 100% use:
```
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           303M  1.9M  301M   1% /run
/dev/vda2        25G   24G     0 100% / <-------Look here
tmpfs           1.5G     0  1.5G   0% /dev/shm
tmpfs           5.0M  8.0K  5.0M   1% /run/lock
shm              64M     0   64M   0% /run/containerd/...
shm              64M     0   64M   0% /run/containerd/...
shm              64M     0   64M   0% /run/containerd/...
shm              64M     0   64M   0% /run/containerd/...
shm              64M     0   64M   0% /run/containerd/...
tmpfs           303M   60K  303M   1% /run/user/1001
```

## Checking Size of Connected Storage Device
From this [GeeksForGeeks](https://www.geeksforgeeks.org/lsblk-command-in-linux-with-examples/) article:
> lsblk provides detailed information about block devices such as hard drives, solid-state drives, and other storage-related devices.

In the output of `lsblk`, we look for the `vda` connected device to find how much of its total size is being used. My output of running `lsblk` was:
```
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0    7:0    0     4K  1 loop /snap/bare/5
loop1    7:1    0  73.9M  1 loop /snap/core22/864
loop2    7:2    0  74.1M  1 loop /snap/core22/1033
loop3    7:3    0 128.9M  1 loop /snap/docker/2904
loop4    7:4    0 262.1M  1 loop /snap/firefox/3728
loop5    7:5    0  63.9M  1 loop /snap/core20/2105
loop6    7:6    0  91.7M  1 loop /snap/gtk-common-themes/1535
loop7    7:7    0   246M  1 loop /snap/firefox/3626
loop8    7:8    0  40.9M  1 loop /snap/snapd/20290
loop9    7:9    0   452K  1 loop /snap/snapd-desktop-integration/83
loop10   7:10   0   6.4M  1 loop /snap/curl/1754
loop11   7:11   0   497M  1 loop /snap/gnome-42-2204/141
loop12   7:12   0  40.4M  1 loop /snap/snapd/20671
loop13   7:13   0  63.5M  1 loop /snap/core20/2015
loop14   7:14   0  12.3M  1 loop /snap/snap-store/959
loop15   7:15   0 496.9M  1 loop /snap/gnome-42-2204/132
sda      8:0    0    10G  0 disk 
sr0     11:0    1  1024M  0 rom
vda    252:0    0    25G  0 disk <-------------Look here
├─vda1 252:1    0     1M  0 part 
└─vda2 252:2    0    25G  0 part /var/snap/firefox/common/host-hunspell
                                 /
```

In the above output, my second partition (`vda2`) is 25GB and the disk size of the device block `vda` is also 25GB at this point. This meant that the total size of the disk had been added as a partition and that its 25G was in complete usage. 

### Increasing Image Size
This is where we start with the process of increasing the size. First, find your image name using:
- Entering KVM & clicking the `Open` button to view VM Details
- Under `VirtIO Disk 1`, copy the `Source Path` where your image resides

<div align="center">

![img](https://i.imgur.com/bdlXZHv.gif)
</div>

Then we start with listing the current size of the image using `ls -lh /var/lib/libvirt/images/master1.qcow2`. My output:
```
-rw-r--r-- 1 libvirt-qemu kvm 25G Jan 29 13:21 /var/lib/libvirt/images/master1.qcow2
```

To increase the image size from the existing 25GB to 10GB more, that is 35GB, I ran `sudo qemu-img resize /var/lib/libvirt/images/master1.qcow2 35G` and gained this output:
```
Image resized
```

#### Verifying Changes
I ran the `ls -lh /var/lib/libvirt/images/master1.qcow2` command again and found that the image size was now `35G`. Then I entered te VM and ran `lsblk` again to find the following output:
```
vda    252:0    0    35G  0 disk 
├─vda1 252:1    0     1M  0 part 
└─vda2 252:2    0    25G  0 part /var/snap/firefox/common/host-hunspell
                                 /
```
This output showed that the VM had now 35GB allocated to it yet 10GB of it was not being used. This therefore verified that the changes made previously had worked and that the disk size had been increased.

## Increasing Partition Size
This was done by running the `sudo fdisk /dev/vda` command, whose output said that a size mismatch was found due to the 10GB free space laying around. Partitioning was done through the following steps:
- Ran `w` to allow write operation and rerunning `sudo fdisk /dev/vda` again
- Ran `p` to view the existing partition table 
- Ran `d` and then entering `2` as the `partition number` to be deleted.
- Ran `n` to create a new partition in the place of the `partition number 2`
- Skipped the `First sector` & `Last sector` prompts for going with the default values:
- The output for me was `Created a new partition 2 of type 'Linux filesystem' and of size 35 GiB.`
- Ran `w` again to peform write operation to let the changes take affect

Once done, I finally ran the **`sudo resize2fs /dev/vda2`** command to clear the changes for allowing the expansion of the filesystem.

#### Verifying the Increase of Disk Space
Lastly, I performed the following to learn if the changes had taken place:
- Ran `lsblk` to check if `vda` did not display any unused space
```
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0    7:0    0     4K  1 loop /snap/bare/5
loop1    7:1    0  73.9M  1 loop /snap/core22/864
loop2    7:2    0  63.9M  1 loop /snap/core20/2105
loop3    7:3    0 128.9M  1 loop /snap/docker/2904
loop4    7:4    0   6.4M  1 loop /snap/curl/1754
loop5    7:5    0 262.1M  1 loop /snap/firefox/3728
loop6    7:6    0  91.7M  1 loop /snap/gtk-common-themes/1535
loop7    7:7    0  40.4M  1 loop /snap/snapd/20671
loop8    7:8    0  40.9M  1 loop /snap/snapd/20290
loop9    7:9    0  74.1M  1 loop /snap/core22/1033
loop10   7:10   0   452K  1 loop /snap/snapd-desktop-integration/83
loop11   7:11   0   497M  1 loop /snap/gnome-42-2204/141
loop12   7:12   0 496.9M  1 loop /snap/gnome-42-2204/132
loop13   7:13   0  63.5M  1 loop /snap/core20/2015
loop14   7:14   0  12.3M  1 loop /snap/snap-store/959
loop15   7:15   0   246M  1 loop /snap/firefox/3626
sda      8:0    0    10G  0 disk 
sr0     11:0    1  1024M  0 rom  
vda    252:0    0    35G  0 disk  <-------Look here
├─vda1 252:1    0     1M  0 part 
└─vda2 252:2    0    35G  0 part /var/snap/firefox/common/host-hunspell
                                 /
```
- Ran `df -h` to verify the increase in disk space
```
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           303M  1.7M  301M   1% /run
/dev/vda2        35G   24G  9.0G  73% / <------Look here
tmpfs           1.5G     0  1.5G   0% /dev/shm
tmpfs           5.0M  8.0K  5.0M   1% /run/lock
tmpfs           303M   60K  303M   1% /run/user/1001
shm              64M     0   64M   0% /run/containerd/...
shm              64M     0   64M   0% /run/containerd/...
shm              64M     0   64M   0% /run/containerd/...
shm              64M     0   64M   0% /run/containerd/...
shm              64M     0   64M   0% /run/containerd/...
```

## Conclusion
Thus, I was able to successfully increase the disk size for my VM. We can run the following commands to increase your VM's size by 10GB:
```
# Outside VM
sudo qemu-img resize /var/lib/libvirt/images/master1.qcow2 35G # Add your own image path (between resize and 35G)
ls -lh /var/lib/libvirt/images/master1.qcow2 

# Inside VM 
1. lsblk  # For verifying the increase in block size
2. sudo fdisk /dev/vda # For incresing partition size
3. # Following the Increasing Patition Size Section
4. df -h # Verifying increase in size 
```

## References
- [StackExchange Forum Post](https://serverfault.com/questions/324281/how-do-you-increase-a-kvm-guests-disk-space?newreg=913afaa7dd1142e9b1bc38a0911efd3b) on 'How do you increase a KVM guest's disk space?'
- [HowToGeek Article](https://www.howtogeek.com/devops/how-to-increase-a-kvm-virtual-machines-disk-size/) on '
How to Increase a KVM Virtual Machine's Disk Size'
- [ComputingForGeeks Article](https://computingforgeeks.com/how-to-extend-increase-kvm-virtual-machine-disk-size/) on 'Extend/increase KVM Virtual Machine (VM) disk size'
- [StackOverflow Post](https://stackoverflow.com/questions/3478761/kvm-disk-increase) on 'Kvm disk increase'
- [StackOverflow Post](https://stackoverflow.com/questions/21711093/how-could-i-give-kvm-more-hard-disk-space) on 'How could I give KVM more hard disk space'
- [StackOverflow Post](https://stackoverflow.com/questions/63130067/increasing-disk-space-for-vm) on 'Increasing disk space for VM'
- [StackOverflow Post](https://stackoverflow.com/questions/66118228/changing-disk-size-of-vm-before-vm-installation-using-qemu-kvm) on 'Changing Disk Size of VM before VM installation using qemu kvm'
- [YouTube Tutorial](https://www.youtube.com/watch?v=N7wvqHvmSN8&ab_channel=Abstractprogrammer) by 'Abstract Programmer' on 'How to enlarge Qemu/kvm virtual disk'
<div align="center">
   
[![IMAGE_ALT](https://img.youtube.com/vi/N7wvqHvmSN8/maxresdefault.jpg)](https://www.youtube.com/watch?v=N7wvqHvmSN8&ab_channel=Abstractprogrammer)

   </div>

_______________________________________