Convert from qcow2 to raw with Python

懵懂的女人 提交于 2019-12-23 05:38:07

问题


How can I use Python to convert a qcow2 image file into a raw image file?

I know of qemu-img, but I'm curious about any Python libraries that might allow me to avoid asking my users to install that tool. It's not packaged with a default Fedora install, and that's what I'm developing for. If there are no other options however, I'll use qemu-img.


回答1:


It seems that qemu-img is a necessity for converting qcow2 image files to raw images. I did not find a solution that avoided calling on this tool. This isn't a big issue though, because qemu-img is widely available in distros' repositories, and is sometimes packaged with distros. In order to make use of this tool in Python, simply ensure that it's installed to the system and then call it programmatically via the subprocess module, like so:

import subprocess

# Assuming file_path is the path to a local qcow2 file
if file_path.endswith('.qcow2'):
    raw_file_path = file_path[:5] + '.raw'
    subprocess.call(['qemu-img', 'convert', file_path, raw_file_path])


来源:https://stackoverflow.com/questions/24069417/convert-from-qcow2-to-raw-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!