1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 /**
19  * Functions for manipulating disk files given to crosvm or QEMU.
20  */
21 
22 #include <string>
23 #include <vector>
24 
25 namespace cuttlefish {
26 
27 enum ImagePartitionType {
28   kLinuxFilesystem = 0,
29   kEfiSystemPartition,
30 };
31 
32 struct ImagePartition {
33   std::string label;
34   std::string image_file_path;
35   ImagePartitionType type;
36   bool read_only;
37 };
38 
39 struct MultipleImagePartition {
40   std::string label;
41   std::vector<std::string> image_file_paths;
42   ImagePartitionType type;
43   bool read_only;
44 };
45 
46 uint64_t AlignToPartitionSize(uint64_t size);
47 
48 /**
49  * Combine the files in `partition` into a single raw disk file and write it to
50  * `output_path`. The raw disk file will have a GUID Partition Table and copy in
51  * the contents of the files mentioned in `partitions`.
52  */
53 void AggregateImage(const std::vector<ImagePartition>& partitions,
54                     const std::string& output_path);
55 
56 /**
57  * Generate the files necessary for booting with a Composite Disk.
58  *
59  * Composite Disk is a crosvm disk format that is a layer of indirection over
60  * other disk files. The Composite Disk file lists names and offsets in the
61  * virtual disk.
62  *
63  * For a complete single disk inside the VM, there must also be a GUID Partition
64  * Table header and footer. These are saved to `header_file` and `footer_file`,
65  * then the specification file containing the file paths and offsets is saved to
66  * `output_composite_path`.
67  */
68 void CreateCompositeDisk(std::vector<ImagePartition> partitions,
69                          const std::string& header_file,
70                          const std::string& footer_file,
71                          const std::string& output_composite_path);
72 
73 /**
74  * Overloaded function to generate a composite disk with multiple images for a
75  * single partition.
76  */
77 void CreateCompositeDisk(std::vector<MultipleImagePartition> partitions,
78                          const std::string& header_file,
79                          const std::string& footer_file,
80                          const std::string& output_composite_path);
81 /**
82  * Generate a qcow overlay backed by a given implementation file.
83  *
84  * qcow, or "QEMU Copy-On-Write" is a file format containing a list of disk
85  * offsets and file contents. This can be combined with a backing file, to
86  * represent an original disk file plus disk updates over that file. The qcow
87  * files can be swapped out and replaced without affecting the original. qcow
88  * is supported by QEMU and crosvm.
89  *
90  * The crosvm binary at `crosvm_path` is used to generate an overlay file at
91  * `output_overlay_path` that functions as an overlay on the file at
92  * `backing_file`.
93  */
94 void CreateQcowOverlay(const std::string& crosvm_path,
95                        const std::string& backing_file,
96                        const std::string& output_overlay_path);
97 
98 }
99