<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yero Bâ</title>
    <description>The latest articles on DEV Community by Yero Bâ (@yero_b_89f2e1eb3c39628b6).</description>
    <link>https://dev.to/yero_b_89f2e1eb3c39628b6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3011512%2F0d4455bb-98a9-49b1-94da-d4d872e81fa2.jpg</url>
      <title>DEV Community: Yero Bâ</title>
      <link>https://dev.to/yero_b_89f2e1eb3c39628b6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yero_b_89f2e1eb3c39628b6"/>
    <language>en</language>
    <item>
      <title>Virtual Machine Snapshot</title>
      <dc:creator>Yero Bâ</dc:creator>
      <pubDate>Wed, 28 May 2025 07:43:43 +0000</pubDate>
      <link>https://dev.to/yero_b_89f2e1eb3c39628b6/virtual-machine-snapshot-2hen</link>
      <guid>https://dev.to/yero_b_89f2e1eb3c39628b6/virtual-machine-snapshot-2hen</guid>
      <description>&lt;h1&gt;
  
  
  Understanding QEMU/KVM Snapshots: A Developer's Guide
&lt;/h1&gt;

&lt;p&gt;Snapshots are one of the most powerful features in virtualization, allowing you to capture and restore VM states instantly. But they're also one of the most misunderstood. Let's dive into what they are, when to use them, and how to avoid common pitfalls.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is a Snapshot?
&lt;/h2&gt;

&lt;p&gt;A snapshot preserves the complete state of a virtual machine at a specific moment in time. This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;VM memory state&lt;/li&gt;
&lt;li&gt;Disk state
&lt;/li&gt;
&lt;li&gt;Configuration settings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;QEMU/KVM leverages the qcow2 disk format to track changes efficiently using copy-on-write technology.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Snapshots
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Internal Snapshots
&lt;/h3&gt;

&lt;p&gt;Stored directly within the qcow2 disk image file, containing both disk and VM state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example workflow:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Day 0&lt;/strong&gt;: Fresh Ubuntu VM in &lt;code&gt;myvm.qcow2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Day 1&lt;/strong&gt;: Create snapshot named &lt;code&gt;before-updates&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Day 2&lt;/strong&gt;: Install updates and applications&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Options&lt;/strong&gt;: Rollback to Day 1 state or delete snapshot to save space&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  External Snapshots
&lt;/h3&gt;

&lt;p&gt;Created as separate files that track only changes after the snapshot point.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example workflow:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Day 0&lt;/strong&gt;: Base image &lt;code&gt;myvm.qcow2&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Day 1&lt;/strong&gt;: Create external snapshot &lt;code&gt;snap.qcow2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Day 2&lt;/strong&gt;: All changes saved to &lt;code&gt;snap.qcow2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cleanup&lt;/strong&gt;: Merge snapshot back to base or lose changes if deleted&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Critical Warning: Snapshots ≠ Backups
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Snapshots are NOT backups!&lt;/strong&gt; Here's why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dependent on the original disk image&lt;/li&gt;
&lt;li&gt;If base image corrupts, all snapshots become unusable&lt;/li&gt;
&lt;li&gt;Designed for temporary use only&lt;/li&gt;
&lt;li&gt;Increase risk of data loss when used as sole recovery method&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pros and Cons
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Speed&lt;/strong&gt;: Nearly instantaneous creation and restoration&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing safety&lt;/strong&gt;: Perfect for experimenting with system changes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility&lt;/strong&gt;: Multiple restore points for different configurations&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency&lt;/strong&gt;: Uses sparse allocation to minimize space usage&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Disadvantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Performance impact&lt;/strong&gt;: Each active snapshot adds I/O overhead&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Growing disk usage&lt;/strong&gt;: Snapshots expand as changes accumulate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chain dependency&lt;/strong&gt;: Corruption affects all subsequent snapshots&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Management complexity&lt;/strong&gt;: Long chains become difficult to maintain&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to Use Snapshots
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Good Use Cases
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Software testing and updates&lt;/li&gt;
&lt;li&gt;Security testing with potentially malicious software&lt;/li&gt;
&lt;li&gt;Training environments that need consistent reset points&lt;/li&gt;
&lt;li&gt;Development sandboxes with easy rollback&lt;/li&gt;
&lt;li&gt;Before applying system patches&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Bad Use Cases
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Production database servers&lt;/li&gt;
&lt;li&gt;Long-running production VMs&lt;/li&gt;
&lt;li&gt;Long-term data preservation&lt;/li&gt;
&lt;li&gt;Mission-critical systems&lt;/li&gt;
&lt;li&gt;As your only recovery strategy&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Managing Snapshots
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Command Line Operations
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Create internal snapshot:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;virsh snapshot-create-as &lt;span class="nt"&gt;--domain&lt;/span&gt; vm_name &lt;span class="nt"&gt;--name&lt;/span&gt; &lt;span class="s2"&gt;"snapshot_name"&lt;/span&gt; &lt;span class="nt"&gt;--description&lt;/span&gt; &lt;span class="s2"&gt;"Description"&lt;/span&gt; &lt;span class="nt"&gt;--atomic&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create external snapshot:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;virsh snapshot-create-as &lt;span class="nt"&gt;--domain&lt;/span&gt; vm_name &lt;span class="nt"&gt;--name&lt;/span&gt; &lt;span class="s2"&gt;"snapshot_name"&lt;/span&gt; &lt;span class="nt"&gt;--disk-only&lt;/span&gt; &lt;span class="nt"&gt;--atomic&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;List snapshots:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;virsh snapshot-list vm_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Restore snapshot:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;virsh snapshot-revert vm_name snapshot_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Delete snapshot:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;virsh snapshot-delete vm_name snapshot_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using Virt-Manager GUI
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open Virt-Manager and select your VM&lt;/li&gt;
&lt;li&gt;Click "Show virtual hardware details"&lt;/li&gt;
&lt;li&gt;Select View → Snapshots&lt;/li&gt;
&lt;li&gt;Use the + button to create new snapshots&lt;/li&gt;
&lt;li&gt;Select and click "Run" to restore snapshots&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Storage Behavior
&lt;/h2&gt;

&lt;p&gt;QEMU/KVM snapshots use qcow2's copy-on-write mechanism:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;base.qcow2 ← snapshot1.qcow2 ← snapshot2.qcow2 ← current_state.qcow2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only changed blocks are written to snapshot layers, creating efficient storage chains.&lt;/p&gt;

&lt;h2&gt;
  
  
  Maintenance Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Regularly delete unneeded snapshots&lt;/li&gt;
&lt;li&gt;Monitor disk usage with &lt;code&gt;qemu-img info --backing-chain&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Commit changes to consolidate long chains&lt;/li&gt;
&lt;li&gt;Check snapshot chain integrity periodically&lt;/li&gt;
&lt;li&gt;Limit chain depth to 10 or fewer snapshots&lt;/li&gt;
&lt;li&gt;Never manually delete snapshot files&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Troubleshooting: VirtualBox to QEMU Migration
&lt;/h2&gt;

&lt;p&gt;When converting VDI files to qcow2, you might encounter boot failures. Here's the fix:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Proper Conversion
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;qemu-img convert &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; vdi &lt;span class="nt"&gt;-O&lt;/span&gt; qcow2 source.vdi destination.qcow2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Critical VM Settings
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Disk Controller&lt;/strong&gt;: Use SATA or IDE (not Virtio unless configured)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CPU Configuration&lt;/strong&gt;: Enable "Copy host CPU configuration"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These settings prevent hardware detection issues that cause boot failures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Snapshots are powerful tools for development and testing workflows, but they require careful management. Use them for short-term testing and experimentation, but always maintain proper backups for production data. With the right approach, snapshots can significantly improve your virtualization workflow while keeping your systems safe.&lt;/p&gt;

&lt;p&gt;Remember: snapshots are for convenience, backups are for survival.&lt;/p&gt;

</description>
      <category>virtualization</category>
      <category>linux</category>
      <category>qemu</category>
      <category>snapshots</category>
    </item>
  </channel>
</rss>
