Repository: netbox-community/netbox
Pull request: 14692 Convert VM disk size
### Fixes: #14692 

![Monosnap Virtual Machines | NetBox 2024-06-06 11-45-36](https://github.com/netbox-community/netbox/assets/99642/3f073f78-7fbe-4295-9849-b127ba52de80)
![Monosnap vm2 | NetBox 2024-06-06 11-46-55](https://github.com/netbox-community/netbox/assets/99642/f8484ded-be06-431a-b95a-fe3c3dce7ff4)


Problem statement:
Allow disk size to be smaller than 1GB
### NetBox version

v3.7.0

### Feature type

Change to existing functionality

### Proposed functionality

I would like to be able to create virtual disks with a size that is smaller than 1GB. Ideally, disk sizes could specified in MB (like memory) or decimal values could be allowed.

### Use case

We operate a lot of small VMs that are just PBX appliances. These only need disks of a few MB in size. At the moment we can not model these VMs properly since the smallest disk size possible is 1GB.

### Database changes

If the disk size field is changed to represent MB no change to the database is necessary, but existing data would have to be converted during migration. Alternatively, the field could be converted to a decimal field (like vCPUs).

### External dependencies

_No response_

Hints from issue discussion:
Either approach, changing the atomic unit or converting the value to a decimal, would be a breaking API change. It's unfortunate that you didn't raise this suggestion during the evaluation period ahead of the v3.7 release. We could have considered the change then, but now that v3.7 has been released this would need to wait for a new minor version bump.

That said, even before the introduction of the new VirtualDisk model, the `disk` attribute on VirtualMachine was always measured in gigabytes, so I'm not sure how much demand there is for the proposed change.
one suggestion that could allow this usecase without it making a major breaking change.

add an extra choice field for disk_size_measurement with options of gb/mb/tb with a default of gb. that would allow people who need to use smaller measurements to select mb as the type and use the existing integer field
Just noting, that small HDDs are very common for standalone EFI partitions. Checking my datastores and I see lot of ~5MB disks for EFI part. Proxmox/Qemu.
Came here to file this while, glad to see I’m not the only one, and I have an additional use case:

I use NetBox to document Linode (_Akamai_) VMs, amongst other things. The storage quotas there are defined in MB, so while in theory documenting a drive in GB is mostly correct, being able to document the actual MB value would be more accurate, and allowing sub-GB values would allow accurate tracking of swap disk size, which is currently impossible (most are 256/512MB).
What would be the impact if it was just allowed to be a float number and keep the GB status. 
It would still be a breaking change as the data type would change.

Commit message: 14692 fix migration

Patch to review:
diff --git a/netbox/templates/virtualization/virtualmachine.html b/netbox/templates/virtualization/virtualmachine.html
index ed8980f5c81..d2603ddd2f8 100644
--- a/netbox/templates/virtualization/virtualmachine.html
+++ b/netbox/templates/virtualization/virtualmachine.html
@@ -137,7 +137,7 @@ <h5 class="card-header">{% trans "Resources" %}</h5>
                   </th>
                   <td>
                     {% if object.disk %}
-                      {{ object.disk }} {% trans "GB" context "Abbreviation for gigabyte" %}
+                      {{ object.disk|humanize_megabytes }}
                     {% else %}
                       {{ ''|placeholder }}
                     {% endif %}
diff --git a/netbox/virtualization/migrations/0039_convert_disk_size.py b/netbox/virtualization/migrations/0039_convert_disk_size.py
new file mode 100644
index 00000000000..d8ced42c150
--- /dev/null
+++ b/netbox/virtualization/migrations/0039_convert_disk_size.py
@@ -0,0 +1,23 @@
+# Generated by Django 5.0.6 on 2024-06-06 17:46
+
+from django.db import migrations
+from django.db.models import F
+
+
+def convert_disk_size(apps, schema_editor):
+    VirtualMachine = apps.get_model('virtualization', 'VirtualMachine')
+    VirtualMachine.objects.update(disk=F('disk') * 1000)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('virtualization', '0038_virtualdisk'),
+    ]
+
+    operations = [
+        migrations.RunPython(
+            code=convert_disk_size,
+            reverse_code=migrations.RunPython.noop
+        ),
+    ]
diff --git a/netbox/virtualization/models/virtualmachines.py b/netbox/virtualization/models/virtualmachines.py
index 92f1a947234..fc446922bbf 100644
--- a/netbox/virtualization/models/virtualmachines.py
+++ b/netbox/virtualization/models/virtualmachines.py
@@ -125,7 +125,7 @@ class VirtualMachine(ContactsMixin, ImageAttachmentsMixin, RenderConfigMixin, Co
     disk = models.PositiveIntegerField(
         blank=True,
         null=True,
-        verbose_name=_('disk (GB)')
+        verbose_name=_('disk (MB)')
     )
 
     # Counter fields
diff --git a/netbox/virtualization/tables/virtualmachines.py b/netbox/virtualization/tables/virtualmachines.py
index 9d194d268b8..f37a1bebc9a 100644
--- a/netbox/virtualization/tables/virtualmachines.py
+++ b/netbox/virtualization/tables/virtualmachines.py
@@ -4,6 +4,7 @@
 from dcim.tables.devices import BaseInterfaceTable
 from netbox.tables import NetBoxTable, columns
 from tenancy.tables import ContactsColumnMixin, TenancyColumnsMixin
+from utilities.templatetags.helpers import humanize_megabytes
 from virtualization.models import VirtualDisk, VirtualMachine, VMInterface
 
 __all__ = (
@@ -106,6 +107,9 @@ class VirtualMachineTable(TenancyColumnsMixin, ContactsColumnMixin, NetBoxTable)
         verbose_name=_('Config Template'),
         linkify=True
     )
+    disk = tables.Column(
+        verbose_name=_('Disk'),
+    )
 
     class Meta(NetBoxTable.Meta):
         model = VirtualMachine
@@ -118,6 +122,9 @@ class Meta(NetBoxTable.Meta):
             'pk', 'name', 'status', 'site', 'cluster', 'role', 'tenant', 'vcpus', 'memory', 'disk', 'primary_ip',
         )
 
+    def render_disk(self, value):
+        return humanize_megabytes(value)
+
 
 #
 # VM components


Code review comments:
diff_hunk: @@ -0,0 +1,23 @@
+# Generated by Django 5.0.6 on 2024-06-06 17:46
+
+from django.db import migrations
+from django.db.models import F
+
+
+def convert_disk_size(apps, schema_editor):
+    VirtualMachine = apps.get_model('virtualization', 'VirtualMachine')
+    VirtualMachine.objects.update(disk=F('disk') * 1000)
path: netbox/virtualization/migrations/0039_convert_disk_size.py
text: @user1:
Can we exclude records with null values, just for efficiency?
```suggestion
    VirtualMachine.objects.filter(disk__isnull=False).update(disk=F('disk') * 1000)
```