Skip to content

Commit 4781148

Browse files
authored
Merge pull request #91 from noeljackson/feat/mount-data
feat(mount): add mount data and improve bind target handling
2 parents 2740337 + 45254bb commit 4781148

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ pub struct MountSpec {
250250

251251
/// Whether the mount point should be mounted readonly.
252252
pub read_only: bool,
253+
254+
/// Optional mount data (e.g., "size=64m" for tmpfs).
255+
pub data: Option<String>,
253256
}
254257

255258
pub trait Mountable {

src/mount.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,22 @@ impl Mountable for MountSpec {
146146
let target_p = target.as_ptr();
147147

148148
if self.create_mountpoint {
149-
fs::create_dir_all(&self.target)?;
149+
let source_is_file = self.bind
150+
&& self
151+
.source
152+
.as_deref()
153+
.and_then(|s| std::path::Path::new(s).metadata().ok())
154+
.map(|m| !m.is_dir())
155+
.unwrap_or(false);
156+
157+
if source_is_file {
158+
if let Some(parent) = std::path::Path::new(&self.target).parent() {
159+
fs::create_dir_all(parent)?;
160+
}
161+
fs::File::create(&self.target)?;
162+
} else {
163+
fs::create_dir_all(&self.target)?;
164+
}
150165
}
151166

152167
let mut flags: c_ulong = libc::MS_SILENT;
@@ -163,10 +178,28 @@ impl Mountable for MountSpec {
163178
flags |= libc::MS_REC;
164179
}
165180

181+
let data_cstr = self
182+
.data
183+
.as_ref()
184+
.map(|d| CString::new(d.as_str()).unwrap());
185+
let data_ptr = data_cstr
186+
.as_ref()
187+
.map(|c| c.as_ptr() as *const libc::c_void)
188+
.unwrap_or(ptr::null());
189+
166190
unsafe {
167-
let rc = libc::mount(source_p, target_p, fstype_p, flags, ptr::null());
191+
let rc = libc::mount(source_p, target_p, fstype_p, flags, data_ptr);
168192
if rc < 0 {
169-
bail!("unable to mount");
193+
let err = io::Error::last_os_error();
194+
bail!(
195+
"unable to mount: source={:?} target={:?} fstype={:?} bind={} flags=0x{:x}: {}",
196+
self.source,
197+
self.target,
198+
self.fstype,
199+
self.bind,
200+
flags,
201+
err
202+
);
170203
}
171204
}
172205

src/wrap.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ impl CreateRequest {
346346
safe: false,
347347
create_mountpoint: false,
348348
read_only: false,
349+
data: None,
349350
};
350351

351352
oldroot
@@ -370,6 +371,7 @@ impl CreateRequest {
370371
safe: true,
371372
create_mountpoint: true,
372373
read_only: false,
374+
data: None,
373375
};
374376
stage_tmpfs
375377
.mount()
@@ -390,6 +392,7 @@ impl CreateRequest {
390392
safe: false,
391393
create_mountpoint: false,
392394
read_only: false,
395+
data: None,
393396
};
394397
stage_bind
395398
.mount()
@@ -409,6 +412,7 @@ impl CreateRequest {
409412
safe: false,
410413
create_mountpoint: false,
411414
read_only: false,
415+
data: None,
412416
};
413417

414418
newroot
@@ -432,6 +436,7 @@ impl CreateRequest {
432436
safe: true,
433437
create_mountpoint: false,
434438
read_only: false,
439+
data: None,
435440
};
436441

437442
procfs
@@ -451,6 +456,7 @@ impl CreateRequest {
451456
safe: mount.safe,
452457
create_mountpoint: mount.create_mountpoint,
453458
read_only: mount.read_only,
459+
data: None,
454460
};
455461

456462
parented_mount

0 commit comments

Comments
 (0)