Coverage for nova/virt/image/model.py: 100%

37 statements  

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

1# 

2# Copyright (C) 2014 Red Hat, Inc 

3# 

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

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

6# a copy of the License at 

7# 

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

9# 

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

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

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

13# License for the specific language governing permissions and limitations 

14# under the License. 

15# 

16 

17from oslo_utils import strutils 

18 

19from nova import exception 

20 

21FORMAT_RAW = "raw" 

22FORMAT_QCOW2 = "qcow2" 

23FORMAT_PLOOP = "ploop" 

24 

25ALL_FORMATS = [ 

26 FORMAT_RAW, 

27 FORMAT_QCOW2, 

28 FORMAT_PLOOP, 

29] 

30 

31 

32class Image(object): 

33 """Base class for all image types. 

34 

35 All image types have a format, though for many of 

36 them only a subset of formats will commonly be 

37 used. For example, block devices are almost 

38 always going to be FORMAT_RAW. Though it is in 

39 fact possible from a technical POV to store a 

40 qcow2 data inside a block device, Nova does not 

41 (at this time) make use of such possibilities. 

42 """ 

43 

44 def __init__(self, format): 

45 """Create a new abstract image 

46 

47 :param format: one of the format constants 

48 """ 

49 super(Image, self).__init__() 

50 

51 self.format = format 

52 

53 if format not in ALL_FORMATS: 

54 raise exception.InvalidImageFormat(format=format) 

55 

56 def __repr__(self): 

57 msg = "<" + self.__class__.__name__ + ":" + str(self.__dict__) + ">" 

58 return strutils.mask_password(msg) 

59 

60 def __eq__(self, other): 

61 return ((self.__class__ == other.__class__) and 

62 (self.__dict__ == other.__dict__)) 

63 

64 def __hash__(self): 

65 return hash(str(self.__dict__)) 

66 

67 

68class LocalImage(Image): 

69 """Class for images that are paths within the 

70 local filesystem 

71 """ 

72 

73 def __init__(self, path, format): 

74 """Create a new local image object 

75 

76 :param path: qualified filename of the image 

77 :param format: one of the format constants 

78 """ 

79 super(LocalImage, self).__init__(format) 

80 

81 self.path = path 

82 

83 

84class LocalFileImage(LocalImage): 

85 """Class for images that are files on a locally 

86 accessible filesystem 

87 """ 

88 

89 def __init__(self, path, format): 

90 """Create a new local file object 

91 

92 :param path: qualified filename of the image 

93 :param format: one of the format constants 

94 """ 

95 super(LocalFileImage, self).__init__(path, format) 

96 

97 

98class LocalBlockImage(LocalImage): 

99 """Class for images that are block devices on 

100 the local host 

101 """ 

102 

103 def __init__(self, path): 

104 """Create a new local file object 

105 

106 :param path: qualified filename of the image 

107 """ 

108 super(LocalBlockImage, self).__init__(path, FORMAT_RAW) 

109 

110 

111class RBDImage(Image): 

112 """Class for images that are volumes on a remote 

113 RBD server 

114 """ 

115 

116 def __init__(self, name, pool, user, password, servers): 

117 """Create a new RBD image object 

118 

119 :param name: name of the image relative to the pool 

120 :param pool: name of the pool holding the image 

121 :param user: username to authenticate as 

122 :param password: credentials for authenticating with 

123 :param servers: list of hostnames for the server 

124 """ 

125 super(RBDImage, self).__init__(FORMAT_RAW) 

126 

127 self.name = name 

128 self.pool = pool 

129 self.user = user 

130 self.password = password 

131 self.servers = servers