gns3-server/gns3server/utils/images.py

284 lines
10 KiB
Python
Raw Normal View History

#
# 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
import stat
import aiofiles
import shutil
from typing import AsyncGenerator
2016-06-07 20:38:01 +03:00
from ..config import Config
from . import force_unix_path
import gns3server.db.models as models
from gns3server.db.repositories.images import ImagesRepository
2016-06-07 20:38:01 +03:00
import logging
2021-04-13 12:16:50 +03:00
log = logging.getLogger(__name__)
2015-11-09 13:28:00 +02:00
def list_images(image_type):
"""
Scan directories for available image for a given type.
:param image_type: image type (dynamips, qemu, iou)
"""
files = set()
images = []
server_config = Config.instance().settings.Server
general_images_directory = os.path.expanduser(server_config.images_path)
# Subfolder of the general_images_directory specific to this VM type
default_directory = default_images_directory(image_type)
for directory in images_directories(image_type):
# 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)
for root, _, filenames in _os_walk(directory, recurse=recurse):
for filename in filenames:
path = os.path.join(root, filename)
if filename not in files:
if filename.endswith(".md5sum") or filename.startswith("."):
continue
2021-04-13 12:16:50 +03:00
elif (
((filename.endswith(".image") or filename.endswith(".bin")) and image_type == "dynamips")
or ((filename.endswith(".bin") or filename.startswith("i86bi")) and image_type == "iou")
or (not filename.endswith(".bin") and not filename.endswith(".image") and image_type == "qemu")
2021-04-13 12:16:50 +03: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:
if image_type in ["dynamips", "iou"]:
with open(os.path.join(root, filename), "rb") as f:
# read the first 7 bytes of the file.
elf_header_start = f.read(7)
# valid IOS images must start with the ELF magic number, be 32-bit, big endian and have an ELF version of 1
2021-04-13 12:16:50 +03:00
if (
not elf_header_start == b"\x7fELF\x01\x02\x01"
and not elf_header_start == b"\x7fELF\x01\x01\x01"
):
continue
2021-04-13 12:16:50 +03:00
images.append(
{
"filename": filename,
"path": force_unix_path(path),
"md5sum": md5sum(os.path.join(root, filename)),
"filesize": os.stat(os.path.join(root, filename)).st_size,
}
)
except OSError as e:
2021-04-13 12:07:58 +03:00
log.warning(f"Can't add image {path}: {str(e)}")
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
def default_images_directory(image_type):
"""
:returns: Return the default directory for an image type.
"""
server_config = Config.instance().settings.Server
img_dir = os.path.expanduser(server_config.images_path)
if image_type == "qemu":
return os.path.join(img_dir, "QEMU")
elif image_type == "iou":
return os.path.join(img_dir, "IOU")
elif image_type == "dynamips" or image_type == "ios":
return os.path.join(img_dir, "IOS")
else:
raise NotImplementedError(f"%s node type is not supported", image_type)
2016-06-07 20:38:01 +03:00
def images_directories(type):
"""
Return all directories where we will look for images
2016-06-07 20:38:01 +03:00
by priority
:param type: Type of emulator
"""
server_config = Config.instance().settings.Server
2016-06-07 20:38:01 +03:00
paths = []
img_dir = os.path.expanduser(server_config.images_path)
type_img_directory = default_images_directory(type)
try:
os.makedirs(type_img_directory, exist_ok=True)
paths.append(type_img_directory)
except (OSError, PermissionError):
pass
for directory in server_config.additional_images_paths:
2016-06-07 20:38:01 +03:00
paths.append(directory)
# Compatibility with old topologies we look in parent directory
paths.append(img_dir)
# 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)]
def md5sum(path, stopped_event=None):
"""
Return the md5sum of an image and cache it on disk
:param path: Path to the image
:param stopped_event: In case you execute this function on thread and would like to have possibility
to cancel operation pass the `threading.Event`
:returns: Digest of the image
"""
if path is None or len(path) == 0 or not os.path.exists(path):
return None
try:
2021-04-13 12:16:50 +03:00
with open(path + ".md5sum") as f:
md5 = f.read().strip()
if len(md5) == 32:
return md5
# Unicode error is when user rename an image to .md5sum ....
except (OSError, UnicodeDecodeError):
pass
try:
m = hashlib.md5()
2021-04-13 12:16:50 +03:00
with open(path, "rb") as f:
while True:
if stopped_event is not None and stopped_event.is_set():
2021-04-13 12:07:58 +03:00
log.error(f"MD5 sum calculation of `{path}` has stopped due to cancellation")
return
buf = f.read(128)
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
try:
2021-04-13 12:16:50 +03:00
with open(f"{path}.md5sum", "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
"""
2021-04-13 12:16:50 +03:00
path = f"{path}.md5sum"
if os.path.exists(path):
os.remove(path)
class InvalidImageError(Exception):
def __init__(self, message: str):
super().__init__()
self._message = message
def __str__(self):
return self._message
def check_valid_image_header(data: bytes, image_type: str, header_magic_len: int) -> None:
if image_type == "ios":
# file must start with the ELF magic number, be 32-bit, big endian and have an ELF version of 1
if data[:header_magic_len] != b'\x7fELF\x01\x02\x01':
raise InvalidImageError("Invalid IOS file detected")
elif image_type == "iou":
# file must start with the ELF magic number, be 32-bit or 64-bit, little endian and have an ELF version of 1
# (normal IOS images are big endian!)
if data[:header_magic_len] != b'\x7fELF\x01\x01\x01' and data[:7] != b'\x7fELF\x02\x01\x01':
raise InvalidImageError("Invalid IOU file detected")
# elif image_type == "qemu":
# if data[:expected_header_magic_len] != b'QFI\xfb':
# raise InvalidImageError("Invalid Qemu file detected (must be raw or qcow2)")
async def write_image(
image_name: str,
image_type: str,
path: str,
stream: AsyncGenerator[bytes, None],
images_repo: ImagesRepository,
check_image_header=True
) -> models.Image:
log.info(f"Writing image file to '{path}'")
# Store the file under its final name only when the upload is completed
tmp_path = path + ".tmp"
os.makedirs(os.path.dirname(path), exist_ok=True)
checksum = hashlib.md5()
header_magic_len = 7
if image_type == "qemu":
header_magic_len = 4
try:
async with aiofiles.open(tmp_path, "wb") as f:
async for chunk in stream:
if check_image_header and len(chunk) >= header_magic_len:
check_image_header = False
check_valid_image_header(chunk, image_type, header_magic_len)
await f.write(chunk)
checksum.update(chunk)
file_size = os.path.getsize(tmp_path)
if not file_size or file_size < header_magic_len:
raise InvalidImageError("The image content is empty or too small to be valid")
checksum = checksum.hexdigest()
duplicate_image = await images_repo.get_image_by_checksum(checksum)
if duplicate_image:
raise InvalidImageError(f"Image {duplicate_image.filename} with same checksum already exists")
except InvalidImageError:
os.remove(tmp_path)
raise
os.chmod(tmp_path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
shutil.move(tmp_path, path)
return await images_repo.add_image(image_name, image_type, path, checksum, checksum_algorithm="md5")