Convert from qcow2 to raw with Python

2019-06-14 03:52发布

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条回答
我想做一个坏孩纸
2楼-- · 2019-06-14 04:25

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])
查看更多
登录 后发表回答