{"id":87,"date":"2015-08-17T15:56:56","date_gmt":"2015-08-17T07:56:56","guid":{"rendered":"http:\/\/vmnote.104.es\/?p=87"},"modified":"2015-08-17T15:56:56","modified_gmt":"2015-08-17T07:56:56","slug":"libvirt-python-api","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2015\/08\/17\/libvirt-python-api\/","title":{"rendered":"\u4f7f\u7528Python API\u4f86\u64cd\u4f5clibvirt\u865b\u64ec\u6a5f\u4ecb\u9762"},"content":{"rendered":"<pre>\u4f7f\u7528Python\u4e5f\u53ef\u4ee5\u900f\u904elibvirt\u6240\u63d0\u4f9b\u7684API\u4f86\u8f15\u6613\u64cd\u4f5c\u865b\u64ec\u6a5f\uff0c\u6211\u5011\u5728\u9019\u88e1\u5148\u793a\u7bc4\u6700\u7c21\u55ae\u7684\u90e8\u4efd\uff0c\u900f\u904ePython\u4f7f\u7528libvirt\u6240\u63d0\u4f9b\u7684API\uff0c\u628a\u76ee\u524d\u7cfb\u7d71\u6b63\u5728\u57f7\u884c\u4e2d\u7684VM\u90fd\u5217\u51fa\u4f86\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a<\/pre>\n<p>[code lang=&#8221;python&#8221;]<br \/>\n#!\/usr\/bin\/python<br \/>\nimport libvirt<br \/>\nimport sys<br \/>\nconn = libvirt.open(&quot;qemu:\/\/\/system&quot;)<\/p>\n<p>for id in conn.listDomainsID():<br \/>\n  vm = conn.lookupByID(id)<br \/>\n  print vm.name()<br \/>\n[\/code]<\/p>\n<p>\u57f7\u884c\u4e4b\u524d\uff0c\u8acb\u5148\u628a\u6b64\u7a0b\u5f0f\u8a2d\u5b9a\u70ba\u53ef\u57f7\u884c\u6a94\uff0c\u7136\u5f8c\u76f4\u63a5\u57f7\u884c\u5373\u53ef\uff0c\u5982\u4e0b\u6240\u793a\uff08\u5047\u8a2d\u6b64\u7a0b\u5f0f\u540d\u70bavirlist.py\uff09\uff1a<\/p>\n<p>[code lang=&#8221;bash&#8221;]<br \/>\n$ chmod +x virlist.py<br \/>\n$ .\/virlist.py<br \/>\n[\/code]<\/p>\n<p>\u4f46\u662f\u8981\u80fd\u5920\u9806\u5229\u57f7\u884c\u6b64\u7a0b\u5f0f\u78bc\uff0c\u4f60\u7684\u7cfb\u7d71\u4e2d\u8981\u5b89\u88dd\u6709\u76f8\u95dc\u7684\u5957\u4ef6\u624d\u884c\u3002\u5047\u8a2d\u4f60\u4f7f\u7528\u7684\u662fUbuntu 14.04\uff0c\u5728\u57f7\u884c\u4e0b\u5217\u5b89\u88dd\u4e4b\u5f8c\u5c31\u597d\u4e86\uff1a<\/p>\n<p>[code lang=&#8221;bash&#8221;]<br \/>\n$ sudo apt-get update<br \/>\n$ sudo apt-get install -y aptitude<br \/>\n$ sudo aptitude -y install qemu-kvm libvirt-bin virtinst bridge-utils<br \/>\n[\/code]<\/p>\n<p>\u5982\u679c\u5305\u62ec\u6c92\u6709\u5728\u57f7\u884c\u7684VM\u4e5f\u8981\u5217\u51fa\u4f86\u7684\u8a71\uff0c\u90a3\u9ebc\u5c31\u8981\u6539\u7528\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>[code lang=&#8221;python&#8221;]<br \/>\n#! \/usr\/bin\/python<br \/>\nimport libvirt<br \/>\nimport sys<br \/>\nconn = libvirt.openReadOnly(&quot;qemu:\/\/\/system&quot;)<\/p>\n<p>print &quot;Running VMs:&quot;<br \/>\nfor id in conn.listDomainsID():<br \/>\n  vm = conn.lookupByID(id)<br \/>\n  print &quot;  ID:&quot; + str(id) + &quot;(&quot; + vm.name() + &quot;)&quot;<\/p>\n<p>print &quot;Inactive VMs:&quot;<br \/>\nfor domname in conn.listDefinedDomains():<br \/>\n  print &quot;  &quot; + domname<br \/>\nconn.close()<\/p>\n<p>[\/code]<\/p>\n<p>\u4ee5\u4e0b\u662f\u57f7\u884c\u7d50\u679c\uff1a<br \/>\n<a href=\"https:\/\/104.es\/wp-content\/uploads\/2015\/08\/2015-10-14_16-34-48.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-349 aligncenter\" src=\"https:\/\/104.es\/wp-content\/uploads\/2015\/08\/2015-10-14_16-34-48.png\" alt=\"2015-10-14_16-34-48\" width=\"652\" height=\"193\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2015\/08\/2015-10-14_16-34-48.png 652w, https:\/\/104.es\/wp-content\/uploads\/2015\/08\/2015-10-14_16-34-48-300x89.png 300w, https:\/\/104.es\/wp-content\/uploads\/2015\/08\/2015-10-14_16-34-48-600x178.png 600w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Python\u4e5f\u53ef\u4ee5\u900f\u904elibvirt\u6240\u63d0\u4f9b\u7684API\u4f86\u8f15\u6613\u64cd\u4f5c\u865b\u64ec\u6a5f\uff0c\u6211\u5011\u5728\u9019\u88e1\u5148\u793a\u7bc4\u6700\u7c21\u55ae\u7684\u90e8\u4efd\uff0c\u900f\u904ePython\u4f7f\u7528libvirt\u6240\u63d0\u4f9b\u7684API\uff0c\u628a\u76ee\u524d\u7cfb\u7d71\u4e2d\u7684VM\u90fd\u5217\u51fa\u4f86\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[97,15],"tags":[165],"class_list":["post-87","post","type-post","status-publish","format-standard","hentry","category-kvm-libvirt","category-python","tag-python-livbirt-api"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}