|
| 1 | +/* |
| 2 | + * Copyright (C) Canonical, Ltd. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; version 3. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +/* |
| 19 | + * This file provides some support for creating and resizing ASIF disk images. |
| 20 | + * There is no official public documentation for the ASIF format. The technical |
| 21 | + * details used here are based on community reverse engineering. For reference, |
| 22 | + * see: https://github.com/huven/asif-format |
| 23 | + */ |
| 24 | + |
| 25 | +#include "applevz_utils.h" |
| 26 | + |
| 27 | +#include <applevz/applevz_wrapper.h> |
| 28 | +#include <multipass/file_ops.h> |
| 29 | +#include <multipass/format.h> |
| 30 | +#include <multipass/logging/log.h> |
| 31 | +#include <multipass/utils/qemu_img_utils.h> |
| 32 | +#include <shared/macos/process_factory.h> |
| 33 | + |
| 34 | +#include <Foundation/Foundation.h> |
| 35 | + |
| 36 | +#include <scope_guard.hpp> |
| 37 | + |
| 38 | +#include <algorithm> |
| 39 | +#include <array> |
| 40 | +#include <filesystem> |
| 41 | +#include <fstream> |
| 42 | +#include <stdexcept> |
| 43 | +#include <vector> |
| 44 | + |
| 45 | +namespace mp = multipass; |
| 46 | +namespace mpl = multipass::logging; |
| 47 | + |
| 48 | +namespace |
| 49 | +{ |
| 50 | +constexpr auto category = "applevz-utils"; |
| 51 | + |
| 52 | +std::string run_process(const QString& program, const QStringList& args, const std::string& desc) |
| 53 | +{ |
| 54 | + mpl::info(category, "Trying to {}", desc); |
| 55 | + |
| 56 | + auto process = MP_PROCFACTORY.create_process(program, args); |
| 57 | + |
| 58 | + if (const auto exit_state = process->execute(); !exit_state.completed_successfully()) |
| 59 | + throw std::runtime_error(fmt::format("Failed to {}: {}; Output: {}", |
| 60 | + desc, |
| 61 | + exit_state.failure_message(), |
| 62 | + process->read_all_standard_error())); |
| 63 | + |
| 64 | + return process->read_all_standard_output().trimmed().toStdString(); |
| 65 | +} |
| 66 | + |
| 67 | +bool is_asif_image(const std::filesystem::path& image_path) |
| 68 | +{ |
| 69 | + // ASIF format uses "shdw" magic bytes (0x73686477) |
| 70 | + const auto file = MP_FILEOPS.open_read(image_path, std::ios::binary); |
| 71 | + file->exceptions(std::ios::badbit); |
| 72 | + if (file->fail()) |
| 73 | + throw std::runtime_error(fmt::format("Failed to open file for reading: {}", image_path)); |
| 74 | + |
| 75 | + std::array<char, 4> magic{}; |
| 76 | + file->read(magic.data(), magic.size()); |
| 77 | + |
| 78 | + return file->gcount() == 4 && std::equal(magic.begin(), magic.end(), "shdw"); |
| 79 | +} |
| 80 | + |
| 81 | +void resize_asif_image(const std::filesystem::path& image_path, const mp::MemorySize& disk_space) |
| 82 | +{ |
| 83 | + run_process( |
| 84 | + QStringLiteral("diskutil"), |
| 85 | + QStringList() << "image" << "resize" << "--size" << QString::number(disk_space.in_bytes()) |
| 86 | + << MP_PLATFORM.path_to_qstr(image_path), |
| 87 | + fmt::format("resize ASIF image: {}, size: {}", image_path, disk_space.human_readable())); |
| 88 | +} |
| 89 | + |
| 90 | +void create_asif_from(const std::filesystem::path& source_path, |
| 91 | + const std::filesystem::path& dest_path) |
| 92 | +{ |
| 93 | + run_process(QStringLiteral("diskutil"), |
| 94 | + QStringList() << "image" << "create" << "from" |
| 95 | + << MP_PLATFORM.path_to_qstr(source_path) |
| 96 | + << MP_PLATFORM.path_to_qstr(dest_path) << "-f" |
| 97 | + << "asif", |
| 98 | + fmt::format("convert {} to ASIF format at {}", source_path, dest_path)); |
| 99 | +} |
| 100 | + |
| 101 | +void make_sparse(const std::filesystem::path& raw_image_path, const mp::MemorySize& disk_space) |
| 102 | +{ |
| 103 | + std::error_code ec; |
| 104 | + std::filesystem::resize_file(raw_image_path, disk_space.in_bytes(), ec); |
| 105 | + |
| 106 | + if (ec) |
| 107 | + throw std::runtime_error(fmt::format("Failed to resize file: {}", ec.message())); |
| 108 | +} |
| 109 | + |
| 110 | +std::filesystem::path convert_to_asif(const std::filesystem::path& source_path, bool destructive) |
| 111 | +{ |
| 112 | + if (is_asif_image(source_path)) |
| 113 | + return source_path; |
| 114 | + |
| 115 | + mpl::info(category, "Converting {} to ASIF format", source_path); |
| 116 | + |
| 117 | + // NO-OP if already RAW |
| 118 | + auto raw_path = mp::backend::convert(source_path, "raw"); |
| 119 | + |
| 120 | + // This is often an intermediate file so we remove it, unless it came from an existing VM |
| 121 | + auto intermediate_cleanup = |
| 122 | + sg::make_scope_guard([intermediate = (raw_path != source_path), raw_path]() noexcept { |
| 123 | + if (intermediate) |
| 124 | + QFile::remove(raw_path); |
| 125 | + }); |
| 126 | + |
| 127 | + auto asif_path = std::filesystem::path(source_path).replace_extension("asif"); |
| 128 | + auto asif_file_cleanup = |
| 129 | + sg::make_scope_guard([&asif_path]() noexcept { QFile::remove(asif_path); }); |
| 130 | + |
| 131 | + create_asif_from(raw_path, asif_path); |
| 132 | + asif_file_cleanup.dismiss(); |
| 133 | + |
| 134 | + return asif_path; |
| 135 | +} |
| 136 | +} // namespace |
| 137 | + |
| 138 | +namespace multipass::applevz |
| 139 | +{ |
| 140 | +std::filesystem::path AppleVZUtils::convert_to_supported_format( |
| 141 | + const std::filesystem::path& image_path, |
| 142 | + bool destructive) const |
| 143 | +{ |
| 144 | + return macos_at_least(26, 0) ? convert_to_asif(image_path, destructive) |
| 145 | + : backend::convert(image_path, "raw"); |
| 146 | +} |
| 147 | + |
| 148 | +void AppleVZUtils::resize_image(const MemorySize& disk_space, |
| 149 | + const std::filesystem::path& image_path) const |
| 150 | +{ |
| 151 | + mpl::trace(category, "Resizing image to: {}", disk_space.human_readable()); |
| 152 | + |
| 153 | + is_asif_image(image_path) ? resize_asif_image(image_path, disk_space) |
| 154 | + : make_sparse(image_path, disk_space); |
| 155 | + |
| 156 | + mpl::trace(category, "Successfully resized image: {}", image_path); |
| 157 | +} |
| 158 | + |
| 159 | +bool AppleVZUtils::macos_at_least(int major, int minor, int patch) const |
| 160 | +{ |
| 161 | + NSOperatingSystemVersion v{major, minor, patch}; |
| 162 | + return [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:v]; |
| 163 | +} |
| 164 | +} // namespace multipass::applevz |
0 commit comments