From f97708a7b785f02e0a109699ae40ce49e08292f7 Mon Sep 17 00:00:00 2001 From: catborise Date: Sun, 8 Nov 2026 12:24:18 +0300 Subject: [PATCH] refactor(instances): a new disk's name prefix comes from vol_dev_type get_new_disk_dev() mapped virtio to vd, ide to hd, fdc to fd and other buses to sd itself; vrtManager.util.vol_dev_type() has that mapping, and the bus change in vrtManager uses it with the same sd fallback. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- instances/tests/test_new_disk_name.py | 12 +++++++++++- instances/utils.py | 11 ++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/instances/tests/test_new_disk_name.py b/instances/tests/test_new_disk_name.py index 8cc6d786..76435381 100644 --- a/instances/tests/test_new_disk_name.py +++ b/instances/tests/test_new_disk_name.py @@ -6,9 +6,10 @@ from unittest.mock import patch from computes.models import Compute from django.contrib.auth import get_user_model -from django.test import TestCase +from django.test import SimpleTestCase, TestCase from django.urls import reverse from instances.models import Instance +from instances.utils import get_new_disk_dev def dev(name, bus): @@ -84,3 +85,12 @@ class NewDiskNameTestCase(TestCase): def test_a_cdrom_without_media_is_empty(self): call = self.attach("add_cdrom", {"bus": "scsi", "media": ""}, status=1) self.assertEqual((call.kwargs["target_bus"], call.args[1]), ("scsi", "")) + + +class BusPrefixTestCase(SimpleTestCase): + def test_each_bus_names_its_disks_with_its_prefix(self): + for bus, name in (("virtio", "vdb"), ("ide", "hdb"), ("fdc", "fdb"), ("sata", "sdb"), ("scsi", "sdb"), + ("usb", "sdb"), ("xen", "sdb")): + with self.subTest(bus=bus): + taken = [{"dev": name[:2] + "a"}] + self.assertEqual(get_new_disk_dev(taken, [], bus), name) # a CD-ROM's name is taken too diff --git a/instances/utils.py b/instances/utils.py index 281c684d..bad97e42 100644 --- a/instances/utils.py +++ b/instances/utils.py @@ -18,7 +18,7 @@ from logs.audit import reason, write from operations.models import Operation from vrtManager.connection import other_hosts, wvmConnect from vrtManager.instance import wvmInstance, wvmInstances -from vrtManager.util import ConnectionFailed, message +from vrtManager.util import ConnectionFailed, message, vol_dev_type from .models import Instance @@ -206,14 +206,7 @@ def check_user_quota(user, instance, cpu, memory, disk_size): def get_new_disk_dev(media, disks, bus): existing_disk_devs = [] existing_media_devs = [] - if bus == "virtio": - dev_base = "vd" - elif bus == "ide": - dev_base = "hd" - elif bus == "fdc": - dev_base = "fd" - else: - dev_base = "sd" + dev_base = vol_dev_type(bus) or "sd" if disks: existing_disk_devs = [disk["dev"] for disk in disks]