mirror of
https://github.com/GNS3/gns3-server
synced 2025-02-08 22:22:41 +00:00
More robust save/restore for VirtualBox linked clone VM hard disks.
This commit is contained in:
parent
4a91d8a6a5
commit
4fc5364ab5
@ -159,7 +159,7 @@ class VirtualBoxVM(BaseVM):
|
|||||||
if self.id and os.path.isdir(os.path.join(self.working_dir, self._vmname)):
|
if self.id and os.path.isdir(os.path.join(self.working_dir, self._vmname)):
|
||||||
vbox_file = os.path.join(self.working_dir, self._vmname, self._vmname + ".vbox")
|
vbox_file = os.path.join(self.working_dir, self._vmname, self._vmname + ".vbox")
|
||||||
yield from self.manager.execute("registervm", [vbox_file])
|
yield from self.manager.execute("registervm", [vbox_file])
|
||||||
yield from self._reattach_hdds()
|
yield from self._reattach_linked_hdds()
|
||||||
else:
|
else:
|
||||||
yield from self._create_linked_clone()
|
yield from self._create_linked_clone()
|
||||||
|
|
||||||
@ -314,7 +314,10 @@ class VirtualBoxVM(BaseVM):
|
|||||||
return hdds
|
return hdds
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def _reattach_hdds(self):
|
def _reattach_linked_hdds(self):
|
||||||
|
"""
|
||||||
|
Reattach linked cloned hard disks.
|
||||||
|
"""
|
||||||
|
|
||||||
hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json")
|
hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json")
|
||||||
try:
|
try:
|
||||||
@ -333,10 +336,67 @@ class VirtualBoxVM(BaseVM):
|
|||||||
device=hdd_info["device"],
|
device=hdd_info["device"],
|
||||||
medium=hdd_file))
|
medium=hdd_file))
|
||||||
|
|
||||||
yield from self._storage_attach('--storagectl "{}" --port {} --device {} --type hdd --medium "{}"'.format(hdd_info["controller"],
|
try:
|
||||||
hdd_info["port"],
|
yield from self._storage_attach('--storagectl "{}" --port {} --device {} --type hdd --medium "{}"'.format(hdd_info["controller"],
|
||||||
hdd_info["device"],
|
hdd_info["port"],
|
||||||
hdd_file))
|
hdd_info["device"],
|
||||||
|
hdd_file))
|
||||||
|
|
||||||
|
except VirtualBoxError as e:
|
||||||
|
log.warn("VirtualBox VM '{name}' [{id}] error reattaching HDD {controller} {port} {device} {medium}: {error}".format(name=self.name,
|
||||||
|
id=self.id,
|
||||||
|
controller=hdd_info["controller"],
|
||||||
|
port=hdd_info["port"],
|
||||||
|
device=hdd_info["device"],
|
||||||
|
medium=hdd_file,
|
||||||
|
error=e))
|
||||||
|
continue
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def save_linked_hdds_info(self):
|
||||||
|
"""
|
||||||
|
Save linked cloned hard disks information.
|
||||||
|
|
||||||
|
:returns: disk table information
|
||||||
|
"""
|
||||||
|
|
||||||
|
hdd_table = []
|
||||||
|
if self._linked_clone:
|
||||||
|
if os.path.exists(self.working_dir):
|
||||||
|
hdd_files = yield from self._get_all_hdd_files()
|
||||||
|
vm_info = yield from self._get_vm_info()
|
||||||
|
for entry, value in vm_info.items():
|
||||||
|
match = re.search("^([\s\w]+)\-(\d)\-(\d)$", entry) # match Controller-PortNumber-DeviceNumber entry
|
||||||
|
if match:
|
||||||
|
controller = match.group(1)
|
||||||
|
port = match.group(2)
|
||||||
|
device = match.group(3)
|
||||||
|
if value in hdd_files and os.path.exists(os.path.join(self.working_dir, self._vmname, "Snapshots", os.path.basename(value))):
|
||||||
|
log.info("VirtualBox VM '{name}' [{id}] detaching HDD {controller} {port} {device}".format(name=self.name,
|
||||||
|
id=self.id,
|
||||||
|
controller=controller,
|
||||||
|
port=port,
|
||||||
|
device=device))
|
||||||
|
hdd_table.append(
|
||||||
|
{
|
||||||
|
"hdd": os.path.basename(value),
|
||||||
|
"controller": controller,
|
||||||
|
"port": port,
|
||||||
|
"device": device,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if hdd_table:
|
||||||
|
try:
|
||||||
|
hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json")
|
||||||
|
with open(hdd_info_file, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(hdd_table, f, indent=4)
|
||||||
|
except OSError as e:
|
||||||
|
log.warning("VirtualBox VM '{name}' [{id}] could not write HHD info file: {error}".format(name=self.name,
|
||||||
|
id=self.id,
|
||||||
|
error=e.strerror))
|
||||||
|
|
||||||
|
return hdd_table
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def close(self):
|
def close(self):
|
||||||
@ -363,47 +423,29 @@ class VirtualBoxVM(BaseVM):
|
|||||||
yield from self.stop()
|
yield from self.stop()
|
||||||
|
|
||||||
if self._linked_clone:
|
if self._linked_clone:
|
||||||
hdd_table = []
|
hdd_table = yield from self.save_linked_hdds_info()
|
||||||
if os.path.exists(self.working_dir):
|
for hdd in hdd_table.copy():
|
||||||
hdd_files = yield from self._get_all_hdd_files()
|
log.info("VirtualBox VM '{name}' [{id}] detaching HDD {controller} {port} {device}".format(name=self.name,
|
||||||
vm_info = yield from self._get_vm_info()
|
id=self.id,
|
||||||
for entry, value in vm_info.items():
|
controller=hdd["controller"],
|
||||||
match = re.search("^([\s\w]+)\-(\d)\-(\d)$", entry) # match Controller-PortNumber-DeviceNumber entry
|
port=hdd["port"],
|
||||||
if match:
|
device=hdd["device"]))
|
||||||
controller = match.group(1)
|
try:
|
||||||
port = match.group(2)
|
yield from self._storage_attach('--storagectl "{}" --port {} --device {} --type hdd --medium none'.format(hdd["controller"],
|
||||||
device = match.group(3)
|
hdd["port"],
|
||||||
if value in hdd_files and os.path.exists(os.path.join(self.working_dir, self._vmname, "Snapshots", os.path.basename(value))):
|
hdd["device"]))
|
||||||
log.info("VirtualBox VM '{name}' [{id}] detaching HDD {controller} {port} {device}".format(name=self.name,
|
except VirtualBoxError as e:
|
||||||
id=self.id,
|
log.warn("VirtualBox VM '{name}' [{id}] error detaching HDD {controller} {port} {device}: {error}".format(name=self.name,
|
||||||
controller=controller,
|
id=self.id,
|
||||||
port=port,
|
controller=hdd["controller"],
|
||||||
device=device))
|
port=hdd["port"],
|
||||||
yield from self._storage_attach('--storagectl "{}" --port {} --device {} --type hdd --medium none'.format(controller,
|
device=hdd["device"],
|
||||||
port,
|
error=e))
|
||||||
device))
|
continue
|
||||||
hdd_table.append(
|
|
||||||
{
|
|
||||||
"hdd": os.path.basename(value),
|
|
||||||
"controller": controller,
|
|
||||||
"port": port,
|
|
||||||
"device": device,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
log.info("VirtualBox VM '{name}' [{id}] unregistering".format(name=self.name, id=self.id))
|
log.info("VirtualBox VM '{name}' [{id}] unregistering".format(name=self.name, id=self.id))
|
||||||
yield from self.manager.execute("unregistervm", [self._name])
|
yield from self.manager.execute("unregistervm", [self._name])
|
||||||
|
|
||||||
if hdd_table:
|
|
||||||
try:
|
|
||||||
hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json")
|
|
||||||
with open(hdd_info_file, "w", encoding="utf-8") as f:
|
|
||||||
json.dump(hdd_table, f, indent=4)
|
|
||||||
except OSError as e:
|
|
||||||
log.warning("VirtualBox VM '{name}' [{id}] could not write HHD info file: {error}".format(name=self.name,
|
|
||||||
id=self.id,
|
|
||||||
error=e.strerror))
|
|
||||||
|
|
||||||
log.info("VirtualBox VM '{name}' [{id}] closed".format(name=self.name, id=self.id))
|
log.info("VirtualBox VM '{name}' [{id}] closed".format(name=self.name, id=self.id))
|
||||||
self._closed = True
|
self._closed = True
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user