|
16 | 16 | */
|
17 | 17 |
|
18 | 18 | #include "base_snapshot.h"
|
19 |
| -#include "daemon/vm_specs.h" // TODO@snapshots move this |
| 19 | +#include "multipass/virtual_machine.h" |
20 | 20 |
|
21 | 21 | #include <multipass/file_ops.h>
|
22 |
| -#include <multipass/id_mappings.h> // TODO@snapshots may be able to drop after extracting JSON utilities |
23 | 22 | #include <multipass/json_utils.h>
|
24 | 23 | #include <multipass/vm_mount.h>
|
| 24 | +#include <multipass/vm_specs.h> |
25 | 25 |
|
26 | 26 | #include <scope_guard.hpp>
|
27 | 27 |
|
28 |
| -#include <QJsonArray> // TODO@snapshots may be able to drop after extracting JSON utilities |
| 28 | +#include <QJsonArray> |
29 | 29 | #include <QString>
|
30 | 30 |
|
31 | 31 | #include <QFile>
|
@@ -68,35 +68,13 @@ QJsonObject read_snapshot_json(const QString& filename)
|
68 | 68 | return json["snapshot"].toObject();
|
69 | 69 | }
|
70 | 70 |
|
71 |
| -std::unordered_map<std::string, mp::VMMount> load_mounts(const QJsonArray& json) |
| 71 | +std::unordered_map<std::string, mp::VMMount> load_mounts(const QJsonArray& mounts_json) |
72 | 72 | {
|
73 | 73 | std::unordered_map<std::string, mp::VMMount> mounts;
|
74 |
| - for (const auto& entry : json) |
| 74 | + for (const auto& entry : mounts_json) |
75 | 75 | {
|
76 |
| - mp::id_mappings uid_mappings; |
77 |
| - mp::id_mappings gid_mappings; |
78 |
| - |
79 |
| - auto target_path = entry.toObject()["target_path"].toString().toStdString(); |
80 |
| - auto source_path = entry.toObject()["source_path"].toString().toStdString(); |
81 |
| - |
82 |
| - for (const QJsonValueRef uid_entry : entry.toObject()["uid_mappings"].toArray()) |
83 |
| - { |
84 |
| - uid_mappings.push_back( |
85 |
| - {uid_entry.toObject()["host_uid"].toInt(), uid_entry.toObject()["instance_uid"].toInt()}); |
86 |
| - } |
87 |
| - |
88 |
| - for (const QJsonValueRef gid_entry : entry.toObject()["gid_mappings"].toArray()) |
89 |
| - { |
90 |
| - gid_mappings.push_back( |
91 |
| - {gid_entry.toObject()["host_gid"].toInt(), gid_entry.toObject()["instance_gid"].toInt()}); |
92 |
| - } |
93 |
| - |
94 |
| - uid_mappings = mp::unique_id_mappings(uid_mappings); |
95 |
| - gid_mappings = mp::unique_id_mappings(gid_mappings); |
96 |
| - auto mount_type = mp::VMMount::MountType(entry.toObject()["mount_type"].toInt()); |
97 |
| - |
98 |
| - mp::VMMount mount{source_path, gid_mappings, uid_mappings, mount_type}; |
99 |
| - mounts[target_path] = std::move(mount); |
| 76 | + const auto& json = entry.toObject(); |
| 77 | + mounts[json["target_path"].toString().toStdString()] = mp::VMMount{json}; |
100 | 78 | }
|
101 | 79 |
|
102 | 80 | return mounts;
|
@@ -146,9 +124,13 @@ mp::BaseSnapshot::BaseSnapshot(const std::string& name, // NOLINT(modernize-p
|
146 | 124 | captured{captured}
|
147 | 125 | {
|
148 | 126 | assert(index > 0 && "snapshot indices need to start at 1");
|
| 127 | + using St = VirtualMachine::State; |
| 128 | + if (state != St::off && state != St::stopped) |
| 129 | + throw std::runtime_error{fmt::format("Unsupported VM state in snapshot: {}", static_cast<int>(state))}; |
| 130 | + if (index < 1) |
| 131 | + throw std::runtime_error{fmt::format("Snapshot index not positive: {}", index)}; |
149 | 132 | if (index > max_snapshots)
|
150 |
| - throw std::runtime_error{fmt::format("Maximum number of snapshots exceeded: {}", max_snapshots)}; |
151 |
| - |
| 133 | + throw std::runtime_error{fmt::format("Maximum number of snapshots exceeded: {}", index)}; |
152 | 134 | if (name.empty())
|
153 | 135 | throw std::runtime_error{"Snapshot names cannot be empty"};
|
154 | 136 | if (num_cores < 1)
|
@@ -224,37 +206,8 @@ QJsonObject mp::BaseSnapshot::serialize() const
|
224 | 206 | QJsonArray json_mounts;
|
225 | 207 | for (const auto& mount : mounts)
|
226 | 208 | {
|
227 |
| - QJsonObject entry; |
228 |
| - entry.insert("source_path", QString::fromStdString(mount.second.source_path)); |
| 209 | + auto entry = mount.second.serialize(); |
229 | 210 | entry.insert("target_path", QString::fromStdString(mount.first));
|
230 |
| - |
231 |
| - QJsonArray uid_mappings; |
232 |
| - |
233 |
| - for (const auto& map : mount.second.uid_mappings) |
234 |
| - { |
235 |
| - QJsonObject map_entry; |
236 |
| - map_entry.insert("host_uid", map.first); |
237 |
| - map_entry.insert("instance_uid", map.second); |
238 |
| - |
239 |
| - uid_mappings.append(map_entry); |
240 |
| - } |
241 |
| - |
242 |
| - entry.insert("uid_mappings", uid_mappings); |
243 |
| - |
244 |
| - QJsonArray gid_mappings; |
245 |
| - |
246 |
| - for (const auto& map : mount.second.gid_mappings) |
247 |
| - { |
248 |
| - QJsonObject map_entry; |
249 |
| - map_entry.insert("host_gid", map.first); |
250 |
| - map_entry.insert("instance_gid", map.second); |
251 |
| - |
252 |
| - gid_mappings.append(map_entry); |
253 |
| - } |
254 |
| - |
255 |
| - entry.insert("gid_mappings", gid_mappings); |
256 |
| - |
257 |
| - entry.insert("mount_type", static_cast<int>(mount.second.mount_type)); |
258 | 211 | json_mounts.append(entry);
|
259 | 212 | }
|
260 | 213 |
|
|
0 commit comments