Add snapshot path feature for Qemu nodes

pull/1931/head
seb 3 years ago
parent 6741d6d7a9
commit 58fc6278b1

@ -250,6 +250,13 @@ class BaseNode:
return self._manager
@property
def snapshot_dir(self):
"""
Return the node snapshot directory
"""
return self._project.node_snapshot_directory(self)
@property
def working_dir(self):
"""
Return the node working directory

@ -221,6 +221,27 @@ class Project:
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create the node working directory: {}".format(e))
return workdir
def node_snapshot_directory(self, node):
"""
Returns a snapshot directory for a specific node.
If the directory doesn't exist, the directory is created.
:param node: Node instance
:returns: Node working directory
"""
snapshot_dir = self.node_snapshot_path(node)
if not self._deleted:
try:
os.makedirs(snapshot_dir, exist_ok=True)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create the snapshot directory: {}".format(e))
return snapshot_dir
def node_snapshot_path(self,node): # Only for Qemu now. TODO, write description
return os.path.join(node.path_snapshot, node.manager.module_name.lower(), node.id)
def node_working_path(self, node):
"""

@ -49,7 +49,6 @@ from ...utils.asyncio import monitor_process
from ...utils.images import md5sum
from ...utils import macaddress_to_int, int_to_macaddress
import logging
log = logging.getLogger(__name__)
@ -71,7 +70,6 @@ class QemuVM(BaseNode):
"""
def __init__(self, name, node_id, project, manager, linked_clone=True, qemu_path=None, console=None, console_type="telnet", platform=None):
super().__init__(name, node_id, project, manager, console=console, console_type=console_type, linked_clone=linked_clone, wrap_console=True)
server_config = manager.config.get_section_config("Server")
self._host = server_config.get("host", "127.0.0.1")
@ -85,6 +83,7 @@ class QemuVM(BaseNode):
self._local_udp_tunnels = {}
self._guest_cid = None
self._command_line_changed = False
self.path_snapshot = os.path.join(project._path, "project-files") # Default path for snapshot
# QEMU VM settings
if qemu_path:
@ -1850,7 +1849,7 @@ class QemuVM(BaseNode):
raise QemuError("Could not check '{}' disk image: {}\n{}".format(disk_name, e, stdout))
if self.linked_clone:
disk = os.path.join(self.working_dir, "{}_disk.qcow2".format(disk_name))
disk = os.path.join(self.snapshot_dir, "{}_disk.qcow2".format(disk_name)) # here
if not os.path.exists(disk):
# create the disk
await self._create_linked_clone(disk_name, disk_image, disk)
@ -1894,7 +1893,7 @@ class QemuVM(BaseNode):
raise QemuError("Cannot resize {} while the VM is running".format(drive_name))
if self.linked_clone:
disk_image_path = os.path.join(self.working_dir, "{}_disk.qcow2".format(drive_name))
disk_image_path = os.path.join(self.snapshot_dir, "{}_disk.qcow2".format(drive_name))
if not os.path.exists(disk_image_path):
disk_image = getattr(self, "_{}_disk_image".format(drive_name))
await self._create_linked_clone(drive_name, disk_image, disk_image_path)
@ -2136,7 +2135,7 @@ class QemuVM(BaseNode):
continue
try:
if self.linked_clone:
disk = os.path.join(self.working_dir, "hd{}_disk.qcow2".format(drive))
disk = os.path.join(self.snapshot_dir, "hd{}_disk.qcow2".format(drive))
else:
disk = disk_image
if not os.path.exists(disk):
@ -2171,7 +2170,7 @@ class QemuVM(BaseNode):
continue
try:
if self.linked_clone:
disk = os.path.join(self.working_dir, "hd{}_disk.qcow2".format(drive))
disk = os.path.join(self.snapshot_dir, "hd{}_disk.qcow2".format(drive))
else:
disk = disk_image
if not os.path.exists(disk):

@ -218,6 +218,10 @@ QEMU_CREATE_SCHEMA = {
"description": "Additional QEMU options",
"type": ["string", "null"],
},
"path_snapshot": {
"description": "Path of snapshots",
"type": "string",
},
"custom_adapters": CUSTOM_ADAPTERS_ARRAY_SCHEMA
},
"additionalProperties": False,
@ -412,6 +416,10 @@ QEMU_UPDATE_SCHEMA = {
"description": "Additional QEMU options",
"type": ["string", "null"],
},
"path_snapshot": {
"description": "Path of snapshots",
"type": "string"
},
"custom_adapters": CUSTOM_ADAPTERS_ARRAY_SCHEMA
},
"additionalProperties": False,
@ -622,6 +630,10 @@ QEMU_OBJECT_SCHEMA = {
"description": "Additional QEMU options",
"type": "string",
},
"path_snapshot": {
"description": "Path of snapshots",
"type": "string"
},
"command_line": {
"description": "Last command line used by GNS3 to start QEMU",
"type": "string"
@ -670,6 +682,7 @@ QEMU_OBJECT_SCHEMA = {
"cpu_throttling",
"process_priority",
"options",
"path_snapshot",
"node_directory",
"command_line",
"status"]

@ -210,6 +210,11 @@ QEMU_TEMPLATE_PROPERTIES = {
"type": "string",
"default": ""
},
"path_snapshot": {
"description": "Path of snapshots",
"type": "string",
"default": ""
},
"custom_adapters": CUSTOM_ADAPTERS_ARRAY_SCHEMA
}

Loading…
Cancel
Save