Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The makeimage parser needs to unregister the loop device on exit. #123

Open
jonesmz opened this issue Dec 4, 2021 · 2 comments
Open

The makeimage parser needs to unregister the loop device on exit. #123

jonesmz opened this issue Dec 4, 2021 · 2 comments

Comments

@jonesmz
Copy link
Contributor

jonesmz commented Dec 4, 2021

Failing to do that leaves the loop device setup perpetually if a problem happens.

@jonesmz
Copy link
Contributor Author

jonesmz commented May 28, 2022

In a bash script, we would do this as a trap that calls a function on exit.

I'm not sure how to accomplish it in python though.

@samip5
Copy link
Contributor

samip5 commented Jan 13, 2024

We could probably do it eg:

Using atexit module (not part of the standard libraries):

import atexit

def cleanup():
    # Detach the loop device associated with the image file
    system(f"losetup --detach {LOOP}")

# Register the cleanup function to be called on exit
atexit.register(cleanup)

# ... (rest of your script)

or by using signals:

import signal

def cleanup(signum, frame):
    # Detach the loop device associated with the image file
    system(f"losetup --detach {LOOP}")
    sys.exit(0)

# Register the signal handler for SIGTERM and SIGINT
signal.signal(signal.SIGTERM, cleanup)
signal.signal(signal.SIGINT, cleanup)

# ... (rest of your script)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: To do
Development

No branches or pull requests

2 participants