arrow_back

Creating a Persistent Disk

Join Sign in
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

Creating a Persistent Disk

Lab 30 minutes universal_currency_alt 1 Credit show_chart Introductory
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

GSP004

Google Cloud self-paced labs logo

Overview

Compute Engine lets you create and run virtual machines on Google infrastructure. You can create virtual machines running different operating systems, including multiple flavors of Linux (Debian, Ubuntu, Suse, Red Hat, CoreOS) and Windows Server!

Compute Engine provides persistent disks for use as the primary storage for your virtual machine instances. Like physical hard drives, persistent disks exist independently of the rest of your machine – if a virtual machine instance is deleted, the attached persistent disk continues to retain its data and can be attached to another instance.

Note: There are 2 types of persistent disks:

  • Standard persistent disk
  • SSD Persistent disk
Learn more about the differences in Storage Option. Each type of persistent disks will have different capacity limits. Read more in the Persistent Disk documentation.

In this hands-on lab, you'll learn how to create a persistent disk and attach it to a virtual machine.

What you'll learn

  • Create a new VM instance and attach a persistent disk
  • Format and mount a persistent disk

Prerequisites

  • Familiarity with standard Linux text editors such as vim, emacs or nano will be helpful

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left. Navigation menu icon

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your Project_ID, . The output contains a line that declares the Project_ID for this session:

Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

Output:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = {{{project_0.project_id | "PROJECT_ID"}}} Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Set the region and zone

  1. Set the project region and zone for this lab:
gcloud config set compute/zone {{{project_0.default_zone | Zone}}} gcloud config set compute/region {{{project_0.default_region | Region}}}
  1. Create a variable for region:
export REGION={{{project_0.default_region | Region}}}
  1. Create a variable for zone:
export ZONE={{{project_0.default_zone | Zone}}}

Learn more from the Regions & Zones documentation.

Note: When you run gcloud on your own machine, the config settings are persisted across sessions. But in Cloud Shell, you need to set this for every new session or reconnection.

Task 1. Create a new instance

First, create a Compute Engine virtual machine instance that has only a boot disk.

Note: You can learn more by creating a virtual machine instance in a different lab, or refer to the Compute Engine documentation.
  1. In Cloud Shell command line, use the gcloud command to create a new virtual machine instance named gcelab:
gcloud compute instances create gcelab --zone $ZONE --machine-type e2-standard-2

Example Output:

Created [...]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS gcelab {{{project_0.default_zone | Zone}}} e2-standard-2 10.240.X.X X.X.X.X RUNNING

The newly created virtual machine instance will have a default 10 GB persistent disk as the boot disk.

Click Check my progress to verify the objective.

Create a new instance in the specified zone.

Task 2. Create a new persistent disk

Note: Because you want to attach this disk to the virtual machine instance you created in the previous step, the zone must be the same.
  1. Still in the Cloud Shell command line, use the following command to create a new disk named mydisk:
gcloud compute disks create mydisk --size=200GB \ --zone $ZONE

Output:

NAME ZONE SIZE_GB TYPE STATUS mydisk {{{project_0.default_zone | Zone}}} 200 pd-standard READY

Click Check my progress to verify the objective.

Create a new persistent disk in the specified zone

Task 3. Attaching a disk

Attaching the persistent disk

You can attach a disk to a running virtual machine. Attach the new disk (mydisk) to the virtual machine instance you just created (gcelab).

  1. Use the following command to attach the disk:
gcloud compute instances attach-disk gcelab --disk mydisk --zone $ZONE

Output:

Updated [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-d12e3215bb368ac5/zones/{{{project_0.default_zone | Zone}}}/instances/gcelab].

That's it!

Finding the persistent disk in the virtual machine

The persistent disk is now available as a block device in the virtual machine instance. Let's take a look.

  1. SSH into the virtual machine:
gcloud compute ssh gcelab --zone $ZONE

Output:

WARNING: The public SSH key file for gcloud does not exist. WARNING: The private SSH key file for gcloud does not exist. WARNING: You do not have an SSH key for gcloud. WARNING: SSH keygen will be executed to generate a key. This tool needs to create the directory [/home/gcpstaging8246_student/.ssh] before being able to generate SSH keys. Do you want to continue (Y/n)? y
  1. At the prompt, enter Y to continue.
  2. When prompted for an RSA key pair passphrase, press ENTER for no passphrase, and then press ENTER again to confirm no passphrase.

Output:

Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/gcpstaging8246_student/.ssh/google_compute_en gine. Your public key has been saved in /home/gcpstaging8246_student/.ssh/google_compute_engine .pub. The key fingerprint is: 6c:04:bf:29:95:0d:93:bc:fe:00:2c:85:86:f8:7a:53 gcpstaging8246_student@cs-6000-devshell-v m-dbb9559d-4412-4801-ad8c-bdaf885541a9 The key's randomart image is: +---[RSA 2048]----+ | . . ...o. | |. . o .oo= | | . . o =.. | | . E o+.o | | . . ..oS | |. o oo | | . . o | | . | | | +-----------------+ Updating project ssh metadata...\Updated [https://www.googleapis.com/compute/v1/projects/ qwiklabs-gcp-d12e3215bb368ac5]. Updating project ssh metadata...done. Waiting for SSH key to propagate. Warning: Permanently added 'compute.7714273689800906026' (ECDSA) to the list of known hosts. Linux gcelab 4.9.0-4-amd64 #1 SMP Debian 4.9.51-1 (2017-09-28) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.
  1. Now find the disk device by listing the disk devices in /dev/disk/by-id/.:
ls -l /dev/disk/by-id/

Output:

lrwxrwxrwx 1 root root 9 Feb 27 02:24 google-persistent-disk-0 -> ../../sda lrwxrwxrwx 1 root root 10 Feb 27 02:24 google-persistent-disk-0-part1 -> ../../sda1 lrwxrwxrwx 1 root root 9 Feb 27 02:25 google-persistent-disk-1 -> ../../sdb lrwxrwxrwx 1 root root 9 Feb 27 02:24 scsi-0Google_PersistentDisk_persistent-disk-0 -> ../../sda lrwxrwxrwx 1 root root 10 Feb 27 02:24 scsi-0Google_PersistentDisk_persistent-disk-0-part1 -> ../../sda1 lrwxrwxrwx 1 root root 9 Feb 27 02:25 scsi-0Google_PersistentDisk_persistent-disk-1 -> ../../sdb

You found the file, the default name is:

scsi-0Google_PersistentDisk_persistent-disk-1.

Note: If you want a different device name, when you attach the disk, you would specify the device-name parameter. For example, to specify a device name, when you attach the disk you would use the command:

gcloud compute instances attach-disk gcelab --disk mydisk --device-name <YOUR_DEVICE_NAME> --zone $ZONE

Formatting and mounting the persistent disk

Once you find the block device, you can partition the disk, format it, and then mount it using the following Linux utilities:

  • mkfs: creates a filesystem
  • mount: attaches to a filesystem
  1. Make a mount point:
sudo mkdir /mnt/mydisk
  1. Next, format the disk with a single ext4 filesystem using the mkfs tool. This command deletes all data from the specified disk:
sudo mkfs.ext4 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/scsi-0Google_PersistentDisk_persistent-disk-1

Last lines of the output:

Allocating group tables: done Writing inode tables: done Creating journal (262144 blocks): done Writing superblocks and filesystem accounting information: done
  1. Now use the mount tool to mount the disk to the instance with the discard option enabled:
sudo mount -o discard,defaults /dev/disk/by-id/scsi-0Google_PersistentDisk_persistent-disk-1 /mnt/mydisk

That's it!

Automatically mount the disk on restart

By default the disk will not be remounted if your virtual machine restarts. To make sure the disk is remounted on restart, you need to add an entry into /etc/fstab.

  1. Open /etc/fstab in nano to edit:
sudo nano /etc/fstab
  1. Add the following below the line that starts with "UUID=...":
/dev/disk/by-id/scsi-0Google_PersistentDisk_persistent-disk-1 /mnt/mydisk ext4 defaults 1 1

/etc/fstab content should look like this:

# /etc/fstab: static file system information UUID=12adc097-f36f-46f9-b377-b2a30cdf422f / ext4 rw,discard,errors=remount-ro,x-systemd.growfs 0 1 UUID=3A31-89F9 /boot/efi vfat defaults 0 0 /dev/disk/by-id/scsi-0Google_PersistentDisk_persistent-disk-1 /mnt/mydisk ext4 defaults 1 1
  1. Save and exit nano by pressing CTRL+O, ENTER, CTRL+X, in that order.

Click Check my progress to verify the objective.

Attaching and Mounting the persistent disk.

Task 4. Test your knowledge

Test your knowledge about Google cloud Platform by taking this quiz.

For migrating data from a persistent disk to another region, reorder the following steps in which they should be performed:

  1. Attach disk
  2. Create disk
  3. Create snapshot
  4. Create instance
  5. Unmount file system(s)

Task 5. Local SSDs

Compute Engine can also attach local SSDs. Local SSDs are physically attached to the server hosting the virtual machine instance to which they are mounted. This tight coupling offers superior performance, with very high input/output operations per second (IOPS) and very low latency compared to persistent disks.

Local SSD performance offers:

  • Less than 1 ms of latency
  • Up to 680,000 read IOPs and 360,000 write IOPs

These performance gains require certain trade-offs in availability, durability, and flexibility. Because of these trade-offs, local SSD storage is not automatically replicated and all data can be lost in the event of a host error or a user configuration error that makes the disk unreachable. Users must take extra precautions to backup their data.

This lab does not cover local SSDs.

  • To maximize the local SSD performance, you'll need to use a special Linux image that supports NVMe. You can learn more about local SSDs in the Local SSD documentation.

Congratulations!

You've learned how to create, find, and attach persistent disks to a virtual machine instance and the key difference between persistent disks and local SSDs. You can use persistent disks to setup and configure your database servers.

Next steps / Learn more

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated: January 15, 2024

Lab Last Tested: January 15, 2024

Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.