Skip to content

Instantly share code, notes, and snippets.

@bashtheshell
Last active December 11, 2024 06:16
Show Gist options
  • Save bashtheshell/59540792cbfbe4b3470f3bfa885969ab to your computer and use it in GitHub Desktop.
Save bashtheshell/59540792cbfbe4b3470f3bfa885969ab to your computer and use it in GitHub Desktop.
Snapshot workaround for UTM as it lacks native snapshot feature

Workaround for lack of native snapshot feature in UTM

This was written when latest release version of UTM was v4.5.3.


NOTE: Snapshots using qemu-img is not supported for macOS VMs due to .img image format as other pre-configured non-macOS VMs are using .qcow2 as the storage backend, which is well-supported.


Credit goes to lnxbil for mentioning this workaround

STEPS:

  1. Run brew install qemu on macOS with Homebrew installed already.

  2. Right-click the VM on the left pane of the UTM window app and select Show in Finder.

  3. The easiest way to determine the location of the .utm VM file (really, it's a folder) is to right-click it then select New Terminal at Folder to open the Terminal, and it'd take you directly to that directory as your $PWD. We'll use the same Terminal for the subsequent steps.

  4. Run cd Data.

    Assuming there's only one .qcow2 file listed in the Data directory as it would be conveniently manageable dealing with just one. Here are the syntaxes you'd only need to know to perform the snapshots using qemu-img command:

    PLEASE NOTE: Snapshots cannot be created, applied, listed, or deleted while the VM is running. It must be in either in suspended or stopped state.

    Command syntax:
        snapshot [--object objectdef] [--image-opts] [-U] [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename
    
    Parameters to snapshot subcommand:
        'snapshot' is the name of the snapshot to create, apply or delete
        '-a' applies a snapshot (revert disk to saved state)
        '-c' creates a snapshot
        '-d' deletes a snapshot
        '-l' lists all snapshots in the given image
  5. To create the snapshot, run:

    qemu-img snapshot F8793221-25FF-47CE-8163-347DD16963E6.qcow2 -c fresh_install

    It's a strong recommendation to use a unique (unused names in the list of snapshots) descriptive TAG name in the last argument even though you can reuse the same name. If you attempt to reuse the same name, the new snapshot would be created with higher ID value than the original or previous snapshots with the same name, which means you would not be able to conveniently delete the latest snapshot as the original or previous snapshots have the higher priority.

  6. To list the snapshot, run:

    qemu-img snapshot F8793221-25FF-47CE-8163-347DD16963E6.qcow2 -l

    Output of the command (ex. containing 2 snapshots with same name):

        Snapshot list:
    ID      TAG               VM_SIZE                DATE        VM_CLOCK     ICOUNT
    1       fresh_install         0 B 2024-06-26 21:45:02  0000:00:00.000          0
    2       fresh_install         0 B 2024-06-26 21:45:14  0000:00:00.000          0
  7. To apply the snapshot, run:

    You can only apply the change to the VM if it's in a suspended or stopped state.

    qemu-img snapshot F8793221-25FF-47CE-8163-347DD16963E6.qcow2 -a fresh_install

    Although, please note that the above command would use the oldest snapshot if the TAG name is the same.

    To reference the snapshot by its ID instead (regardless of the duplicated TAG names), use the following command:

    qemu-img snapshot F8793221-25FF-47CE-8163-347DD16963E6.qcow2 -a 2
  8. To delete the snapshot, run:

    You can only delete the snapshot if the associated VM is in a suspended or stopped state.

    qemu-img snapshot F8793221-25FF-47CE-8163-347DD16963E6.qcow2 -d fresh_install

    Just like the apply command in the previous step, the command would delete the oldest snapshot if the TAG name is the same. However, the snapshot cannot be deleted by its ID value as shown in the snapshot list. So, it's strongly encouraged to give snapshot name a unique TAG name.

  9. To view the actual disk size versus the virtual size along with a list of associated snapshots, you can use the command:

    qemu-img info F8793221-25FF-47CE-8163-347DD16963E6.qcow2

    Please remember, you cannot list snapshots while the VM is running due to .qcow2 file being locked during use, preventing write operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment