Coverage for nova/virt/disk/vfs/api.py: 74%

37 statements  

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

1# Copyright 2012 Red Hat, Inc. 

2# 

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

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

5# a copy of the License at 

6# 

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

8# 

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

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

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

12# License for the specific language governing permissions and limitations 

13# under the License. 

14 

15from oslo_log import log as logging 

16from oslo_utils import importutils 

17 

18LOG = logging.getLogger(__name__) 

19 

20 

21class VFS(object): 

22 """Interface for manipulating disk image. 

23 

24 The VFS class defines an interface for manipulating files within 

25 a virtual disk image filesystem. This allows file injection code 

26 to avoid the assumption that the virtual disk image can be mounted 

27 in the host filesystem. 

28 

29 All paths provided to the APIs in this class should be relative 

30 to the root of the virtual disk image filesystem. Subclasses 

31 will translate paths as required by their implementation. 

32 """ 

33 

34 # Class level flag to indicate whether we can consider 

35 # that guestfs is ready to be used. 

36 guestfs_ready = False 

37 

38 @staticmethod 

39 def instance_for_image(image, partition): 

40 """Get a VFS instance for the image 

41 

42 :param image: instance of nova.virt.image.model.Image 

43 :param partition: the partition number to access 

44 """ 

45 

46 LOG.debug("Instance for image image=%(image)s " 

47 "partition=%(partition)s", 

48 {'image': image, 'partition': partition}) 

49 

50 LOG.debug("Using primary VFSGuestFS") 

51 vfs = importutils.import_object( 

52 "nova.virt.disk.vfs.guestfs.VFSGuestFS", 

53 image, partition) 

54 if not VFS.guestfs_ready: 

55 # Inspect for capabilities and keep 

56 # track of the result only if succeeded. 

57 vfs.inspect_capabilities() 

58 VFS.guestfs_ready = True 

59 return vfs 

60 

61 def __init__(self, image, partition): 

62 """Create a new local VFS instance 

63 

64 :param image: instance of nova.virt.image.model.Image 

65 :param partition: the partition number to access 

66 """ 

67 

68 self.image = image 

69 self.partition = partition 

70 

71 def setup(self, mount=True): 

72 """Performs any one-time setup. 

73 

74 Perform any one-time setup tasks to make the virtual filesystem 

75 available to future API calls. 

76 """ 

77 pass 

78 

79 def teardown(self): 

80 """Releases all resources initialized in the setup method.""" 

81 pass 

82 

83 def make_path(self, path): 

84 """Creates a directory @path. 

85 

86 Create a directory @path, including all intermedia path components 

87 if they do not already exist. 

88 """ 

89 pass 

90 

91 def append_file(self, path, content): 

92 """Appends @content to the end of the file. 

93 

94 Append @content to the end of the file identified by @path, creating 

95 the file if it does not already exist. 

96 """ 

97 pass 

98 

99 def replace_file(self, path, content): 

100 """Replaces contents of the file. 

101 

102 Replace the entire contents of the file identified by @path, with 

103 @content, creating the file if it does not already exist. 

104 """ 

105 pass 

106 

107 def read_file(self, path): 

108 """Returns the entire contents of the file identified by @path.""" 

109 pass 

110 

111 def has_file(self, path): 

112 """Returns a True if the file identified by @path exists.""" 

113 pass 

114 

115 def set_permissions(self, path, mode): 

116 """Sets the permissions on the file. 

117 

118 Set the permissions on the file identified by @path to @mode. The file 

119 must exist prior to this call. 

120 """ 

121 pass 

122 

123 def set_ownership(self, path, user, group): 

124 """Sets the ownership on the file. 

125 

126 Set the ownership on the file identified by @path to the username 

127 @user and groupname @group. Either of @user or @group may be None, 

128 in which case the current ownership will be left unchanged. 

129 The ownership must be passed in string form, allowing subclasses to 

130 translate to uid/gid form as required. The file must exist prior to 

131 this call. 

132 """ 

133 pass 

134 

135 def get_image_fs(self): 

136 """Returns the filesystem type or an empty string. 

137 

138 Determine the filesystem type whether the disk image is 

139 partition less. 

140 """ 

141 pass