Python 调用libguestfs 实现文件注入(Openstack)
1)导入相关模块(openstack已扩展完毕,我们只需调用即可)
a)from nova.virt.disk.vfs importapi as vfs
b)from nova.virt.disk import api as disk
2)设置相关变量
a)p_w_picpath ='/data0/public/instances/b2cc2766-72cf-45d2-852e-801a9b9ecb73/disk.local'
b)fmt='qcow2'
c)partition=1
3)调用vfs.VFS,打开IMG
a)fs =vfs.VFS.instance_for_p_w_picpath(p_w_picpath, fmt, partition)
b)fs.setup()
4)注入相关数据信息
a)fps =open('/etc/sysconfig/network-scripts/ifcfg-em2').readlines()
b)fp =open('/etc/sysconfig/network-scripts/ifcfg-em1').readlines()
c)for i in fps:
i.fs.append_file("/ifcfg-em1",i)
d)for i in fp:
i.fs.append_file("/ifcfg-em2",i)
e)写入完毕
5)VFS 相关模块
a)['__class__', '__delattr__','__dict__', '__doc__', '__format__', '__getattribute__', '__hash__','__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__','__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__','_canonicalize_path', 'append_file', 'handle', 'has_file', 'imgfile', 'imgfmt','instance_for_p_w_picpath', 'make_path', 'partition', 'read_file', 'replace_file','set_ownership', 'set_permissions', 'setup', 'setup_os', 'setup_os_inspect','setup_os_root', 'setup_os_static', 'teardown']