mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69ef63a0ef | ||
|
|
346844ca72 | ||
|
|
4f1fe13d91 | ||
|
|
17a6c6b842 | ||
|
|
3c4b724433 | ||
|
|
01885adb99 | ||
|
|
ce5f5f638e | ||
|
|
be837166e9 | ||
|
|
8d29c194e3 | ||
|
|
8a82ad0ac2 | ||
|
|
369f3bebed | ||
|
|
0b6b229a38 | ||
|
|
f55bdb1775 | ||
|
|
55505e0b3e | ||
|
|
df347c1db8 | ||
|
|
66a93b1c3d | ||
|
|
ca66292f5f | ||
|
|
89edba0f85 |
2
.github/workflows/bvt.yaml
vendored
2
.github/workflows/bvt.yaml
vendored
@@ -1,7 +1,7 @@
|
|||||||
name: BVT
|
name: BVT
|
||||||
on: [pull_request]
|
on: [pull_request]
|
||||||
env:
|
env:
|
||||||
RUST_VERSION: 1.52
|
RUST_VERSION: 1.69.0
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: Build
|
name: Build
|
||||||
|
|||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -8,7 +8,3 @@ Cargo.lock
|
|||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
/target
|
|
||||||
**/*.rs.bk
|
|
||||||
Cargo.lock
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ repository = "https://github.com/kata-containers/cgroups-rs"
|
|||||||
keywords = ["linux", "cgroup", "containers", "isolation"]
|
keywords = ["linux", "cgroup", "containers", "isolation"]
|
||||||
categories = ["os", "api-bindings", "os::unix-apis"]
|
categories = ["os", "api-bindings", "os::unix-apis"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
version = "0.3.2"
|
version = "0.3.4"
|
||||||
authors = ["The Kata Containers community <kata-dev@lists.katacontainers.io>", "Levente Kurusa <lkurusa@acm.org>", "Sam Wilson <tecywiz121@hotmail.com>"]
|
authors = ["The Kata Containers community <kata-dev@lists.katacontainers.io>", "Levente Kurusa <lkurusa@acm.org>", "Sam Wilson <tecywiz121@hotmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
homepage = "https://github.com/kata-containers/cgroups-rs"
|
homepage = "https://github.com/kata-containers/cgroups-rs"
|
||||||
|
|||||||
@@ -73,8 +73,13 @@ impl Cgroup {
|
|||||||
self.hier.v2()
|
self.hier.v2()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the path the cgroup is located at.
|
||||||
|
pub fn path(&self) -> &str {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
|
||||||
/// Create this control group.
|
/// Create this control group.
|
||||||
fn create(&self) -> Result<()> {
|
pub fn create(&self) -> Result<()> {
|
||||||
if self.hier.v2() {
|
if self.hier.v2() {
|
||||||
create_v2_cgroup(self.hier.root(), &self.path, &self.specified_controllers)
|
create_v2_cgroup(self.hier.root(), &self.path, &self.specified_controllers)
|
||||||
} else {
|
} else {
|
||||||
@@ -492,6 +497,13 @@ impl Cgroup {
|
|||||||
v.dedup();
|
v.dedup();
|
||||||
v
|
v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks if the cgroup exists.
|
||||||
|
///
|
||||||
|
/// Returns true if at least one subsystem exists.
|
||||||
|
pub fn exists(&self) -> bool {
|
||||||
|
self.subsystems().iter().any(|e| e.to_controller().exists())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ pub enum DeviceType {
|
|||||||
Block,
|
Block,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::derivable_impls)]
|
||||||
impl Default for DeviceType {
|
impl Default for DeviceType {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
DeviceType::All
|
DeviceType::All
|
||||||
@@ -170,9 +171,9 @@ impl ControllerInternal for DevicesController {
|
|||||||
|
|
||||||
for i in &res.devices {
|
for i in &res.devices {
|
||||||
if i.allow {
|
if i.allow {
|
||||||
let _ = self.allow_device(i.devtype, i.major, i.minor, &i.access);
|
self.allow_device(i.devtype, i.major, i.minor, &i.access)?;
|
||||||
} else {
|
} else {
|
||||||
let _ = self.deny_device(i.devtype, i.major, i.minor, &i.access);
|
self.deny_device(i.devtype, i.major, i.minor, &i.access)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,7 +239,13 @@ impl DevicesController {
|
|||||||
let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms);
|
let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms);
|
||||||
self.open_path("devices.allow", true).and_then(|mut file| {
|
self.open_path("devices.allow", true).and_then(|mut file| {
|
||||||
file.write_all(final_str.as_ref()).map_err(|e| {
|
file.write_all(final_str.as_ref()).map_err(|e| {
|
||||||
Error::with_cause(WriteFailed("devices.allow".to_string(), final_str), e)
|
Error::with_cause(
|
||||||
|
WriteFailed(
|
||||||
|
self.get_path().join("devices.allow").display().to_string(),
|
||||||
|
final_str,
|
||||||
|
),
|
||||||
|
e,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -271,7 +278,13 @@ impl DevicesController {
|
|||||||
let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms);
|
let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms);
|
||||||
self.open_path("devices.deny", true).and_then(|mut file| {
|
self.open_path("devices.deny", true).and_then(|mut file| {
|
||||||
file.write_all(final_str.as_ref()).map_err(|e| {
|
file.write_all(final_str.as_ref()).map_err(|e| {
|
||||||
Error::with_cause(WriteFailed("devices.deny".to_string(), final_str), e)
|
Error::with_cause(
|
||||||
|
WriteFailed(
|
||||||
|
self.get_path().join("devices.deny").display().to_string(),
|
||||||
|
final_str,
|
||||||
|
),
|
||||||
|
e,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -309,43 +309,16 @@ impl Default for V2 {
|
|||||||
|
|
||||||
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
||||||
|
|
||||||
#[cfg(any(
|
|
||||||
all(target_os = "linux", not(target_env = "musl")),
|
|
||||||
target_os = "android"
|
|
||||||
))]
|
|
||||||
pub fn is_cgroup2_unified_mode() -> bool {
|
pub fn is_cgroup2_unified_mode() -> bool {
|
||||||
use nix::sys::statfs;
|
use nix::sys::statfs;
|
||||||
|
|
||||||
let path = std::path::Path::new(UNIFIED_MOUNTPOINT);
|
let path = std::path::Path::new(UNIFIED_MOUNTPOINT);
|
||||||
let fs_stat = statfs::statfs(path);
|
let fs_stat = match statfs::statfs(path) {
|
||||||
if fs_stat.is_err() {
|
Ok(fs_stat) => fs_stat,
|
||||||
return false;
|
Err(_) => return false,
|
||||||
}
|
};
|
||||||
|
|
||||||
// FIXME notwork, nix will not compile CGROUP2_SUPER_MAGIC because not(target_env = "musl")
|
fs_stat.filesystem_type() == statfs::CGROUP2_SUPER_MAGIC
|
||||||
fs_stat.unwrap().filesystem_type() == statfs::CGROUP2_SUPER_MAGIC
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const INIT_CGROUP_PATHS: &str = "/proc/1/cgroup";
|
|
||||||
|
|
||||||
#[cfg(all(target_os = "linux", target_env = "musl"))]
|
|
||||||
pub fn is_cgroup2_unified_mode() -> bool {
|
|
||||||
let lines = fs::read_to_string(INIT_CGROUP_PATHS);
|
|
||||||
if lines.is_err() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for line in lines.unwrap().lines() {
|
|
||||||
let fields: Vec<&str> = line.split(':').collect();
|
|
||||||
if fields.len() != 3 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if fields[0] != "0" {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn auto() -> Box<dyn Hierarchy> {
|
pub fn auto() -> Box<dyn Hierarchy> {
|
||||||
|
|||||||
@@ -879,6 +879,7 @@ pub enum MaxValue {
|
|||||||
Value(i64),
|
Value(i64),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::derivable_impls)]
|
||||||
impl Default for MaxValue {
|
impl Default for MaxValue {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
MaxValue::Max
|
MaxValue::Max
|
||||||
|
|||||||
@@ -593,7 +593,10 @@ impl MemController {
|
|||||||
.open_path("memory.current", false)
|
.open_path("memory.current", false)
|
||||||
.and_then(read_u64_from)
|
.and_then(read_u64_from)
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
max_usage_in_bytes: 0,
|
max_usage_in_bytes: self
|
||||||
|
.open_path("memory.peak", false)
|
||||||
|
.and_then(read_u64_from)
|
||||||
|
.unwrap_or(0),
|
||||||
move_charge_at_immigrate: 0,
|
move_charge_at_immigrate: 0,
|
||||||
numa_stat: NumaStat::default(),
|
numa_stat: NumaStat::default(),
|
||||||
oom_control: OomControl::default(),
|
oom_control: OomControl::default(),
|
||||||
@@ -736,7 +739,10 @@ impl MemController {
|
|||||||
.open_path("memory.swap.current", false)
|
.open_path("memory.swap.current", false)
|
||||||
.and_then(read_u64_from)
|
.and_then(read_u64_from)
|
||||||
.unwrap_or(0),
|
.unwrap_or(0),
|
||||||
max_usage_in_bytes: 0,
|
max_usage_in_bytes: self
|
||||||
|
.open_path("memory.swap.peak", false)
|
||||||
|
.and_then(read_u64_from)
|
||||||
|
.unwrap_or(0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -844,13 +850,16 @@ impl MemController {
|
|||||||
/// Set the memory usage limit of the control group, in bytes.
|
/// Set the memory usage limit of the control group, in bytes.
|
||||||
pub fn set_limit(&self, limit: i64) -> Result<()> {
|
pub fn set_limit(&self, limit: i64) -> Result<()> {
|
||||||
let mut file_name = "memory.limit_in_bytes";
|
let mut file_name = "memory.limit_in_bytes";
|
||||||
|
let mut limit_str = limit.to_string();
|
||||||
if self.v2 {
|
if self.v2 {
|
||||||
file_name = "memory.max";
|
file_name = "memory.max";
|
||||||
|
if limit == -1 {
|
||||||
|
limit_str = "max".to_string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.open_path(file_name, true).and_then(|mut file| {
|
self.open_path(file_name, true).and_then(|mut file| {
|
||||||
file.write_all(limit.to_string().as_ref()).map_err(|e| {
|
file.write_all(limit_str.as_ref())
|
||||||
Error::with_cause(WriteFailed(file_name.to_string(), limit.to_string()), e)
|
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), limit_str), e))
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -881,13 +890,16 @@ impl MemController {
|
|||||||
/// Set the memory+swap limit of the control group, in bytes.
|
/// Set the memory+swap limit of the control group, in bytes.
|
||||||
pub fn set_memswap_limit(&self, limit: i64) -> Result<()> {
|
pub fn set_memswap_limit(&self, limit: i64) -> Result<()> {
|
||||||
let mut file_name = "memory.memsw.limit_in_bytes";
|
let mut file_name = "memory.memsw.limit_in_bytes";
|
||||||
|
let mut limit_str = limit.to_string();
|
||||||
if self.v2 {
|
if self.v2 {
|
||||||
file_name = "memory.swap.max";
|
file_name = "memory.swap.max";
|
||||||
|
if limit == -1 {
|
||||||
|
limit_str = "max".to_string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.open_path(file_name, true).and_then(|mut file| {
|
self.open_path(file_name, true).and_then(|mut file| {
|
||||||
file.write_all(limit.to_string().as_ref()).map_err(|e| {
|
file.write_all(limit_str.as_ref())
|
||||||
Error::with_cause(WriteFailed(file_name.to_string(), limit.to_string()), e)
|
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), limit_str), e))
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ fn test_kill_cgroup() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
assert!(!status.is_none());
|
assert!(status.is_some());
|
||||||
}
|
}
|
||||||
cg.delete().unwrap();
|
cg.delete().unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user