Skip to content

Commit 7059bf7

Browse files
committed
Support block devices
1 parent c54ec38 commit 7059bf7

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

src/cgroup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::mem::ManuallyDrop;
77
use std::path::{Path, PathBuf};
88

99
// The numerical representation below needs to match BPF_DEVCG constants.
10-
#[allow(unused)]
1110
#[repr(u32)]
11+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1212
pub enum DeviceType {
1313
Block = 1,
1414
Character = 2,

src/dev/device.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use std::fmt::{self, Display, Formatter};
22
use std::path::{Path, PathBuf};
33

4+
use crate::cgroup::DeviceType;
5+
46
#[derive(Debug, Clone)]
57
pub struct DevNode {
68
pub path: PathBuf,
9+
pub ty: DeviceType,
710
pub devnum: (u32, u32),
811
}
912

@@ -16,17 +19,22 @@ pub struct Device {
1619

1720
impl Device {
1821
pub fn from_udev(device: udev::Device) -> Self {
19-
let devnode = device
20-
.devnum()
21-
.zip(device.devnode())
22-
.map(|(devnum, devnode)| {
23-
let major = rustix::fs::major(devnum);
24-
let minor = rustix::fs::minor(devnum);
25-
DevNode {
26-
path: devnode.to_owned(),
27-
devnum: (major, minor),
28-
}
29-
});
22+
let devnode = device.devnode().and_then(|devnode| {
23+
let devnum = device.devnum()?;
24+
let major = rustix::fs::major(devnum);
25+
let minor = rustix::fs::minor(devnum);
26+
// Only block subsystem produce block device, everything else are character device.
27+
let ty = if device.subsystem()? == "block" {
28+
DeviceType::Block
29+
} else {
30+
DeviceType::Character
31+
};
32+
Some(DevNode {
33+
path: devnode.to_owned(),
34+
ty,
35+
devnum: (major, minor),
36+
})
37+
});
3038
Self { device, devnode }
3139
}
3240

src/hotplug/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ impl HotPlug {
7070
.filter_map(|dev| dev.matches(&device))
7171
.collect();
7272

73-
self.container.device(devnode.devnum, Access::all()).await?;
74-
self.container.mknod(&devnode.path, devnode.devnum).await?;
73+
self.container
74+
.device(devnode.ty, devnode.devnum, Access::all())
75+
.await?;
76+
self.container
77+
.mknod(&devnode.path, devnode.ty, devnode.devnum)
78+
.await?;
7579
for symlink in &symlinks {
7680
self.container.symlink(&devnode.path, symlink).await?;
7781
}
@@ -89,7 +93,7 @@ impl HotPlug {
8993

9094
let devnode = device.devnode().unwrap();
9195
self.container
92-
.device(devnode.devnum, Access::empty())
96+
.device(devnode.ty, devnode.devnum, Access::empty())
9397
.await?;
9498
self.container.rm(&devnode.path).await?;
9599
for symlink in &device.symlinks {

src/runc/container.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,12 @@ impl Container {
215215
Ok(())
216216
}
217217

218-
pub async fn mknod(&self, node: &Path, (major, minor): (u32, u32)) -> Result<()> {
218+
pub async fn mknod(
219+
&self,
220+
node: &Path,
221+
ty: DeviceType,
222+
(major, minor): (u32, u32),
223+
) -> Result<()> {
219224
let ns = crate::util::namespace::MntNamespace::of_pid(self.pid)?;
220225
ns.enter(|| {
221226
if let Some(parent) = node.parent() {
@@ -225,7 +230,11 @@ impl Container {
225230
rustix::fs::mknodat(
226231
rustix::fs::CWD,
227232
node,
228-
FileType::CharacterDevice,
233+
if ty == DeviceType::Character {
234+
FileType::CharacterDevice
235+
} else {
236+
FileType::BlockDevice
237+
},
229238
Mode::from(0o644),
230239
rustix::fs::makedev(major, minor),
231240
)?;
@@ -252,13 +261,16 @@ impl Container {
252261
})
253262
}
254263

255-
pub async fn device(&self, (major, minor): (u32, u32), access: Access) -> Result<()> {
256-
self.cgroup_device_filter.lock().await.set_permission(
257-
DeviceType::Character,
258-
major,
259-
minor,
260-
access,
261-
)?;
264+
pub async fn device(
265+
&self,
266+
ty: DeviceType,
267+
(major, minor): (u32, u32),
268+
access: Access,
269+
) -> Result<()> {
270+
self.cgroup_device_filter
271+
.lock()
272+
.await
273+
.set_permission(ty, major, minor, access)?;
262274
Ok(())
263275
}
264276
}

0 commit comments

Comments
 (0)