{"id":198,"date":"2017-07-18T16:27:52","date_gmt":"2017-07-18T16:27:52","guid":{"rendered":"http:\/\/info.ffteixeira.net\/2017\/07\/18\/kvm-virsh\/"},"modified":"2017-07-18T16:27:52","modified_gmt":"2017-07-18T16:27:52","slug":"kvm-virsh","status":"publish","type":"post","link":"https:\/\/blog.ffteixeira.net\/?p=198","title":{"rendered":"KVM\/Virsh"},"content":{"rendered":"<p class=\"line862\">You can create, delete, run, stop, and manage your virtual machines from the command line, using a tool called <em>virsh<\/em>. <em>Virsh<\/em> is particularly useful for advanced Linux administrators, interested in script or automating some aspects of managing their virtual machines<\/p>\n<p class=\"line867\">&nbsp;<\/p>\n<h3 id=\"Installing\">Installing<\/h3>\n<p class=\"line862\">Install <em>virsh<\/em>:<\/p>\n<pre>\nsudo apt-get install libvirt-bin<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<h3 id=\"Connecting\">Connecting<\/h3>\n<p class=\"line874\">Connect to your hypervisor. This can be local, or even remote. In most cases, if you want to manage VMs running on the local hypervisor:<\/p>\n<pre>\n$ virsh connect qemu:\/\/\/system\nConnecting to uri: qemu:\/\/\/system<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<h3 id=\"Listing_VMs\">Listing VMs<\/h3>\n<p class=\"line867\">&nbsp;<\/p>\n<pre>\n$ virsh list\n Id Name                 State\n----------------------------------\n  1 foo                  running<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<h3 id=\"Creating_a_Virtual_Machine\">Creating a Virtual Machine<\/h3>\n<p class=\"line862\">Virtual Machines managed by <em>virsh<\/em> are created by describing the virtual machine in a <em>libvirt<\/em> XML file, and importing that XML file into <em>virsh<\/em>.<\/p>\n<p class=\"line874\">You can export the XML of an existing virtual machine:<\/p>\n<pre>\n$ virsh dumpxml foo &gt; \/tmp\/foo.xml\nConnecting to uri: qemu:\/\/\/system<\/pre>\n<p class=\"line862\">And then edit <tt>\/tmp\/foo.xml<\/tt>, which should be rather straightforward. For more information about <em>libvirt<\/em> XML format, see:<\/p>\n<ul>\n<li>\n<p class=\"line891\"><a class=\"http\" href=\"http:\/\/libvirt.org\/format.html\">http:\/\/libvirt.org\/format.html<\/a><\/p>\n<\/li>\n<\/ul>\n<p class=\"line874\">Once you have an XML file describing the new virtual machine you want to create, import it into virsh, and run it immediately:<\/p>\n<pre>\n$ virsh create \/tmp\/foo_new.xml \nConnecting to uri: qemu:\/\/\/system\nDomain foo_new created from \/tmp\/foo_new.xml\n$ virsh list\nConnecting to uri: qemu:\/\/\/system\n Id Name                 State\n----------------------------------\n  3 foo_new              running<\/pre>\n<p class=\"line874\">Alternatively, if you want to define it, but not run it, you could have used:<\/p>\n<pre>\n$ virsh define \/tmp\/foo_new.xml<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<h3 id=\"Working_with_a_Running_Virtual_Machine\">Working with a Running Virtual Machine<\/h3>\n<p class=\"line874\">Once a virtual machine is running, you can manage it in many different ways, such as:<\/p>\n<pre>\n$ virsh start foo<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<pre>\n$ virsh reboot foo<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<pre>\n$ virsh shutdown foo<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<pre>\n$ virsh suspend foo<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<pre>\n$ virsh resume foo<\/pre>\n<p class=\"line862\">You can also affect the memory, dynamically attach devices, interfaces, modify the networking configuration, etc. This guide in this wiki page is clearly not comprehensive. For a complete description of <em>virsh<\/em> commands, see:<\/p>\n<pre>\n$ man virsh<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<h4 id=\"Console\">Console<\/h4>\n<p class=\"line874\">Sometimes, it&#039;s useful to attach to the console of a running VM, to obtain debugging information, etc.<\/p>\n<pre>\n$ virsh console foo\nConnected to domain foo\nEscape character is ^]<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<h4 id=\"Details\">Details<\/h4>\n<p class=\"line874\">To view the details about a particular virtual machine:<\/p>\n<pre>\n$ virsh dumpxml foo<\/pre>\n<p class=\"line874\">These can be saved to a file, modified, and imported again using:<\/p>\n<pre>\n$ virsh define foo<\/pre>\n<p class=\"line867\">&nbsp;<\/p>\n<h3 id=\"Deleting_a_Virtual_Machine\">Deleting a Virtual Machine<\/h3>\n<p class=\"line874\">To delete a virtual machine, first terminate it (if running), and then undefine it:<\/p>\n<pre>\n$ virsh destroy foo_new\n$ virsh undefine foo_new<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/help.ubuntu.com\/community\/KVM\/Virsh\" target=\"_blank\" rel=\"noopener noreferrer\">Credits<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can create, delete, run, stop, and manage your virtual machines from the command line, using a tool called virsh. Virsh is particularly useful for advanced Linux administrators, interested in script or automating some aspects of managing their virtual machines &hellip; <a href=\"https:\/\/blog.ffteixeira.net\/?p=198\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-198","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts\/198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=198"}],"version-history":[{"count":0,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}