Before installing a major operating system or application update, create a snapshot of the VM. If the update causes problems, you can quickly return to the original state by reverting to the snapshot.

Example 3: Creating Multiple Identical Environments

For deploying a scalable web application, you may need several identical VMs. Create one VM, configure it, create a snapshot, and then clone it from the snapshot as many times as needed. This is much faster than configuring each VM from scratch.

Creating a Snapshot in VMware ESXi

How to clone a virtual machine? - VMware vSphere Client interface for creating snapshots
VMware ESXi provides a user-friendly vSphere Client graphical interface for managing snapshots. In addition, you can use the command line (ESXi Shell) or vCLI (VMware Command-Line Interface).
VPS хостинг

Виртуальные серверы с гарантированными ресурсами

Выбрать VPS

Creating a Snapshot via vSphere Client:

  • Log in to vSphere Client and connect to the ESXi host or vCenter Server.
  • Select the VM for which you want to create a snapshot.
  • Right-click on the VM and select «Snapshots» -> «Take Snapshot…».
  • Enter a name and description for the snapshot.
  • Select the «Memory» option (include VM memory in the snapshot) if necessary. Including memory increases the size of the snapshot and the time it takes to create, but allows you to return to the exact state of the VM, including the state of running applications.
  • Click «OK».
Creating a Snapshot via Command Line (ESXi Shell):

vim-cmd vmfs/snapshot/create 

Where is the ID of your virtual machine. To find out the ID, you can use the command:

vim-cmd vmsvc/getallvms

Full example:

vim-cmd vmfs/snapshot/create 123 snapshot-name snapshot-description 0 0

In this command:

  • 123 — Virtual machine ID.
  • snapshot-name — Snapshot name.
  • snapshot-description — Snapshot description.
  • 0 — Include memory (0 — no, 1 — yes).
  • 0 — Quiesce (0 — no, 1 — yes). Quiesce attempts to pause the guest OS file system to ensure data consistency. Requires VMware Tools to be installed.
Creating a Snapshot via vCLI:

vmware-cmd 

Where is the path to the VM configuration file (.vmx). For example:

vmware-cmd /vmfs/volumes/datastore1/MyVM/MyVM.vmx createsnapshot "SnapshotName" "SnapshotDescription" 0 0

This command does the same as the ESXi Shell example, but is executed from a client machine where vCLI is installed.

Cloning a VM from a Snapshot in Proxmox VE

Proxmox VE also supports creating snapshots and cloning from them. The process of cloning from a snapshot in Proxmox VE is slightly different from VMware, but also relatively simple.

Creating a Snapshot in Proxmox VE:

  • Log in to the Proxmox VE web interface.
  • Select the VM for which you want to create a snapshot.
  • Go to the «Snapshots» tab.
  • Click the «Take Snapshot» button.
  • Enter a snapshot name and description (optional).
  • Click the «Take Snapshot» button.
Cloning a VM from a Snapshot in Proxmox VE (via the web interface):

  • Select the VM you want to clone from a snapshot.
  • Go to the «Snapshots» tab.
  • Select the snapshot you want to clone from.
  • Click the «Clone» button.
  • In the dialog box that appears, specify:
    • «Target Storage»: Select the storage for the cloned VM.
    • «VMID»: Specify a new ID for the cloned VM (must be unique).
    • «Name»: Specify a name for the cloned VM.
    • «Mode»: Select «Full Clone» (creates a full copy of the disk) or «Linked Clone» (creates a clone that references the original snapshot, taking up less space but dependent on the original snapshot). For cloning from a snapshot, «Linked Clone» is recommended.
    • «Target Node»: Select the node on which the cloned VM will run.
    • «Start after creation»: Start the cloned VM immediately after creation.
  • Click the «Clone» button.
Cloning a VM from a Snapshot in Proxmox VE (via the command line):

qm clone  --newid  --name  --storage  --snapshot 

For example:

qm clone 100 --newid 200 --name clonevm --storage local-lvm --snapshot snap1

In this command:

  • 100 — ID of the original VM.
  • 200 — ID of the cloned VM.
  • clonevm — Name of the cloned VM.
  • local-lvm — Name of the storage where the cloned VM will be placed.
  • snap1 — Name of the snapshot from which the VM will be cloned.

To create a linked clone, add the --linked 1 option:

qm clone 100 --newid 200 --name clonevm --storage local-lvm --snapshot snap1 --linked 1
Important: When using a linked clone, deleting the original snapshot will corrupt the cloned VM.

Automating the Cloning Process with Terraform

Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to automate the creation, modification, and management of infrastructure, including virtual machines. Using Terraform to clone VMs makes the process repeatable, predictable, and automated. We’ll look at an example of automating VM cloning in VMware vSphere.

Prerequisites:

  • Terraform installed.
  • VMware vSphere provider for Terraform configured.
  • An account with sufficient privileges in vSphere.
Example Terraform Configuration:

resource "vsphere_virtual_machine" "template_vm" {
  name             = "template-vm"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = 2
  memory   = 4096

  guest_id = "ubuntu64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
    adapter_type = "e1000e"
  }

  disk {
    label            = "disk0"
    size             = 20
    eagerly_scrub = false
    thin_provisioned = true
  }
}

resource "vsphere_virtual_machine" "cloned_vm" {
  name             = "cloned-vm"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = 2
  memory   = 4096

  guest_id = "ubuntu64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
    adapter_type = "e1000e"
  }

  clone {
    template_uuid = vsphere_virtual_machine.template_vm.id

    customize {
      linux_options {
        host_name = "cloned-vm"
        domain    = "example.com"
      }

      network_interface {
        ipv4_address = "192.168.1.100"
        ipv4_netmask = 24
      }

      ipv4_gateway = "192.168.1.1"
    }
  }
}

In this example:

  • vsphere_virtual_machine "template_vm" — Defines the original VM (template) from which cloning will be performed. In this example, a new VM «template-vm» is created. In a real scenario, you can use an existing VM.
  • vsphere_virtual_machine "cloned_vm" — Defines the cloned VM «cloned-vm».
  • clone { template_uuid = vsphere_virtual_machine.template_vm.id } — Specifies that the VM «cloned_vm» should be cloned from the VM «template_vm». template_uuid points to the ID of the original VM.
  • customize — Allows you to configure parameters of the cloned VM, such as hostname, IP address, and gateway. This is important to avoid IP address and name conflicts on the network.
Automating Cloning from a Snapshot (using snapshot_uuid):

data "vsphere_virtual_machine" "template_vm" {
  name          = "MyTemplateVM"
}

data "vsphere_virtual_machine_snapshot" "latest_snapshot" {
  virtual_machine_uuid = data.vsphere_virtual_machine.template_vm.id
  sort               = "modify_time_descending"
  most_recent        = true
}

resource "vsphere_virtual_machine" "cloned_vm" {
  name             = "ClonedVMFromSnapshot"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = 2
  memory   = 4096

  guest_id = "ubuntu64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
    adapter_type = "e1000e"
  }

  clone {
    template_uuid = data.vsphere_virtual_machine.template_vm.id
    snapshot_uuid = data.vsphere_virtual_machine_snapshot.latest_snapshot.id

    customize {
      linux_options {
        host_name = "ClonedVMFromSnapshot"
        domain    = "example.com"
      }

      network_interface {
        ipv4_address = "192.168.1.101"
        ipv4_netmask = 24
      }

      ipv4_gateway = "192.168.1.1"
    }
  }
}

In this example:

  • data "vsphere_virtual_machine" "template_vm" — Retrieves information about an existing VM «MyTemplateVM».
  • data "vsphere_virtual_machine_snapshot" "latest_snapshot" — Retrieves information about the latest snapshot of this VM.
  • clone { snapshot_uuid = data.vsphere_virtual_machine_snapshot.latest_snapshot.id } — Specifies that cloning should be performed from the specified snapshot.
Running Terraform:

  • Save the configuration to a file, for example, main.tf.
  • Run the terraform init command to initialize Terraform.
  • Run the terraform plan command to view the changes that will be made.
  • Run the terraform apply command to apply the configuration and create the cloned VM.
Automation with Terraform greatly simplifies the VM cloning process, especially in large infrastructures. This approach makes it easy to scale and manage your virtual machines.

Snapshot Management and Storage: Best Practices

Snapshots are a powerful tool, but improper management can lead to performance issues and disk space shortages. It is important to follow best practices for managing and storing snapshots.

Best Practices:

  • Limit the Number of Snapshots: Try not to create too many snapshots for a single VM. A large number of snapshots can significantly reduce VM performance and increase the time to revert to a snapshot. It is recommended to have no more than 2-3 snapshots per VM.
  • Limit Snapshot Retention Time: Snapshots are not intended for long-term storage. Use them only for short-term tasks such as testing or backing up before making important changes. Delete snapshots as soon as they are no longer needed. It is recommended to store snapshots for no more than 72 hours.
  • Monitor Disk Space: Regularly check disk space usage, especially on storage locations where snapshots are stored. Snapshots can quickly take up a lot of space, which can lead to performance and stability issues. Use monitoring tools provided by your virtualization platform to track disk space usage.
  • Plan Snapshot Deletion: Develop a plan for deleting old snapshots. For example, you can set a rule to automatically delete snapshots older than a certain age.
  • Avoid Snapshots on Production VMs: Whenever possible, avoid creating snapshots on production VMs, especially during peak loads. Creating a snapshot can cause a brief VM freeze, which can affect service availability. If you need to create a snapshot on a production VM, schedule it for a time of minimal load.
  • Use Full Cloning for Long-Term Backup: Snapshots are not a replacement for full backups. For long-term data storage, use full cloning or other backup methods.
  • Understand Snapshot Types: Know the difference between snapshots with memory and without memory. Snapshots with memory are useful for reverting to the exact state of the VM, including the state of running applications, but they take up more space and require more time to create.
Example: Monitoring Disk Space in VMware vSphere:

  • Log in to vSphere Client.
  • Select the datastore you want to check.
  • Go to the «Monitor» -> «Performance» tab.
  • Select «Disk» from the «Chart options» drop-down list.
  • Check the «Used space» and «Free space» graphs.
Example: Deleting Old Snapshots in Proxmox VE (via the command line):

qm snapshot-delete   

Where is the VM ID and is the name of the snapshot to delete. For example:

qm snapshot-delete 100 snap1

This command will delete the «snap1» snapshot from the VM with ID 100.

By following these best practices, you can effectively manage snapshots and avoid problems associated with their use. Proper snapshot management will help you maintain the high performance and stability of your virtual infrastructure.

Example 2: Backups Before Important Changes

Before installing a major operating system or application update, create a snapshot of the VM. If the update causes problems, you can quickly return to the original state by reverting to the snapshot.

Example 3: Creating Multiple Identical Environments

For deploying a scalable web application, you may need several identical VMs. Create one VM, configure it, create a snapshot, and then clone it from the snapshot as many times as needed. This is much faster than configuring each VM from scratch.

Creating a Snapshot in VMware ESXi

How to clone a virtual machine? - VMware vSphere Client interface for creating snapshots
VMware ESXi provides a user-friendly vSphere Client graphical interface for managing snapshots. In addition, you can use the command line (ESXi Shell) or vCLI (VMware Command-Line Interface).

Creating a Snapshot via vSphere Client:

  • Log in to vSphere Client and connect to the ESXi host or vCenter Server.
  • Select the VM for which you want to create a snapshot.
  • Right-click on the VM and select «Snapshots» -> «Take Snapshot…».
  • Enter a name and description for the snapshot.
  • Select the «Memory» option (include VM memory in the snapshot) if necessary. Including memory increases the size of the snapshot and the time it takes to create, but allows you to return to the exact state of the VM, including the state of running applications.
  • Click «OK».
Creating a Snapshot via Command Line (ESXi Shell):

vim-cmd vmfs/snapshot/create 

Where is the ID of your virtual machine. To find out the ID, you can use the command:

vim-cmd vmsvc/getallvms

Full example:

vim-cmd vmfs/snapshot/create 123 snapshot-name snapshot-description 0 0

In this command:

  • 123 — Virtual machine ID.
  • snapshot-name — Snapshot name.
  • snapshot-description — Snapshot description.
  • 0 — Include memory (0 — no, 1 — yes).
  • 0 — Quiesce (0 — no, 1 — yes). Quiesce attempts to pause the guest OS file system to ensure data consistency. Requires VMware Tools to be installed.
Creating a Snapshot via vCLI:

vmware-cmd 

Where is the path to the VM configuration file (.vmx). For example:

vmware-cmd /vmfs/volumes/datastore1/MyVM/MyVM.vmx createsnapshot "SnapshotName" "SnapshotDescription" 0 0

This command does the same as the ESXi Shell example, but is executed from a client machine where vCLI is installed.

Cloning a VM from a Snapshot in Proxmox VE

Proxmox VE also supports creating snapshots and cloning from them. The process of cloning from a snapshot in Proxmox VE is slightly different from VMware, but also relatively simple.

Creating a Snapshot in Proxmox VE:

  • Log in to the Proxmox VE web interface.
  • Select the VM for which you want to create a snapshot.
  • Go to the «Snapshots» tab.
  • Click the «Take Snapshot» button.
  • Enter a snapshot name and description (optional).
  • Click the «Take Snapshot» button.
Cloning a VM from a Snapshot in Proxmox VE (via the web interface):

  • Select the VM you want to clone from a snapshot.
  • Go to the «Snapshots» tab.
  • Select the snapshot you want to clone from.
  • Click the «Clone» button.
  • In the dialog box that appears, specify:
    • «Target Storage»: Select the storage for the cloned VM.
    • «VMID»: Specify a new ID for the cloned VM (must be unique).
    • «Name»: Specify a name for the cloned VM.
    • «Mode»: Select «Full Clone» (creates a full copy of the disk) or «Linked Clone» (creates a clone that references the original snapshot, taking up less space but dependent on the original snapshot). For cloning from a snapshot, «Linked Clone» is recommended.
    • «Target Node»: Select the node on which the cloned VM will run.
    • «Start after creation»: Start the cloned VM immediately after creation.
  • Click the «Clone» button.
Cloning a VM from a Snapshot in Proxmox VE (via the command line):

qm clone  --newid  --name  --storage  --snapshot 

For example:

qm clone 100 --newid 200 --name clonevm --storage local-lvm --snapshot snap1

In this command:

  • 100 — ID of the original VM.
  • 200 — ID of the cloned VM.
  • clonevm — Name of the cloned VM.
  • local-lvm — Name of the storage where the cloned VM will be placed.
  • snap1 — Name of the snapshot from which the VM will be cloned.

To create a linked clone, add the --linked 1 option:

qm clone 100 --newid 200 --name clonevm --storage local-lvm --snapshot snap1 --linked 1
Important: When using a linked clone, deleting the original snapshot will corrupt the cloned VM.

Automating the Cloning Process with Terraform

Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to automate the creation, modification, and management of infrastructure, including virtual machines. Using Terraform to clone VMs makes the process repeatable, predictable, and automated. We’ll look at an example of automating VM cloning in VMware vSphere.

Prerequisites:

  • Terraform installed.
  • VMware vSphere provider for Terraform configured.
  • An account with sufficient privileges in vSphere.
Example Terraform Configuration:

resource "vsphere_virtual_machine" "template_vm" {
  name             = "template-vm"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = 2
  memory   = 4096

  guest_id = "ubuntu64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
    adapter_type = "e1000e"
  }

  disk {
    label            = "disk0"
    size             = 20
    eagerly_scrub = false
    thin_provisioned = true
  }
}

resource "vsphere_virtual_machine" "cloned_vm" {
  name             = "cloned-vm"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = 2
  memory   = 4096

  guest_id = "ubuntu64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
    adapter_type = "e1000e"
  }

  clone {
    template_uuid = vsphere_virtual_machine.template_vm.id

    customize {
      linux_options {
        host_name = "cloned-vm"
        domain    = "example.com"
      }

      network_interface {
        ipv4_address = "192.168.1.100"
        ipv4_netmask = 24
      }

      ipv4_gateway = "192.168.1.1"
    }
  }
}

In this example:

  • vsphere_virtual_machine "template_vm" — Defines the original VM (template) from which cloning will be performed. In this example, a new VM «template-vm» is created. In a real scenario, you can use an existing VM.
  • vsphere_virtual_machine "cloned_vm" — Defines the cloned VM «cloned-vm».
  • clone { template_uuid = vsphere_virtual_machine.template_vm.id } — Specifies that the VM «cloned_vm» should be cloned from the VM «template_vm». template_uuid points to the ID of the original VM.
  • customize — Allows you to configure parameters of the cloned VM, such as hostname, IP address, and gateway. This is important to avoid IP address and name conflicts on the network.
Automating Cloning from a Snapshot (using snapshot_uuid):

data "vsphere_virtual_machine" "template_vm" {
  name          = "MyTemplateVM"
}

data "vsphere_virtual_machine_snapshot" "latest_snapshot" {
  virtual_machine_uuid = data.vsphere_virtual_machine.template_vm.id
  sort               = "modify_time_descending"
  most_recent        = true
}

resource "vsphere_virtual_machine" "cloned_vm" {
  name             = "ClonedVMFromSnapshot"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = 2
  memory   = 4096

  guest_id = "ubuntu64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
    adapter_type = "e1000e"
  }

  clone {
    template_uuid = data.vsphere_virtual_machine.template_vm.id
    snapshot_uuid = data.vsphere_virtual_machine_snapshot.latest_snapshot.id

    customize {
      linux_options {
        host_name = "ClonedVMFromSnapshot"
        domain    = "example.com"
      }

      network_interface {
        ipv4_address = "192.168.1.101"
        ipv4_netmask = 24
      }

      ipv4_gateway = "192.168.1.1"
    }
  }
}

In this example:

  • data "vsphere_virtual_machine" "template_vm" — Retrieves information about an existing VM «MyTemplateVM».
  • data "vsphere_virtual_machine_snapshot" "latest_snapshot" — Retrieves information about the latest snapshot of this VM.
  • clone { snapshot_uuid = data.vsphere_virtual_machine_snapshot.latest_snapshot.id } — Specifies that cloning should be performed from the specified snapshot.
Running Terraform:

  • Save the configuration to a file, for example, main.tf.
  • Run the terraform init command to initialize Terraform.
  • Run the terraform plan command to view the changes that will be made.
  • Run the terraform apply command to apply the configuration and create the cloned VM.
Automation with Terraform greatly simplifies the VM cloning process, especially in large infrastructures. This approach makes it easy to scale and manage your virtual machines.

Snapshot Management and Storage: Best Practices

Snapshots are a powerful tool, but improper management can lead to performance issues and disk space shortages. It is important to follow best practices for managing and storing snapshots.

Best Practices:

  • Limit the Number of Snapshots: Try not to create too many snapshots for a single VM. A large number of snapshots can significantly reduce VM performance and increase the time to revert to a snapshot. It is recommended to have no more than 2-3 snapshots per VM.
  • Limit Snapshot Retention Time: Snapshots are not intended for long-term storage. Use them only for short-term tasks such as testing or backing up before making important changes. Delete snapshots as soon as they are no longer needed. It is recommended to store snapshots for no more than 72 hours.
  • Monitor Disk Space: Regularly check disk space usage, especially on storage locations where snapshots are stored. Snapshots can quickly take up a lot of space, which can lead to performance and stability issues. Use monitoring tools provided by your virtualization platform to track disk space usage.
  • Plan Snapshot Deletion: Develop a plan for deleting old snapshots. For example, you can set a rule to automatically delete snapshots older than a certain age.
  • Avoid Snapshots on Production VMs: Whenever possible, avoid creating snapshots on production VMs, especially during peak loads. Creating a snapshot can cause a brief VM freeze, which can affect service availability. If you need to create a snapshot on a production VM, schedule it for a time of minimal load.
  • Use Full Cloning for Long-Term Backup: Snapshots are not a replacement for full backups. For long-term data storage, use full cloning or other backup methods.
  • Understand Snapshot Types: Know the difference between snapshots with memory and without memory. Snapshots with memory are useful for reverting to the exact state of the VM, including the state of running applications, but they take up more space and require more time to create.
Example: Monitoring Disk Space in VMware vSphere:

  • Log in to vSphere Client.
  • Select the datastore you want to check.
  • Go to the «Monitor» -> «Performance» tab.
  • Select «Disk» from the «Chart options» drop-down list.
  • Check the «Used space» and «Free space» graphs.
Example: Deleting Old Snapshots in Proxmox VE (via the command line):

qm snapshot-delete   

Where is the VM ID and is the name of the snapshot to delete. For example:

qm snapshot-delete 100 snap1

This command will delete the «snap1» snapshot from the VM with ID 100.

By following these best practices, you can effectively manage snapshots and avoid problems associated with their use. Proper snapshot management will help you maintain the high performance and stability of your virtual infrastructure.

How to Clone a Virtual Machine: A Detailed Guide to Creating Snapshots

Cloning a virtual machine (VM) is a critical operation for testing, backing up, and quickly deploying identical environments. However, a full clone, especially of large VMs, can take significant time and disk space. In this article, we will explore in detail a more efficient method: creating snapshots and cloning based on them. We’ll look at various scenarios, tools, and best practices for effectively using this method, ensuring fast and reliable cloning of your VMs.

Contents:

  1. Advantages of Snapshot-Based Cloning
  2. Creating a Snapshot in VMware ESXi
  3. Cloning a VM from a Snapshot in Proxmox VE
  4. Automating the Cloning Process with Terraform
  5. Snapshot Management and Storage: Best Practices

Advantages of Snapshot-Based Cloning

How to clone a virtual machine? - Comparing full cloning and snapshot-based cloning
Traditional VM cloning involves creating a full copy of the virtual disk. This can be a resource-intensive process, especially for large VMs. Snapshots, on the other hand, capture the state of a VM at a specific point in time. When cloning from a snapshot, a new VM is created that references the original snapshot, and all changes are written to a separate file (delta disk). This leads to the following advantages:

  • Time Savings: Creating a snapshot and cloning from it is significantly faster than a full clone, especially for large VMs.
  • Disk Space Savings: Snapshot-based cloning takes up less space because it doesn’t require a full disk copy. Changes are only written to the delta disk.
  • Rapid Recovery: Ability to quickly revert to a previous VM state, which is useful for testing and development.
  • Doesn’t Affect Primary VM Performance: Creating a snapshot has minimal impact on the primary VM’s performance, unlike a full clone. However, long-term storage and a large number of snapshots can affect performance.
Example 1: Development and Testing

Suppose you have a Production VM with a web server and database. You want to test an application update without risking disruption to Production. Create a snapshot of the VM, clone it from the snapshot, and test the update in the cloned environment. If something goes wrong, you can simply delete the cloned VM and revert to the original snapshot.

Example 2: Backups Before Important Changes

Before installing a major operating system or application update, create a snapshot of the VM. If the update causes problems, you can quickly return to the original state by reverting to the snapshot.

Example 3: Creating Multiple Identical Environments

For deploying a scalable web application, you may need several identical VMs. Create one VM, configure it, create a snapshot, and then clone it from the snapshot as many times as needed. This is much faster than configuring each VM from scratch.

Creating a Snapshot in VMware ESXi

How to clone a virtual machine? - VMware vSphere Client interface for creating snapshots
VMware ESXi provides a user-friendly vSphere Client graphical interface for managing snapshots. In addition, you can use the command line (ESXi Shell) or vCLI (VMware Command-Line Interface).

Creating a Snapshot via vSphere Client:

  • Log in to vSphere Client and connect to the ESXi host or vCenter Server.
  • Select the VM for which you want to create a snapshot.
  • Right-click on the VM and select «Snapshots» -> «Take Snapshot…».
  • Enter a name and description for the snapshot.
  • Select the «Memory» option (include VM memory in the snapshot) if necessary. Including memory increases the size of the snapshot and the time it takes to create, but allows you to return to the exact state of the VM, including the state of running applications.
  • Click «OK».
Creating a Snapshot via Command Line (ESXi Shell):

vim-cmd vmfs/snapshot/create 

Where is the ID of your virtual machine. To find out the ID, you can use the command:

vim-cmd vmsvc/getallvms

Full example:

vim-cmd vmfs/snapshot/create 123 snapshot-name snapshot-description 0 0

In this command:

  • 123 — Virtual machine ID.
  • snapshot-name — Snapshot name.
  • snapshot-description — Snapshot description.
  • 0 — Include memory (0 — no, 1 — yes).
  • 0 — Quiesce (0 — no, 1 — yes). Quiesce attempts to pause the guest OS file system to ensure data consistency. Requires VMware Tools to be installed.
Creating a Snapshot via vCLI:

vmware-cmd 

Where is the path to the VM configuration file (.vmx). For example:

vmware-cmd /vmfs/volumes/datastore1/MyVM/MyVM.vmx createsnapshot "SnapshotName" "SnapshotDescription" 0 0

This command does the same as the ESXi Shell example, but is executed from a client machine where vCLI is installed.

Cloning a VM from a Snapshot in Proxmox VE

Proxmox VE also supports creating snapshots and cloning from them. The process of cloning from a snapshot in Proxmox VE is slightly different from VMware, but also relatively simple.

Creating a Snapshot in Proxmox VE:

  • Log in to the Proxmox VE web interface.
  • Select the VM for which you want to create a snapshot.
  • Go to the «Snapshots» tab.
  • Click the «Take Snapshot» button.
  • Enter a snapshot name and description (optional).
  • Click the «Take Snapshot» button.
Cloning a VM from a Snapshot in Proxmox VE (via the web interface):

  • Select the VM you want to clone from a snapshot.
  • Go to the «Snapshots» tab.
  • Select the snapshot you want to clone from.
  • Click the «Clone» button.
  • In the dialog box that appears, specify:
    • «Target Storage»: Select the storage for the cloned VM.
    • «VMID»: Specify a new ID for the cloned VM (must be unique).
    • «Name»: Specify a name for the cloned VM.
    • «Mode»: Select «Full Clone» (creates a full copy of the disk) or «Linked Clone» (creates a clone that references the original snapshot, taking up less space but dependent on the original snapshot). For cloning from a snapshot, «Linked Clone» is recommended.
    • «Target Node»: Select the node on which the cloned VM will run.
    • «Start after creation»: Start the cloned VM immediately after creation.
  • Click the «Clone» button.
Cloning a VM from a Snapshot in Proxmox VE (via the command line):

qm clone  --newid  --name  --storage  --snapshot 

For example:

qm clone 100 --newid 200 --name clonevm --storage local-lvm --snapshot snap1

In this command:

  • 100 — ID of the original VM.
  • 200 — ID of the cloned VM.
  • clonevm — Name of the cloned VM.
  • local-lvm — Name of the storage where the cloned VM will be placed.
  • snap1 — Name of the snapshot from which the VM will be cloned.

To create a linked clone, add the --linked 1 option:

qm clone 100 --newid 200 --name clonevm --storage local-lvm --snapshot snap1 --linked 1
Important: When using a linked clone, deleting the original snapshot will corrupt the cloned VM.

Automating the Cloning Process with Terraform

Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to automate the creation, modification, and management of infrastructure, including virtual machines. Using Terraform to clone VMs makes the process repeatable, predictable, and automated. We’ll look at an example of automating VM cloning in VMware vSphere.

Prerequisites:

  • Terraform installed.
  • VMware vSphere provider for Terraform configured.
  • An account with sufficient privileges in vSphere.
Example Terraform Configuration:

resource "vsphere_virtual_machine" "template_vm" {
  name             = "template-vm"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = 2
  memory   = 4096

  guest_id = "ubuntu64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
    adapter_type = "e1000e"
  }

  disk {
    label            = "disk0"
    size             = 20
    eagerly_scrub = false
    thin_provisioned = true
  }
}

resource "vsphere_virtual_machine" "cloned_vm" {
  name             = "cloned-vm"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = 2
  memory   = 4096

  guest_id = "ubuntu64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
    adapter_type = "e1000e"
  }

  clone {
    template_uuid = vsphere_virtual_machine.template_vm.id

    customize {
      linux_options {
        host_name = "cloned-vm"
        domain    = "example.com"
      }

      network_interface {
        ipv4_address = "192.168.1.100"
        ipv4_netmask = 24
      }

      ipv4_gateway = "192.168.1.1"
    }
  }
}

In this example:

  • vsphere_virtual_machine "template_vm" — Defines the original VM (template) from which cloning will be performed. In this example, a new VM «template-vm» is created. In a real scenario, you can use an existing VM.
  • vsphere_virtual_machine "cloned_vm" — Defines the cloned VM «cloned-vm».
  • clone { template_uuid = vsphere_virtual_machine.template_vm.id } — Specifies that the VM «cloned_vm» should be cloned from the VM «template_vm». template_uuid points to the ID of the original VM.
  • customize — Allows you to configure parameters of the cloned VM, such as hostname, IP address, and gateway. This is important to avoid IP address and name conflicts on the network.
Automating Cloning from a Snapshot (using snapshot_uuid):

data "vsphere_virtual_machine" "template_vm" {
  name          = "MyTemplateVM"
}

data "vsphere_virtual_machine_snapshot" "latest_snapshot" {
  virtual_machine_uuid = data.vsphere_virtual_machine.template_vm.id
  sort               = "modify_time_descending"
  most_recent        = true
}

resource "vsphere_virtual_machine" "cloned_vm" {
  name             = "ClonedVMFromSnapshot"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = 2
  memory   = 4096

  guest_id = "ubuntu64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
    adapter_type = "e1000e"
  }

  clone {
    template_uuid = data.vsphere_virtual_machine.template_vm.id
    snapshot_uuid = data.vsphere_virtual_machine_snapshot.latest_snapshot.id

    customize {
      linux_options {
        host_name = "ClonedVMFromSnapshot"
        domain    = "example.com"
      }

      network_interface {
        ipv4_address = "192.168.1.101"
        ipv4_netmask = 24
      }

      ipv4_gateway = "192.168.1.1"
    }
  }
}

In this example:

  • data "vsphere_virtual_machine" "template_vm" — Retrieves information about an existing VM «MyTemplateVM».
  • data "vsphere_virtual_machine_snapshot" "latest_snapshot" — Retrieves information about the latest snapshot of this VM.
  • clone { snapshot_uuid = data.vsphere_virtual_machine_snapshot.latest_snapshot.id } — Specifies that cloning should be performed from the specified snapshot.
Running Terraform:

  • Save the configuration to a file, for example, main.tf.
  • Run the terraform init command to initialize Terraform.
  • Run the terraform plan command to view the changes that will be made.
  • Run the terraform apply command to apply the configuration and create the cloned VM.
Automation with Terraform greatly simplifies the VM cloning process, especially in large infrastructures. This approach makes it easy to scale and manage your virtual machines.

Snapshot Management and Storage: Best Practices

Snapshots are a powerful tool, but improper management can lead to performance issues and disk space shortages. It is important to follow best practices for managing and storing snapshots.

Best Practices:

  • Limit the Number of Snapshots: Try not to create too many snapshots for a single VM. A large number of snapshots can significantly reduce VM performance and increase the time to revert to a snapshot. It is recommended to have no more than 2-3 snapshots per VM.
  • Limit Snapshot Retention Time: Snapshots are not intended for long-term storage. Use them only for short-term tasks such as testing or backing up before making important changes. Delete snapshots as soon as they are no longer needed. It is recommended to store snapshots for no more than 72 hours.
  • Monitor Disk Space: Regularly check disk space usage, especially on storage locations where snapshots are stored. Snapshots can quickly take up a lot of space, which can lead to performance and stability issues. Use monitoring tools provided by your virtualization platform to track disk space usage.
  • Plan Snapshot Deletion: Develop a plan for deleting old snapshots. For example, you can set a rule to automatically delete snapshots older than a certain age.
  • Avoid Snapshots on Production VMs: Whenever possible, avoid creating snapshots on production VMs, especially during peak loads. Creating a snapshot can cause a brief VM freeze, which can affect service availability. If you need to create a snapshot on a production VM, schedule it for a time of minimal load.
  • Use Full Cloning for Long-Term Backup: Snapshots are not a replacement for full backups. For long-term data storage, use full cloning or other backup methods.
  • Understand Snapshot Types: Know the difference between snapshots with memory and without memory. Snapshots with memory are useful for reverting to the exact state of the VM, including the state of running applications, but they take up more space and require more time to create.
Example: Monitoring Disk Space in VMware vSphere:

  • Log in to vSphere Client.
  • Select the datastore you want to check.
  • Go to the «Monitor» -> «Performance» tab.
  • Select «Disk» from the «Chart options» drop-down list.
  • Check the «Used space» and «Free space» graphs.
Example: Deleting Old Snapshots in Proxmox VE (via the command line):

qm snapshot-delete   

Where is the VM ID and is the name of the snapshot to delete. For example:

qm snapshot-delete 100 snap1

This command will delete the «snap1» snapshot from the VM with ID 100.

By following these best practices, you can effectively manage snapshots and avoid problems associated with their use. Proper snapshot management will help you maintain the high performance and stability of your virtual infrastructure.