Default images have 10GB, but I need more (30GB approx). If I create a disk of 30GB using one of that default images, the usable space it's 10GB, not 30GB. I know I can install the distro using tools like debootstrap, but that seems unnecessarily complicated. is there any other way to do it?
This may be a new feature as of this writing, but go to
- https://console.cloud.google.com/compute/
- Click on Disks in the left menu
- In the main pane that loads, click on your instance
- In the upper horizontal menu that loads, click on Edit
- The former disk size will become editable. Change the disk size to your desired size.
- Restart your instance (sudo restart -r now). On reboot, df -h lists your new disk size.
So, one approach to answering your question is to create an instance with the default disk and simply resize it via the Compute Engine GUI.
You can create a boot disk larger than 10GB but then you'll need to repartition it, because by default, the provided VM images expand to 10GB so you'll need to use these instructions and run fdisk
, reboot, and then run resize2fs
to expand the usable space to the full size of the disk. You can automate it so that it runs as part of instance creation by using startup scripts.
Edit 1: I have open-sourced my scripts which do this for you automatically at boot using the startup-script
metadata. You can find sample code in my GitHub repo created specifically for this question, which has been verified to work with CentOS and Debian. See fdisk.sh
for the repartitioning and gcloud.sh
for the deployment commands.
Edit 2: Alternatively, you can also create an additional disk and attach it to your instance but you'll also need to format and mount it before you can use it.
growroot.sh
which uses cloud-init
and cloud-initramfs-growroot
which works well to repartition the container-vm image. I've incorporated it into my gcutil.sh
script so it should be easy to use. Please try it out and let me know if it works for you. –
Cup You can easily do this without having to manually resize/partition/format a disk or any of the complications introduced in all the other answers on StackOverflow. All you really need to do is:
- Separately create a new disk with the desired capacity based on the image you want to use.
- Choose that disk as the boot disk for the instance you're creating.
Here's a detailed explanation of how to do this using the Google Developers Console:
First, you'll have to create a new blank disk to be used as the root partition for your new instance. To do this, go to Compute / Compute Engine / Disks and click "New disk". Remember to make sure the disk is in the same zone as the instance you want to launch otherwise you wont be able to use it. Next, change the "Source Type" for the disk to "Image". Then select the appropriate image and specify the disk size you want and hit Create.
Now that you have your boot disk ready, go to create your new instance (Compute / Compute Engine / VM Instances and click "New instance"). In the new instance form, change the Boot Source to Existing Disk and select the disk you just created.
It should be relatively simple to do the same thing using a couple of gcloud
commands.
A safer method than editing the partition directly and which doesn't require maintaining your own images, is dracut's growroot module & cloud-init.
I've used this with CentOS 6 & 7 on Google Compute, AWS & Azure.
## you'll need to be root or use sudo
yum -y install epel-release
yum -y install cloud-init cloud-initramfs-tools dracut-modules-growroot cloud-utils-growpart
rpm -qa kernel | sed -e 's/^kernel-//' | xargs -I {} dracut -f /boot/initramfs-{}.img {}
# reboot for the resize to take affect
The partition will be resized automatically during the next boot.
Notes:
- This is built into Ubuntu, which is why you don't see the problem there.
- The partition size problem is seen with RedHat & CentOS with most pre-built images, not only Google Cloud. This method should work anywhere.
When you create an instance and specify a larger root disk size than the default, your instance will still end up with a partition table and filesystem size of 10GB.
In order to use the extra space you'll need to expand the /dev/sda1 partition and then resize the filesystem on it.
To resize the /dev/sda1 partition (THIS IS DANGEROUS AND SHOULD ONLY BE DONE INSIDE A BRAND NEW CLEAN INSTANCE):
echo "16," | sudo sfdisk /dev/sda
sudo reboot
To resize the root filesystem:
sudo resize2fs /dev/sda1
You should be good to go after this.
Consider to add aditional persitance disc toyour instance which will hold user application data. It migth be very usefull later. Because you your application data may evolve and scale in diffrent ways and of course independent of choosed OS. E.G you can do snapshots of that addtional disc independently. With that you gane full flexibility of your data in the cloud and you don't need to take OS with you data ;) P.S. Of coure new Desics of snapshots can be greater. See instructions abowe
I've added the following to an instance's 'startup-script' metadata so it resizes on the first two boots:
# Resize /dev/sda1 primary disk partition to use full space if not already done
if [[ "$(fdisk -l /dev/sda1 | grep '^Disk.*10\.[0-9] GB' | sed 's/.*, \([0-9]*\) bytes$/\1/')" -eq 10735321600 ]]; then
echo -e "c\nu\nd\nn\np\n1\n\n\nw\n" | fdisk /dev/sda
reboot
elif [[ "$(df -P /dev/sda1 | tail -n +2 | awk '{print $2}')" -le 10319160 ]]; then
resize2fs /dev/sda1
fi
Adapt this to your needs, and test it first! I've only tested on CentOS 6. Also, this does not necessarily work if you want to keep an instance disk at 10 GB (IOPS is best at 200 GB or larger, so, unless IO is of no concern to you, I suggest always creating larger disks).
The general idea can be applied on your startup-script: check the disk partition size to see if it's larger than the default for the image Google supplied. If it's the same as Google's default, run fdisk
for the maximum size permitted on the virtual hard disk and reboot. Upon the next boot, check the file system size and resize to the maximum size of the partition. Once those two actions are performed, it'll be skipped on subsequent reboots.
First create a disk from UI with existing image. Now use this image when creating an instance.
Here are the steps I followed to resize a Google Cloud compute instance. It's not enough to just create a new instance with a bigger disk, you also need to modify the partition and extend the filesystem as follows:
- Create a new disk snapshot on original instance
- Create new compute instance with boot disk based on that snapshot with the new desired size
- ssh to your new compute instance
- as root use fdisk to resize the partition
- as root use resize2fs to extend the root partition
Last two steps are described in detail here:
https://cloud.google.com/compute/docs/disks/persistent-disks#manualrepartition
You'll then need to cut over to your new instance as required or you could restart your original instance with the newly created disk image.
© 2022 - 2024 — McMap. All rights reserved.