14fc7eb3dSSebastien Boeuf# Snapshot and Restore 24fc7eb3dSSebastien Boeuf 34fc7eb3dSSebastien BoeufThe goal for the snapshot/restore feature is to provide the user with the 44fc7eb3dSSebastien Boeufability to take a snapshot of a previously paused virtual machine. This 54fc7eb3dSSebastien Boeufsnapshot can be used as the base for creating new identical virtual machines, 64fc7eb3dSSebastien Boeufwithout the need to boot them from scratch. The restore codepath takes the 74fc7eb3dSSebastien Boeufsnapshot and creates the exact same virtual machine, restoring the previously 84fc7eb3dSSebastien Boeufsaved states. The new virtual machine is restored in a paused state, as it was 94fc7eb3dSSebastien Boeufbefore the snapshot was performed. 104fc7eb3dSSebastien Boeuf 11e6e58e6dSSebastien Boeuf## Snapshot a Cloud Hypervisor VM 124fc7eb3dSSebastien Boeuf 13e6e58e6dSSebastien BoeufFirst thing, we must run a Cloud Hypervisor VM: 144fc7eb3dSSebastien Boeuf 154fc7eb3dSSebastien Boeuf```bash 164fc7eb3dSSebastien Boeuf./cloud-hypervisor \ 174fc7eb3dSSebastien Boeuf --api-socket /tmp/cloud-hypervisor.sock \ 184fc7eb3dSSebastien Boeuf --cpus boot=4 \ 194fc7eb3dSSebastien Boeuf --memory size=4G \ 205c7164e5SRob Bradford --kernel vmlinux \ 214fc7eb3dSSebastien Boeuf --cmdline "root=/dev/vda1 console=hvc0 rw" \ 224fc7eb3dSSebastien Boeuf --disk path=focal-server-cloudimg-amd64.raw 234fc7eb3dSSebastien Boeuf``` 244fc7eb3dSSebastien Boeuf 254fc7eb3dSSebastien BoeufAt any point in time when the VM is running, one might choose to pause it: 264fc7eb3dSSebastien Boeuf 274fc7eb3dSSebastien Boeuf```bash 28fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/cloud-hypervisor.sock pause 294fc7eb3dSSebastien Boeuf``` 304fc7eb3dSSebastien Boeuf 314fc7eb3dSSebastien BoeufOnce paused, the VM can be safely snapshot into the specified directory and 324fc7eb3dSSebastien Boeufusing the following command: 334fc7eb3dSSebastien Boeuf 344fc7eb3dSSebastien Boeuf```bash 35fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/cloud-hypervisor.sock snapshot file:///home/foo/snapshot 364fc7eb3dSSebastien Boeuf``` 374fc7eb3dSSebastien Boeuf 384fc7eb3dSSebastien BoeufGiven the directory was present on the system, the snapshot will succeed and 394fc7eb3dSSebastien Boeufit should contain the following files: 404fc7eb3dSSebastien Boeuf 414fc7eb3dSSebastien Boeuf```bash 424fc7eb3dSSebastien Boeufll /home/foo/snapshot/ 434fc7eb3dSSebastien Boeuftotal 4194536 444fc7eb3dSSebastien Boeufdrwxrwxr-x 2 foo bar 4096 Jul 22 11:50 ./ 454fc7eb3dSSebastien Boeufdrwxr-xr-x 47 foo bar 4096 Jul 22 11:47 ../ 4610676b74SSebastien Boeuf-rw------- 1 foo bar 1084 Jul 22 11:19 config.json 47b552aebbSSebastien Boeuf-rw------- 1 foo bar 4294967296 Jul 22 11:19 memory-ranges 4810676b74SSebastien Boeuf-rw------- 1 foo bar 217853 Jul 22 11:19 state.json 494fc7eb3dSSebastien Boeuf``` 504fc7eb3dSSebastien Boeuf 5110676b74SSebastien Boeuf`config.json` contains the virtual machine configuration. It is used to create 5210676b74SSebastien Boeufa similar virtual machine with the correct amount of CPUs, RAM, and other 5310676b74SSebastien Boeufexpected devices. It is stored in a human readable format so that it could be 5410676b74SSebastien Boeufmodified between the snapshot and restore phases to achieve some very special 5510676b74SSebastien Boeufuse cases. But for most cases, manually modifying the configuration should not 5610676b74SSebastien Boeufbe needed. 5710676b74SSebastien Boeuf 58b552aebbSSebastien Boeuf`memory-ranges` stores the content of the guest RAM. 594fc7eb3dSSebastien Boeuf 6010676b74SSebastien Boeuf`state.json` contains the virtual machine state. It is used to restore each 6110676b74SSebastien Boeufcomponent in the state it was left before the snapshot occurred. 624fc7eb3dSSebastien Boeuf 63e6e58e6dSSebastien Boeuf## Restore a Cloud Hypervisor VM 644fc7eb3dSSebastien Boeuf 654fc7eb3dSSebastien BoeufGiven that one has access to an existing snapshot in `/home/foo/snapshot`, 664fc7eb3dSSebastien Boeufit is possible to create a new VM based on this snapshot with the following 674fc7eb3dSSebastien Boeufcommand: 684fc7eb3dSSebastien Boeuf 694fc7eb3dSSebastien Boeuf```bash 704fc7eb3dSSebastien Boeuf./cloud-hypervisor \ 714fc7eb3dSSebastien Boeuf --api-socket /tmp/cloud-hypervisor.sock \ 724fc7eb3dSSebastien Boeuf --restore source_url=file:///home/foo/snapshot 734fc7eb3dSSebastien Boeuf``` 744fc7eb3dSSebastien Boeuf 754fc7eb3dSSebastien BoeufOr using two different commands from two terminals: 764fc7eb3dSSebastien Boeuf 774fc7eb3dSSebastien Boeuf```bash 784fc7eb3dSSebastien Boeuf# First terminal 794fc7eb3dSSebastien Boeuf./cloud-hypervisor --api-socket /tmp/cloud-hypervisor.sock 804fc7eb3dSSebastien Boeuf 814fc7eb3dSSebastien Boeuf# Second terminal 82fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/cloud-hypervisor.sock restore source_url=file:///home/foo/snapshot 834fc7eb3dSSebastien Boeuf``` 844fc7eb3dSSebastien Boeuf 854fc7eb3dSSebastien BoeufRemember the VM is restored in a `paused` state, which was the VM's state when 864fc7eb3dSSebastien Boeufit was snapshot. For this reason, one must explicitly `resume` the VM before to 874fc7eb3dSSebastien Boeufstart using it. 884fc7eb3dSSebastien Boeuf 894fc7eb3dSSebastien Boeuf```bash 90fa22cb0bSRavi kumar Veeramally./ch-remote --api-socket=/tmp/cloud-hypervisor.sock resume 914fc7eb3dSSebastien Boeuf``` 924fc7eb3dSSebastien Boeuf 934fc7eb3dSSebastien BoeufAt this point, the VM is fully restored and is identical to the VM which was 944fc7eb3dSSebastien Boeufsnapshot earlier. 95*26670e47SPurna Pavan Chandra 96*26670e47SPurna Pavan Chandra## Restore a VM with new Net FDs 97*26670e47SPurna Pavan ChandraFor a VM created with FDs explicitly passed to NetConfig, a set of valid FDs 98*26670e47SPurna Pavan Chandraneed to be provided along with the VM restore command in the following syntax: 99*26670e47SPurna Pavan Chandra 100*26670e47SPurna Pavan Chandra```bash 101*26670e47SPurna Pavan Chandra# First terminal 102*26670e47SPurna Pavan Chandra./cloud-hypervisor --api-socket /tmp/cloud-hypervisor.sock 103*26670e47SPurna Pavan Chandra 104*26670e47SPurna Pavan Chandra# Second terminal 105*26670e47SPurna Pavan Chandra./ch-remote --api-socket=/tmp/cloud-hypervisor.sock restore source_url=file:///home/foo/snapshot net_fds=[net1@[23,24],net2@[25,26]] 106*26670e47SPurna Pavan Chandra``` 107*26670e47SPurna Pavan ChandraIn the example above, the net device with id `net1` will be backed by FDs '23' 108*26670e47SPurna Pavan Chandraand '24', and the net device with id `net2` will be backed by FDs '25' and '26' 109*26670e47SPurna Pavan Chandrafrom the restored VM. 1104fc7eb3dSSebastien Boeuf 1114fc7eb3dSSebastien Boeuf## Limitations 1124fc7eb3dSSebastien Boeuf 113b552aebbSSebastien BoeufVFIO devices and Intel SGX are out of scope. 114