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
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.
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:
Create Image
.Name
for your image.Image
asSource
.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.
As pointed out by other answers:
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: