Author: Jacob Mammoliti


Introduction

Google Cloud Platform (GCP) offers a huge selection of OS images that are consistently updated and available to spin up virtual machines. However, sometimes there is the need to utilize a custom boot image when there is a need to run a hardened image or to run an OS that isn’t available as a public image. Another use case we often see is the need to run Red Hat Enterprise Linux (RHEL) instances in the cloud leveraging existing subscriptions. GCP does offer RHEL 6 and 7 images, but these images have incorporated the cost of the subscription in the cloud fee. To avoid double paying, users can upload custom image to deploy RHEL compute instances.

Prerequisites

To complete this blog, you’ll need:

  • A GCP account
  • Access to a GCP project where you can create a buckets, images, and compute instances
  • The Google SDK installed on your machine

The Process

To get started, download the latest KVM version of RHEL from access.redhat.com which at the time of writing this is 7.5. This will provide a qcow2 image that we will eventually upload. GCP requires that we upload a TAR package that has a GZIP compression with a raw disk inside, so the first step after downloading the KVM image is to convert it to a RAW format. Leverage the qemu-img tool to convert the qcow2 image to RAW format as shown below.

Please note: it is crucial to name the raw disk “disk.raw” as this is the name GCP looks for when it decompresses the TAR and will fail when trying to build the image if the name is different.

~]$ qemu-img convert -f qcow2 \
                     -O raw rhel-server-7.5-x86_64-kvm.qcow2 disk.raw

Once the image in RAW format, the next step is to compress the image as a TAR package with GZIP compression. Give the compressed file a meaningful name (such as the example below) as this will help with identification as additional images are uploaded.

~]$ gtar -Sczf gcerhel75.tar.gz disk.raw

This process may take a bit of time but once completed, it is time to upload the compressed image to a GCP Storage Bucket. If you are unfamiliar with Buckets, Buckets are one of the many storage solutions Google offers and is a solution that provides users a place to store objects, such as the compressed image.

Utilizing the gsutil utility allows us to interact with Google Cloud Buckets. The first step is to create the Bucket that will contain the image. Bucket names must be globally unique within your organization, so use something clever.

~]$ gsutil mb gs://jacobsimagebucket

Now that the Bucket is created, upload the image utilizing the gsutil tool again to copy the local image to the Bucket. Again, this may take some time depending on your internet connection and the size of the image.

~]$ gsutil cp gcerhel75.tar.gz gs://jacobsimagebucket
bucket_preview

The next step is to create an image that will be stored and used when building future compute instances in the GCP project. Sticking with the Google SDK, utilize the gcloud utility to create this Image. Again, use a meaningful name here as this will help to identify the appropriate image when trying to select a boot ISO for a compute instance.

~]$ gcloud compute images create gcerhel75 \
		--source-uri gs://jacobsimagebucket/gcerhel75.tar.gz
image_preview

Once that has completed, the custom image is successfully uploaded to the Google Cloud Platform! The final step is to create a compute instance and verify that the virtual machine boots properly. Again, the gcloud tool can be used here to create a compute instance. The example below omits the customizations such as the machine type and disk size for simplicity and will create an n1-standard-1 machine by default.

~]$ gcloud compute instances create jacob-test \
		--image rhel7 --zone us-central1-a

Created [https://www.googleapis.com/compute/v1/projects/<redacted>].
NAME        ZONE           MACHINE_TYPE     INTERNAL_IP    STATUS
jacob-test  us-central1-a  n1-standard-1    10.128.0.2     RUNNING

Conclusion

That’s all there is to successfully uploading a custom image to the Google Cloud Platform. The process is straightforward as long as you follow the correct naming conventions and file types. If you are at all curious about further customizing images or Google Cloud Platform in general, please drop a comment below or contact me through the chat icon on the website homepage! I have been heavily focusing on the Google Cloud Platform recently and would love to hear what you think about it and what challenges you are trying to solve.

Tagged:



//comments