-
Notifications
You must be signed in to change notification settings - Fork 784
Expand file tree
/
Copy pathqemu_virtual_machine.cpp
More file actions
828 lines (716 loc) · 27 KB
/
qemu_virtual_machine.cpp
File metadata and controls
828 lines (716 loc) · 27 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/*
* Copyright (C) Canonical, Ltd.
*
* 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; version 3.
*
* 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/>.
*
*/
#include "qemu_virtual_machine.h"
#include "qemu_mount_handler.h"
#include "qemu_snapshot.h"
#include "qemu_vm_process_spec.h"
#include "qemu_vmstate_process_spec.h"
#include <multipass/exceptions/internal_timeout_exception.h>
#include <multipass/exceptions/virtual_machine_state_exceptions.h>
#include <multipass/file_ops.h>
#include <multipass/format.h>
#include <multipass/ip_address.h>
#include <multipass/json_utils.h>
#include <multipass/logging/log.h>
#include <multipass/memory_size.h>
#include <multipass/platform.h>
#include <multipass/top_catch_all.h>
#include <multipass/utils.h>
#include <multipass/utils/qemu_img_utils.h>
#include <multipass/vm_mount.h>
#include <multipass/vm_status_monitor.h>
#include <QFile>
#include <QProcess>
#include <QString>
#include <QTemporaryFile>
#include <cassert>
namespace mp = multipass;
namespace mpl = mp::logging;
namespace mpu = mp::utils;
using namespace std::chrono_literals;
namespace
{
constexpr auto suspend_tag = "suspend";
constexpr auto machine_type_key = "machine_type";
constexpr auto arguments_key = "arguments";
constexpr auto mount_data_key = "mount_data";
constexpr auto mount_source_key = "source";
constexpr auto mount_arguments_key = "arguments";
constexpr int shutdown_timeout = 300000; // unit: ms, 5 minute timeout for shutdown/suspend
constexpr int kill_process_timeout = 5000; // unit: ms, 5 seconds timeout for killing the process
QString get_vm_machine(const boost::json::value& metadata)
{
return mp::lookup_or<QString>(metadata, machine_type_key, {});
}
QStringList get_arguments(const boost::json::value& metadata)
{
return mp::lookup_or<QStringList>(metadata, arguments_key, {});
}
auto mount_args_from_json(const boost::json::object& object)
{
mp::QemuVirtualMachine::MountArgs mount_args;
if (!object.contains(mount_data_key))
return mount_args;
const auto& mount_data_map = object.at(mount_data_key).as_object();
for (const auto& [tag, mount_data] : mount_data_map)
{
const auto& source = mount_data.at(mount_source_key);
const auto& args = mount_data.at(mount_arguments_key);
try
{
mount_args[tag] = {value_to<std::string>(source), value_to<QStringList>(args)};
}
catch (const boost::system::system_error& e)
{
if (e.code() == boost::json::error::not_string)
continue;
throw;
}
}
return mount_args;
}
auto make_qemu_process(const mp::VirtualMachineDescription& desc,
const std::optional<boost::json::object>& resume_metadata,
const mp::QemuVirtualMachine::MountArgs& mount_args,
const QStringList& platform_args)
{
if (!MP_FILEOPS.exists(desc.image.image_path) || !QFile::exists(desc.cloud_init_iso))
{
throw std::runtime_error("cannot start VM without an image");
}
std::optional<mp::QemuVMProcessSpec::ResumeData> resume_data;
if (resume_metadata)
{
const auto& data = resume_metadata.value();
resume_data = mp::QemuVMProcessSpec::ResumeData{suspend_tag,
get_vm_machine(data),
get_arguments(data)};
}
auto process_spec =
std::make_unique<mp::QemuVMProcessSpec>(desc, platform_args, mount_args, resume_data);
auto process = mp::platform::make_process(std::move(process_spec));
mpl::debug(desc.vm_name, "process working dir '{}'", process->working_directory());
mpl::info(desc.vm_name, "process program '{}'", process->program());
mpl::info(desc.vm_name, "process arguments '{}'", process->arguments().join(", "));
return process;
}
boost::json::object qmp_execute_json(const QString& cmd)
{
return {{"execute", cmd.toStdString()}};
}
boost::json::object hmc_to_qmp_json(const QString& command_line)
{
auto qmp = qmp_execute_json("human-monitor-command");
qmp["arguments"] = {{"command-line", command_line.toStdString()}};
return qmp;
}
std::string get_qemu_machine_type(const QStringList& platform_args)
{
QTemporaryFile dump_file;
if (!dump_file.open())
{
return "";
}
auto process_spec =
std::make_unique<mp::QemuVmStateProcessSpec>(dump_file.fileName(), platform_args);
auto process = mp::platform::make_process(std::move(process_spec));
auto process_state = process->execute();
if (!process_state.completed_successfully())
{
throw std::runtime_error(fmt::format(
"Internal error: qemu-system-{} failed getting vmstate ({}) with output:\n{}",
HOST_ARCH,
process_state.failure_message(),
process->read_all_standard_error()));
}
if (auto data = dump_file.readAll(); !data.isEmpty())
{
auto vmstate = boost::json::parse(std::string_view(data));
return mp::lookup_or<std::string>(
mp::lookup_or<boost::json::value>(vmstate, "vmschkmachine", {}),
"Name",
"");
}
return "";
}
auto mount_args_to_json(const mp::QemuVirtualMachine::MountArgs& mount_args)
{
boost::json::object object;
for (const auto& [tag, mount_data] : mount_args)
{
const auto& [source, args] = mount_data;
object[tag] = {{mount_source_key, source},
{mount_arguments_key, boost::json::value_from(args)}};
}
return object;
}
auto generate_metadata(const QStringList& platform_args,
const QStringList& proc_args,
const mp::QemuVirtualMachine::MountArgs& mount_args)
{
return boost::json::object{{machine_type_key, get_qemu_machine_type(platform_args)},
{arguments_key, boost::json::value_from(proc_args)},
{mount_data_key, mount_args_to_json(mount_args)}};
}
QStringList extract_snapshot_tags(const QByteArray& snapshot_list_output_stream)
{
QStringList lines = QString{snapshot_list_output_stream}.split('\n');
QStringList snapshot_tags;
// Snapshot list:
// ID TAG VM SIZE DATE VM CLOCK ICOUNT
// 2 @s2 0 B 2024-06-11 23:22:59 00:00:00.000 0
// 3 @s3 0 B 2024-06-12 12:30:37 00:00:00.000 0
// The first two lines are headers
for (int i = 2; i < lines.size(); ++i)
{
// Qt::SkipEmptyParts improve the robustness of the code, it can keep the result correct in
// the case of leading and trailing spaces.
QStringList entries = lines[i].split(QRegularExpression{R"(\s+)"}, Qt::SkipEmptyParts);
if (entries.count() >= 2)
{
snapshot_tags.append(entries[1]);
}
}
return snapshot_tags;
}
} // namespace
mp::QemuVirtualMachine::QemuVirtualMachine(const VirtualMachineDescription& desc,
QemuPlatform* qemu_platform,
VMStatusMonitor& monitor,
const SSHKeyProvider& key_provider,
AvailabilityZone& zone,
const Path& instance_dir,
bool remove_snapshots)
: BaseVirtualMachine{mp::backend::instance_image_has_snapshot(desc.image.image_path,
suspend_tag)
? State::suspended
: State::off,
desc.vm_name,
key_provider,
zone,
instance_dir},
desc{desc},
qemu_platform{qemu_platform},
monitor{&monitor},
mount_args{mount_args_from_json(monitor.retrieve_metadata_for(vm_name))}
{
connect_vm_signals();
// only for clone case where the vm recreation purges the snapshot data
if (remove_snapshots)
{
remove_snapshots_from_backend();
}
expected_shutdown = mp::utils::expects_shutdown_from_cloud_init(desc.user_data_config);
}
mp::QemuVirtualMachine::~QemuVirtualMachine()
{
if (vm_process)
{
update_shutdown_status = false;
mp::top_catch_all(vm_name, [this]() {
if (state == State::running)
{
suspend();
}
else
{
shutdown();
}
});
}
}
void mp::QemuVirtualMachine::start()
{
initialize_vm_process();
if (state == State::suspended)
{
mpl::info(vm_name, "Resuming from a suspended state");
update_shutdown_status = true;
is_starting_from_suspend = true;
network_deadline = std::chrono::steady_clock::now() + 5s;
}
else
{
// remove the mount arguments from the rest of the arguments, as they are stored separately
// for easier retrieval
auto proc_args = vm_process->arguments();
for (const auto& [_, mount_data] : mount_args)
for (const auto& arg : mount_data.second)
proc_args.removeOne(arg);
monitor->update_metadata_for(
vm_name,
generate_metadata(qemu_platform->vmstate_platform_args(), proc_args, mount_args));
}
vm_process->start();
connect_vm_signals();
if (!vm_process->wait_for_started())
{
auto process_state = vm_process->process_state();
if (process_state.error)
{
mpl::error(vm_name, "Qemu failed to start: {}", process_state.error->message);
throw std::runtime_error(
fmt::format("failed to start qemu instance: {}", process_state.error->message));
}
else if (process_state.exit_code)
{
mpl::error(vm_name,
"Qemu quit unexpectedly with exit code {} and with output:\n{}",
process_state.exit_code.value(),
vm_process->read_all_standard_error());
throw std::runtime_error(
fmt::format("qemu quit unexpectedly with exit code {}, check logs for more details",
process_state.exit_code.value()));
}
}
vm_process->write(QByteArray::fromStdString(serialize(qmp_execute_json("qmp_capabilities"))));
}
void mp::QemuVirtualMachine::shutdown(ShutdownPolicy shutdown_policy)
{
std::unique_lock<std::mutex> lock{state_mutex};
disconnect_vm_signals();
try
{
check_state_for_shutdown(shutdown_policy);
}
catch (const VMStateIdempotentException& e)
{
mpl::log_message(mpl::Level::info, vm_name, e.what());
return;
}
if (shutdown_policy == ShutdownPolicy::Poweroff)
{
mpl::info(vm_name, "Forcing shutdown");
if (vm_process)
{
mpl::info(vm_name, "Killing process");
force_shutdown = true;
lock.unlock();
vm_process->kill();
if (vm_process != nullptr && !vm_process->wait_for_finished(kill_process_timeout))
{
throw std::runtime_error{fmt::format(
"The QEMU process did not finish within {} milliseconds after being killed",
kill_process_timeout)};
}
}
else
{
mpl::debug(vm_name, "No process to kill");
}
const auto has_suspend_snapshot =
mp::backend::instance_image_has_snapshot(desc.image.image_path, suspend_tag);
if (has_suspend_snapshot != (state == State::suspended)) // clang-format off
mpl::warn(vm_name, "Image has {} suspension snapshot, but the state is {}",
has_suspend_snapshot ? "a" : "no",
static_cast<short>(state)); // clang-format on
if (has_suspend_snapshot)
{
mpl::info(vm_name, "Deleting suspend image");
mp::backend::delete_snapshot_from_image(desc.image.image_path, suspend_tag);
}
state = State::off;
}
else
{
lock.unlock();
drop_ssh_session();
if (vm_process && vm_process->running())
{
vm_process->write(
QByteArray::fromStdString(serialize(qmp_execute_json("system_powerdown"))));
if (vm_process->wait_for_finished(shutdown_timeout))
{
lock.lock();
state = State::off;
}
else
{
throw std::runtime_error{fmt::format(
"The QEMU process did not finish within {} milliseconds after being shutdown",
shutdown_timeout)};
}
}
}
}
void mp::QemuVirtualMachine::suspend()
{
if ((state == State::running || state == State::delayed_shutdown) && vm_process->running())
{
if (update_shutdown_status)
{
state = State::suspending;
handle_state_update();
update_shutdown_status = false;
}
drop_ssh_session();
vm_process->write(QByteArray::fromStdString(
serialize(hmc_to_qmp_json(QString{"savevm "} + suspend_tag))));
vm_process->wait_for_finished(shutdown_timeout);
vm_process.reset(nullptr);
}
else if (state == State::off || state == State::suspended || state == State::unavailable)
{
// TODO: format state directly
mpl::info(vm_name, "Ignoring suspend issued while stopped/suspended/unavailable");
monitor->on_suspend();
}
}
mp::VirtualMachine::State mp::QemuVirtualMachine::current_state()
{
return state;
}
int mp::QemuVirtualMachine::ssh_port()
{
return 22;
}
void mp::QemuVirtualMachine::handle_state_update()
{
monitor->persist_state_for(vm_name, state);
}
void mp::QemuVirtualMachine::on_started()
{
state = State::starting;
handle_state_update();
monitor->on_resume();
}
void mp::QemuVirtualMachine::on_error()
{
state = State::off;
handle_state_update();
}
void mp::QemuVirtualMachine::on_shutdown()
{
{
std::unique_lock lock{state_mutex};
auto old_state = state;
state = State::off;
if (old_state == State::starting)
state_wait.wait(lock, [this] { return shutdown_while_starting; });
management_ip = std::nullopt;
drop_ssh_session();
handle_state_update();
vm_process.reset(nullptr);
}
monitor->on_shutdown();
}
void mp::QemuVirtualMachine::on_suspend()
{
drop_ssh_session();
state = State::suspended;
monitor->on_suspend();
}
void mp::QemuVirtualMachine::on_restart()
{
drop_ssh_session();
state = State::restarting;
handle_state_update();
management_ip = std::nullopt;
monitor->on_restart(vm_name);
}
std::string mp::QemuVirtualMachine::ssh_hostname(std::chrono::milliseconds timeout)
{
fetch_ip(timeout);
assert(management_ip && "Should have thrown otherwise");
return management_ip->as_string();
}
std::string mp::QemuVirtualMachine::ssh_username()
{
return desc.ssh_username;
}
std::optional<mp::IPAddress> mp::QemuVirtualMachine::management_ipv4()
{
if (!management_ip)
management_ip = qemu_platform->get_ip_for(desc.default_mac_address);
return management_ip;
}
void mp::QemuVirtualMachine::wait_until_ssh_up(std::chrono::milliseconds timeout)
{
BaseVirtualMachine::wait_until_ssh_up(timeout);
if (is_starting_from_suspend)
{
emit on_delete_memory_snapshot();
emit on_synchronize_clock();
}
}
void mp::QemuVirtualMachine::initialize_vm_process()
{
vm_process = make_qemu_process(
desc,
((state == State::suspended) ? std::make_optional(monitor->retrieve_metadata_for(vm_name))
: std::nullopt),
mount_args,
qemu_platform->vm_platform_args(desc));
QObject::connect(vm_process.get(), &Process::started, [this]() {
mpl::info(vm_name, "process started");
on_started();
});
QObject::connect(vm_process.get(), &Process::ready_read_standard_output, [this]() {
auto qmp_output = vm_process->read_all_standard_output();
mpl::debug(vm_name, "QMP: {}", qmp_output);
auto qmp_object =
boost::json::parse(qmp_output.split('\n').first().toStdString()).as_object();
if (auto event = qmp_object.if_contains("event"))
{
auto event_str = value_to<std::string>(*event);
if (event_str == "RESET" && state != State::restarting)
{
mpl::info(vm_name, "VM restarting");
on_restart();
}
else if (event_str == "POWERDOWN")
{
mpl::info(vm_name, "VM powering down");
}
else if (event_str == "SHUTDOWN")
{
mpl::info(vm_name, "VM shut down");
}
else if (event_str == "STOP")
{
mpl::info(vm_name, "VM suspending");
}
else if (event_str == "RESUME")
{
mpl::info(vm_name, "VM suspended");
if (state == State::suspending || state == State::running)
{
vm_process->kill();
on_suspend();
}
}
}
else if (auto error = qmp_object.if_contains("error"))
{
mpl::error(vm_name, "QMP error: {}", value_to<std::string>(error->at("desc")));
}
});
QObject::connect(vm_process.get(), &Process::ready_read_standard_error, [this]() {
const auto error_buf = vm_process->read_all_standard_error(); // keep alive while using data
const auto* error_data = error_buf.data();
save_error_msg(error_data);
mpl::log_message(mpl::Level::warning, vm_name, error_data);
});
QObject::connect(
vm_process.get(),
&Process::state_changed,
[this](QProcess::ProcessState newState) {
mpl::info(vm_name, "process state changed to {}", utils::qenum_to_string(newState));
});
QObject::connect(vm_process.get(),
&Process::error_occurred,
[this](QProcess::ProcessError error, QString error_string) {
// We just kill the process when suspending, so we don't want to print
// out any scary error messages for this state
if (update_shutdown_status)
{
const auto log_level =
force_shutdown ? mpl::Level::info : mpl::Level::error;
mpl::log(log_level,
vm_name,
"process error occurred {} {}",
utils::qenum_to_string(error),
error_string);
on_error();
}
});
QObject::connect(vm_process.get(), &Process::finished, [this](ProcessState process_state) {
if (process_state.exit_code)
{
mpl::info(vm_name,
"process finished with exit code {}",
process_state.exit_code.value());
}
if (process_state.error)
{
if (process_state.error->state == QProcess::Crashed &&
(state == State::suspending || state == State::suspended))
{
// when suspending, we ask Qemu to savevm. Once it confirms that's done, we kill it.
// Catch the "crash"
mpl::debug(vm_name, "Suspended VM successfully stopped");
}
else
{
const auto log_level = force_shutdown ? mpl::Level::info : mpl::Level::error;
mpl::log(log_level, vm_name, "error: {}", process_state.error->message);
// reset force_shutdown so that subsequent errors can be accurately reported
force_shutdown = false;
}
}
if (update_shutdown_status || state == State::starting)
{
on_shutdown();
}
});
}
void mp::QemuVirtualMachine::connect_vm_signals()
{
std::unique_lock lock{vm_signal_mutex};
if (vm_signals_connected)
return;
QObject::connect(
this,
&QemuVirtualMachine::on_delete_memory_snapshot,
this,
[this] {
mpl::debug(vm_name, "Deleted memory snapshot");
vm_process->write(QByteArray::fromStdString(
serialize(hmc_to_qmp_json(QString("delvm ") + suspend_tag))));
is_starting_from_suspend = false;
},
Qt::QueuedConnection);
// The following is the actual code to reset the network via QMP if an IP address is not
// obtained after starting from suspend. This will probably be deprecated in the future.
QObject::connect(
this,
&QemuVirtualMachine::on_reset_network,
this,
[this] {
mpl::debug(vm_name, "Resetting the network");
auto qmp = qmp_execute_json("set_link");
qmp["arguments"] = {{"name", "virtio-net-pci.0"}, {"up", false}};
vm_process->write(QByteArray::fromStdString(serialize(qmp)));
qmp.at("arguments").at("up") = true;
vm_process->write(QByteArray::fromStdString(serialize(qmp)));
},
Qt::QueuedConnection);
QObject::connect(
this,
&QemuVirtualMachine::on_synchronize_clock,
this,
[this]() {
try
{
mpl::debug(vm_name, "Syncing RTC clock");
ssh_exec("sudo timedatectl set-local-rtc 0 --adjust-system-clock");
}
catch (const std::exception& e)
{
mpl::warn(vm_name, "Failed to sync clock: {}", e.what());
}
},
Qt::QueuedConnection);
vm_signals_connected = true;
}
void mp::QemuVirtualMachine::disconnect_vm_signals()
{
std::unique_lock lock{vm_signal_mutex};
disconnect(this, &QemuVirtualMachine::on_delete_memory_snapshot, nullptr, nullptr);
disconnect(this, &QemuVirtualMachine::on_reset_network, nullptr, nullptr);
disconnect(this, &QemuVirtualMachine::on_synchronize_clock, nullptr, nullptr);
vm_signals_connected = false;
}
void mp::QemuVirtualMachine::update_cpus(int num_cores)
{
assert(num_cores > 0);
desc.num_cores = num_cores;
}
void mp::QemuVirtualMachine::resize_memory(const MemorySize& new_size)
{
desc.mem_size = new_size;
}
void mp::QemuVirtualMachine::resize_disk(const MemorySize& new_size)
{
assert(new_size > desc.disk_space);
mp::backend::resize_instance_image(new_size, desc.image.image_path);
desc.disk_space = new_size;
}
void mp::QemuVirtualMachine::add_network_interface(int /* not used on this backend */,
const std::string& default_mac_addr,
const NetworkInterface& extra_interface)
{
desc.extra_interfaces.push_back(extra_interface);
add_extra_interface_to_instance_cloud_init(default_mac_addr, extra_interface);
}
mp::MountHandler::UPtr mp::QemuVirtualMachine::make_native_mount_handler(const std::string& target,
const VMMount& mount)
{
return std::make_unique<QemuMountHandler>(this, &key_provider, target, mount);
}
void mp::QemuVirtualMachine::remove_snapshots_from_backend() const
{
const QStringList snapshot_tag_list =
extract_snapshot_tags(backend::snapshot_list_output(desc.image.image_path));
for (const auto& snapshot_tag : snapshot_tag_list)
{
backend::delete_snapshot_from_image(desc.image.image_path, snapshot_tag);
}
}
mp::QemuVirtualMachine::MountArgs& mp::QemuVirtualMachine::modifiable_mount_args()
{
return mount_args;
}
auto mp::QemuVirtualMachine::make_specific_snapshot(const std::string& snapshot_name,
const std::string& comment,
const std::string& instance_id,
const VMSpecs& specs,
std::shared_ptr<Snapshot> parent)
-> std::shared_ptr<Snapshot>
{
assert(state == VirtualMachine::State::off ||
state == VirtualMachine::State::stopped); // would need QMP otherwise
return std::make_shared<QemuSnapshot>(snapshot_name,
comment,
instance_id,
std::move(parent),
specs,
*this,
desc);
}
bool multipass::QemuVirtualMachine::unplugged()
{
return BaseVirtualMachine::unplugged() || !vm_process || !vm_process->running();
}
auto mp::QemuVirtualMachine::make_specific_snapshot(const QString& filename)
-> std::shared_ptr<Snapshot>
{
return std::make_shared<QemuSnapshot>(filename, *this, desc);
}
void mp::QemuVirtualMachine::fetch_ip(std::chrono::milliseconds timeout)
{
if (management_ip)
return;
auto action = [this] {
detect_aborted_start();
return ((management_ip = qemu_platform->get_ip_for(desc.default_mac_address)))
? mpu::TimeoutAction::done
: mpu::TimeoutAction::retry;
};
auto on_timeout = [this, &timeout] {
state = State::unknown;
throw InternalTimeoutException{"determine IP address", timeout};
};
mpu::try_action_for(on_timeout, timeout, action);
}
void mp::QemuVirtualMachine::refresh_start()
{
if (is_starting_from_suspend)
{
// Due to https://github.com/canonical/multipass/issues/2374, the DHCP address is removed
// from the dnsmasq leases file, so if the daemon restarts while an instance is suspended
// and then starts the instance, the daemon won't be able to reach the instance since the
// instance won't refresh it's IP address. The following will force the instance to refresh
// by resetting the network at 5 seconds and then every 30 seconds until the start timeout
// is reached.
if (std::chrono::steady_clock::now() > network_deadline)
{
network_deadline = std::chrono::steady_clock::now() + 30s;
emit on_reset_network();
}
}
}