skip to Main Content

I am attempting to create an instance template from an existing powered off VM. The VM is a pretty basic Centos Linux box. After a bit of googling the problem I came up with the following but I can’t figure out how to format the disk device name. There are not a lot of examples out there I can find. Thanks in advance for any light you can shed on the subject.

$gcloud compute instance-templates create plainid-pocv12-cent8-autostart --source-instance ajm-centos8 --configure-disk=device-name=/dev/sda,instantiate-from=source-image,auto-delete=true

ERROR: (gcloud.compute.instance-templates.create) Could not fetch resource:
 - Invalid value for field 'resource.sourceInstanceParams.diskConfigs[0].deviceName': '/dev/sda'. Device name specified in disk instantiation config not found in source instance: '/dev/sda'.

Per the suggestions below, I ran the describe. Looks like the device name is also ‘ajm-centos8’

$gcloud compute instances describe ajm-centos8
canIpForward: false
confidentialInstanceConfig:
  enableConfidentialCompute: false
cpuPlatform: Intel Haswell
creationTimestamp: '2020-10-05T06:04:50.611-07:00'
deletionProtection: false
description: ''
disks:
- autoDelete: true
  boot: true
  deviceName: ajm-centos8
  diskSizeGb: '30'
  guestOsFeatures:
  - type: UEFI_COMPATIBLE
  - type: VIRTIO_SCSI_MULTIQUEUE
  - type: SEV_CAPABLE
  index: 0
  interface: SCSI
  kind: compute#attachedDisk
  licenses:
  - https://www.googleapis.com/compute/v1/projects/centos-cloud/global/licenses/centos-8
  mode: READ_WRITE
  source: https://www.googleapis.com/compute/v1/projects/plainid-presales/zones/us-east1-b/disks/ajm-centos8
  type: PERSISTENT

So now I am running the following command. It creates a template yay! However, the new template does not have any of the software I installed on the original VM installed on it.

$gcloud compute instance-templates create plainid-pocv12-cent8-autostart --source-instance ajm-centos8 --configure-disk=device-name=ajm-centos8,instantiate-from=source-image,auto-delete=true
Created [https://www.googleapis.com/compute/v1/projects/plainid-presales/global/instanceTemplates/plainid-pocv12-cent8-autostart].
NAME                            MACHINE_TYPE                   PREEMPTIBLE  CREATION_TIMESTAMP
plainid-pocv12-cent8-autostart  custom (e2, 2 vCPU, 6.00 GiB)               2020-10-09T13:55:02.563-07:00

3

Answers


  1. Instance templates are basically for defining and saving machine configuration, like the machine type, boot disk image or container image, labels, and other instance properties. Then the template can be used instantly to create individual VMs or MIG. You can take a look at Instance templates for more details.

    Documentation for creating an instance template based on an existing instance looks helpful here.Here in this document you can take a look at the options for overriding how disks are defined in the template.

    Login or Signup to reply.
  2. For you to be able to retain any program installed on your VM to your instance template, you must create the Instance template using the image you used from your original Instance.

    Follow the steps below to create an image from your original instance:

    1. Go to Console Menu > Compute Engine > Images.
    2. Click Create Image.
    3. Provide a Name for your image.
    4. Choose Image as Source .
    5. Click Create.

    From here, you can create your Instance template using the created image and all the application or programs that are pre installed on the image will be retained.

    Login or Signup to reply.
  3. As pointed out by other answers:

    • instance-template: Saves VM configuration such as machine-type, service-account, scopes, etc. But does not save the disk.
    • image: Saves the disk that can be used by VMs created from the template.

    So you need to create an image and then a template that uses that image. Here is how to do it from the command line:

    # Create the Golden VM with all the options you want
    $ gcloud compute instances create goldenvm --image-project=almalinux-cloud --image-family=almalinux-8 ...
    
    # Log to goldenvm and work your magic
    $ ssh goldenvm...
    
    # Power off goldenvm
    
    # Create an image from goldenvm disk
    $ gcloud compute images create goldenvm-image --source-disk=goldenvm --family=almalinux-8 --storage-location=europe-west2
    
    # Create a template from goldenvm that will use goldenvm-image
    $ gcloud compute instance-templates create goldenvm-template --source-instance=goldenvm --configure-disk=device-name=persistent-disk-0,instantiate-from=custom-image,custom-image="projects/your-project/global/images/goldenvm-image"
    
    # To create VMs using the template
    $ gcloud compute instances create  testvm --source-instance-template=goldenvm-template
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search