Two Ways to Snapshot
Goal
Create both internal snapshots and external snapshots of qcow2, and confirm the difference between the two methods in the actual file structure.
Why it matters
Internal snapshots are convenient because they are managed as a single file, but they are hard to back up — because the original keeps changing. An external snapshot creates a new overlay that uses the current image as its backing and freezes the original at that point. That is why a backup tool can copy the original safely, and this is the standard of practical backup workflows. Rolling back is simple too, ending with discarding the overlay. In exchange, the number of files grows and you need to manage the chain.
Note: you must not touch an image with qemu-img while the VM is running. This lab proceeds only in a stopped state.
Steps
- Create the
/root/snapdirectory and create/root/snap/disk.qcow2as a 1G qcow2. - Create an internal snapshot named
before-upgrade. - Save the snapshot list to
/root/snap/list1.txt.before-upgrademust be visible. - Create two more,
after-patchandrollback-point, and save the list to/root/snap/list3.txt. There must be exactly 3 snapshots. - Apply (roll back to) the
before-upgradesnapshot, and write the exit code of that command to/root/snap/applied.txtin the formrc=0. - Delete the
after-patchsnapshot, and save the remaining list to/root/snap/list2.txt. There must be exactly 2 snapshots. - Create an external snapshot. Create
/root/snap/disk.ext1.qcow2withdisk.qcow2as its backing, and save the backing chain output to/root/snap/chain.txt. - Create
/root/snap/compare.txtwith the following 4 lines.INTERNAL_COUNT=<6번 이후 남은 내부 스냅샷 개수>/EXTERNAL_FILES=2/BACKUP_FRIENDLY=external/ROLLBACK_INTERNAL=-a(the placeholder is the number of internal snapshots remaining after step 6)
Notes
- Internal snapshots are done with just four commands:
qemu-img snapshot -c <이름> <파일>/-l/-a <이름>/-d <이름>(the placeholders are the snapshot name and the image file). - An external snapshot is
qemu-img create -f qcow2 -b <현재이미지> -F qcow2 <새오버레이>(the placeholders are the current image and the new overlay). - The
-loutput has two header lines, so exclude them when counting. - Common mistake 1: using
-ainstead of-din step 6. It applies rather than deletes. - Common mistake 2: deleting the backing side instead of the overlay in step 7 breaks the whole chain.
Prepare the lab image
Create the /root/snap directory and create /root/snap/disk.qcow2 as a 1G qcow2.
Use a new image separate from the previous lab. A small size is fine.
Create an internal snapshot
Create an internal snapshot named before-upgrade.
The -c of qemu-img snapshot is create. You give the name as an argument.
Snapshot list
Save the snapshot list to /root/snap/list1.txt. before-upgrade must be visible.
-l shows the ID, TAG, size, and time as a table. There are header lines, so be careful when counting.
Create several points in time
Create two more, after-patch and rollback-point, and save the list to /root/snap/list3.txt. There must be exactly 3 snapshots.
Just repeat the same command, changing only the name. Three must be visible in the list.
Roll back to a snapshot
Apply (roll back to) the before-upgrade snapshot, and write the exit code of that command to /root/snap/applied.txt in the form rc=0.
-a rolls the image contents back to the specified snapshot's point in time. The list itself remains.
Delete a snapshot
Delete the after-patch snapshot, and save the remaining list to /root/snap/list2.txt. There must be exactly 2 snapshots.
If you delete one with -d, it disappears from the list. Check the remaining count.
Create an external snapshot
Create an external snapshot. Create /root/snap/disk.ext1.qcow2 with disk.qcow2 as its backing, and save the backing chain output to /root/snap/chain.txt.
Creating a new overlay that uses the current image as its backing is itself an external snapshot.
Comparison table of the two methods
Create /root/snap/compare.txt with the following 4 lines.
INTERNAL_COUNT=<6번 이후 남은 내부 스냅샷 개수> / EXTERNAL_FILES=2 / BACKUP_FRIENDLY=external / ROLLBACK_INTERNAL=-a (the placeholder is the number of internal snapshots remaining after step 6)
The values must be what you actually confirmed in the earlier steps. Judge backup friendliness by whether the original is fixed.