How to get a bigger boot disk on Google Compute En

2019-01-14 09:54发布

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?

9条回答
2楼-- · 2019-01-14 10:31

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.

查看更多
看我几分像从前
3楼-- · 2019-01-14 10:33

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.

查看更多
够拽才男人
4楼-- · 2019-01-14 10:40

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-14 10:40

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.

查看更多
相关推荐>>
6楼-- · 2019-01-14 10:41

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.
查看更多
疯言疯语
7楼-- · 2019-01-14 10:43

This may be a new feature as of this writing, but go to

  1. https://console.cloud.google.com/compute/
  2. Click on Disks in the left menu
  3. In the main pane that loads, click on your instance
  4. In the upper horizontal menu that loads, click on Edit
  5. The former disk size will become editable. Change the disk size to your desired size.
  6. 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.

查看更多
登录 后发表回答