1
1
use std:: collections:: HashMap ;
2
2
use std:: env;
3
3
use std:: path:: Path ;
4
- use std:: process:: Command ;
4
+ use std:: path:: PathBuf ;
5
+ use std:: process:: { Command , Stdio } ;
5
6
use std:: sync:: OnceLock ;
6
7
7
8
use anyhow:: { anyhow, Context , Result } ;
8
- use camino:: Utf8Path ;
9
- use camino:: Utf8PathBuf ;
9
+ use camino:: { Utf8Path , Utf8PathBuf } ;
10
10
use fn_error_context:: context;
11
11
use regex:: Regex ;
12
12
use serde:: Deserialize ;
@@ -217,13 +217,23 @@ impl LoopbackDevice {
217
217
let dev = Utf8PathBuf :: from ( dev. trim ( ) ) ;
218
218
tracing:: debug!( "Allocated loopback {dev}" ) ;
219
219
220
- // Try to spawn cleanup helper process - if it fails, make it fatal
221
- let cleanup_handle = Self :: spawn_cleanup_helper ( dev. as_str ( ) )
222
- . context ( "Failed to spawn loopback cleanup helper" ) ?;
220
+ // Try to spawn cleanup helper, but don't fail if it doesn't work
221
+ let cleanup_handle = match Self :: spawn_cleanup_helper ( dev. as_str ( ) ) {
222
+ Ok ( handle) => Some ( handle) ,
223
+ Err ( e) => {
224
+ tracing:: warn!(
225
+ "Failed to spawn loopback cleanup helper for {}: {}. \
226
+ Loopback device may not be cleaned up if process is interrupted.",
227
+ dev,
228
+ e
229
+ ) ;
230
+ None
231
+ }
232
+ } ;
223
233
224
234
Ok ( Self {
225
235
dev : Some ( dev) ,
226
- cleanup_handle : Some ( cleanup_handle ) ,
236
+ cleanup_handle,
227
237
} )
228
238
}
229
239
@@ -236,14 +246,12 @@ impl LoopbackDevice {
236
246
/// Spawn a cleanup helper process that will clean up the loopback device
237
247
/// if the parent process dies unexpectedly
238
248
fn spawn_cleanup_helper ( device_path : & str ) -> Result < LoopbackCleanupHandle > {
239
- use std:: process:: { Command , Stdio } ;
240
-
241
- // Get the path to our own executable
242
- let self_exe =
243
- std:: fs:: read_link ( "/proc/self/exe" ) . context ( "Failed to read /proc/self/exe" ) ?;
249
+ // Try multiple strategies to find the bootc binary
250
+ let bootc_path = Self :: find_bootc_binary ( )
251
+ . context ( "Failed to locate bootc binary for cleanup helper" ) ?;
244
252
245
253
// Create the helper process
246
- let mut cmd = Command :: new ( self_exe ) ;
254
+ let mut cmd = Command :: new ( bootc_path ) ;
247
255
cmd. args ( [ "loopback-cleanup-helper" , "--device" , device_path] ) ;
248
256
249
257
// Set environment variable to indicate this is a cleanup helper
@@ -252,7 +260,7 @@ impl LoopbackDevice {
252
260
// Set up stdio to redirect to /dev/null
253
261
cmd. stdin ( Stdio :: null ( ) ) ;
254
262
cmd. stdout ( Stdio :: null ( ) ) ;
255
- cmd . stderr ( Stdio :: null ( ) ) ;
263
+ // Don't redirect stderr so we can see error messages
256
264
257
265
// Spawn the process
258
266
let child = cmd
@@ -262,6 +270,44 @@ impl LoopbackDevice {
262
270
Ok ( LoopbackCleanupHandle { child } )
263
271
}
264
272
273
+ /// Find the bootc binary using multiple strategies
274
+ fn find_bootc_binary ( ) -> Result < PathBuf > {
275
+ // Strategy 1: Try /proc/self/exe (works in most cases)
276
+ if let Ok ( exe_path) = std:: fs:: read_link ( "/proc/self/exe" ) {
277
+ if exe_path. exists ( ) {
278
+ return Ok ( exe_path) ;
279
+ } else {
280
+ tracing:: warn!( "/proc/self/exe points to non-existent path: {:?}" , exe_path) ;
281
+ }
282
+ } else {
283
+ tracing:: warn!( "Failed to read /proc/self/exe" ) ;
284
+ }
285
+
286
+ // Strategy 2: Try argv[0] from std::env
287
+ if let Some ( argv0) = std:: env:: args ( ) . next ( ) {
288
+ let argv0_path = PathBuf :: from ( argv0) ;
289
+ if argv0_path. is_absolute ( ) && argv0_path. exists ( ) {
290
+ return Ok ( argv0_path) ;
291
+ }
292
+ // If it's relative, try to resolve it
293
+ if let Ok ( canonical) = argv0_path. canonicalize ( ) {
294
+ return Ok ( canonical) ;
295
+ }
296
+ }
297
+
298
+ // Strategy 3: Try common installation paths
299
+ let common_paths = [ "/usr/bin/bootc" , "/usr/local/bin/bootc" ] ;
300
+
301
+ for path in & common_paths {
302
+ let path_buf = PathBuf :: from ( path) ;
303
+ if path_buf. exists ( ) {
304
+ return Ok ( path_buf) ;
305
+ }
306
+ }
307
+
308
+ anyhow:: bail!( "Could not locate bootc binary using any available strategy" )
309
+ }
310
+
265
311
// Shared backend for our `close` and `drop` implementations.
266
312
fn impl_close ( & mut self ) -> Result < ( ) > {
267
313
// SAFETY: This is the only place we take the option
@@ -272,7 +318,7 @@ impl LoopbackDevice {
272
318
273
319
// Kill the cleanup helper since we're cleaning up normally
274
320
if let Some ( mut cleanup_handle) = self . cleanup_handle . take ( ) {
275
- // Send SIGTERM to the child process
321
+ // Send SIGTERM to the child process and let it do the cleanup
276
322
let _ = cleanup_handle. child . kill ( ) ;
277
323
}
278
324
@@ -311,22 +357,32 @@ pub async fn run_loopback_cleanup_helper(device_path: &str) -> Result<()> {
311
357
. await ;
312
358
313
359
// Clean up the loopback device
314
- let status = std:: process:: Command :: new ( "losetup" )
360
+ let output = std:: process:: Command :: new ( "losetup" )
315
361
. args ( [ "-d" , device_path] )
316
- . status ( ) ;
362
+ . output ( ) ;
317
363
318
- match status {
319
- Ok ( exit_status ) if exit_status . success ( ) => {
364
+ match output {
365
+ Ok ( output ) if output . status . success ( ) => {
320
366
// Log to systemd journal instead of stderr
321
367
tracing:: info!( "Cleaned up leaked loopback device {}" , device_path) ;
322
368
std:: process:: exit ( 0 ) ;
323
369
}
324
- Ok ( _) => {
325
- tracing:: error!( "Failed to clean up loopback device {}" , device_path) ;
370
+ Ok ( output) => {
371
+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
372
+ tracing:: error!(
373
+ "Failed to clean up loopback device {}: {}. Stderr: {}" ,
374
+ device_path,
375
+ output. status,
376
+ stderr. trim( )
377
+ ) ;
326
378
std:: process:: exit ( 1 ) ;
327
379
}
328
380
Err ( e) => {
329
- tracing:: error!( "Error cleaning up loopback device {}: {}" , device_path, e) ;
381
+ tracing:: error!(
382
+ "Error executing losetup to clean up loopback device {}: {}" ,
383
+ device_path,
384
+ e
385
+ ) ;
330
386
std:: process:: exit ( 1 ) ;
331
387
}
332
388
}
0 commit comments