Path: blob/main/devices/src/virtio/vhost_user_backend/block/sys/linux.rs
5394 views
// Copyright 2022 The ChromiumOS Authors1// Use of this source code is governed by a BSD-style license that can be2// found in the LICENSE file.34use anyhow::Context;5use argh::FromArgs;6use base::RawDescriptor;7use cros_async::Executor;8use hypervisor::ProtectionType;910use crate::virtio::base_features;11use crate::virtio::block::DiskOption;12use crate::virtio::vhost_user_backend::BackendConnection;13use crate::virtio::BlockAsync;1415#[derive(FromArgs)]16#[argh(subcommand, name = "block")]17/// Block device18pub struct Options {19#[argh(option, arg_name = "PATH", hidden_help)]20/// deprecated - please use --socket-path instead21socket: Option<String>,22#[argh(option, arg_name = "PATH")]23/// path to the vhost-user socket to bind to.24/// If this flag is set, --fd cannot be specified.25socket_path: Option<String>,26#[argh(option, arg_name = "FD")]27/// file descriptor of a connected vhost-user socket.28/// If this flag is set, --socket-path cannot be specified.29fd: Option<RawDescriptor>,3031#[argh(option, arg_name = "PATH<:read-only>")]32/// path and options of the disk file.33file: String,34}3536/// Starts a vhost-user block device.37/// Returns an error if the given `args` is invalid or the device fails to run.38pub fn start_device(opts: Options) -> anyhow::Result<()> {39let ex = Executor::new().context("failed to create executor")?;4041let mut fileopts = opts.file.split(':').collect::<Vec<_>>();42let filename = fileopts.remove(0);4344let disk = DiskOption {45path: filename.into(),46read_only: fileopts.contains(&"read-only"),47sparse: false,48..DiskOption::default()49};5051let block = Box::new(BlockAsync::new(52base_features(ProtectionType::Unprotected),53disk.open()?,54&disk,55None,56None,57None,58)?);5960let conn =61BackendConnection::from_opts(opts.socket.as_deref(), opts.socket_path.as_deref(), opts.fd)?;6263conn.run_device(ex, block)64}656667