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;
18 use android_system_virtualizationservice::aidl::android::system::virtualizationservice::PartitionType::PartitionType;
19 use binder::ParcelFileDescriptor;
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.
command_create_partition( service: &dyn IVirtualizationService, image_path: &Path, size: u64, partition_type: PartitionType, ) -> Result<(), Error>26 pub fn command_create_partition(
27 service: &dyn IVirtualizationService,
28 image_path: &Path,
29 size: u64,
30 partition_type: PartitionType,
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
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 ))?;
48 Ok(())
49 }
50