From ccb94be2fae6aca94a8a021c0b5602ae68f8d7c4 Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Aug 06 2015 11:44:32 +0000 Subject: We can setup the instance default storage. --- diff --git a/computer/virt.py b/computer/virt.py index 8179182..996d9a6 100644 --- a/computer/virt.py +++ b/computer/virt.py @@ -1,3 +1,4 @@ +import os import subprocess def system(cmd): @@ -12,3 +13,44 @@ def system(cmd): out, err = ret.communicate() returncode = ret.returncode return out, err, returncode + + +def download_image(url, path): + """ + Downloads an image to a given directory. + :param url: URL of the image. + :param path: Directory to the file in. + :return: If download happens properly, returns True, otherwise False + """ + cmd = 'wget %s -O %s' % (url, os.path.dirname(path)) + out, err, ret_code = system(cmd) + if ret_code: + return False + return True + + +def setup_instance_storage(image_name, instance_name, path='/var/lib/libvirt/images/', + size='5G', device='/dev/sda1'): + """ + It creates the image to be used in the instance. + + :param image_name: For now, the qcow2 image name (absolute path of the image). + :param instance_name: Name of the instance for the storage. + :param path: Where this image should go. + :param size: New size of the instance. + :param device: The partition we should resize. + :return: Tuple, (Boolean, str). + """ + file_path = os.path.join(path, '%s.qcow2' % instance_name) + cmd = 'qemu-img create -f qcow2 -o preallocation=metadata {0} {1}'.format(file_path, size) + out, err, ret_code = system(cmd) + if ret_code: + return False, 'Storage creation failed.' + cmd = 'virt-resize -v --expand {0} {1} {2}'.format(device, image_name, file_path) + out, err, ret_code = system(cmd) + if ret_code: + return False, 'Resize failed.' + + return True, file_path + +