Skip to content

Commit 118df96

Browse files
committed
fix(rootfs): better resource management
refactor(rootfs): better resource management
1 parent ab20161 commit 118df96

File tree

2 files changed

+358
-275
lines changed

2 files changed

+358
-275
lines changed

dadk/src/actions/rootfs/v2/disk_img.rs

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,14 @@ fn mount_partitioned_image(
8585
disk_image_path: &PathBuf,
8686
disk_mount_path: &PathBuf,
8787
) -> Result<()> {
88-
let mut loop_device = ManuallyDrop::new(
88+
let loop_device = ManuallyDrop::new(
8989
LoopDeviceBuilder::new()
9090
.img_path(disk_image_path.clone())
9191
.detach_on_drop(false)
9292
.build()
9393
.map_err(|e| anyhow!("Failed to create loop device: {}", e))?,
9494
);
9595

96-
loop_device
97-
.attach()
98-
.map_err(|e| anyhow!("mount: Failed to attach loop device: {}", e))?;
99-
10096
let dev_path = loop_device.partition_path(1)?;
10197
mount_unpartitioned_image(ctx, &dev_path, disk_mount_path)?;
10298

@@ -125,55 +121,32 @@ fn mount_unpartitioned_image(
125121
pub fn umount(ctx: &DADKExecContext) -> Result<()> {
126122
let disk_img_path = ctx.disk_image_path();
127123
let disk_mount_path = ctx.disk_mount_path();
128-
let mut loop_device = LoopDeviceBuilder::new().img_path(disk_img_path).build();
129124

130-
let should_detach_loop_device: bool;
131-
if let Ok(loop_device) = loop_device.as_mut() {
132-
if let Err(e) = loop_device.attach_by_exists() {
133-
log::trace!("umount: Failed to attach loop device: {}", e);
134-
}
125+
if disk_mount_path.exists() {
126+
log::trace!("Unmounted disk image at {}", disk_mount_path.display());
135127

136-
should_detach_loop_device = loop_device.attached();
137-
} else {
138-
should_detach_loop_device = false;
139-
}
128+
let cmd = Command::new("umount").arg(disk_mount_path).output()?;
140129

141-
if disk_mount_path.exists() {
142-
let cmd = Command::new("umount")
143-
.arg(disk_mount_path)
144-
.output()
145-
.map_err(|e| anyhow!("Failed to umount disk image: {}", e));
146-
match cmd {
147-
Ok(cmd) => {
148-
if !cmd.status.success() {
149-
let e = anyhow!(
150-
"Failed to umount disk image: {}",
151-
String::from_utf8_lossy(&cmd.stderr)
152-
);
153-
if should_detach_loop_device {
154-
log::error!("{}", e);
155-
} else {
156-
return Err(e);
157-
}
158-
}
159-
}
160-
Err(e) => {
161-
if should_detach_loop_device {
162-
log::error!("{}", e);
163-
} else {
164-
return Err(e);
165-
}
166-
}
130+
if !cmd.status.success() {
131+
return Err(anyhow!(
132+
"Failed to umount disk image: {}",
133+
String::from_utf8_lossy(&cmd.stderr)
134+
));
167135
}
168-
}
169136

170-
if let Ok(loop_device) = loop_device {
171-
let loop_dev_path = loop_device.dev_path().cloned();
172-
173-
log::info!("Loop device going to detached: {:?}", loop_dev_path);
137+
let loop_device = LoopDeviceBuilder::new()
138+
.img_path(disk_img_path)
139+
.detach_on_drop(true)
140+
.build()?;
141+
// the loop device will be detached automatically when _loop_device is dropped
142+
log::trace!("Detaching {}", loop_device.dev_path());
143+
Ok(())
144+
} else {
145+
Err(anyhow!(
146+
"Disk image mount point does not exist: {}",
147+
disk_mount_path.display()
148+
))
174149
}
175-
176-
Ok(())
177150
}
178151

179152
/// Ensures the provided disk image path is not a device node.
@@ -196,13 +169,10 @@ fn create_partitioned_image(ctx: &DADKExecContext, disk_image_path: &PathBuf) ->
196169
let part_type = ctx.rootfs().partition.partition_type;
197170
DiskPartitioner::create_partitioned_image(disk_image_path, part_type)?;
198171
// 挂载loop设备
199-
let mut loop_device = LoopDeviceBuilder::new()
172+
let loop_device = LoopDeviceBuilder::new()
200173
.img_path(disk_image_path.clone())
201-
.build()
202-
.map_err(|e| anyhow!("Failed to create loop device: {}", e))?;
203-
loop_device
204-
.attach()
205-
.map_err(|e| anyhow!("creat: Failed to attach loop device: {}", e))?;
174+
.detach_on_drop(false)
175+
.build()?;
206176

207177
let partition_path = loop_device.partition_path(1)?;
208178
let fs_type = ctx.rootfs().metadata.fs_type;
@@ -261,15 +231,11 @@ pub fn show_mount_point(ctx: &DADKExecContext) -> Result<()> {
261231

262232
pub fn show_loop_device(ctx: &DADKExecContext) -> Result<()> {
263233
let disk_image_path = ctx.disk_image_path();
264-
let mut loop_device = LoopDeviceBuilder::new()
265-
.detach_on_drop(false)
234+
let loop_device = LoopDeviceBuilder::new()
266235
.img_path(disk_image_path)
236+
.detach_on_drop(false)
267237
.build()?;
268-
if let Err(e) = loop_device.attach_by_exists() {
269-
log::error!("Failed to attach loop device: {}", e);
270-
} else {
271-
println!("{}", loop_device.dev_path().unwrap());
272-
}
238+
println!("{}", loop_device.dev_path());
273239
Ok(())
274240
}
275241

0 commit comments

Comments
 (0)