This repository was archived by the owner on Apr 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdiskImage.cpp
More file actions
160 lines (137 loc) · 4.89 KB
/
diskImage.cpp
File metadata and controls
160 lines (137 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright 2022 Vinícius Ferrão <vinicius@ferrao.net.br>
* SPDX-License-Identifier: Apache-2.0
*/
#include <cloysterhpc/cloyster.h>
#include <cloysterhpc/diskImage.h>
#include <cloysterhpc/functions.h>
#include <cloysterhpc/models/os.h>
#include <cloysterhpc/services/files.h>
#include <cloysterhpc/services/log.h>
#include <cloysterhpc/services/options.h>
#include <unordered_map>
// @FIXME: This file need some work
//
// - The ISO can be probed for more information usign isoinfo command
// - The isKnownImage is initializing data, this is a little weird
const std::filesystem::path& DiskImage::getPath() const { return m_path; }
void DiskImage::setPath(const std::filesystem::path& path)
{
if (path.extension() != ".iso")
throw std::runtime_error("Disk Image must have ISO extension");
// Verify checksum only if the image is known.
if (isKnownImage(path)) {
#ifdef NDEBUG
if (!hasVerifiedChecksum(path))
throw std::runtime_error("Disk Image checksum isn't valid");
#endif
}
m_path = path;
}
bool DiskImage::isKnownImage(const std::filesystem::path& path)
{
constexpr auto chooseDistro = [](std::string_view imageView)
-> std::optional<cloyster::models::OS::Distro> {
if (imageView.starts_with("Rocky")) {
return cloyster::models::OS::Distro::Rocky;
} else if (imageView.starts_with("rhel")) {
return cloyster::models::OS::Distro::RHEL;
} else if (imageView.starts_with("OracleLinux")) {
return cloyster::models::OS::Distro::OL;
} else if (imageView.starts_with("AlmaLinux")) {
return cloyster::models::OS::Distro::AlmaLinux;
} else {
return std::nullopt;
}
};
for (const auto& image : m_knownImageFilename) {
if (path.filename().string() == image) {
LOG_TRACE("Disk image is recognized")
auto imageView = std::string_view(image);
const auto distro = chooseDistro(imageView);
if (distro) {
m_distro = distro;
return true;
}
}
}
const auto distro
= chooseDistro(std::string_view(path.filename().string()));
if (distro) {
m_distro = distro;
return true;
}
cloyster::functions::abort(
"Disk image is unknown. Maybe you're using a custom image or "
"changed the default name?");
return false;
}
cloyster::models::OS::Distro DiskImage::getDistro() const
{
LOG_ASSERT(m_distro.has_value(), "Trying to getDistro() uninitialized");
return m_distro.value();
}
// BUG: Consider removing/reimplement this method
bool DiskImage::hasVerifiedChecksum(const std::filesystem::path& path)
{
const auto opts = cloyster::Singleton<cloyster::services::Options>::get();
if (opts->dryRun) {
LOG_INFO("Dry Run: Would verify disk image checksum.")
return true;
}
LOG_INFO("Verifying disk image checksum... This may take a while, use "
"`--skip disk-checksum` to skip")
if (opts->shouldSkip("disk-checksum")) {
LOG_WARN(
"Skiping disk the image checksum because `--skip disk-checksum`");
return true;
}
// BUG: This should no be hardcoded here. An ancillary file should be used
std::unordered_map<std::string, std::string> hash_map = {
{ "rhel-8.8-x86_64-dvd.iso",
"517abcc67ee3b7212f57e180f5d30be3e8269e7a99e127a3399b7935c7e00a0"
"9" },
{ "OracleLinux-R8-U8-x86_64-dvd.iso",
"cae39116245ff7c3c86d5305d9c11430ce5c4e512987563435ac59c37a082d7"
"e" },
{ "Rocky-8.8-x86_64-dvd1.iso",
"7b8bdfe189cf24ae5c2d6a88f7a0b5f3012d23f9332c47943d538b4bc03a370"
"4" },
{ "AlmaLinux-8.8-x86_64-dvd.iso",
"635b30b967b509a32a1a3d81401db9861922acb396d065922b39405a43a04a3"
"1" },
{ "Rocky-9.5-x86_64-dvd.iso",
"ba60c3653640b5747610ddfb4d09520529bef2d1d83c1feb86b0c84dff31e04"
"e" }
};
auto checksum = cloyster::services::files::checksum(path);
LOG_INFO("SHA256 checksum of file {} is: {}", path.string(), checksum);
if (checksum == hash_map.find(path.filename().string())->second) {
LOG_TRACE("Checksum - The disk image is valid")
return true;
}
LOG_TRACE("Checksum - The disk image is invalid. Maybe you're using a "
"custom image?");
return false;
}
#ifdef BUILD_TESTING
#include <doctest/doctest.h>
#else
#define DOCTEST_CONFIG_DISABLE
#include <doctest/doctest.h>
#endif
TEST_SUITE("Disk image test suite")
{
/*
DiskImage diskImage;
const auto path = std::filesystem::current_path() / "/sample/checksum.iso";
TEST_CASE("Verify if is unknown image")
{
REQUIRE_FALSE(diskImage.isKnownImage(path));
}
TEST_CASE("Verify invalid checksum")
{
REQUIRE_FALSE(diskImage.hasVerifiedChecksum(path));
}
*/
}