List only valid existing IOS images (for IOS router wizard).

pull/370/head
grossmj 9 years ago
parent be85d95ff7
commit 4171aebfdb

@ -655,3 +655,33 @@ class Dynamips(BaseManager):
Return the full path of the images directory on disk
"""
return os.path.join(os.path.expanduser(self.config.get_section_config("Server").get("images_path", "~/GNS3/images")), "IOS")
@asyncio.coroutine
def list_images(self):
"""
Return the list of available IOS images.
:returns: Array of hash
"""
image_dir = self.get_images_directory()
try:
files = os.listdir(image_dir)
except FileNotFoundError:
return []
files.sort()
images = []
for filename in files:
if filename[0] != "." and not filename.endswith(".md5sum"):
try:
path = os.path.join(image_dir, filename)
with open(path, "rb") as f:
# read the first 7 bytes of the file.
elf_header_start = f.read(7)
except OSError as e:
print(e)
continue
# valid IOS images must start with the ELF magic number, be 32-bit, big endian and have an ELF version of 1
if elf_header_start == b'\x7fELF\x01\x02\x01':
images.append({"filename": filename})
return images

Loading…
Cancel
Save