Skip to content

Commit 6b06570

Browse files
committed
container: allow opening containers without locking
1 parent cba1954 commit 6b06570

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

ciel/src/container.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub struct Container {
4949
instance: Instance,
5050
config: Arc<ContainerConfig>,
5151
#[allow(unused)]
52-
lock: Arc<FileLock>,
52+
lock: Option<Arc<FileLock>>,
5353
ns_name: String,
5454

5555
rootfs_path: PathBuf,
@@ -89,15 +89,21 @@ impl Drop for FileLock {
8989
}
9090

9191
impl Container {
92-
/// Opens the build container, locking it exclusively.
93-
pub fn open(instance: Instance) -> Result<Self> {
94-
let lock = File::options()
95-
.read(true)
96-
.write(true)
97-
.create(true)
98-
.open(instance.directory().join(".lock"))?;
99-
fs3::FileExt::lock_exclusive(&lock)?;
100-
let lock = FileLock(lock);
92+
/// Opens the build container.
93+
///
94+
/// If `lock` is true, it will be locked exclusively.
95+
pub fn open(instance: Instance, lock: bool) -> Result<Self> {
96+
let lock = if lock {
97+
let lock = File::options()
98+
.read(true)
99+
.write(true)
100+
.create(true)
101+
.open(instance.directory().join(".lock"))?;
102+
fs3::FileExt::lock_exclusive(&lock)?;
103+
Some(Arc::new(FileLock(lock)))
104+
} else {
105+
None
106+
};
101107

102108
let ns_name = make_container_ns_name(instance.name())?;
103109
let rootfs_path = instance.workspace().directory().join(instance.name());
@@ -134,7 +140,7 @@ impl Container {
134140
Ok(Self {
135141
instance,
136142
config: Arc::new(config),
137-
lock: Arc::new(lock),
143+
lock,
138144
ns_name,
139145
rootfs_path,
140146
config_path,
@@ -598,7 +604,9 @@ impl Deref for OwnedContainer {
598604
impl Drop for OwnedContainer {
599605
fn drop(&mut self) {
600606
let instance = self.0.instance().to_owned();
601-
self.0.lock.force_unlock();
607+
if let Some(lock) = &self.0.lock {
608+
lock.force_unlock();
609+
}
602610
instance.destroy().unwrap();
603611
}
604612
}

ciel/src/instance.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ impl Instance {
8989
///
9090
/// This is equivalent to calling [Container::open].
9191
pub fn open(&self) -> Result<Container> {
92-
Container::open(self.to_owned())
92+
Container::open(self.to_owned(), true)
93+
}
94+
95+
/// Opens the build container for further operations without locking.
96+
pub fn open_unlocked(&self) -> Result<Container> {
97+
Container::open(self.to_owned(), false)
9398
}
9499

95100
/// Destories the container, removing all related files.

cli/src/actions/workspace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn list_instances() -> Result<()> {
2727
writeln!(&mut formatter, "NAME\tMOUNTED\tSTARTED\tBOOTED")?;
2828

2929
for inst in ws.instances()? {
30-
let container = inst.open()?;
30+
let container = inst.open_unlocked()?;
3131
let state = container.state()?;
3232
let (mounted, started, running) = match state {
3333
ContainerState::Down => (false, false, false),

0 commit comments

Comments
 (0)