In the previous post we have learned how you can change your machine type of running VM instance on Google Compute Engine. This post will guide you how you can move your running instance from one project to some other project.
1) We need to make image of instance and copy it on Google Bucket. For this SSH into instance that you want to move.
Developer Console -> Compute Engine -> VM Instance -> SSH (any-instance)
2) Make temporary directory to copy the image of running instance
sudo mkdir /mnt/tmp
3) Now list the drives
ls -l /dev/disk/by-id/google-*
4) Check disk that you want to export to bucket (as we are moving instance and it is mostly installed on sda)
5) As we are going to create image on temporary folder so there must have enough space for new image file. For this we can mount non persistent disk if not enough space on prime drive (sda). In case you don’t have external disk you can create new one from Developer Console and attach it to running instance.
6) The following command will format and mount the external non persistent disk (sdb)
sudo /usr/share/google/safe_format_and_mount -m "mkfs.ext4 -F" /dev/sdb /mnt/tmp
7) Now using imagebunde package create image of vm instance
sudo gcimagebundle -d /dev/sda -o /mnt/tmp/ --log_file=/tmp/abc.log
8) Now check the new tar file in location
ls -l /mnt/tmp
9) To export this on Google Bucket we need to provide owner permission to service account. For this check the service account by this command
curl "http://metadata/computeMetadata/v1/instance/service- accounts/" -H "Metadata-Flavor:Google"
10) Now provide this service account the owner permission on Google Bucket from Developer Console
11) Change the permission of your newly created image (tar file)
chmod u=rwx,g=rwx,o=rwx mnt/tmp/d0e9184d539ae7c0274590942ef05af13450.image.tar.gz
12 ) Copy this tar file to Google Bucket using
gsutil cp /mnt/tmp/d0e9184d539ae7c0274590942ef05af13450.image.tar.gz gs://bucket_name
13 ) Check this file on Google Bucket and share it publicly.
14) Now under new project create a new image from Developer Console
Developer Console -> Compute Engine -> Images -> New Image -> Select image source from google bucket
15) Make an instance from Developer Console using this image created above.