guestfs-python

概要

import guestfs
 g = guestfs.GuestFS(python_return_dict=True)
 g.add_drive_opts("disk.img", format="raw", readonly=1)
 g.launch()
g = guestfs.GuestFS(python_return_dict=True)

这表明您的程序希望接收返回哈希表的API中的方法的Python dicts。

在libguestfs的未来版本中,这将成为默认版本。

import guestfs    # python加载guestfs库
output = "disk.img" 
g = guestfs.GuestFS(python_return_dict=True)   # 创建一个GuestFS实例
g.add_drive_opts("",format="qcow2",readonly=0)  # 挂载img到libguestfs
g.launch()        # 后台运行libguestfs
 partitions = g.list_partitions()             #  获取所有分区
 g.mount(partitions[1],'/')                    # 第二个分区挂载到根目录
 g.upload("/etc/resolv.conf""/etc/resolv.conf")     # g.upload("src文件", "dst文件"),上传文件
 g.close()        # 虽然会自动close,还是再手工close最保险



g.mkfs("ext4", partitions[0])    #在分区上创建文件系统。

 

# Create some files and directories.
 g.touch("/empty")
 message = "Hello, world
"
 g.write("/hello", message)
 g.mkdir("/foo")
 
 # This one uploads the local file /etc/resolv.conf into
 # the disk image.
 g.upload("/etc/resolv.conf", "/foo/resolv.conf")
 
 # Because we wrote to the disk and we want to detect write
 # errors, call g.shutdown.  You don't need to do this:
 # g.close will do it implicitly.
 g.shutdown()
 
 # Note also that handles are automatically closed if they are
 # reaped by reference counting.  You only need to call close
 # if you want to close the handle right away.
 g.close()

自行翻译。。
 
原文地址:https://www.cnblogs.com/heitaoq/p/9473062.html