'Clone a google cloud VM

I have google cloud VM with Ubuntu installed along with various services and libraries. I need to make a similar bootable VM with the same OS and all the data, libraries etc in the already configured VM. How do I clone the VM with these requirements?

I tried to create an image from the already existing VM and could not SSH into it.

So I retraced my installations step by step trying to figure out which step is breaking the image. I created an Ubuntu(18.04) VM and used that to create an image. The instance I created using the image did allow me to SSH into.

Next installed Ubuntu desktop and xorg server and created an image after that. Using that image, I created a new VM and tried to SSH into it. But unfortunately, the SSH connection could not be established. So I think it is these installations that are causing the error if it is not some sort of system error.

Below are the exact commands I ran to install these after creating an Ubuntu(18.04) VM:

 sudo passwd username
  sudo su -
  passwd
  apt update && apt upgrade -y
  adduser username root
  adduser username admin
  adduser username sudo

  apt-get install ubuntu-desktop -y
  apt-get install xserver-xorg-video-dummy
  
  nano /etc/X11/xorg.conf

and pasted the following into the .conf file

Section "Device"
    Identifier  "Configured Video Device"
    Driver      "dummy"
  EndSection
  Section "Monitor"
    Identifier  "Configured Monitor"
    HorizSync 31.5-48.5
    VertRefresh 50-70
  EndSection
  Section "Screen"
    Identifier  "Default Screen"
    Monitor     "Configured Monitor"
    Device      "Configured Video Device"
    DefaultDepth 24
    SubSection "Display"
    Depth 24
    Modes "1600x900"
    EndSubSection
  EndSection

After this state, I created the image using which I could not instantiate a VM that I could SSH into.



Solution 1:[1]

Since you have your VM ready and running; backup your image as per this GCP document. Follow the guidelines before you begin the process which were mentioned in the document like updating Google cloud CLI setting default region and zone and for general image guidelines. Few networking features may require guest operating system mode. You can also check how to export a custom image to cloud storage. You can also consider the Snapshot Approach.

Solution 2:[2]

Follow this process in order to create the image exactly as the one you have already set up and you know is working correctly. As you may already know, this is a custom image so they are available only to your Cloud project. You can create a custom image from boot disks and other images if you would like also. Then, use the custom image to create an instance.

I will also suggest you to give a look at this document which would give you a deeper knowledge on the task.

Regards,

Solution 3:[3]

Just spin up a new container from a disk snapshot, if you need an exact copy. And if you cannot SSH, you may either not have a SSH public key provisioned, no external IP assigned, or :22 closed.

gcloud ssh always works. One can as well provision project-wide SSH keys, which all VM in the project will inherit then. The documentation below: About VM metadata explains this all in detail.

My personal favorite are rather startup scripts, which describe the configuration, instead of copying it. And it's not so difficult to get started with these: cat ~/.bash_history > rocky8_startup.sh. In a software-defined data-center, it might make sense to use software-defined configurations (one simply cannot alternate the installation per VM instance, when starting with a disk snapshot).


xserver-xorg-video-dummy is questionable, because one can enable display device -
but unless recording the screen, this driver might still suffice; eg. for VNC sessions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhijith Chitrapu
Solution 2 Cristopher
Solution 3