blob: affa28e051e347a81086e260d90e8ecd9b98c338 [file] [log] [blame]
Jiyong Park48b354d2021-07-15 15:04:38 +09001// 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
17use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::IVirtualizationService;
Jiyong Park9dd389e2021-08-23 20:42:59 +090018use android_system_virtualizationservice::aidl::android::system::virtualizationservice::PartitionType::PartitionType;
Alan Stokes0e82b502022-08-08 14:44:48 +010019use binder::ParcelFileDescriptor;
Jiyong Park48b354d2021-07-15 15:04:38 +090020use anyhow::{Context, Error};
21use std::convert::TryInto;
22use std::fs::OpenOptions;
23use std::path::Path;
24
25/// Initialise an empty partition image of the given size to be used as a writable partition.
26pub fn command_create_partition(
Andrew Walbran616d13f2022-05-12 18:35:55 +000027 service: &dyn IVirtualizationService,
Jiyong Park48b354d2021-07-15 15:04:38 +090028 image_path: &Path,
29 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +090030 partition_type: PartitionType,
Jiyong Park48b354d2021-07-15 15:04:38 +090031) -> 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 Park9dd389e2021-08-23 20:42:59 +090039 .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 Park48b354d2021-07-15 15:04:38 +090048 Ok(())
49}