bootstrap-vz/common/tasks/loopback.py

61 lines
1.8 KiB
Python
Raw Normal View History

from base import Task
from common import phases
from filesystem import UnmountVolume
from common.tools import log_check_call
class Create(Task):
description = 'Creating a loopback volume'
phase = phases.volume_creation
def run(self, info):
loopback_filename = 'loopback-{id:x}.img'.format(id=info.run_id)
import os.path
info.loopback_file = os.path.join(info.manifest.volume['loopback_dir'], loopback_filename)
log_check_call(['/bin/dd',
2013-08-17 17:28:46 +02:00
'if=/dev/zero', 'of=' + info.loopback_file,
'bs=1M', 'seek=' + str(info.manifest.volume['size']), 'count=0'])
class CreateQemuImg(Task):
description = 'Creating a loopback volume with qemu'
2013-08-11 18:26:01 +02:00
phase = phases.volume_creation
def run(self, info):
loopback_filename = 'loopback-{id:x}.img'.format(id=info.run_id)
import os.path
info.loopback_file = os.path.join(info.manifest.volume['loopback_dir'], loopback_filename)
log_check_call(['/usr/bin/qemu-img', 'create', '-f', 'raw',
2013-08-17 17:28:46 +02:00
info.loopback_file, str(info.manifest.volume['size']) + 'M'])
class Attach(Task):
description = 'Attaching the loopback volume'
phase = phases.volume_creation
2013-08-11 18:26:01 +02:00
after = [Create, CreateQemuImg]
def run(self, info):
info.bootstrap_device = {}
command = ['/sbin/losetup', '--show', '--find', info.loopback_file]
[info.bootstrap_device['path']] = log_check_call(command)
class Detach(Task):
description = 'Detaching the loopback volume'
phase = phases.volume_unmounting
after = [UnmountVolume]
def run(self, info):
log_check_call(['/sbin/losetup', '--detach', info.bootstrap_device['path']])
del info.bootstrap_device
class Delete(Task):
description = 'Deleting the loopback volume'
phase = phases.cleaning
def run(self, info):
from os import remove
remove(info.loopback_file)
del info.loopback_file