DRDAVIDBANNER opened a new pull request, #14804:
URL: https://github.com/apache/cloudstack/pull/14804
### Description
This PR adds an opt-in way to take volume snapshots of KVM QCOW2 volumes on
file-based primary storage (NFS, SharedMountPoint and Filesystem) without
copying the volume.
Today, a volume snapshot of such a volume that is kept only on primary
storage (`snapshot.backup.to.secondary=false`) is a full copy.
`KVMStorageProcessor` runs `qemu-img convert` of the whole volume into the
`snapshots` directory of the pool, and reverting copies the whole snapshot back
(`LibvirtRevertSnapshotCommandWrapper`). Both take time proportional to the
size of the volume, so tens of minutes for a volume of a few hundred GB or
more. The disk-only Instance snapshots for file-based storage (#9524, #10632)
already avoid the copy by keeping the snapshot in the backing chain of the
volume. But an Instance snapshot covers all the volumes of the Instance and
blocks attaching, detaching and migrating volumes, adding and removing NICs,
and taking volume snapshots, so it cannot replace a volume snapshot in
workflows that do those things.
With the new setting `kvm.file.based.volume.snapshot.no.copy` (storage pool
scope, default `false`), a volume snapshot is taken the same way as one volume
of a disk-only Instance snapshot:
- **Take**: the current file of the volume is frozen and becomes the
snapshot, and the volume moves onto a new, empty QCOW2 delta on top of it. For
a running Instance this is one `virDomainSnapshotCreateXML` call (disk-only,
atomic, no metadata) for that disk. For a stopped Instance, or a detached
volume, it is `qemu-img create -b`. Nothing is copied.
- **Revert** (the Instance must be stopped, as today): the current delta of
the volume is replaced by a new, empty delta on top of the snapshot. Nothing is
copied.
- **Delete**: the child of the snapshot is merged into it. The child is the
next snapshot of the volume or, for the most recent snapshot, the current delta
of the volume. It is merged with `blockcommit` if the Instance is running and
`qemu-img commit` and `rebase` otherwise. The cost is proportional to the data
written after the snapshot, not to the size of the volume.
Because the snapshot travels with the volume (only `volumes.path` changes),
the volume can still be detached, attached to another Instance, or attached as
the root volume while it has such snapshots.
#### Implementation
- New `KvmFileBasedNoCopySnapshotStrategy` (engine/storage/snapshot),
`HIGHEST` priority:
- **TAKE**: only when the setting is enabled for the pool of the volume
and all of these hold:
- KVM, QCOW2, not encrypted;
- the pool is file-based and not managed;
- snapshots are not backed up to secondary storage in the zone;
- `kvm.incremental.snapshot` is false;
- no copy to other zones or pools and no quiesce was requested;
- the Instance, if any, is Running or Stopped, has no Instance snapshots
and no backup offering.
In every other case the existing strategies are used, as today.
- **REVERT/DELETE/BACKUP** of the snapshots it created: these are marked
with the snapshot detail `kvmFileBasedNoCopySnapshot`.
- The agent side reuses the existing `CreateDiskOnlyVmSnapshotCommand`,
`RevertDiskOnlyVmSnapshotCommand` and `MergeDiskOnlyVmSnapshotCommand`, called
for a single volume. No new agent commands.
- **Agent fix needed for a single volume**: with
`VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY`, Libvirt also takes an external snapshot
of the disks that are *not* listed in the snapshot XML, which would move them
onto files CloudStack does not know about.
`LibvirtComputingResource#createSnapshotXmlAndNewVolumePathMap` now lists every
other disk of the domain with `snapshot='no'`. CD-ROMs and floppies are
skipped. When all the volumes are snapshotted, as disk-only Instance snapshots
do, the XML is unchanged.
- **Mixed versions during upgrades**: the agent advertises the new host
capability `host.kvm.diskonlysnapshot.singlevolume` (synced by
`AgentManagerImpl` like the NVRAM capability). A no-copy snapshot of a
*running* Instance is only taken on a host that advertises it; otherwise the
regular snapshot is taken.
- **Single chain**: only the most recent no-copy snapshot of a volume can be
reverted, so the snapshots of a volume are always one chain ordered by creation
and no delta tree is created. Deleting any snapshot keeps the chain linear.
- **Guards while a volume has no-copy snapshots** (they would break or lose
the chain):
- taking an Instance snapshot of its Instance
(`VMSnapshotManagerImpl#allocVMSnapshot`). Without this guard,
`StorageVMSnapshotStrategy` could accept such an Instance snapshot, and
reverting it would replace the top of the chain;
- volume migration (`migrateVolume`) and Instance storage migration
(`migrateVirtualMachine` to a pool and `migrateVirtualMachineWithVolume`);
- reverting a *regular* snapshot of that volume, which would replace the
top of the chain;
- using a no-copy snapshot to create a volume, a template or an Instance,
to archive it, or to copy it to other zones or pools.
- Volume deletion needs no change: the existing KVM path deletes the
primary-storage snapshot files of the volume.
#### Limitations (possible follow-ups)
- No quiesce, encrypted volumes, incremental snapshots or backup to
secondary.
- No creating volumes or templates from these snapshots. On KVM this should
be possible later, because a `qemu-img convert` of the snapshot file flattens
its backing chain, but it needs its own testing.
- Only the latest snapshot of a volume can be reverted.
Documentation (cloudstack-documentation) to follow once the behaviour is
agreed.
### Types of changes
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Enhancement (improves an existing feature and functionality)
- [ ] Cleanup (Code refactoring and cleanup, that may add test cases)
- [ ] Build/CI
- [ ] Test (unit or integration test code)
### Feature/Enhancement Scale or Bug Severity
#### Feature/Enhancement Scale
- [ ] Major
- [x] Minor
### Screenshots (if appropriate):
### How Has This Been Tested?
Unit tests:
- `KvmFileBasedNoCopySnapshotStrategyTest` (new, 40 tests):
- the `canHandle` matrix for every eligibility condition, including the
host capability;
- take for running Instances, stopped Instances and detached volumes, and
take failures;
- revert of the latest snapshot, plus refusal for older snapshots and
running Instances;
- delete of the most recent, a middle and the oldest snapshot, checking
the merge trees and metadata updates;
- delete failures, deleted volumes, snapshots in Error, other zones, and
busy volumes and Instances.
- `LibvirtComputingResourceTest`: snapshot XML excludes the other disks with
`snapshot='no'`, skips CD-ROMs, and is unchanged when all disks are snapshotted.
- `SnapshotManagerImplTest`: detail lookup, volume check, the validation
helpers, and refusal to revert a regular snapshot while the volume has no-copy
snapshots.
- `AgentManagerImplTest`: the new capability is stored from the ReadyAnswer.
- `VMSnapshotManagerTest`: Instance snapshots are refused while a volume of
the Instance has no-copy snapshots.
All unit tests of the touched modules pass: api, engine/orchestration,
engine/storage/snapshot, server (4366 tests) and plugins/hypervisors/kvm.
Manual testing. Debian packages were built from this branch and installed on
Ubuntu 24.04 (libvirt 10.0.0, QEMU 8.2.2, MySQL 8.0). The management server,
KVM agent and NFS server (primary and secondary) ran on one host, with nested
CirrOS Instances. The setup was an Advanced zone with an L2 guest network,
`snapshot.backup.to.secondary=false` and the new setting enabled on the pools.
The checks were scripted; all 35 passed:
- **Capability**: the host advertises
`host.kvm.diskonlysnapshot.singlevolume`.
- **Take on a running Instance**: the snapshot of one data volume of a
running Instance with three disks is BackedUp. The volume moves onto a new
delta backed by the frozen file. The domain's other disks and their DB paths
are unchanged, and the frozen file keeps the data written before the snapshot.
- **Guards**: `migrateVolume` to another pool and `migrateVirtualMachine` of
the stopped Instance to another pool are refused. So are `createVolume` and
`createTemplate` from the snapshot, Instance snapshots while a volume has
no-copy snapshots (no Instance snapshot record is left), and revert of a
snapshot that is not the latest.
- **Snapshot travels with the volume**: the volume with an open snapshot is
detached, the Instance's ROOT is detached, and the volume is attached as ROOT
(deviceid 0). The Instance boots on the volume's delta, and the data written
after the snapshot is live.
- **Revert**: after stopping the Instance, revert puts the volume on a new
delta over the snapshot, and the pre-snapshot data is back. The original layout
is then restored and the Instance started again.
- **Delete on a running Instance, three snapshots**:
- deleting the middle snapshot merges the next snapshot into it; that
snapshot then lives in the merged file with the expected data, and the live
data is intact;
- deleting the newest snapshot runs an active commit, and the volume goes
back onto the merged file with every write intact;
- deleting the last snapshot puts the volume back on its original file,
with a chain of only the file and the template.
- **Stopped Instance and detached volume**: take, revert and delete work
through the `qemu-img` path.
- **Setting disabled**: snapshots are regular copies and the volume path is
unchanged.
- **Timing on a volume with 2 GiB of data**: the regular snapshot took 9.8 s
and the no-copy snapshot 2.1 s. The 2.1 s is mostly the test's 2-second polling
of the async job; the management-server log shows the agent call completing in
about 0.2 s. The regular snapshot's time grows with the volume size; the
no-copy one does not.
The libvirt behaviour behind the agent fix was also checked on its own with
the same libvirt and QEMU versions. With the previous XML, a disk-only snapshot
that lists only one disk moved the domain's other two disks onto new overlays.
With the new XML, only the listed disk moved.
#### How did you try to break this feature and the system with this change?
- **The agent fails or times out on take**: the snapshot is marked failed,
the volume keeps its path, and the agent wrapper deletes the new delta.
- **The agent succeeds but the management server fails afterwards**: the
volume path is updated first, so the volume never points to the frozen file.
The snapshot ends in Error and the frozen file stays in the backing chain. That
is harmless, and deleting the snapshot only removes its metadata.
- **A snapshot of one volume of a multi-volume running Instance**: the other
disks are excluded explicitly, and the host capability prevents this on agents
without the fix.
- **Merge failure on delete**: the snapshot and the volume keep their
metadata and the volume goes back to Ready; the delete can be retried.
- **Concurrent operations**: take runs in the Instance work queue. Delete
moves the volume to Snapshotting while merging, which blocks other snapshot,
attach and migration operations on it.
- **Mixed with Instance snapshots, KBOSS backups or incremental snapshots**:
refused in both directions. `KvmFileBasedStorageVmSnapshotStrategy` already
refuses Instance snapshots of volumes with snapshots. Manual testing showed
that `StorageVMSnapshotStrategy` would still accept one, hence the new check in
`VMSnapshotManagerImpl#allocVMSnapshot`.
- **Settings**: `snapshot.backup.to.secondary` is not dynamic, so it needs a
management server restart before no-copy snapshots are taken. The new setting
is dynamic, but setting values are cached for 30 s.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]