You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
4.5 KiB
HTML
108 lines
4.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="description" content="">
|
|
<meta name="keywords" content="Blog, articles, guide, cheatsheet, virsh, virtual machine">
|
|
<meta name="author" content="Nathan (Aney) Steel">
|
|
<meta name="theme-color" content="white">
|
|
<meta name="theme-color" content="black">
|
|
<link rel="stylesheet" type="text/css" href="/main.css">
|
|
<link rel="icon" type="image/png" href="/images/favicon.svg">
|
|
<title>Virsh Cheatsheet</title>
|
|
</head>
|
|
|
|
<body>
|
|
<header>
|
|
<h1>Virsh Cheatsheet</h1>
|
|
<hr/>
|
|
<nav>
|
|
<a href="/">home</a>
|
|
<a href="/equipment.html">equipment</a>
|
|
<a href="/projects.html">projects</a>
|
|
<a href="/blog/">blog</a>
|
|
<a href="/sitemap.html">misc</a>
|
|
<a href="/support.html">support</a>
|
|
</nav>
|
|
<hr/>
|
|
</header>
|
|
|
|
<main>
|
|
<section>
|
|
<p class="intro">Virsh is an extremely powerful tool for managing KVM/QEMU virtual machines. From restarting, to changing hardware, snapshotting, and cloning machines. I'll cover the basics of Virsh here, as it's all I personally use.</p>
|
|
<h2>List VMs</h2>
|
|
<pre><code>virsh list</code></pre>
|
|
<p>List all, including offline vms</p>
|
|
<pre><code>virsh list --all</code></pre>
|
|
|
|
<h2>Start/Stop</h2>
|
|
<pre><code>virsh start $vm</code></pre>
|
|
<pre><code>virsh shutdown $vm</code></pre>
|
|
<pre><code>virsh reboot $vm</code></pre>
|
|
<p>If the VM refuses to shutdown, etc. destroy performs an ungraceful shutdown</p>
|
|
<pre><code>virsh destroy $vm</code></pre>
|
|
|
|
<h3>Autostart</h3>
|
|
<pre><code>virsh autostart $vm</code></pre>
|
|
<pre><code>virsh autostart $vm --disable</code></pre>
|
|
|
|
<h2>Rename</h2>
|
|
<pre><code>virsh domrename $vm $new_name</code></pre>
|
|
|
|
<h2>Delete</h2>
|
|
<pre><code>virsh undefine $vm</code></pre>
|
|
<h3>Delete Snapshots</h3>
|
|
<p>If your VM has Snapshots it won't delete as simply, so delete those first<p>
|
|
<p>List the snapshots</p>
|
|
<pre><code>virsh snapshot-list --domain $vm</code></pre>
|
|
<p>And delete each snapshot with the following</p>
|
|
<pre><code>virsh snapshot-delete --domain $vm --snapshotname $snapshot</code></pre>
|
|
<h3>Deleting with all storage</h3>
|
|
<p>Delete the VM along with all the virtual storage</p>
|
|
<pre><code>virsh undefine --domain $vm --remove-all-storage</code></pre>
|
|
|
|
<h2>Snapshots</h2>
|
|
<h3>Create</h3>
|
|
<p>Save a snapshot of the VMs current state</p>
|
|
<pre><code>virsh snapshot-create-as --domain $vm --name "$snapshot_name"</code></pre>
|
|
<h3>Restore/Revert</h3>
|
|
<p>Revert the VM to the state it was in during the snapshot</p>
|
|
<pre><code>virsh snapshot-revert $vm $snapshot_name</code></pre>
|
|
<h3>Delete Snapshot</h3>
|
|
<p>Delete the snapshot, this doesn't delete anything else related to the VM</p>
|
|
<pre><code>virsh snapshot-delete --domain $vm --snapshotname $snapshot_name</code></pre>
|
|
|
|
<h2>(TODO)Drive management</h2>
|
|
|
|
<h2>Change Memory</h2>
|
|
<p>In variantions of 512M, 1G, etc</p>
|
|
<pre><code>virsh setmem $vm $ram</code></pre>
|
|
<p>The amount of RAM the VM has assigned to it, this cannot be higher than the max, but can be altered on the fly (if I recall correctly).</p>
|
|
<pre><code>virsh setmaxmem $vm $ram</code></pre>
|
|
<p>The max mem sets the maximum amount of RAM the VM can use, and can only be set whilst the VM is offline</p>
|
|
<p>My recommendation here is to set a higher maxmem than you'd need, so if you do need to add some more RAM, it doesn't require any downtime.<p>
|
|
|
|
<h2>Change vCPU cores</h2>
|
|
<p>This is a little more tricky, as it involves editing the XML file</p>
|
|
<pre><code>virsh edit $vm</code></pre>
|
|
<p>Then edit the vcpus section, change between the tags</p>
|
|
<pre><code><vcpu placement='static'>$vcpus</vcpu></code></pre>
|
|
|
|
<h2>Connect via serial/console</h2>
|
|
<p>This is a means to connect to your VMs via terminal</p>
|
|
<pre><code>virsh console $vm</code></pre>
|
|
<p>To do this you will likely need to first run the following command on the VM itself. This won't be required if you created the VM with console, but best to double check.</p>
|
|
<pre><code>systemctl enable serial-getty@ttyS0.service</code></pre>
|
|
|
|
</section>
|
|
</main>
|
|
|
|
<footer>
|
|
<hr/>
|
|
<p>Written by <a href="http://aney.co.uk" target="_blank" rel="noopener">@aney</a> with <a href="https://danluu.com/web-bloat/" target="_blank" rel="noopener">web bloat</a> in mind | <a href="https://github.com/Aney/website" target="_blank" rel="noopener">Source Code</a></p>
|
|
</footer>
|
|
</body>
|
|
</html>
|
|
|