Skip to content

Commit

Permalink
[vm factory] using the new read_from function to read the cloud-init-…
Browse files Browse the repository at this point in the history
…config.iso instead of mounting.
  • Loading branch information
georgeliao committed Feb 6, 2024
1 parent 7e4a838 commit dac94dd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
1 change: 1 addition & 0 deletions include/multipass/cloud_init_iso.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CloudInitIso
{
public:
void add_file(const std::string& name, const std::string& data);
void replace_file(const std::string& name, const std::string& data);
void write_to(const Path& path);
void read_from(const std::filesystem::path& path);

Expand Down
15 changes: 15 additions & 0 deletions src/iso/cloud_init_iso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,21 @@ void mp::CloudInitIso::add_file(const std::string& name, const std::string& data
files.push_back(FileEntry{name, data});
}

void mp::CloudInitIso::replace_file(const std::string& name, const std::string& data)

Check warning on line 474 in src/iso/cloud_init_iso.cpp

View check run for this annotation

Codecov / codecov/patch

src/iso/cloud_init_iso.cpp#L474

Added line #L474 was not covered by tests
{
if (auto iter = std::find_if(files.begin(),

Check warning on line 476 in src/iso/cloud_init_iso.cpp

View check run for this annotation

Codecov / codecov/patch

src/iso/cloud_init_iso.cpp#L476

Added line #L476 was not covered by tests
files.end(),
[name](const FileEntry& file_entry) -> bool { return file_entry.name == name; });
iter == std::end(files))

Check warning on line 479 in src/iso/cloud_init_iso.cpp

View check run for this annotation

Codecov / codecov/patch

src/iso/cloud_init_iso.cpp#L478-L479

Added lines #L478 - L479 were not covered by tests
{
throw std::runtime_error("Did not find the target file in the file list");

Check warning on line 481 in src/iso/cloud_init_iso.cpp

View check run for this annotation

Codecov / codecov/patch

src/iso/cloud_init_iso.cpp#L481

Added line #L481 was not covered by tests
}
else
{
*iter = FileEntry{name, data};

Check warning on line 485 in src/iso/cloud_init_iso.cpp

View check run for this annotation

Codecov / codecov/patch

src/iso/cloud_init_iso.cpp#L485

Added line #L485 was not covered by tests
}
}

void mp::CloudInitIso::write_to(const Path& path)
{
QFile f{path};
Expand Down
53 changes: 6 additions & 47 deletions src/platform/backends/qemu/qemu_virtual_machine_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,61 +80,20 @@ mp::VirtualMachine::UPtr mp::QemuVirtualMachineFactory::create_vm_and_instance_d
dest_instance_data_directory,
std::filesystem::copy_options::recursive);

const fs::path cloud_init_config_iso_file_path = dest_instance_data_directory / "cloud-init-config.iso";
CloudInitIso qemu_iso;
qemu_iso.read_from(cloud_init_config_iso_file_path);

Check warning on line 85 in src/platform/backends/qemu/qemu_virtual_machine_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/backends/qemu/qemu_virtual_machine_factory.cpp#L83-L85

Added lines #L83 - L85 were not covered by tests
const YAML::Node network_data =
mpu::make_cloud_init_network_config(dest_vm_spec.default_mac_address, dest_vm_spec.extra_interfaces);
CloudInitIso qemu_iso;
if (!network_data.IsNull())

Check warning on line 88 in src/platform/backends/qemu/qemu_virtual_machine_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/backends/qemu/qemu_virtual_machine_factory.cpp#L87-L88

Added lines #L87 - L88 were not covered by tests
{
qemu_iso.add_file("network-config", mpu::emit_cloud_config(network_data));
}
const YAML::Node meta_data = mpu::make_cloud_init_meta_config(destination_name);
qemu_iso.add_file("meta-data", mpu::emit_cloud_config(meta_data));

// create the mount folder
const fs::path cloud_init_mount_point = dest_instance_data_directory / "cidata";
if (std::error_code err; !MP_FILEOPS.create_directory(cloud_init_mount_point, err))
{
throw std::runtime_error{
fmt::format("Could not create mount point for cloud-init-config.iso file of the instance: {} ",
destination_name)};
}

// sudo mount -o loop cloud-init-config.iso
// /root/.local/share/multipassd/vault/instances/adaptive-cat-clone/cidata
const fs::path cloud_init_config_iso_file_path = dest_instance_data_directory / "cloud-init-config.iso";
const std::string mount_command =
fmt::format("mount -o loop {} {}", cloud_init_config_iso_file_path.string(), cloud_init_mount_point.string());
if (int return_code = std::system(mount_command.c_str()); return_code != 0)
{
throw std::runtime_error{fmt::format("Error executing command : {} ", mount_command)};
qemu_iso.replace_file("network-config", mpu::emit_cloud_config(network_data));

Check warning on line 90 in src/platform/backends/qemu/qemu_virtual_machine_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/backends/qemu/qemu_virtual_machine_factory.cpp#L90

Added line #L90 was not covered by tests
}

// load files and add to qemu_iso and write to the .iso file
for (const auto filename_str : {"user-data", "vendor-data"})
{
const auto stream = MP_FILEOPS.open_read(cloud_init_mount_point / fs::path(filename_str));
std::stringstream buffer;
buffer << stream->rdbuf();
const std::string file_contents = buffer.str();
qemu_iso.add_file(filename_str, file_contents);
}
const YAML::Node meta_data = mpu::make_cloud_init_meta_config(destination_name);
qemu_iso.replace_file("meta-data", mpu::emit_cloud_config(meta_data));
qemu_iso.write_to(QString::fromStdString(cloud_init_config_iso_file_path.string()));

Check warning on line 95 in src/platform/backends/qemu/qemu_virtual_machine_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/backends/qemu/qemu_virtual_machine_factory.cpp#L93-L95

Added lines #L93 - L95 were not covered by tests

// sudo umount /root/.local/share/multipassd/vault/instances/adaptive-cat-clone/cidata
const std::string unmount_command = fmt::format("umount {}", cloud_init_mount_point.string());
if (int return_code = std::system(unmount_command.c_str()); return_code != 0)
{
throw std::runtime_error{fmt::format("Error executing command : {} ", unmount_command)};
}

// delete the created mount folder
if (std::error_code err; !MP_FILEOPS.remove(cloud_init_mount_point, err))
{
throw std::runtime_error{
fmt::format("Could not remove mount point for cloud-init-config.iso file of the instance: {} ",
destination_name)};
}

// start to construct VirtualMachineDescription
mp::VirtualMachineDescription dest_vm_desc{dest_vm_spec.num_cores,

Check warning on line 98 in src/platform/backends/qemu/qemu_virtual_machine_factory.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/backends/qemu/qemu_virtual_machine_factory.cpp#L98

Added line #L98 was not covered by tests
dest_vm_spec.mem_size,
Expand Down

0 comments on commit dac94dd

Please sign in to comment.