2015-06-17 18:11:25 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import hashlib
|
|
|
|
|
2016-06-07 20:38:01 +03:00
|
|
|
from ..config import Config
|
|
|
|
from . import force_unix_path
|
2023-08-03 05:09:24 +03:00
|
|
|
from io import DEFAULT_BUFFER_SIZE
|
2016-06-07 20:38:01 +03:00
|
|
|
|
2015-07-16 19:56:36 +03:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2015-06-17 18:11:25 +03:00
|
|
|
|
2015-11-09 13:28:00 +02:00
|
|
|
|
2023-05-05 17:40:58 +03:00
|
|
|
def list_images(emulator_type):
|
2016-06-08 15:14:03 +03:00
|
|
|
"""
|
2023-05-05 17:40:58 +03:00
|
|
|
Scan directories for available image for a given emulator type
|
2016-06-08 15:14:03 +03:00
|
|
|
|
2023-05-05 17:40:58 +03:00
|
|
|
:param emulator_type: emulator type (dynamips, qemu, iou)
|
2016-06-08 15:14:03 +03:00
|
|
|
"""
|
|
|
|
files = set()
|
2016-11-28 20:49:50 +02:00
|
|
|
images = []
|
|
|
|
|
|
|
|
server_config = Config.instance().get_section_config("Server")
|
|
|
|
general_images_directory = os.path.expanduser(server_config.get("images_path", "~/GNS3/images"))
|
|
|
|
|
2023-05-05 17:40:58 +03:00
|
|
|
# Subfolder of the general_images_directory specific to this emulator type
|
|
|
|
default_directory = default_images_directory(emulator_type)
|
2016-11-28 20:49:50 +02:00
|
|
|
|
2023-05-05 17:40:58 +03:00
|
|
|
for directory in images_directories(emulator_type):
|
2016-11-28 20:49:50 +02:00
|
|
|
|
|
|
|
# We limit recursion to path outside the default images directory
|
|
|
|
# the reason is in the default directory manage file organization and
|
|
|
|
# it should be flatten to keep things simple
|
|
|
|
recurse = True
|
|
|
|
if os.path.commonprefix([directory, general_images_directory]) == general_images_directory:
|
|
|
|
recurse = False
|
|
|
|
|
2016-09-28 11:27:30 +03:00
|
|
|
directory = os.path.normpath(directory)
|
2016-11-28 20:49:50 +02:00
|
|
|
for root, _, filenames in _os_walk(directory, recurse=recurse):
|
|
|
|
for filename in filenames:
|
|
|
|
if filename not in files:
|
|
|
|
if filename.endswith(".md5sum") or filename.startswith("."):
|
2016-08-18 20:26:17 +03:00
|
|
|
continue
|
2023-05-05 17:40:58 +03:00
|
|
|
elif ((filename.endswith(".image") or filename.endswith(".bin")) and emulator_type == "dynamips") \
|
|
|
|
or ((filename.endswith(".bin") or filename.startswith("i86bi")) and emulator_type == "iou") \
|
|
|
|
or (not filename.endswith(".bin") and not filename.endswith(".image") and emulator_type == "qemu"):
|
2016-11-28 20:49:50 +02:00
|
|
|
files.add(filename)
|
|
|
|
|
|
|
|
# It the image is located in the standard directory the path is relative
|
|
|
|
if os.path.commonprefix([root, default_directory]) != default_directory:
|
|
|
|
path = os.path.join(root, filename)
|
|
|
|
else:
|
|
|
|
path = os.path.relpath(os.path.join(root, filename), default_directory)
|
|
|
|
|
|
|
|
try:
|
2023-05-05 17:40:58 +03:00
|
|
|
if emulator_type in ["dynamips", "iou"]:
|
2016-11-28 20:49:50 +02:00
|
|
|
with open(os.path.join(root, filename), "rb") as f:
|
|
|
|
# read the first 7 bytes of the file.
|
|
|
|
elf_header_start = f.read(7)
|
2024-05-09 11:21:05 +03:00
|
|
|
# valid IOU or IOS images must start with the ELF magic number, be 32-bit or 64-bit,
|
|
|
|
# little endian and have an ELF version of 1
|
|
|
|
if elf_header_start != b'\x7fELF\x02\x01\x01' and elf_header_start != b'\x7fELF\x01\x01\x01':
|
2016-11-28 20:49:50 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
images.append({
|
|
|
|
"filename": filename,
|
2017-04-18 15:23:29 +03:00
|
|
|
"path": force_unix_path(path),
|
2016-11-28 20:49:50 +02:00
|
|
|
"md5sum": md5sum(os.path.join(root, filename)),
|
|
|
|
"filesize": os.stat(os.path.join(root, filename)).st_size})
|
|
|
|
except OSError as e:
|
2018-03-15 09:17:39 +02:00
|
|
|
log.warning("Can't add image {}: {}".format(path, str(e)))
|
2016-11-28 20:49:50 +02:00
|
|
|
return images
|
|
|
|
|
|
|
|
|
|
|
|
def _os_walk(directory, recurse=True, **kwargs):
|
|
|
|
"""
|
|
|
|
Work like os.walk but if recurse is False just list current directory
|
|
|
|
"""
|
|
|
|
if recurse:
|
|
|
|
for root, dirs, files in os.walk(directory, **kwargs):
|
|
|
|
yield root, dirs, files
|
|
|
|
else:
|
|
|
|
files = []
|
|
|
|
for filename in os.listdir(directory):
|
|
|
|
if os.path.isfile(os.path.join(directory, filename)):
|
|
|
|
files.append(filename)
|
|
|
|
yield directory, [], files
|
|
|
|
|
|
|
|
|
2023-05-05 17:40:58 +03:00
|
|
|
def default_images_directory(emulator_type):
|
2016-11-28 20:49:50 +02:00
|
|
|
"""
|
|
|
|
:returns: Return the default directory for a node type
|
|
|
|
"""
|
|
|
|
server_config = Config.instance().get_section_config("Server")
|
|
|
|
img_dir = os.path.expanduser(server_config.get("images_path", "~/GNS3/images"))
|
2023-05-05 17:40:58 +03:00
|
|
|
if emulator_type == "qemu":
|
2016-11-28 20:49:50 +02:00
|
|
|
return os.path.join(img_dir, "QEMU")
|
2023-05-05 17:40:58 +03:00
|
|
|
elif emulator_type == "iou":
|
2016-11-28 20:49:50 +02:00
|
|
|
return os.path.join(img_dir, "IOU")
|
2023-05-05 17:40:58 +03:00
|
|
|
elif emulator_type == "dynamips":
|
2016-11-28 20:49:50 +02:00
|
|
|
return os.path.join(img_dir, "IOS")
|
|
|
|
else:
|
2023-05-05 17:40:58 +03:00
|
|
|
raise NotImplementedError("%s node type is not supported", emulator_type)
|
2016-06-08 15:14:03 +03:00
|
|
|
|
|
|
|
|
2023-05-05 17:40:58 +03:00
|
|
|
def images_directories(emulator_type):
|
2016-06-07 20:38:01 +03:00
|
|
|
"""
|
2019-07-10 12:16:50 +03:00
|
|
|
Return all directories where we will look for images
|
2016-06-07 20:38:01 +03:00
|
|
|
by priority
|
|
|
|
|
2023-05-05 17:40:58 +03:00
|
|
|
:param emulator_type: Type of emulator
|
2016-06-07 20:38:01 +03:00
|
|
|
"""
|
|
|
|
server_config = Config.instance().get_section_config("Server")
|
|
|
|
|
|
|
|
paths = []
|
|
|
|
img_dir = os.path.expanduser(server_config.get("images_path", "~/GNS3/images"))
|
2023-05-05 17:40:58 +03:00
|
|
|
type_img_directory = default_images_directory(emulator_type)
|
2017-01-27 11:56:48 +02:00
|
|
|
try:
|
|
|
|
os.makedirs(type_img_directory, exist_ok=True)
|
|
|
|
paths.append(type_img_directory)
|
|
|
|
except (OSError, PermissionError):
|
|
|
|
pass
|
2016-09-27 18:15:59 +03:00
|
|
|
for directory in server_config.get("additional_images_path", "").split(";"):
|
2016-06-07 20:38:01 +03:00
|
|
|
paths.append(directory)
|
|
|
|
# Compatibility with old topologies we look in parent directory
|
|
|
|
paths.append(img_dir)
|
2019-03-18 10:33:37 +02:00
|
|
|
# Return only the existing paths
|
2016-06-07 20:38:01 +03:00
|
|
|
return [force_unix_path(p) for p in paths if os.path.exists(p)]
|
|
|
|
|
|
|
|
|
2018-01-29 11:18:13 +02:00
|
|
|
def md5sum(path, stopped_event=None):
|
2015-06-17 18:11:25 +03:00
|
|
|
"""
|
|
|
|
Return the md5sum of an image and cache it on disk
|
|
|
|
|
|
|
|
:param path: Path to the image
|
2018-01-29 11:18:13 +02:00
|
|
|
:param stopped_event: In case you execute this function on thread and would like to have possibility
|
|
|
|
to cancel operation pass the `threading.Event`
|
2015-06-17 18:11:25 +03:00
|
|
|
:returns: Digest of the image
|
|
|
|
"""
|
|
|
|
|
2015-06-19 17:36:25 +03:00
|
|
|
if path is None or len(path) == 0 or not os.path.exists(path):
|
2015-06-17 18:11:25 +03:00
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(path + '.md5sum') as f:
|
2018-06-04 20:01:20 +03:00
|
|
|
md5 = f.read().strip()
|
2016-10-20 18:24:05 +03:00
|
|
|
if len(md5) == 32:
|
|
|
|
return md5
|
2016-07-28 13:34:41 +03:00
|
|
|
# Unicode error is when user rename an image to .md5sum ....
|
|
|
|
except (OSError, UnicodeDecodeError):
|
2015-06-17 18:11:25 +03:00
|
|
|
pass
|
|
|
|
|
2015-06-19 17:36:25 +03:00
|
|
|
try:
|
|
|
|
m = hashlib.md5()
|
2023-08-02 10:41:51 +03:00
|
|
|
log.debug("Calculating MD5 sum of `{}`".format(path))
|
2015-06-19 17:36:25 +03:00
|
|
|
with open(path, 'rb') as f:
|
|
|
|
while True:
|
2018-01-29 11:18:13 +02:00
|
|
|
if stopped_event is not None and stopped_event.is_set():
|
|
|
|
log.error("MD5 sum calculation of `{}` has stopped due to cancellation".format(path))
|
|
|
|
return
|
2023-08-03 05:09:24 +03:00
|
|
|
buf = f.read(DEFAULT_BUFFER_SIZE)
|
2015-06-19 17:36:25 +03:00
|
|
|
if not buf:
|
|
|
|
break
|
|
|
|
m.update(buf)
|
|
|
|
digest = m.hexdigest()
|
|
|
|
except OSError as e:
|
|
|
|
log.error("Can't create digest of %s: %s", path, str(e))
|
|
|
|
return None
|
2015-06-17 18:11:25 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
with open('{}.md5sum'.format(path), 'w+') as f:
|
|
|
|
f.write(digest)
|
|
|
|
except OSError as e:
|
|
|
|
log.error("Can't write digest of %s: %s", path, str(e))
|
|
|
|
|
|
|
|
return digest
|
|
|
|
|
|
|
|
|
|
|
|
def remove_checksum(path):
|
|
|
|
"""
|
|
|
|
Remove the checksum of an image from cache if exists
|
|
|
|
"""
|
|
|
|
|
|
|
|
path = '{}.md5sum'.format(path)
|
|
|
|
if os.path.exists(path):
|
|
|
|
os.remove(path)
|