Coverage for nova/virt/disk/mount/loop.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-17 15:08 +0000

1# Copyright 2011 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"""Support for mounting images with the loop device.""" 

15 

16from oslo_log import log as logging 

17 

18from nova.i18n import _ 

19import nova.privsep.fs 

20from nova.virt.disk.mount import api 

21 

22LOG = logging.getLogger(__name__) 

23 

24 

25class LoopMount(api.Mount): 

26 """loop back support for raw images.""" 

27 mode = 'loop' 

28 

29 def _inner_get_dev(self): 

30 out, err = nova.privsep.fs.loopsetup(self.image.path) 

31 if err: 

32 self.error = _('Could not attach image to loopback: %s') % err 

33 LOG.info('Loop mount error: %s', self.error) 

34 self.linked = False 

35 self.device = None 

36 return False 

37 

38 self.device = out.strip() 

39 LOG.debug("Got loop device %s", self.device) 

40 self.linked = True 

41 return True 

42 

43 def get_dev(self): 

44 # NOTE(mikal): the retry is required here in case we are low on loop 

45 # devices. Note however that modern kernels will use more loop devices 

46 # if they exist. If you're seeing lots of retries, consider adding 

47 # more devices. 

48 return self._get_dev_retry_helper() 

49 

50 def unget_dev(self): 

51 if not self.linked: 

52 return 

53 

54 # NOTE(mikal): On some kernels, losetup -d will intermittently fail, 

55 # thus leaking a loop device unless the losetup --detach is retried: 

56 # https://lkml.org/lkml/2012/9/28/62 

57 LOG.debug("Release loop device %s", self.device) 

58 nova.privsep.fs.loopremove(self.device) 

59 self.linked = False 

60 self.device = None