Skip to content

Commit 04cd107

Browse files
authored
Merge pull request #2448 from GNS3/bugfix/3664
Fix listing images
2 parents 83921a4 + 19cabdf commit 04cd107

File tree

5 files changed

+117
-76
lines changed

5 files changed

+117
-76
lines changed

gns3server/utils/images.py

+47-31
Original file line numberDiff line numberDiff line change
@@ -45,45 +45,61 @@ def list_images(emulator_type):
4545

4646
# We limit recursion to path outside the default images directory
4747
# the reason is in the default directory manage file organization and
48-
# it should be flatten to keep things simple
48+
# it should be flat to keep things simple
4949
recurse = True
5050
if os.path.commonprefix([directory, general_images_directory]) == general_images_directory:
5151
recurse = False
5252

5353
directory = os.path.normpath(directory)
5454
for root, _, filenames in _os_walk(directory, recurse=recurse):
5555
for filename in filenames:
56-
if filename not in files:
57-
if filename.endswith(".md5sum") or filename.startswith("."):
56+
if filename in files:
57+
log.debug("File {} has already been found, skipping...".format(filename))
58+
continue
59+
if filename.endswith(".md5sum") or filename.startswith("."):
60+
continue
61+
62+
files.add(filename)
63+
64+
filesize = os.stat(os.path.join(root, filename)).st_size
65+
if filesize < 7:
66+
log.debug("File {} is too small to be an image, skipping...".format(filename))
67+
continue
68+
69+
try:
70+
with open(os.path.join(root, filename), "rb") as f:
71+
# read the first 7 bytes of the file.
72+
elf_header_start = f.read(7)
73+
if emulator_type == "dynamips" and elf_header_start != b'\x7fELF\x01\x02\x01':
74+
# IOS images must start with the ELF magic number, be 32-bit, big endian and have an ELF version of 1
75+
log.warning("IOS image {} does not start with a valid ELF magic number, skipping...".format(filename))
5876
continue
59-
elif ((filename.endswith(".image") or filename.endswith(".bin")) and emulator_type == "dynamips") \
60-
or ((filename.endswith(".bin") or filename.startswith("i86bi")) and emulator_type == "iou") \
61-
or (not filename.endswith(".bin") and not filename.endswith(".image") and emulator_type == "qemu"):
62-
files.add(filename)
63-
64-
# It the image is located in the standard directory the path is relative
65-
if os.path.commonprefix([root, default_directory]) != default_directory:
66-
path = os.path.join(root, filename)
67-
else:
68-
path = os.path.relpath(os.path.join(root, filename), default_directory)
69-
70-
try:
71-
if emulator_type in ["dynamips", "iou"]:
72-
with open(os.path.join(root, filename), "rb") as f:
73-
# read the first 7 bytes of the file.
74-
elf_header_start = f.read(7)
75-
# valid IOU or IOS images must start with the ELF magic number, be 32-bit or 64-bit,
76-
# little endian and have an ELF version of 1
77-
if elf_header_start != b'\x7fELF\x02\x01\x01' and elf_header_start != b'\x7fELF\x01\x01\x01':
78-
continue
79-
80-
images.append({
81-
"filename": filename,
82-
"path": force_unix_path(path),
83-
"md5sum": md5sum(os.path.join(root, filename)),
84-
"filesize": os.stat(os.path.join(root, filename)).st_size})
85-
except OSError as e:
86-
log.warning("Can't add image {}: {}".format(path, str(e)))
77+
elif emulator_type == "iou" and elf_header_start != b'\x7fELF\x02\x01\x01' and elf_header_start != b'\x7fELF\x01\x01\x01':
78+
# IOU images must start with the ELF magic number, be 32-bit or 64-bit, little endian and have an ELF version of 1
79+
log.warning("IOU image {} does not start with a valid ELF magic number, skipping...".format(filename))
80+
continue
81+
elif emulator_type == "qemu" and elf_header_start[:4] == b'\x7fELF':
82+
# QEMU images should not start with an ELF magic number
83+
log.warning("QEMU image {} starts with an ELF magic number, skipping...".format(filename))
84+
continue
85+
86+
# It the image is located in the standard directory the path is relative
87+
if os.path.commonprefix([root, default_directory]) != default_directory:
88+
path = os.path.join(root, filename)
89+
else:
90+
path = os.path.relpath(os.path.join(root, filename), default_directory)
91+
92+
images.append(
93+
{
94+
"filename": filename,
95+
"path": force_unix_path(path),
96+
"md5sum": md5sum(os.path.join(root, filename)),
97+
"filesize": filesize
98+
}
99+
)
100+
101+
except OSError as e:
102+
log.warning("Can't add image {}: {}".format(path, str(e)))
87103
return images
88104

89105

tests/compute/test_manager.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@ async def test_list_images(qemu, tmpdir):
239239
os.makedirs(tmp_images_dir, exist_ok=True)
240240
for image in fake_images:
241241
with open(os.path.join(tmp_images_dir, image), "w+") as f:
242-
f.write("1")
242+
f.write("1234567")
243243

244244
with patch("gns3server.utils.images.default_images_directory", return_value=str(tmp_images_dir)):
245245
assert sorted(await qemu.list_images(), key=lambda k: k['filename']) == [
246-
{"filename": "a.qcow2", "path": "a.qcow2", "md5sum": "c4ca4238a0b923820dcc509a6f75849b", "filesize": 1},
247-
{"filename": "b.qcow2", "path": "b.qcow2", "md5sum": "c4ca4238a0b923820dcc509a6f75849b", "filesize": 1}
246+
{"filename": "a.qcow2", "path": "a.qcow2", "md5sum": "fcea920f7412b5da7be0cf42b8c93759", "filesize": 7},
247+
{"filename": "b.qcow2", "path": "b.qcow2", "md5sum": "fcea920f7412b5da7be0cf42b8c93759", "filesize": 7}
248248
]
249249

250250

@@ -255,19 +255,19 @@ async def test_list_images_recursives(qemu, tmpdir):
255255
fake_images = ["a.qcow2", "b.qcow2", ".blu.qcow2", "a.qcow2.md5sum"]
256256
for image in fake_images:
257257
with open(os.path.join(tmp_images_dir, image), "w+") as f:
258-
f.write("1")
258+
f.write("1234567")
259259
os.makedirs(os.path.join(tmp_images_dir, "c"))
260260
fake_images = ["c.qcow2", "c.qcow2.md5sum"]
261261
for image in fake_images:
262262
with open(os.path.join(tmp_images_dir, "c", image), "w+") as f:
263-
f.write("1")
263+
f.write("1234567")
264264

265265
with patch("gns3server.utils.images.default_images_directory", return_value=str(tmp_images_dir)):
266266

267267
assert sorted(await qemu.list_images(), key=lambda k: k['filename']) == [
268-
{"filename": "a.qcow2", "path": "a.qcow2", "md5sum": "c4ca4238a0b923820dcc509a6f75849b", "filesize": 1},
269-
{"filename": "b.qcow2", "path": "b.qcow2", "md5sum": "c4ca4238a0b923820dcc509a6f75849b", "filesize": 1},
270-
{"filename": "c.qcow2", "path": force_unix_path(os.path.sep.join(["c", "c.qcow2"])), "md5sum": "c4ca4238a0b923820dcc509a6f75849b", "filesize": 1}
268+
{"filename": "a.qcow2", "path": "a.qcow2", "md5sum": "fcea920f7412b5da7be0cf42b8c93759", "filesize": 7},
269+
{"filename": "b.qcow2", "path": "b.qcow2", "md5sum": "fcea920f7412b5da7be0cf42b8c93759", "filesize": 7},
270+
{"filename": "c.qcow2", "path": force_unix_path(os.path.sep.join(["c", "c.qcow2"])), "md5sum": "fcea920f7412b5da7be0cf42b8c93759", "filesize": 7}
271271
]
272272

273273

tests/handlers/api/compute/test_dynamips.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def fake_image(tmpdir):
144144

145145
path = str(tmpdir / "7200.bin")
146146
with open(path, "wb+") as f:
147-
f.write(b'\x7fELF\x01\x01\x01')
147+
f.write(b'\x7fELF\x01\x02\x01')
148148
os.chmod(path, stat.S_IREAD)
149149
return path
150150

@@ -168,7 +168,7 @@ async def test_images(compute_api, tmpdir, fake_image, fake_file):
168168
assert response.json == [{"filename": "7200.bin",
169169
"path": "7200.bin",
170170
"filesize": 7,
171-
"md5sum": "e573e8f5c93c6c00783f20c7a170aa6c"
171+
"md5sum": "b0d5aa897d937aced5a6b1046e8f7e2e"
172172
}]
173173

174174

tests/handlers/api/compute/test_qemu.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def fake_qemu_vm(images_dir):
4545
img_dir = os.path.join(images_dir, "QEMU")
4646
bin_path = os.path.join(img_dir, "linux载.img")
4747
with open(bin_path, "w+") as f:
48-
f.write("1")
48+
f.write("1234567")
4949
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
5050
return bin_path
5151

@@ -101,7 +101,7 @@ async def test_qemu_create_with_params(compute_api, compute_project, base_params
101101
assert response.json["project_id"] == compute_project.id
102102
assert response.json["ram"] == 1024
103103
assert response.json["hda_disk_image"] == "linux载.img"
104-
assert response.json["hda_disk_image_md5sum"] == "c4ca4238a0b923820dcc509a6f75849b"
104+
assert response.json["hda_disk_image_md5sum"] == "fcea920f7412b5da7be0cf42b8c93759"
105105

106106

107107
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
@@ -279,7 +279,7 @@ async def test_images(compute_api, fake_qemu_vm):
279279

280280
response = await compute_api.get("/qemu/images")
281281
assert response.status == 200
282-
assert {"filename": "linux载.img", "path": "linux载.img", "md5sum": "c4ca4238a0b923820dcc509a6f75849b", "filesize": 1} in response.json
282+
assert {"filename": "linux载.img", "path": "linux载.img", "md5sum": "fcea920f7412b5da7be0cf42b8c93759", "filesize": 7} in response.json
283283

284284

285285
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Does not work on Windows")

tests/utils/test_images.py

+57-32
Original file line numberDiff line numberDiff line change
@@ -114,30 +114,49 @@ def test_remove_checksum(tmpdir):
114114

115115
def test_list_images(tmpdir):
116116

117-
path1 = tmpdir / "images1" / "IOS" / "test1.image"
118-
path1.write(b'\x7fELF\x01\x01\x01', ensure=True)
119-
path1 = force_unix_path(str(path1))
120-
121-
path2 = tmpdir / "images2" / "test2.image"
122-
path2.write(b'\x7fELF\x01\x01\x01', ensure=True)
123-
path2 = force_unix_path(str(path2))
117+
# IOS image in the images directory
118+
ios_image_1 = tmpdir / "images1" / "IOS" / "ios_image_1.image"
119+
ios_image_1.write(b'\x7fELF\x01\x02\x01', ensure=True)
120+
ios_image_1 = force_unix_path(str(ios_image_1))
124121

125-
# Invalid image because not a valid elf file
126-
path = tmpdir / "images2" / "test_invalid.image"
127-
path.write(b'NOTANELF', ensure=True)
122+
# IOS image in an additional images path
123+
ios_image_2 = tmpdir / "images2" / "ios_image_2.image"
124+
ios_image_2.write(b'\x7fELF\x01\x02\x01', ensure=True)
125+
ios_image_2 = force_unix_path(str(ios_image_2))
128126

129-
if sys.platform.startswith("linux"):
130-
path3 = tmpdir / "images1" / "IOU" / "test3.bin"
131-
path3.write(b'\x7fELF\x02\x01\x01', ensure=True)
132-
path3 = force_unix_path(str(path3))
127+
# Not a valid elf file
128+
not_elf_file = tmpdir / "images1" / "IOS" / "not_elf.image"
129+
not_elf_file.write(b'NOTANELF', ensure=True)
130+
not_elf_file = force_unix_path(str(not_elf_file))
133131

134-
path4 = tmpdir / "images1" / "QEMU" / "test4.qcow2"
135-
path4.write("1", ensure=True)
136-
path4 = force_unix_path(str(path4))
132+
# Invalid image because it is very small
133+
small_file = tmpdir / "images1" / "too_small.image"
134+
small_file.write(b'1', ensure=True)
137135

138-
path5 = tmpdir / "images1" / "QEMU" / "test4.qcow2.md5sum"
139-
path5.write("1", ensure=True)
140-
path5 = force_unix_path(str(path5))
136+
if sys.platform.startswith("linux"):
137+
# 64-bit IOU image
138+
iou_image_1 = tmpdir / "images1" / "IOU" / "iou64.bin"
139+
iou_image_1.write(b'\x7fELF\x02\x01\x01', ensure=True)
140+
iou_image_1 = force_unix_path(str(iou_image_1))
141+
# 32-bit IOU image
142+
iou_image_2 = tmpdir / "images1" / "IOU" / "iou32.bin"
143+
iou_image_2.write(b'\x7fELF\x01\x01\x01', ensure=True) # 32-bit IOU image
144+
iou_image_2 = force_unix_path(str(iou_image_2))
145+
146+
147+
# Qemu image
148+
qemu_image_1 = tmpdir / "images1" / "QEMU" / "qemu_image.qcow2"
149+
qemu_image_1.write("1234567", ensure=True)
150+
qemu_image_1 = force_unix_path(str(qemu_image_1))
151+
152+
# ELF file inside the Qemu
153+
elf_file = tmpdir / "images1" / "QEMU" / "elf_file.bin"
154+
elf_file.write(b'\x7fELF\x02\x01\x01', ensure=True) # ELF file
155+
elf_file = force_unix_path(str(elf_file))
156+
157+
md5sum_file = tmpdir / "images1" / "QEMU" / "image.qcow2.md5sum"
158+
md5sum_file.write("1", ensure=True)
159+
md5sum_file = force_unix_path(str(md5sum_file))
141160

142161
with patch("gns3server.config.Config.get_section_config", return_value={
143162
"images_path": str(tmpdir / "images1"),
@@ -146,34 +165,40 @@ def test_list_images(tmpdir):
146165

147166
assert list_images("dynamips") == [
148167
{
149-
'filename': 'test1.image',
168+
'filename': 'ios_image_1.image',
150169
'filesize': 7,
151-
'md5sum': 'e573e8f5c93c6c00783f20c7a170aa6c',
152-
'path': 'test1.image'
170+
'md5sum': 'b0d5aa897d937aced5a6b1046e8f7e2e',
171+
'path': 'ios_image_1.image'
153172
},
154173
{
155-
'filename': 'test2.image',
174+
'filename': 'ios_image_2.image',
156175
'filesize': 7,
157-
'md5sum': 'e573e8f5c93c6c00783f20c7a170aa6c',
158-
'path': str(path2)
176+
'md5sum': 'b0d5aa897d937aced5a6b1046e8f7e2e',
177+
'path': str(ios_image_2)
159178
}
160179
]
161180

162181
if sys.platform.startswith("linux"):
163182
assert list_images("iou") == [
164183
{
165-
'filename': 'test3.bin',
184+
'filename': 'iou64.bin',
166185
'filesize': 7,
167186
'md5sum': 'c73626d23469519894d58bc98bee9655',
168-
'path': 'test3.bin'
187+
'path': 'iou64.bin'
188+
},
189+
{
190+
'filename': 'iou32.bin',
191+
'filesize': 7,
192+
'md5sum': 'e573e8f5c93c6c00783f20c7a170aa6c',
193+
'path': 'iou32.bin'
169194
}
170195
]
171196

172197
assert list_images("qemu") == [
173198
{
174-
'filename': 'test4.qcow2',
175-
'filesize': 1,
176-
'md5sum': 'c4ca4238a0b923820dcc509a6f75849b',
177-
'path': 'test4.qcow2'
199+
'filename': 'qemu_image.qcow2',
200+
'filesize': 7,
201+
'md5sum': 'fcea920f7412b5da7be0cf42b8c93759',
202+
'path': 'qemu_image.qcow2'
178203
}
179204
]

0 commit comments

Comments
 (0)