使用Python从qcow2转换为raw [英] Convert from qcow2 to raw with Python

查看:132
本文介绍了使用Python从qcow2转换为raw的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Python将qcow2图片文件转换为原始图片文件?

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

我知道 qemu-img ,但我很好奇任何Python库,可能允许我避免要求我的用户安装该工具。它不包装与默认Fedora安装,这是我正在开发的。如果没有其他选项,我会使用 qemu-img

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.

推荐答案

似乎 qemu-img 是将qcow2图像文件转换为原始图像的必要条件。我没有找到一个解决方案,避免调用这个工具。这不是一个大问题,因为 qemu-img 在distros的存储库中广泛可用,并且有时与distros打包。为了在Python中使用这个工具,只需确保它安装到系统,然后通过 subprocess 模块以编程方式调用它,如下:

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])

这篇关于使用Python从qcow2转换为raw的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆