Exporting an Image

This page describes how to export a custom image to Google Cloud Storage as a tar.gz file. If you need to create an image from an instance, read the Creating a custom image documentation.

You can export a custom image as backup or for sharing by exporting the image as a tar.gz file to Google Cloud Storage. This method is ideal for sharing individual images with other projects that do not have access to your images.

Alternatively, if you are comfortable making all images within a project accessible to another user or project, you can share images by granting the Compute Engine Image User Role.

Before you begin

Exporting an image

To create and export an image:

  1. 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.

  2. Create a snapshot of the disk. Name the snapshot image-snapshot.

    gcloud compute disks snapshot [SNAPSHOT_NAME] --snapshot-names image-snapshot
    

    where [SNAPSHOT_NAME] is the name of the disk from which you created the snapshot.

  3. Use the image-snapshot snapshot to create a new disk named image-disk by running the following command:

    gcloud compute disks create image-disk --source-snapshot image-snapshot
    
  4. Create a temporary disk named temporary-disk to 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]
    

    where [SIZE] is the size of the temporary disk in GB or TB units. For example, specify 100GB to create a 100 gigabyte disk.

  5. Create a new instance and enable storage-rw scope on the instance. Also, attach the image-disk and the temporary-disk to the instance as secondary disks with specific device-name attributes.

    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
    

    where [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.

  6. Connect to your instance:

    gcloud compute ssh [INSTANCE_NAME]
    

    where [INSTANCE_NAME] is the instance to connect to.

  7. 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
    
  8. 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 /home directory 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.

    1. Create a directory where you can mount your disk or partition.

      sudo mkdir /mnt/image-disk
      
    2. Use the ls command to determine which disk or disk partition you need to mount.

      ls /dev/disk/by-id/
      

      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-disk ID points to the full disk from which you want to create an image. The google-image-disk-part1 ID 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.

      google-image-disk
      google-image-disk-part1
      
    3. Mount the disk or the partition. If your disk has a partition table, mount the individual partitions for your disk. For example, mount google-image-disk-part1.

      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 google-image-disk disk.

      sudo mount /dev/disk/by-id/google-image-disk /mnt/image-disk
      
    4. Modify the files in the /mnt/image-disk directory to configure the files on the disk. As an example, you might remove the /mnt/image-disk/home/[USER]/.ssh/authorized_keys file to protect your SSH keys from being shared.

    5. After you are done modifying files, unmount the disk.

      sudo umount /mnt/image-disk/
      
  9. 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:

    cd /mnt/tmp
    
    sudo tar czvf myimage.tar.gz disk.raw
    

    This command will create an image of the instance in the following location:

    /mnt/tmp/myimage.tar.gz
    
  10. Upload an 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.

    1. Create a bucket using gsutil.

      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]

      where [BUCKET_NAME] is the name of the bucket to be created.

    2. Copy your file to your new bucket.

      me@example-instance:~$ gsutil cp /mnt/tmp/myimage.tar.gz gs://[BUCKET_NAME]

      where [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.

What's next

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Compute Engine Documentation