Table of Content

Google cloud hands-on guide: Creating a Persistent Disk

qwiklabs GCP notes.

what is persistent disk

Google Compute Engine provides persistent disks for use as the primary storage for your virtual machine instances.

  • persistent disks exist independently of the rest of your machine
  • when vm deleted, attached persistent disk continues to retain its data, can be attached to another vm

2 kind of persistent disk type

  • Standard persistent disk
  • SSD Persistent disk

create persistent disk via gcloud cli

create a vm gcelab

gcloud compute instances create gcelab --zone us-central1-a
Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-86b609a9115a2f5a/zones/us-central1-a/instances/gcelab].
NAME    ZONE           MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
gcelab  us-central1-a  n1-standard-1               10.128.0.2   104.197.141.39  RUNNING

create mydisk 200GB

gcloud compute disks create mydisk --size=200GB --zone us-central1-a
NAME    ZONE           SIZE_GB  TYPE         STATUS
mydisk  us-central1-a  200      pd-standard  READY

New disks are unformatted. You must format and mount a disk before it
can be used. You can find instructions on how to do this at:

https://cloud.google.com/compute/docs/disks/add-persistent-disk#formatting

attach mydisk to vm gcelab,

gcloud compute instances attach-disk gcelab --disk mydisk --zone us-central1-a
Updated [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-86b609a9115a2f5a/zones/us-central1-a/instances/gcelab].

verify disk created

google150702_student@qwiklabs-gcp-3fbe14f6eb2265b8:~$ gcloud compute disks list
NAME    ZONE           SIZE_GB  TYPE         STATUS
gcelab  us-central1-a  10       pd-standard  READY
mydisk  us-central1-a  200      pd-standard  READY

ssh to vm

gcloud compute ssh gcelab --zone us-central1-a
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.
WARNING: SSH keygen will be executed to generate a key.
This tool needs to create the directory
[/home/google150688_student/.ssh] before being able to generate SSH
keys.

Do you want to continue (Y/n)?  y

Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/google150688_student/.ssh/google_compute_engine.
Your public key has been saved in /home/google150688_student/.ssh/google_compute_engine.pub.
The key fingerprint is:
87:78:90:96:ef:f8:7d:8c:f2:94:32:60:21:5f:34:b0 google150688_student@cs-6000-devshell-vm-dcb3ae53-6b79-46fb-abfd-8cc7c2c0aeb2
The key's randomart image is:
+---[RSA 2048]----+
|      ..o        |
|       = .       |
|    . E .        |
|     + * .       |
|      = S .      |
|     . = . .     |
|      . + oo     |
|       ..=. o    |
|        .oo.     |
+-----------------+
Updating project ssh metadata.../Updated [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-86b609a9115a2f5a].
Updating project ssh metadata...done.
Waiting for SSH key to propagate.
Warning: Permanently added 'compute.7186489777545280626' (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.

find disk device name

ls -l /dev/disk/by-id/
total 0
lrwxrwxrwx 1 root root  9 Dec  2 06:05 google-persistent-disk-0 -> ../../sda
lrwxrwxrwx 1 root root 10 Dec  2 06:05 google-persistent-disk-0-part1 -> ../../sda1
lrwxrwxrwx 1 root root  9 Dec  2 06:07 google-persistent-disk-1 -> ../../sdb
lrwxrwxrwx 1 root root  9 Dec  2 06:05 scsi-0Google_PersistentDisk_persistent-disk-0 -> ../../sda
lrwxrwxrwx 1 root root 10 Dec  2 06:05 scsi-0Google_PersistentDisk_persistent-disk-0-part1 -> ../../sda1
lrwxrwxrwx 1 root root  9 Dec  2 06:07 scsi-0Google_PersistentDisk_persistent-disk-1 -> ../../sdb
google150688_student@gcelab:~$

so new created disk device name:
scsi-0Google_PersistentDisk_persistent-disk-1

format and mount persistent disk

create mount point

sudo mkdir /mnt/mydisk

create new filesystem for new disk device

google150688_student@gcelab:~$ sudo mkfs.ext4 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/scsi-0Google_PersistentDisk_persistent-disk-1
mke2fs 1.43.4 (31-Jan-2017)
Discarding device blocks: done             
Creating filesystem with 52428800 4k blocks and 13107200 inodes
Filesystem UUID: 0fcd85ac-1e43-49f4-937c-e19baff76821
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done     
Writing inode tables: done     
Creating journal (262144 blocks):
done
Writing superblocks and filesystem accounting information: done 

mount new disk

google150688_student@gcelab:~$ sudo mount -o discard,defaults /dev/disk/by-id/scsi-0Google_PersistentDisk_persistent-disk-1 /mnt/mydisk
google150688_student@gcelab:~$ ls -ltr /mnt/mydisk
total 16
drwx------ 2 root root 16384 Dec  2 06:15 lost+found

auto mount persistent disk

If you want to disk auto mounted on next vm reboot, can add entry to /etc/fstab,

sudo vi /etc/fstab
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