Boot disk images in GCP - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When creating virtual machines, boot disk images are copied to start the machine. Understanding how the time to create these machines grows helps us plan better.
We want to know: how does the time to create multiple VMs with boot disk images grow as we add more machines?
Analyze the time complexity of creating multiple VMs each with a boot disk image.
for i in range(n):
compute.instances().insert(
project=project_id,
zone=zone,
body={
'name': f'vm-{i}',
'disks': [{
'boot': True,
'initializeParams': {'sourceImage': image_url}
}],
'machineType': machine_type
}
).execute()
This code creates n virtual machines, each with a boot disk initialized from the same image.
Look at what repeats as we create more VMs.
- Primary operation: API call to create a VM with a boot disk image.
- How many times: Once per VM, so n times.
Each VM creation requires copying the boot disk image once. So, if you create more VMs, the total work grows directly with the number of VMs.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 VM creation calls |
| 100 | 100 VM creation calls |
| 1000 | 1000 VM creation calls |
Pattern observation: The number of operations grows in a straight line as you add more VMs.
Time Complexity: O(n)
This means the time to create VMs grows directly with how many you want to create.
[X] Wrong: "Creating multiple VMs with the same boot image happens instantly after the first one."
[OK] Correct: Each VM needs its own boot disk copy, so the work repeats for each VM, not just once.
Understanding how resource creation scales helps you design systems that grow smoothly. This skill shows you can think about costs and delays as systems get bigger.
"What if we used a shared persistent disk instead of separate boot disks? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of boot disk images
Boot disk images contain the operating system that a VM uses to start and run.Step 2: Identify the correct function in GCP context
In GCP, the boot disk image is the source of the OS for the VM instance.Final Answer:
It provides the operating system for a virtual machine (VM). -> Option BQuick Check:
Boot disk image = OS provider [OK]
- Confusing boot disk image with data disk
- Thinking it manages network or billing
- Assuming it stores user files
Solution
Step 1: Recall the correct gcloud command for listing images
The command to list images uses 'gcloud compute images list' with a project filter for public images.Step 2: Identify the public image project
Public images like Debian are under projects such as 'debian-cloud'. So filtering by --project=debian-cloud lists those images.Final Answer:
gcloud compute images list --project=debian-cloud -> Option AQuick Check:
List images by project = gcloud compute images list --project=debian-cloud [OK]
- Using --public flag which does not exist
- Listing disks instead of images
- Listing instances instead of images
gcloud compute instances create my-vm \ --image-family=ubuntu-2204-lts \ --image-project=ubuntu-os-cloud
What OS will the VM run?
Solution
Step 1: Analyze the image family and project
The image family 'ubuntu-2204-lts' in project 'ubuntu-os-cloud' refers to Ubuntu 22.04 Long Term Support.Step 2: Match image family to OS version
Ubuntu 22.04 LTS is the latest stable Ubuntu release matching the image family name.Final Answer:
Ubuntu 22.04 LTS -> Option CQuick Check:
Image family ubuntu-2204-lts = Ubuntu 22.04 LTS [OK]
- Confusing Ubuntu with Debian or CentOS
- Ignoring image-project parameter
- Assuming Windows from Linux image family
gcloud compute instances create test-vm \ --image-family=centos-7 \ --image-project=centos-cloud \ --boot-disk-size=5GB
The command fails with an error about disk size. What is the likely cause?
Solution
Step 1: Understand boot disk size requirements
CentOS 7 images require a minimum boot disk size larger than 5GB, usually 10GB or more.Step 2: Check command parameters
The image family and project are correct, and VM name is valid, so the error is due to insufficient disk size.Final Answer:
The boot disk size is too small for the CentOS 7 image. -> Option DQuick Check:
Disk size too small causes boot failure [OK]
- Assuming image family or project is wrong
- Ignoring disk size minimum requirements
- Thinking VM name causes disk size error
Solution
Step 1: Create a snapshot of the existing VM's boot disk
This captures the current state of the disk safely and efficiently.Step 2: Create a custom image from the snapshot
Using the snapshot, you create a reusable boot disk image for new VMs.Final Answer:
Create a snapshot of the VM's boot disk, then create an image from the snapshot. -> Option AQuick Check:
Snapshot then image = reusable boot disk [OK]
- Trying to copy VM directly without image
- Exporting disk unnecessarily
- Using existing disk directly for new VMs
