I really enjoyed the Qwiklabs – GCP Essentials – Creating a Persistent Disk lab, but I think Qwiklabs could extend the lab a bit further by showing how the disk is persistent by blowing away a VM, starting a new one and reattaching the disk. So I decided to try it. You can follow the steps below to do just that.
Start here after you’ve finished the “Creating a Persistent Disk” lab.
Let’s create a new folder on our persistent disk. Run mkdir /mnt/mydisk (while still SSH-ing to the VM)
Now add a file there with some text vi /mnt/mydisk/tmp/hello.txt
Press i to switch to edit mode and type in some text (It was “Hi There!” in my case) then follow this sequence to save Esc : wq
It’s time to say goodbye to your VM and delete it.
In the Cloud Shell execute gcloud compute instances delete gcelab –zone=us-central1-c and you will see something similar to the below
The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the `--keep-disks` flag is given and specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost. - [gcelab] in [us-central1-c] Do you want to continue (Y/n)? y Deleted [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-678cde46183b8e95/zones/us-central1-c/instances/gcelab].
You can also go to the Cloud Console and verify that your “old” VM is gone.
Create a new VM (using Cloud Console or Shell) with the name of gcelab2
When it is ready use the following command (in the Cloud Shell) to attach the disk:
gcloud compute instances attach-disk gcelab2 –disk mydisk –zone us-central1-c
SSH to the VM
Execute the below commands (similar to the ones you did as part of attaching the persistent disk to gcelab VM) necessary to create a new mount point and mount the disk. DON’T run the format (mkfs) command
sudo mkdir /mnt/mydisk sudo mount -o discard,defaults /dev/disk/by-id/scsi-0Google_PersistentDisk_persisten t-disk-1 /mnt/mydisk
You can run df -kh to see the disk mounted and available
google245648_student@gcelab2:~$ df -kh Filesystem Size Used Avail Use% Mounted on udev 1.8G 0 1.8G 0% /dev tmpfs 371M 7.5M 364M 2% /run /dev/sda1 9.8G 983M 8.4G 11% / tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sdb 196G 61M 186G 1% /mnt/mydisk
Don’t forget to check your message from the old VM
google245648_student@gcelab2:~$ cat /mnt/mydisk/tmp/hello.txt Hi There!