How to move a VM instance between projects on Google Cloud Platform

Robert Kubis
May 20, 2015 · 4 min read

Currently there is no one-line command to move a Linux VM instance between Google Cloud Platform projects, but I’ve created an easy to follow step-by-step guide that you can use. (Note: This guide does not cover Windows.)

Quick summary of steps needed

  • Detach the boot disk from the VM that you intend to move by deselecting “Delete boot disk on instance delete” and terminating the VM
  • Create an image from the detached boot disk
  • Upload the image to Google Cloud Storage and share it with the new project
  • Create a custom image under the new project based on the image you uploaded to Google Cloud Storage
  • Create a new VM instance under the new project based on the custom image

Detailed steps of moving a VM between projects

Deselect boot disk deletion (Important!) and terminate the VM that you want to move (I recommend shutting down gracefully before termination)

Image for post
Image for post

Create a temporary disk in the same zone as the disk from your terminated VM. Make it large enough to fit your VM image (use at least twice the size of the persistent disk of your VM - larger means faster)

Image for post

Create a VM instance with full access to Google Cloud Storage under the old project. Use the same zone the temporary and terminated VM disks are in. Don’t attach the temporary and terminated VM disks yet.

Image for post
Image for post
Select Full access to Storage

Attach the temporary disk and the disk from your terminated VM to the running VM we just created for the image preparation. This attaches the disks after boot and avoids boot complications.

Image for post

SSH into the preparation VM, format and mount the temporary disk

Image for post
$ /usr/share/google/safe_format_and_mount -m "mkfs.ext4 -F" /dev/sdb /mnt/temp
$ cd /mnt/temp

Use gcimagebundle to create a compressed image of the boot disk from your terminated VM. Once that is done upload it to a bucket in Google Cloud Storage with gsutil. Jump to the next step for instructions on how to create a bucket in Google Cloud Storage for the upload.

$ cd /mnt/temp
$ sudo gcimagebundle -d /dev/sdc -r /mnt/old-vm-disk -o /mnt/temp/ --log_file=/mnt/temp/image-create.log
> Created tar.gz file at /mnt/temp/62a70588477be42caf45c5c5c58a7e59a8aa410e.image.tar.gz
$ gsutil cp 62a70588477be42caf45c5c5c58a7e59a8aa410e.image.tar.gz gs://vm-transfer
> Uploading ...588477be42caf45c5c5c58a7e59a8aa410e.image.tar.gz: 235.73 MiB/235.73 MiB

While gcimagebundle is running create a Google Cloud Storage bucket in your old project where the preparation VM is running

Image for post

Make the Google Cloud Storage Object accessible to you in your new project by adding your email to the object permissions

Image for post
Image for post

Create a custom image from the Google Cloud Storage object in your new project

Image for post
Image for post
Image for post

Create a VM instance from your custom image and voilà! ☺

Image for post

Last but not least, cleanup all the resource you don’t need anymore. Once you’ve verified that the move of your VM was successful you can delete the temporary disk, the disk from the terminated VM, the preparation VM and the bucket in Google Cloud Storage from the old project.

This blog post used information from https://cloud.google.com/compute/docs/images#export_an_image_to_google_cloud_storage

Google Cloud - Community

Google Cloud community articles and blogs

Robert Kubis

Written by

Google Cloud DA, Developer, Do-it mentality, Enthusiast. Enjoying tech trends, food, travel, all kinds of outdoor activities, flying. Views are my own.

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Robert Kubis

Written by

Google Cloud DA, Developer, Do-it mentality, Enthusiast. Enjoying tech trends, food, travel, all kinds of outdoor activities, flying. Views are my own.

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store