gcp disk vm - ghdrako/doc_snipets GitHub Wiki

https://cloud.google.com/compute/docs/disks/

Detach and remove disk from vm

gcloud compute disks list

gcloud compute disks describe my-disk --zone=us-east1-a

sudo umount /dev/disk/by-id/google-DEVICE_NAME

gcloud compute instances detach-disk my-instance --disk=my-disk
# or
gcloud compute instances detach-disk my-instance --device-name=my-device

gcloud compute disks delete my-disk --zone=us-east1-a

Create and mout disk

gcloud compute disks create my-disk-1 my-disk-2 --zone=us-east1-a

# connect ssh to vm
sudo lsblk                                                                             # list the disks that are attached to your instance and 
                                                                                       # find the disk that you want to format and mount
sudo mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/DEVICE_NAME # format disk
sudo mkdir -p /mnt/disks/MOUNT_DIR                                                     # create mount dir
sudo mount -o discard,defaults /dev/DEVICE_NAME /mnt/disks/MOUNT_DIR                   # mount disk
sudo chmod a+w /mnt/disks/MOUNT_DIR                                                    # configure permition to mounted disk
sudo chmod a+rx /mnt/disks

Mout from fstab

blkid /dev/sdb
ls -l /dev/disk/by-uuid/
UUID=UUID_VALUE /mnt/disks/MOUNT_DIR ext4 discard,defaults,MOUNT_OPTION 0 2   # add to /etc/fstab

Resizing a persistent disk

https://cloud.google.com/compute/docs/disks/working-with-persistent-disks#gcloud

gcloud compute disks resize DISK_NAME --size DISK_SIZE

# connect to vm
sudo df -Th
sudo lsblk

# Resize the root partition and file system on the boot disk.
sudo parted -sm /dev/sda -- resizepart 1 -1  # Resize the root partition
sudo sgdisk --move-second-header /dev/sda    # Move GPT data structures to the end of the disk to align the GPT table
sudo partprobe /dev/sda                      # Read the new partition table using partprobe.
sudo resize2fs /dev/sda1                     # If you are using ext4, use the resize2fs command to extend the file system
sudo xfs_growfs -d /                         # If you are using xfs, use the xfs_growfs command to extend the file system
df -h /dev/sdb                               # Verify

# Resize the file system on the non-boot data disk.
sudo resize2fs /dev/DEVICE_NAME              # If you are using ext4, use the resize2fs command to extend the file system
sudo xfs_growfs MOUNT_DIR                    # If you are using xfs, use the xfs_growfs command to extend the file system
sudo df -h /dev/sdb                               # Verify