API for creating a qemu disk image

This commit is contained in:
Julien Duponchelle committed 2015-07-27 19:18:36 +02:00
1 parent 5d8c90d138
commit 5b0c36c0d6
6 files changed
+123 -60

No files matched your search

+29
View File
@@ -187,3 +187,32 @@ class Qemu(BaseManager):
"""
return os.path.join(os.path.expanduser(self.config.get_section_config("Server").get("images_path", "~/GNS3/images")), "QEMU")
@asyncio.coroutine
def create_disk(self, qemu_img, path, options):
"""
Create a qemu disk with qemu-img
:param qemu_img: qemu-img binary path
:param path: Image path
:param options: Disk image creation options
"""
try:
img_format = options.pop("format")
img_size = options.pop("size")
if not os.path.isabs(path):
directory = self.get_images_directory()
os.makedirs(directory, exist_ok=True)
path = os.path.join(directory, os.path.basename(path))
command = [qemu_img, "create", "-f", img_format]
for option in sorted(options.keys()):
command.extend(["-o", "{}={}".format(option, options[option])])
command.append(path)
command.append("{}M".format(img_size))
process = yield from asyncio.create_subprocess_exec(*command)
yield from process.wait()
except (OSError, subprocess.SubprocessError) as e:
raise QemuError("Could create disk image {}:{}".format(path, e))
-29
View File
@@ -189,7 +189,6 @@ class QemuVM(BaseVM):
:param hda_disk_image: QEMU hda disk image path
"""
self._hda_disk_image = self.manager.get_abs_image_path(hda_disk_image)
log.info('QEMU VM "{name}" [{id}] has set the QEMU hda disk image path to {disk_image}'.format(name=self._name,
id=self._id,
@@ -1242,31 +1241,3 @@ class QemuVM(BaseVM):
answer["kernel_image_md5sum"] = md5sum(self._kernel_image)
return answer
@asyncio.coroutine
def _create_disk(self, name, options):
"""
Create a qemu disk with qemu-img
:param name: Image name without the extension
:param options: Disk image creation options
:returns: Image name with the extensions
"""
img_format = options.pop("format")
img_size = options.pop("size")
img_name = "{}.{}".format(name, img_format)
qemu_img = self._get_qemu_img()
command = [qemu_img, "create", "-f", img_format]
for option in sorted(options.keys()):
command.extend(["-o", "{}={}".format(option, options[option])])
command.append(os.path.join(self.working_dir, img_name))
command.append("{}M".format(img_size))
try:
process = yield from asyncio.create_subprocess_exec(*command)
yield from process.wait()
except (OSError, subprocess.SubprocessError) as e:
raise QemuError("Could create disk image {}:{}".format(name, e))
return img_name