Coverage for nova/virt/libvirt/volume/smbfs.py: 100%
39 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
13import re
14import warnings
16import nova.conf
17from nova.virt.libvirt import utils as libvirt_utils
18from nova.virt.libvirt.volume import fs
19from nova.virt.libvirt.volume import remotefs
21CONF = nova.conf.CONF
23USERNAME_REGEX = re.compile(r"(user(?:name)?)=(?:[^ ,]+\\)?([^ ,]+)")
26class LibvirtSMBFSVolumeDriver(fs.LibvirtBaseFileSystemVolumeDriver):
27 """Class implements libvirt part of volume driver for SMBFS."""
29 def __init__(self, host):
30 super(LibvirtSMBFSVolumeDriver, self).__init__(host)
31 warnings.warn('SMBFS volume support is deprecated',
32 category=DeprecationWarning, stacklevel=2)
34 def _get_mount_point_base(self):
35 return CONF.libvirt.smbfs_mount_point_base
37 def get_config(self, connection_info, disk_info):
38 """Returns xml for libvirt."""
39 conf = super(LibvirtSMBFSVolumeDriver,
40 self).get_config(connection_info, disk_info)
42 conf.source_type = 'file'
43 conf.driver_cache = 'writeback'
44 conf.source_path = connection_info['data']['device_path']
45 conf.driver_format = connection_info['data'].get('format', 'raw')
46 return conf
48 def connect_volume(self, connection_info, instance):
49 """Connect the volume."""
50 smbfs_share = connection_info['data']['export']
51 mount_path = self._get_mount_path(connection_info)
53 if not libvirt_utils.is_mounted(mount_path, smbfs_share):
54 mount_options = self._parse_mount_options(connection_info)
55 remotefs.mount_share(mount_path, smbfs_share,
56 export_type='cifs', options=mount_options)
58 device_path = self._get_device_path(connection_info)
59 connection_info['data']['device_path'] = device_path
61 def disconnect_volume(self, connection_info, instance, force=False):
62 """Disconnect the volume."""
63 smbfs_share = connection_info['data']['export']
64 mount_path = self._get_mount_path(connection_info)
65 remotefs.unmount_share(mount_path, smbfs_share)
67 def _parse_mount_options(self, connection_info):
68 mount_options = " ".join(
69 [connection_info['data'].get('options') or '',
70 CONF.libvirt.smbfs_mount_options])
72 if not USERNAME_REGEX.findall(mount_options):
73 mount_options = mount_options + ' -o username=guest'
74 else:
75 # Remove the Domain Name from user name
76 mount_options = USERNAME_REGEX.sub(r'\1=\2', mount_options)
77 return mount_options.strip(", ").split(' ')