使用libvirt抓取linux kvm虚拟机的缩略图

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

import libvirt
import os
import uuid
try:
    from PIL import Image
    print("PIL")
except ImportError:
    import Image
def handler(stream, buf, opaque):
    fd = opaque
    os.write(fd, buf)
THUMBNAIL_SIZE =(256, 256)
thumbnail = '/home/hcc/test/screenshot/test-' + str(uuid.uuid4())
command = "touch " + thumbnail
print(command)
os.system(command)
fd = os.open(thumbnail, os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0644)
try:
    conn = libvirt.open('qemu:///system')
    d1 = conn.lookupByName('test')
    print(d1.info())
    print(d1.name())
    stream = conn.newStream(0)
    d1.screenshot(stream, 0, 0)
    stream.recvAll(handler, fd)
    if os.path.getsize(thumbnail) == 0:
        image = Image.new("RGB", THUMBNAIL_SIZE, 'black')
        image.save(thumbnail)
    else:
        print("else")
        im = Image.open(thumbnail)
        im.thumbnail(THUMBNAIL_SIZE)
        im.save(thumbnail,'PNG')
except libvirt.libvirtError:
    try:
        stream.abor()
    except:
        pass
else:
    stream.finish()
finally:
    os.close(fd)