If you need to move your Compute Engine boot disk data
outside of your Compute Engine project, you can export a boot disk
image to Cloud Storage as a
tar.gz file. If you need to
create a persistent disk image to use when you create new persistent disks on
Compute Engine, read
Creating a Custom Image.
You can export a custom image as backup or for sharing by exporting the image to Cloud Storage. This method is ideal for sharing individual images with other projects that do not have access to your images. Alternatively, you can share images by granting the Compute Engine Image User Role on the image or on the project that contains it.
The following diagram shows some typical workflows for the creation and reuse of a custom image.
Before you begin
- If you want to use the command-line examples in this guide:
- If you want to use the API examples in this guide, set up API access.
- Read the images page.
Exporting an image with a single command
The preferred way to export an image to Cloud Storage is to use the
gcloud compute images export
command. This command uses
to chain together the multiple steps that are required to export an image. It
assumes that you have already
created an image,
for example, with the
gcloud compute images create
Using the gcloud command-line tool, run:
gcloud compute images export --destination-uri [DESTINATION_URI] \ --image [IMAGE]
[DESTINATION_URI]is the Cloud Storage URI destination for the exported virtual disk file.
[IMAGE]is the name of the disk image to export.
For example, the following command exports an image named
my-project to a Cloud Storage bucket named
my-bucket. By default, the
image is exported as a disk.raw file and is compressed into the
gcloud compute images export --destination-uri gs://my-bucket/my-image.tar.gz \ --image my-image --project my-project
For a list of all available flags see the
gcloud compute images export
Creating and exporting an image manually
gcloud compute images create and
gcloud compute images export
commands do not meet your requirements, you can create and export an image
manually from a Compute Engine instance. This process has discrete
steps to first create an image and then export an image.
In the following example, note the created disk is called image-disk.
To create and export an image:
Optionally, stop the instance that the disk is attached to before you create the snapshot. Stopping the instance ensures the integrity of the disk contents in the snapshot.
Create a snapshot of the disk. Name the snapshot
gcloud compute disks snapshot [DISK_NAME] --snapshot-names image-snapshot
[DISK_NAME]is the name of the disk from which you create the snapshot.
image-snapshotsnapshot to create a new disk named
image-diskby running the following command:
gcloud compute disks create image-disk --source-snapshot image-snapshot
Create a temporary disk named
temporary-diskto hold your tar file, and specify the
[SIZE]of the disk to be at least 50% larger than the image disk.
You can detach and delete the disk afterwards.
gcloud compute disks create temporary-disk --size [SIZE]
[SIZE]is the size of the temporary disk in GB or TB units. For example, specify
100GBto create a 100 gigabyte disk.
Create a new instance and enable
storage-rwscope on the instance. Also, attach the
temporary-diskto the instance as secondary disks with specific
gcloud compute instances create [INSTANCE_NAME] --scopes storage-rw \ --disk name=image-disk,device-name=image-disk \ --disk name=temporary-disk,device-name=temporary-disk
[INSTANCE_NAME]is the name of the instance to create.
Note that you're passing in service account scopes so that you can upload your file to Google Cloud Storage in later steps.
Review the details about starting a new instance if necessary.
gcloud compute ssh [INSTANCE_NAME]
[INSTANCE_NAME]is the instance to connect to.
Format and mount the temporary disk. Formatting the disk deletes the contents of the temporary disk.
sudo mkdir /mnt/tmp
sudo mkfs.ext4 -F /dev/disk/by-id/google-temporary-disk
sudo mount -o discard,defaults /dev/disk/by-id/google-temporary-disk /mnt/tmp
Optionally, you can mount the image disk and make additional changes before you create the tar file. For example, you might want to delete any existing files from the
/homedirectory if you do not want them to be part of your image. Mount the disk partitions that you need to modify, modify the files on the disk that you need to change, and then unmount the disk when you are done.
Create a directory where you can mount your disk or partition.
sudo mkdir /mnt/image-disk
lscommand to determine which disk or disk partition you need to mount.
The command prints a list of disk IDs and partitions. For example, the following disk has a partition table with one partition. The
google-image-diskID points to the full disk from which you want to create an image. The
google-image-disk-part1ID points to the first partition on this disk. Mount the partition if you need to make changes to the disk, then create the image from the full disk.
Mount the disk or the partition. If your disk has a partition table, mount the individual partitions for your disk. For example, mount
sudo mount /dev/disk/by-id/google-image-disk-part1 /mnt/image-disk
Alternatively, if your disk is raw formatted with no partition table, mount the full
sudo mount /dev/disk/by-id/google-image-disk /mnt/image-disk
Modify the files in the
/mnt/image-diskdirectory to configure the files on the disk. As an example, you might remove the
/mnt/image-disk/home/[USER]/.ssh/authorized_keysfile to protect your SSH keys from being shared.
After you are done modifying files, unmount the disk.
sudo umount /mnt/image-disk/
Create a tar file of your image.
When you finish customizing the files on the image disk, create a raw disk file on your temporary disk. The name of the raw disk image must be 'disk.raw':
sudo dd if=/dev/disk/by-id/google-image-disk of=/mnt/tmp/disk.raw bs=4096
Then tar and gzip this file:
sudo tar czvf myimage.tar.gz disk.raw
This command will create an image of the instance in the following location:
Upload the image into Google Cloud Storage
To upload the tar file to Google Cloud Storage, use the gsutil command line tool that comes preinstalled on your instance.
Create a bucket using
Make sure to review the Bucket and Naming Guidelines before you create your bucket. Then, create your bucket using the following command:
me@example-instance:~$ gsutil mb gs://[BUCKET_NAME]
[BUCKET_NAME]is the name of the bucket to be created.
Copy your file to your new bucket.
me@example-instance:~$ gsutil cp /mnt/tmp/myimage.tar.gz gs://[BUCKET_NAME]
[BUCKET_NAME]is the bucket to copy the file.
That's it, you have exported your file into Google Cloud Storage. You can now share the image with other people, or use the tar file to add a new image to a Google Cloud Platform Console project.