Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [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 | |
| 15 | //! Command to create an empty partition |
| 16 | |
| 17 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::IVirtualizationService; |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 18 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::PartitionType::PartitionType; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 19 | use android_system_virtualizationservice::binder::{ParcelFileDescriptor, Strong}; |
| 20 | use anyhow::{Context, Error}; |
| 21 | use std::convert::TryInto; |
| 22 | use std::fs::OpenOptions; |
| 23 | use std::path::Path; |
| 24 | |
| 25 | /// Initialise an empty partition image of the given size to be used as a writable partition. |
| 26 | pub fn command_create_partition( |
| 27 | service: Strong<dyn IVirtualizationService>, |
| 28 | image_path: &Path, |
| 29 | size: u64, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 30 | partition_type: PartitionType, |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 31 | ) -> Result<(), Error> { |
| 32 | let image = OpenOptions::new() |
| 33 | .create_new(true) |
| 34 | .read(true) |
| 35 | .write(true) |
| 36 | .open(image_path) |
| 37 | .with_context(|| format!("Failed to create {:?}", image_path))?; |
| 38 | service |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 39 | .initializeWritablePartition( |
| 40 | &ParcelFileDescriptor::new(image), |
| 41 | size.try_into()?, |
| 42 | partition_type, |
| 43 | ) |
| 44 | .context(format!( |
| 45 | "Failed to initialize partition type: {:?}, size: {}", |
| 46 | partition_type, size |
| 47 | ))?; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 48 | Ok(()) |
| 49 | } |