Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 15 | //! Functions for creating a composite disk image. |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 16 | |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 17 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::Partition::Partition; |
Andrew Walbran | b31c3f1 | 2021-08-03 13:17:00 +0000 | [diff] [blame] | 18 | use anyhow::{anyhow, Context, Error}; |
Jiyong Park | 2cb63b1 | 2021-11-05 14:29:23 +0900 | [diff] [blame] | 19 | use disk::{ |
| 20 | create_composite_disk, create_disk_file, ImagePartitionType, PartitionInfo, MAX_NESTING_DEPTH, |
| 21 | }; |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 22 | use std::fs::{File, OpenOptions}; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 23 | use std::os::unix::io::AsRawFd; |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 24 | use std::path::{Path, PathBuf}; |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 25 | |
| 26 | /// Constructs a composite disk image for the given list of partitions, and opens it ready to use. |
| 27 | /// |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 28 | /// Returns the composite disk image file, and a list of files whose file descriptors must be passed |
| 29 | /// to any process which wants to use it. This is necessary because the composite image contains |
| 30 | /// paths of the form `/proc/self/fd/N` for the partition images. |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 31 | pub fn make_composite_image( |
| 32 | partitions: &[Partition], |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 33 | zero_filler_path: &Path, |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 34 | output_path: &Path, |
| 35 | header_path: &Path, |
| 36 | footer_path: &Path, |
| 37 | ) -> Result<(File, Vec<File>), Error> { |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 38 | let (partitions, mut files) = convert_partitions(partitions)?; |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 39 | |
| 40 | let mut composite_image = OpenOptions::new() |
| 41 | .create_new(true) |
| 42 | .read(true) |
| 43 | .write(true) |
| 44 | .open(output_path) |
| 45 | .with_context(|| format!("Failed to create composite image {:?}", output_path))?; |
| 46 | let mut header_file = |
| 47 | OpenOptions::new().create_new(true).read(true).write(true).open(header_path).with_context( |
| 48 | || format!("Failed to create composite image header {:?}", header_path), |
| 49 | )?; |
| 50 | let mut footer_file = |
| 51 | OpenOptions::new().create_new(true).read(true).write(true).open(footer_path).with_context( |
| 52 | || format!("Failed to create composite image header {:?}", footer_path), |
| 53 | )?; |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 54 | let zero_filler_file = File::open(&zero_filler_path).with_context(|| { |
| 55 | format!("Failed to open composite image zero filler {:?}", zero_filler_path) |
| 56 | })?; |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 57 | |
| 58 | create_composite_disk( |
| 59 | &partitions, |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 60 | &fd_path_for_file(&zero_filler_file), |
| 61 | &fd_path_for_file(&header_file), |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 62 | &mut header_file, |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 63 | &fd_path_for_file(&footer_file), |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 64 | &mut footer_file, |
| 65 | &mut composite_image, |
| 66 | )?; |
| 67 | |
| 68 | // Re-open the composite image as read-only. |
| 69 | let composite_image = File::open(&output_path) |
| 70 | .with_context(|| format!("Failed to open composite image {:?}", output_path))?; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 71 | |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 72 | files.push(header_file); |
| 73 | files.push(footer_file); |
| 74 | files.push(zero_filler_file); |
| 75 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 76 | Ok((composite_image, files)) |
| 77 | } |
| 78 | |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 79 | /// Given the AIDL config containing a list of partitions, with a [`ParcelFileDescriptor`] for each |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 80 | /// partition, returns the corresponding list of PartitionInfo and the list of files whose file |
| 81 | /// descriptors must be passed to any process using the composite image. |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 82 | fn convert_partitions(partitions: &[Partition]) -> Result<(Vec<PartitionInfo>, Vec<File>), Error> { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 83 | // File descriptors to pass to child process. |
| 84 | let mut files = vec![]; |
| 85 | |
| 86 | let partitions = partitions |
| 87 | .iter() |
| 88 | .map(|partition| { |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 89 | // TODO(b/187187765): This shouldn't be an Option. |
| 90 | let file = partition |
| 91 | .image |
| 92 | .as_ref() |
| 93 | .context("Invalid partition image file descriptor")? |
| 94 | .as_ref() |
| 95 | .try_clone() |
| 96 | .context("Failed to clone partition image file descriptor")?; |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 97 | let path = fd_path_for_file(&file); |
Richard Fung | 0383ab9 | 2022-03-24 18:16:25 +0000 | [diff] [blame] | 98 | let size = get_partition_size(&file, &path)?; |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 99 | files.push(file); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 100 | |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 101 | Ok(PartitionInfo { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 102 | label: partition.label.to_owned(), |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 103 | path, |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 104 | partition_type: ImagePartitionType::LinuxFilesystem, |
| 105 | writable: partition.writable, |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 106 | size, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 107 | }) |
| 108 | }) |
| 109 | .collect::<Result<_, Error>>()?; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 110 | |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 111 | Ok((partitions, files)) |
| 112 | } |
| 113 | |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 114 | fn fd_path_for_file(file: &File) -> PathBuf { |
| 115 | let fd = file.as_raw_fd(); |
| 116 | format!("/proc/self/fd/{}", fd).into() |
| 117 | } |
| 118 | |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 119 | /// Find the size of the partition image in the given file by parsing the header. |
| 120 | /// |
| 121 | /// This will work for raw, QCOW2, composite and Android sparse images. |
Richard Fung | 0383ab9 | 2022-03-24 18:16:25 +0000 | [diff] [blame] | 122 | fn get_partition_size(partition: &File, path: &Path) -> Result<u64, Error> { |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 123 | // TODO: Use `context` once disk::Error implements std::error::Error. |
Richard Fung | 0383ab9 | 2022-03-24 18:16:25 +0000 | [diff] [blame] | 124 | Ok(create_disk_file(partition.try_clone()?, MAX_NESTING_DEPTH, path) |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 125 | .map_err(|e| anyhow!("Failed to open partition image: {}", e))? |
| 126 | .get_len()?) |
| 127 | } |