libs: enhance kata-sys-util and kata-types

Fixes: #3785
Signed-off-by: Quanwei Zhou <quanweiZhou@linux.alibaba.com>
This commit is contained in:
Quanwei Zhou 2022-03-26 17:33:41 +08:00 committed by Fupan Li
parent 3ff0db05a7
commit 9887272db9
12 changed files with 293 additions and 35 deletions

80
src/agent/Cargo.lock generated
View File

@ -325,7 +325,7 @@ checksum = "ec3245a0ca564e7f3c797d20d833a6870f57a728ac967d5225b3ffdef4465011"
dependencies = [
"lazy_static",
"log",
"rand",
"rand 0.8.4",
]
[[package]]
@ -446,6 +446,17 @@ dependencies = [
"slab",
]
[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.3"
@ -454,7 +465,7 @@ checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi",
"wasi 0.10.2+wasi-snapshot-preview1",
]
[[package]]
@ -636,6 +647,7 @@ dependencies = [
name = "kata-sys-util"
version = "0.1.0"
dependencies = [
"byteorder",
"cgroups-rs",
"chrono",
"common-path",
@ -646,6 +658,7 @@ dependencies = [
"nix 0.23.1",
"oci",
"once_cell",
"rand 0.7.3",
"serde_json",
"slog",
"slog-scope",
@ -962,7 +975,7 @@ dependencies = [
"lazy_static",
"percent-encoding",
"pin-project",
"rand",
"rand 0.8.4",
"serde",
"thiserror",
"tokio",
@ -1279,6 +1292,19 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom 0.1.16",
"libc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc 0.2.0",
]
[[package]]
name = "rand"
version = "0.8.4"
@ -1286,9 +1312,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
"rand_chacha 0.3.1",
"rand_core 0.6.3",
"rand_hc 0.3.1",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core 0.5.1",
]
[[package]]
@ -1298,7 +1334,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.6.3",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom 0.1.16",
]
[[package]]
@ -1307,7 +1352,16 @@ version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
"getrandom 0.2.3",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core 0.5.1",
]
[[package]]
@ -1316,7 +1370,7 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core",
"rand_core 0.6.3",
]
[[package]]
@ -1664,7 +1718,7 @@ checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
dependencies = [
"cfg-if 1.0.0",
"libc",
"rand",
"rand 0.8.4",
"redox_syscall",
"remove_dir_all",
"winapi",
@ -1986,6 +2040,12 @@ dependencies = [
"tokio-vsock",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"

82
src/libs/Cargo.lock generated
View File

@ -156,7 +156,7 @@ checksum = "ec3245a0ca564e7f3c797d20d833a6870f57a728ac967d5225b3ffdef4465011"
dependencies = [
"lazy_static",
"log",
"rand",
"rand 0.8.5",
]
[[package]]
@ -263,6 +263,17 @@ dependencies = [
"slab",
]
[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.6"
@ -351,6 +362,7 @@ checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
name = "kata-sys-util"
version = "0.1.0"
dependencies = [
"byteorder",
"cgroups-rs",
"chrono",
"common-path",
@ -362,6 +374,7 @@ dependencies = [
"num_cpus",
"oci",
"once_cell",
"rand 0.7.3",
"serde_json",
"serial_test",
"slog",
@ -657,9 +670,9 @@ dependencies = [
[[package]]
name = "protobuf"
version = "2.27.1"
version = "2.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf7e6d18738ecd0902d30d1ad232c9125985a3422929b16c65517b38adc14f96"
checksum = "8e86d370532557ae7573551a1ec8235a0f8d6cb276c7c9e6aa490b511c447485"
dependencies = [
"serde",
"serde_derive",
@ -667,18 +680,18 @@ dependencies = [
[[package]]
name = "protobuf-codegen"
version = "2.27.1"
version = "2.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aec1632b7c8f2e620343439a7dfd1f3c47b18906c4be58982079911482b5d707"
checksum = "de113bba758ccf2c1ef816b127c958001b7831136c9bc3f8e9ec695ac4e82b0c"
dependencies = [
"protobuf",
]
[[package]]
name = "protobuf-codegen-pure"
version = "2.27.1"
version = "2.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f8122fdb18e55190c796b088a16bdb70cd7acdcd48f7a8b796b58c62e532cc6"
checksum = "2d1a4febc73bf0cada1d77c459a0c8e5973179f1cfd5b0f1ab789d45b17b6440"
dependencies = [
"protobuf",
"protobuf-codegen",
@ -706,6 +719,19 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom 0.1.16",
"libc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc",
]
[[package]]
name = "rand"
version = "0.8.5"
@ -713,8 +739,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_chacha 0.3.1",
"rand_core 0.6.3",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core 0.5.1",
]
[[package]]
@ -724,7 +760,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.6.3",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom 0.1.16",
]
[[package]]
@ -733,7 +778,16 @@ version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
"getrandom 0.2.6",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core 0.5.1",
]
[[package]]
@ -1104,6 +1158,12 @@ dependencies = [
"nix",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"

View File

@ -11,6 +11,7 @@ license = "Apache-2.0"
edition = "2018"
[dependencies]
byteorder = "~1"
cgroups = { package = "cgroups-rs", version = "0.2.7" }
chrono = "0.4.0"
common-path = "=1.0.0"
@ -23,6 +24,7 @@ serde_json = "1.0.73"
slog = "2.5.2"
slog-scope = "4.4.0"
subprocess = "0.2.8"
rand = "^0.7.2"
thiserror = "1.0.30"
kata-types = { path = "../kata-types" }

View File

@ -12,6 +12,7 @@ pub mod hooks;
pub mod k8s;
pub mod mount;
pub mod numa;
pub mod rand;
pub mod spec;
pub mod validate;

View File

@ -262,6 +262,7 @@ pub fn bind_mount_unchecked<S: AsRef<Path>, D: AsRef<Path>>(
.canonicalize()
.map_err(|_e| Error::InvalidPath(src.to_path_buf()))?;
create_mount_destination(src, dst, "/", "bind")?;
// Bind mount `src` to `dst`.
mount(
Some(&abs_src),

View File

@ -0,0 +1,10 @@
// Copyright (c) 2019-2022 Alibaba Cloud
// Copyright (c) 2019-2022 Ant Group
//
// SPDX-License-Identifier: Apache-2.0
//
mod random_bytes;
pub use random_bytes::RandomBytes;
mod uuid;
pub use uuid::UUID;

View File

@ -0,0 +1,51 @@
// Copyright (c) 2019-2022 Alibaba Cloud
// Copyright (c) 2019-2022 Ant Group
//
// SPDX-License-Identifier: Apache-2.0
//
use std::fmt;
use rand::RngCore;
pub struct RandomBytes {
pub bytes: Vec<u8>,
}
impl RandomBytes {
pub fn new(n: usize) -> Self {
let mut bytes = vec![0u8; n];
rand::thread_rng().fill_bytes(&mut bytes);
Self { bytes }
}
}
impl fmt::LowerHex for RandomBytes {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for byte in &self.bytes {
write!(f, "{:x}", byte)?;
}
Ok(())
}
}
impl fmt::UpperHex for RandomBytes {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for byte in &self.bytes {
write!(f, "{:X}", byte)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn random_bytes() {
let b = RandomBytes::new(16);
assert_eq!(b.bytes.len(), 16);
println!("{:?}", b.bytes);
}
}

View File

@ -0,0 +1,69 @@
// Copyright (c) 2019-2022 Alibaba Cloud
// Copyright (c) 2019-2022 Ant Group
//
// SPDX-License-Identifier: Apache-2.0
//
use std::{convert::From, fmt};
use byteorder::{BigEndian, ByteOrder};
use rand::RngCore;
pub struct UUID([u8; 16]);
impl Default for UUID {
fn default() -> Self {
Self::new()
}
}
impl UUID {
pub fn new() -> Self {
let mut b = [0u8; 16];
rand::thread_rng().fill_bytes(&mut b);
b[6] = (b[6] & 0x0f) | 0x40;
b[8] = (b[8] & 0x3f) | 0x80;
Self(b)
}
}
impl From<&UUID> for String {
fn from(from: &UUID) -> Self {
let time_low = BigEndian::read_u32(&from.0[..4]);
let time_mid = BigEndian::read_u16(&from.0[4..6]);
let time_hi = BigEndian::read_u16(&from.0[6..8]);
let clk_seq_hi = from.0[8];
let clk_seq_low = from.0[9];
let mut buf = [0u8; 8];
buf[2..].copy_from_slice(&from.0[10..]);
let node = BigEndian::read_u64(&buf);
format!(
"{:08x}-{:04x}-{:04x}-{:02x}{:02x}-{:012x}",
time_low, time_mid, time_hi, clk_seq_hi, clk_seq_low, node
)
}
}
impl fmt::Display for UUID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", String::from(self))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_uuid() {
let uuid = UUID::new();
let sss: String = String::from(&uuid);
println!("{}", sss);
let uuid2 = UUID([0u8, 1u8, 2u8, 3u8, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
let sss2 = String::from(&uuid2);
println!("Display: {}", uuid2);
assert_eq!(&sss2, "00010203-0405-0607-0809-0a0b0c0d0e0f");
}
}

View File

@ -68,7 +68,7 @@ pub fn get_hypervisor_plugin(name: &str) -> Option<Arc<dyn ConfigPlugin>> {
}
/// Configuration information for block device.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct BlockDeviceInfo {
/// Disable block device from being used for a container's rootfs.
///
@ -194,7 +194,7 @@ impl BlockDeviceInfo {
}
/// Guest kernel boot information.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct BootInfo {
/// Path to guest kernel file on host
#[serde(default)]
@ -245,7 +245,7 @@ impl BootInfo {
}
/// Virtual CPU configuration information.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct CpuInfo {
/// CPU features, comma-separated list of cpu features to pass to the cpu.
/// For example, `cpu_features = "pmu=off,vmx=off"
@ -321,7 +321,7 @@ impl CpuInfo {
}
/// Configuration information for shared filesystem, such virtio-9p and virtio-fs.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct DebugInfo {
/// This option changes the default hypervisor and kernel parameters to enable debug output
/// where available.
@ -358,7 +358,7 @@ impl DebugInfo {
}
/// Virtual machine device configuration information.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct DeviceInfo {
/// Bridges can be used to hot plug devices.
///
@ -427,7 +427,7 @@ impl DeviceInfo {
}
/// Configuration information for virtual machine.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct MachineInfo {
/// Virtual machine model/type.
#[serde(default)]
@ -495,7 +495,7 @@ impl MachineInfo {
}
/// Virtual machine memory configuration information.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct MemoryInfo {
/// Default memory size in MiB for SB/VM.
#[serde(default)]
@ -597,7 +597,7 @@ impl MemoryInfo {
}
/// Configuration information for virtual machine.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct NetworkInfo {
/// If vhost-net backend for virtio-net is not desired, set to true.
///
@ -635,7 +635,7 @@ impl NetworkInfo {
}
/// Configuration information for virtual machine.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct SecurityInfo {
/// Enable running QEMU VMM as a non-root user.
///
@ -721,7 +721,7 @@ impl SecurityInfo {
}
/// Configuration information for shared filesystem, such virtio-9p and virtio-fs.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct SharedFsInfo {
/// Shared file system type:
/// - virtio-fs (default)
@ -862,7 +862,7 @@ impl SharedFsInfo {
}
/// Common configuration information for hypervisors.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Hypervisor {
/// Path to the hypervisor executable.
#[serde(default)]
@ -1035,7 +1035,7 @@ mod vendor {
use super::*;
/// Vendor customization runtime configuration.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct HypervisorVendor {}
impl ConfigOps for HypervisorVendor {}

View File

@ -8,7 +8,7 @@
use super::*;
/// Vendor customization runtime configuration.
#[derive(Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct HypervisorVendor {}
impl ConfigOps for HypervisorVendor {}

View File

@ -17,6 +17,10 @@ pub struct Runtime {
#[serde(default)]
pub name: String,
/// Hypervisor name: Plan to support dragonball, qemu
#[serde(default)]
pub hypervisor_name: String,
/// If enabled, the runtime will log additional debug messages to the system log.
#[serde(default, rename = "enable_debug")]
pub debug: bool,

View File

@ -12,7 +12,7 @@ async = ["ttrpc/async", "async-trait"]
[dependencies]
ttrpc = { version = "0.6.0" }
async-trait = { version = "0.1.42", optional = true }
protobuf = { version = "2.23.0", features = ["with-serde"] }
protobuf = { version = "=2.14.0", features = ["with-serde"] }
serde = { version = "1.0.130", features = ["derive"], optional = true }
serde_json = { version = "1.0.68", optional = true }
oci = { path = "../oci" }