2013-09-15 13:19:45 +02:00
|
|
|
from base.fs.volume import Volume
|
|
|
|
from common.tools import log_check_call
|
|
|
|
|
|
|
|
|
|
|
|
class LoopbackVolume(Volume):
|
|
|
|
|
2013-12-15 00:51:51 +01:00
|
|
|
extension = 'raw'
|
|
|
|
|
2013-09-15 13:19:45 +02:00
|
|
|
def create(self, image_path):
|
|
|
|
self.fsm.create(image_path=image_path)
|
|
|
|
|
2013-09-22 21:20:09 +02:00
|
|
|
def _before_create(self, e):
|
2013-09-15 13:19:45 +02:00
|
|
|
self.image_path = e.image_path
|
2014-01-19 12:39:07 +01:00
|
|
|
vol_size = str(self.size.get_qty_in('MiB')) + 'M'
|
2014-02-23 22:16:10 +01:00
|
|
|
log_check_call(['qemu-img', 'create', '-f', 'raw', self.image_path, vol_size])
|
2013-09-15 13:19:45 +02:00
|
|
|
|
2013-09-22 21:20:09 +02:00
|
|
|
def _before_attach(self, e):
|
2014-02-23 22:16:10 +01:00
|
|
|
[self.loop_device_path] = log_check_call(['losetup', '--show', '--find', self.image_path])
|
2013-09-15 13:19:45 +02:00
|
|
|
self.device_path = self.loop_device_path
|
|
|
|
|
2013-09-22 21:20:09 +02:00
|
|
|
def _before_detach(self, e):
|
2014-02-23 22:16:10 +01:00
|
|
|
log_check_call(['losetup', '--detach', self.loop_device_path])
|
2013-09-15 13:19:45 +02:00
|
|
|
del self.loop_device_path
|
|
|
|
del self.device_path
|
|
|
|
|
2013-09-22 21:20:09 +02:00
|
|
|
def _before_delete(self, e):
|
2013-09-15 13:19:45 +02:00
|
|
|
from os import remove
|
|
|
|
remove(self.image_path)
|
|
|
|
del self.image_path
|