Created
February 4, 2019 19:50
-
-
Save Wenzel/a8e9d7793634c0a71fa5d68dd695f8be to your computer and use it in GitHub Desktop.
Mount a qcow with libguestfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Mount qcow in libguestfs. | |
Usage: | |
mount_guestfs.py [options] <disk_path> | |
mount_guestfs.py (-h | --help) | |
Options: | |
-d --debug Enable debugging | |
-h --help Show this screen. | |
--version Show version. | |
""" | |
import logging | |
from docopt import docopt | |
import guestfs | |
from IPython import embed | |
def main(): | |
args = docopt(__doc__) | |
debug = args['--debug'] | |
disk = args['<disk_path>'] | |
log_level = logging.INFO | |
if debug: | |
log_level = logging.DEBUG | |
logging.basicConfig(level=log_level) | |
g = guestfs.GuestFS(python_return_dict=True) | |
logging.info('attaching disk') | |
g.add_drive_opts(disk, readonly=1) | |
logging.info('launching libguestfs back-end') | |
g.launch() | |
logging.info('inspecting operating systems') | |
roots = g.inspect_os() | |
root = roots[0] | |
logging.info('get mountpoints') | |
mps = g.inspect_get_mountpoints(root) | |
logging.info('mounting filesystem') | |
for k,v in mps.items(): | |
g.mount_ro(v,k) | |
embed() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment