blob: acebbf236d43a59138ca5b445636afd938be757a [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;
18use android_system_virtualizationservice::binder::{ParcelFileDescriptor, Strong};
19use anyhow::{Context, Error};
20use std::convert::TryInto;
21use std::fs::OpenOptions;
22use std::path::Path;
23
24/// Initialise an empty partition image of the given size to be used as a writable partition.
25pub fn command_create_partition(
26 service: Strong<dyn IVirtualizationService>,
27 image_path: &Path,
28 size: u64,
29) -> Result<(), Error> {
30 let image = OpenOptions::new()
31 .create_new(true)
32 .read(true)
33 .write(true)
34 .open(image_path)
35 .with_context(|| format!("Failed to create {:?}", image_path))?;
36 service
37 .initializeWritablePartition(&ParcelFileDescriptor::new(image), size.try_into()?)
38 .context("Failed to initialize partition with size {}, size")?;
39 Ok(())
40}