{"id":369,"date":"2015-10-20T09:43:10","date_gmt":"2015-10-20T01:43:10","guid":{"rendered":"https:\/\/drho.club\/?p=369"},"modified":"2015-10-20T09:43:10","modified_gmt":"2015-10-20T01:43:10","slug":"operate-virtual-machine-methods","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2015\/10\/20\/operate-virtual-machine-methods\/","title":{"rendered":"\u5feb\u901f\u64cd\u4f5cVirtual Machine\u7684\u5e7e\u500b\u65b9\u6cd5"},"content":{"rendered":"<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u9664\u4e86\u76f4\u63a5\u64b0\u5beb\u539f\u751f\u7684C\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4ee5\u547c\u53eblibvirt API\u7684\u65b9\u5f0f\u4f86\u64cd\u4f5cVM\u4e4b\u5916\uff0c\u5176\u5be6\u6700\u7c21\u55ae\u7684\u65b9\u6cd5\u662f\u900f\u904evirsh\u6240\u63d0\u4f9b\u7684\u6307\u4ee4\u4f86\u5b8c\u6210\u3002\u5927\u90e8\u4efd\u7684\u5de5\u4f5c\u5176\u5be6\u4f7f\u7528virsh\u7684\u6307\u4ee4\u96c6\u90fd\u53ef\u4ee5\u505a\u5230\u3002virsh\u662f\u96a8\u9644\u5728libvirt package\u4e2d\u7528\u4f86\u5354\u52a9\u7ba1\u7406\u8005\u57f7\u884clibvirt API\u7684\u4e00\u7d44\u5de5\u5177\u7a0b\u5f0f\u96c6\uff0c\u53ef\u4ee5\u900f\u904e\u5b83\u7684\u6587\u5b57\u5f0f\u4ea4\u8ac7\u4ecb\u9762\u4f86\u5b8c\u6210\u5de5\u4f5c\uff0c\u4e5f\u53ef\u4ee5\u5728\u7d42\u7aef\u6a5f\u4e2d\u7684\u547d\u4ee4\u63d0\u793a\u5b57\u5143\u4f7f\u7528\u9019\u4e9b\u6307\u4ee4\u3002\u5b8c\u6574\u7684virsh command\u9023\u7d50\u5728\u6b64\uff1a<a href=\"http:\/\/libvirt.org\/sources\/virshcmdref\/html-single\/\" target=\"_blank\" rel=\"noopener\">http:\/\/libvirt.org\/sources\/virshcmdref\/html-single\/<\/a>\u3002\u4ee5\u4e0b\u662f\u7c21\u6613\u7684\u64cd\u4f5c\u904e\u7a0b\uff1a<\/p>\n<p>[code lang=&#8221;bash&#8221;]<br \/>\n[root@ncu-note python_libvirt]# virsh list &#8211;all<br \/>\n Id    \u540d\u7a31                         \u72c0\u614b<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n &#8211;     vm-cen                         \u95dc\u6a5f<br \/>\n &#8211;     vm-cen-clone                   \u95dc\u6a5f<br \/>\n &#8211;     winxp                          \u95dc\u6a5f<\/p>\n<p>[root@ncu-note python_libvirt]# virsh start vm-cen<br \/>\n\u5340\u57df vm-cen \u5df2\u958b\u555f<\/p>\n<p>[root@ncu-note python_libvirt]# virsh list &#8211;all<br \/>\n Id    \u540d\u7a31                         \u72c0\u614b<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n 2     vm-cen                         \u57f7\u884c\u4e2d<br \/>\n &#8211;     vm-cen-clone                   \u95dc\u6a5f<br \/>\n &#8211;     winxp                          \u95dc\u6a5f<\/p>\n<p>[root@ncu-note python_libvirt]#<br \/>\n[\/code]<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u7136\u800c\uff0c\u9664\u4e86virsh command\u4e4b\u5916\uff0c\u5176\u5be6\u900f\u904ePython\u7684\u4ea4\u8ac7\u5f0f\u4ecb\u9762\u4e5f\u53ef\u4ee5\u5b8c\u6210\u4e0a\u8ff0\u7684\u5de5\u4f5c\uff0c\u9664\u4e86\u76f4\u63a5\u64cd\u4f5c\u7684\u6307\u4ee4\u4e4b\u5916\uff0c\u56e0\u70baPython\u8a9e\u8a00\u672c\u8eab\u7684\u529f\u80fd\uff0c\u76f4\u5728\u63a5Python\u4e2d\u5c31\u53ef\u4ee5\u900f\u904e\u8b8a\u6578\u548c\u6d41\u7a0b\u63a7\u5236\u6307\u4ee4\uff0c\u9032\u884c\u66f4\u5b8c\u6574\u7684\u64cd\u4f5c\u3002<\/p>\n<p>[code lang=&#8221;python&#8221;]<br \/>\n[root@ncu-note python_libvirt]# python<br \/>\nPython 2.7.5 (default, Jun 24 2015, 00:41:19)<br \/>\n[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2<br \/>\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.<br \/>\n&gt;&gt;&gt; import libvirt<br \/>\n&gt;&gt;&gt; conn = libvirt.open(&quot;qemu:\/\/\/system&quot;)<br \/>\n&gt;&gt;&gt; doms = conn.listAllDomains()<br \/>\n&gt;&gt;&gt; for dom in doms:<br \/>\n&#8230;   print &quot;ID: &quot;, dom.ID(), &quot;, Name: &quot;, dom.name(), &quot;, UUID:&quot;, dom.UUIDString()<br \/>\n&#8230;<br \/>\nID:  2 , Name:  vm-cen , UUID: 74d93d74-089d-4426-be90-8e374a16bc53<br \/>\nID:  -1 , Name:  winxp , UUID: 406539b9-cb6a-4be1-9234-62674c20c159<br \/>\nID:  -1 , Name:  vm-cen-clone , UUID: f02fc808-8497-4010-87ac-c8cefbd03ac9<br \/>\n&gt;&gt;&gt;<br \/>\n[\/code]<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u53ea\u8981\u5728\u64cd\u4f5c\u4e4b\u524dimport libvirt\u5373\u53ef\u3002\u6240\u6709\u7684libvirt API\u5e7e\u4e4e\u90fd\u5df2\u88ab\u5305\u5728libvrt-python\u4e4b\u4e2d\uff0c\u4e5f\u56e0\u6b64\u5c31\u53ef\u4ee5\u900f\u904ePython\u7a0b\u5f0f\u78bc\u7684\u65b9\u5f0f\u4f86\u5148\u64cd\u4f5c\u770b\u770b\uff0c\u6c92\u6709\u554f\u984c\u518d\u628a\u5b83\u5beb\u7247.py\u7684\u7a0b\u5f0f\u78bc\u4e2d\u5373\u53ef\uff0c\u5c31\u50cf\u662f\u6211\u5011\u4e4b\u524d\u7684\u5e7e\u7bc7\u6587\u7ae0\u6240\u4ecb\u7d39\u7684\u4e00\u6a23\u3002\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9664\u4e86\u76f4\u63a5\u64b0\u5beb\u539f\u751f\u7684C\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4ee5\u547c\u53eblibvirt API\u7684\u65b9\u5f0f\u4f86\u64cd\u4f5cVM\u4e4b\u5916\uff0c\u5176\u5be6\u6700\u7c21\u55ae\u7684\u65b9\u6cd5\u662f\u900f\u904evirsh\u6240\u63d0\u4f9b\u7684\u6307\u4ee4\u4f86\u5b8c\u6210\u3002\u5927\u90e8\u4efd\u7684\u5de5\u4f5c\u5176\u5be6\u4f7f\u7528virsh\u7684\u6307\u4ee4\u96c6\u90fd\u53ef\u4ee5\u505a\u5230\u3002virsh\u662f\u96a8\u9644\u5728libvirt package\u4e2d\u7528\u4f86\u5354\u52a9\u7ba1\u7406\u8005\u57f7\u884clibvirt API\u7684\u4e00\u7d44\u5de5\u5177\u7a0b\u5f0f\u96c6\uff0c\u53ef\u4ee5\u900f\u904e\u5b83\u7684\u6587\u5b57\u5f0f\u4ea4\u8ac7\u4ecb\u9762\u4f86\u5b8c\u6210\u5de5\u4f5c\uff0c\u4e5f\u53ef\u4ee5\u5728\u7d42\u7aef\u6a5f\u4e2d\u7684\u547d\u4ee4\u63d0\u793a\u5b57\u5143\u4f7f\u7528\u9019\u4e9b\u6307\u4ee4\u3002\u5b8c\u6574\u7684virsh command\u9023\u7d50\u5728\u6b64\uff1ahttp:\/\/libvirt.org\/sources\/virshcmdref\/html-single\/\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[97,15,100],"tags":[142,164,188],"class_list":["post-369","post","type-post","status-publish","format-standard","hentry","category-kvm-libvirt","category-python","category-vm","tag-libvirt-python","tag-python-libvirt-program","tag-virsh-command"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/369","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=369"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/369\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}