Skip to content

Commit be4e8fb

Browse files
committed
fix(rootfs): better resource management
1 parent ab20161 commit be4e8fb

File tree

2 files changed

+229
-232
lines changed

2 files changed

+229
-232
lines changed

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

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ fn mount_partitioned_image(
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,34 @@ 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();
129-
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-
}
135-
136-
should_detach_loop_device = loop_device.attached();
137-
} else {
138-
should_detach_loop_device = false;
139-
}
140-
124+
141125
if disk_mount_path.exists() {
126+
log::trace!("Unmounted disk image at {}", disk_mount_path.display());
127+
142128
let cmd = Command::new("umount")
143129
.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+
.output()?;
131+
132+
if !cmd.status.success() {
133+
return Err(anyhow!(
134+
"Failed to umount disk image: {}",
135+
String::from_utf8_lossy(&cmd.stderr)
136+
));
167137
}
168-
}
169-
170-
if let Ok(loop_device) = loop_device {
171-
let loop_dev_path = loop_device.dev_path().cloned();
172138

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

179154
/// Ensures the provided disk image path is not a device node.
@@ -198,11 +173,8 @@ fn create_partitioned_image(ctx: &DADKExecContext, disk_image_path: &PathBuf) ->
198173
// 挂载loop设备
199174
let mut loop_device = LoopDeviceBuilder::new()
200175
.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))?;
176+
.detach_on_drop(false)
177+
.build()?;
206178

207179
let partition_path = loop_device.partition_path(1)?;
208180
let fs_type = ctx.rootfs().metadata.fs_type;
@@ -261,15 +233,11 @@ pub fn show_mount_point(ctx: &DADKExecContext) -> Result<()> {
261233

262234
pub fn show_loop_device(ctx: &DADKExecContext) -> Result<()> {
263235
let disk_image_path = ctx.disk_image_path();
264-
let mut loop_device = LoopDeviceBuilder::new()
265-
.detach_on_drop(false)
236+
let loop_device = LoopDeviceBuilder::new()
266237
.img_path(disk_image_path)
238+
.detach_on_drop(false)
267239
.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-
}
240+
println!("{}", loop_device.dev_path());
273241
Ok(())
274242
}
275243

0 commit comments

Comments
 (0)