Coverage for nova/virt/libvirt/volume/volume.py: 98%

89 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +0000

1# Copyright 2011 OpenStack Foundation 

2# (c) Copyright 2013 Hewlett-Packard Development Company, L.P. 

3# All Rights Reserved. 

4# 

5# Licensed under the Apache License, Version 2.0 (the "License"); you may 

6# not use this file except in compliance with the License. You may obtain 

7# a copy of the License at 

8# 

9# http://www.apache.org/licenses/LICENSE-2.0 

10# 

11# Unless required by applicable law or agreed to in writing, software 

12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

14# License for the specific language governing permissions and limitations 

15# under the License. 

16 

17"""Volume drivers for libvirt.""" 

18 

19from oslo_log import log as logging 

20 

21import nova.conf 

22from nova import exception 

23from nova import profiler 

24from nova.virt import block_device as driver_block_device 

25from nova.virt.libvirt import config as vconfig 

26 

27CONF = nova.conf.CONF 

28LOG = logging.getLogger(__name__) 

29 

30 

31@profiler.trace_cls("volume_api") 

32class LibvirtBaseVolumeDriver(object): 

33 """Base class for volume drivers.""" 

34 

35 def __init__(self, host, is_block_dev): 

36 self.host = host 

37 self.is_block_dev = is_block_dev 

38 

39 def get_config(self, connection_info, disk_info): 

40 """Returns xml for libvirt.""" 

41 conf = vconfig.LibvirtConfigGuestDisk() 

42 

43 conf.source_device = disk_info['type'] 

44 conf.driver_format = "raw" 

45 conf.driver_cache = "none" 

46 conf.target_dev = disk_info['dev'] 

47 conf.target_bus = disk_info['bus'] 

48 conf.serial = connection_info.get('serial') 

49 

50 if CONF.libvirt.virt_type in ('qemu', 'kvm'): 

51 # the QEMU backend supports multiple backends, so tell libvirt 

52 # which one to use 

53 conf.driver_name = 'qemu' 

54 

55 # Support for block size tuning 

56 data = {} 

57 if 'data' in connection_info: 

58 data = connection_info['data'] 

59 if 'logical_block_size' in data: 

60 conf.logical_block_size = data['logical_block_size'] 

61 if 'physical_block_size' in data: 

62 conf.physical_block_size = data['physical_block_size'] 

63 

64 # Extract rate_limit control parameters 

65 if 'qos_specs' in data and data['qos_specs']: 

66 tune_opts = ['total_bytes_sec', 'read_bytes_sec', 

67 'write_bytes_sec', 'total_iops_sec', 

68 'read_iops_sec', 'write_iops_sec', 

69 'read_bytes_sec_max', 'read_iops_sec_max', 

70 'write_bytes_sec_max', 'write_iops_sec_max', 

71 'total_bytes_sec_max', 'total_iops_sec_max', 

72 'size_iops_sec'] 

73 specs = data['qos_specs'] 

74 if isinstance(specs, dict): 

75 for k, v in specs.items(): 

76 if k in tune_opts: 76 ↛ 75line 76 didn't jump to line 75 because the condition on line 76 was always true

77 new_key = 'disk_' + k 

78 setattr(conf, new_key, v) 

79 else: 

80 LOG.warning('Unknown content in connection_info/' 

81 'qos_specs: %s', specs) 

82 

83 # Extract access_mode control parameters 

84 if 'access_mode' in data and data['access_mode']: 

85 access_mode = data['access_mode'] 

86 if access_mode in ('ro', 'rw'): 

87 conf.readonly = access_mode == 'ro' 

88 else: 

89 LOG.error('Unknown content in ' 

90 'connection_info/access_mode: %s', 

91 access_mode) 

92 raise exception.InvalidVolumeAccessMode( 

93 access_mode=access_mode) 

94 

95 # Configure usage of discard 

96 if data.get('discard', False) is True: 

97 conf.driver_discard = 'unmap' 

98 

99 # NOTE(melwitt): We set the device address unit number manually in the 

100 # case of the virtio-scsi controller, in order to allow attachment of 

101 # up to 256 devices. So, we should only be setting the address tag 

102 # if we intend to set the unit number. Otherwise, we will let libvirt 

103 # handle autogeneration of the address tag. 

104 # See https://bugs.launchpad.net/nova/+bug/1792077 for details. 

105 if disk_info['bus'] == 'scsi' and 'unit' in disk_info: 

106 # The driver is responsible to create the SCSI controller 

107 # at index 0. 

108 conf.device_addr = vconfig.LibvirtConfigGuestDeviceAddressDrive() 

109 conf.device_addr.controller = 0 

110 # In order to allow up to 256 disks handled by one 

111 # virtio-scsi controller, the device addr should be 

112 # specified. 

113 conf.device_addr.unit = disk_info['unit'] 

114 

115 if connection_info.get('multiattach', False): 

116 # Note that driver_cache should be disabled (none) when using 

117 # a shareable disk. 

118 conf.shareable = True 

119 

120 volume_id = driver_block_device.get_volume_id(connection_info) 

121 conf.alias = vconfig.make_libvirt_device_alias(volume_id) 

122 volume_secret = None 

123 if volume_id: 

124 volume_secret = self.host.find_secret('volume', volume_id) 

125 if volume_secret: 

126 conf.volume_encryption = vconfig.LibvirtConfigGuestDiskEncryption() 

127 secret = vconfig.LibvirtConfigGuestDiskEncryptionSecret() 

128 secret.type = 'passphrase' 

129 secret.uuid = volume_secret.UUIDString() 

130 conf.volume_encryption.format = 'luks' 

131 conf.volume_encryption.secret = secret 

132 

133 return conf 

134 

135 def connect_volume(self, connection_info, instance): 

136 """Connect the volume.""" 

137 pass 

138 

139 def disconnect_volume(self, connection_info, instance, force=False): 

140 """Disconnect the volume.""" 

141 pass 

142 

143 def extend_volume(self, connection_info, instance, requested_size): 

144 """Extend the volume. 

145 

146 :param connection_info: connection information about the volume 

147 that has been extended. 

148 :param instance: instance connected to the newly extended volume. 

149 :param requested_size: new extended size (in bytes) for the volume to 

150 be extended. 

151 

152 :returns: the new size to use when resizing the disk in QEMU. 

153 

154 Note: the requested_size parameter is not used by all volume drivers 

155 """ 

156 raise NotImplementedError() 

157 

158 

159class LibvirtVolumeDriver(LibvirtBaseVolumeDriver): 

160 """Class for volumes backed by local file.""" 

161 

162 def __init__(self, host): 

163 super(LibvirtVolumeDriver, 

164 self).__init__(host, is_block_dev=True) 

165 

166 def get_config(self, connection_info, disk_info): 

167 """Returns xml for libvirt.""" 

168 conf = super(LibvirtVolumeDriver, 

169 self).get_config(connection_info, disk_info) 

170 conf.source_type = "block" 

171 conf.source_path = connection_info['data']['device_path'] 

172 return conf 

173 

174 

175class LibvirtFakeVolumeDriver(LibvirtBaseVolumeDriver): 

176 """Driver to attach fake volumes to libvirt.""" 

177 

178 def __init__(self, host): 

179 super(LibvirtFakeVolumeDriver, 

180 self).__init__(host, is_block_dev=True) 

181 

182 def get_config(self, connection_info, disk_info): 

183 """Returns xml for libvirt.""" 

184 conf = super(LibvirtFakeVolumeDriver, 

185 self).get_config(connection_info, disk_info) 

186 conf.source_type = "network" 

187 conf.source_protocol = "fake" 

188 conf.source_name = "fake" 

189 return conf