How to shrink Google Cloud Instance Disk and Possible way to Shrink Boot Disk

      Comments Off on How to shrink Google Cloud Instance Disk and Possible way to Shrink Boot Disk

This article will walk you through the steps to shrink a persistent disk in google cloud. Also we will discuss the possibility to shrink the boot disk of GCP instance and where we stuck.

Introduction:

All of us have faced the situation where we need to reduce the size of the persistence disk of a instance while it is under utilized. We all have that doubt if it possible to reduce the size of the volume. Here we will see how we can reduce the size of the GCP volume.

We are using CentOS to show this demo, This is also valid for all operating system [Filesystem commands will change accordingly ].

How to Shrink Big GCP Additional Disk:

This steps given below will work if you have additional GCP Disks with huge space and it is under-utilised. Consider you have a 100GiB [/dev/sdb1] disk which is under-utilised and you want to shrink it to 50GiB.

Step 1: Login and check the disk utilisation of /dev/sdb1
[[email protected] ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda1        20G  1.7G   19G   9% /

devtmpfs        1.8G     0  1.8G   0% /dev

tmpfs           1.8G     0  1.8G   0% /dev/shm

/dev/sdb1        99G  7.1G   87G   8% /opt

Also check the format of the Disk /dev/sdb1 in this step, this will help to create same format of small disk.

[[email protected] ~]# blkid -s TYPE -o value /dev/sdb1
ext4

Here we can clearly see this /dev/sdb1 is taking only 7.1GiB space out of 87GiB and it is formatted as ext4 file system. Now we are good to shrink the volume.

Step 2 : Create a small disk and attach with the same instance

Create one small disk with 50GiB space and attach it as /dev/sdc of the instance.

[[email protected] ~]# lsblk 

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
└─sda1   8:1    0   20G  0 part /
sdb      8:16   0  100G  0 disk 
└─sdb1   8:17   0  100G  0 part /opt
sdc      8:32   0   50G  0 disk 

Step 3: Create /dev/sdc1 partition from /dev/sdc

This step is optional if you don’t want to partition the disk. Use the below command to create sdc1 partition from the sdc,

# fdisk /dev/sdc

# partprobe

Step 4 : Format the disk to ext4 filesystem

# mkfs.ext4 /dev/sdc1 

# blkid -s TYPE -o value /dev/sdc1
ext4

Step 5 : Mount the small disk in a directory under /mnt

# mkdir -p /mnt/small-disk

# mount /dev/sdc1 /mnt/small-disk/

# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  1.7G   19G   9% /
devtmpfs        1.8G     0  1.8G   0% /dev
tmpfs           1.8G     0  1.8G   0% /dev/shm
/dev/sdb1        99G  7.1G   87G   8% /opt
/dev/sdc1        50G   53M   47G   1% /mnt/small-disk

Step 6 : Use rsync to copy all data to the small disk

We prefer rsync since it ensures each and every blocks are copied.

# rsync -avHAXxSP /opt/* /mnt/small-disk

Ensure the data copied without any loss by finding the checksum,

# find /opt/ -type f -name "*" -exec md5sum {} + | awk '{print $1}' | sort | md5sum
1a2137df621f84c2497d07a7d155b1f3 -

# find /mnt/small-disk/ -type f -name "*" -exec md5sum {} + | awk '{print $1}' | sort | md5sum
1a2137df621f84c2497d07a7d155b1f3 -

Step 7 : Unmount big disk safely and mount the small disk in /opt

For Temporary Mount,

Unmount the big disk /dev/sdb1 and also remove entry from /etc/fstab if it is permanently mounted. We can even change the UUID of /dev/sdb1 to sdc1 from /etc/fstab  instead of removing it.

# umount /dev/sdc1

# umount /dev/sdb1

# mount /dev/sdc1 /opt
# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  2.3G   18G  12% /
devtmpfs        1.8G     0  1.8G   0% /dev
tmpfs           1.8G     0  1.8G   0% /dev/shm
tmpfs           1.8G  9.4M  1.8G   1% /run
/dev/sdc1        50G   53M   47G   1% /opt

Finally we could see the new small volume mounted on /opt.

For Permanent Mount,

1, Remove /dev/sdb1 entry from /etc/fstab if entry is there.

2, Stop the Instance and Detach both volumes.

2, Attach the small disk and Start the machine.

3, Now you could see small disk attached at /dev/sdb1

[[email protected] ~]# lsblk 

NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda      8:0    0  20G  0 disk 
└─sda1   8:1    0  20G  0 part /
sdb      8:16   0  50G  0 disk 
└─sdb1   8:17   0  50G  0 part 

4, Get the UUID of /dev/sdb1 and create an entry like below,

# blkid /dev/sdb1 
/dev/sdb1: UUID="823db525-82d9-467e-acdf-7379cbd851" TYPE="ext4" 

# vim /etc/fstab
UUID=823db525-82d9-467e-acdf-7379cbd851 /opt ext4 nofail,noatime,defaults,nodev 0 2

5, Now we are good to mount the volume,

# mount -a

# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  2.3G   18G  12% /
devtmpfs        1.8G     0  1.8G   0% /dev
tmpfs           1.8G     0  1.8G   0% /dev/shm
tmpfs           1.8G  9.4M  1.8G   1% /run
/dev/sdb1        50G   53M   47G   1% /opt

To make the article readable i’m splitting this into two post, You can see the possible way to Shrink Boot Disk in my next post.

Sharing is caring!

About Dinesh Sobitharaj C

An IT professional having multiple years of experience in IT Infrastructure planning, System integrations, Project implementation and delivery. Devops Enthusiast skilled with broad ranges of technology adoption. Well versed with Cloud Computing and Application Service Offerings such as SaaS/PaaS/IaaS. Expert in aligning business goals, mission and process to architect innovative solutions and strategies.