Skip to content

Commit

Permalink
Improve CDROM device validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Qubad786 committed May 17, 2024
1 parent 266ebd9 commit cef5275
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/middlewared/middlewared/plugins/vm/devices/cdrom.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os

from middlewared.schema import Dict, Str
from middlewared.plugins.boot import BOOT_POOL_NAME
from middlewared.schema import Dict, File
from middlewared.service import CallError
from middlewared.validators import Match
from middlewared.utils.zfs import query_imported_fast_impl
from middlewared.validators import check_path_resides_within_volume_sync, Match

from .device import Device
from .utils import create_element, disk_from_number, LIBVIRT_USER
Expand All @@ -12,9 +14,12 @@ class CDROM(Device):

schema = Dict(
'attributes',
Str(
File(
'path', required=True, validators=[
Match(r'^[^{}]*$', explanation='Path should not contain "{", "}" characters')
Match(
r'^/mnt/[^{}]*$',
explanation='Path must not contain "{", "}" characters, and it should start with "/mnt/"'
),
], empty=False
),
)
Expand All @@ -40,9 +45,12 @@ def xml(self, *args, **kwargs):

def _validate(self, device, verrors, old=None, vm_instance=None, update=True):
path = device['attributes']['path']
if not os.path.exists(path):
verrors.add('attributes.path', f'Unable to locate CDROM device at {path!r}')
elif not self.middleware.call_sync('vm.device.disk_uniqueness_integrity_check', device, vm_instance):
check_path_resides_within_volume_sync(
verrors, 'attributes.path', path, [
i['name'] for i in query_imported_fast_impl().values() if i['name'] != BOOT_POOL_NAME
]
)
if not self.middleware.call_sync('vm.device.disk_uniqueness_integrity_check', device, vm_instance):
verrors.add(
'attributes.path',
f'{vm_instance["name"]} has "{self.identity()}" already configured'
Expand Down

0 comments on commit cef5275

Please sign in to comment.