Skip to content

Instantly share code, notes, and snippets.

@ctron
Last active May 4, 2022 04:30
Show Gist options
  • Save ctron/88fa70926fc14c894e42ecf373cb271e to your computer and use it in GitHub Desktop.
Save ctron/88fa70926fc14c894e42ecf373cb271e to your computer and use it in GitHub Desktop.
Reserving additional space on Hetzner cloud instances using CentOS 7
#cloud-config
# we need to disable the initial call to "growpart" as otherwise the first partition would consume
# all space on the dist
#
# The final disk layout is:
# #0 0 - 10GB - ext4 on /
# #1 10GB - 100% - lvm
#
# The reason for this approach is that when using CentOS on Hetzner you cannot use
# the "disk_setup" mechanism from cloud-init, it simply is ignored.
growpart:
mode: off
runcmd:
- [ parted, "/dev/sda", mkpart, primary, ext2, "10GB", "100%" ] # create a new partition, starting at 10GB
- [ parted, "/dev/sda", set, "2", lvm, on ] # set LVM flag
- [ growpart, "/dev/sda", "1" ] # grow first partition to 10GB
- [ partx, --update, "/dev/sda" ] # reload partition table
- [ resize2fs, /dev/sda1 ] # resize first partition (/) to 10GB
- [ pvcreate, "/dev/sda2" ] # create PV on /dev/sda2 (100%-10GB)
- [ vgcreate, vg1, "/dev/sda2" ] # create VG, adding PV /dev/sda2
repo_update: true
repo_upgrade: all
packages:
- lvm2 # missing in the Hetzner image
@robertoschwald
Copy link

robertoschwald commented Oct 6, 2020

Seems to be ignored here when trying to instantiate CentOS8 instance. Is still:

Number  Start   End     Size    File system  Name  Flags
14      1049kB  2097kB  1049kB                     bios_grub
15      2097kB  69.2MB  67.1MB  fat16              boot, esp
 1      69.2MB  81.9GB  81.9GB  ext4

Logs:

cloud cloud-init[1064]: schema.py[WARNING]: Invalid config:
cloud cloud-init[1064]: runcmd.1: ['parted', '/dev/sda', 'set', '2', 'lvm', True] is not valid under any of the given schemas
cloud-init[1630]: Warning: Not all of the space available to /dev/sda appears to be used, you can
cloud-init[1630]: fix the GPT to use all of the space (an extra 152838144 blocks) or continue with
cloud-init[1630]: the current setting?

@ctron
Copy link
Author

ctron commented Oct 6, 2020

Interesting … I never tried that. I changed the title to CentOS 7 though.

@robertoschwald
Copy link

robertoschwald commented Oct 6, 2020

Got it working for CentOS8 Hetzner image as well. Problem is that gparted is complaining that not all space is used, therefore you can't use the whole disk at the beginning. So fix this, first.

#cloud-config

# we need to disable the initial call to "growpart" as otherwise the first partition would consume
# all space on the dist
# 1. Fix potential parted error "Not all of the space available to.."
# 2. Create a 2nd XFS partition starting at 10GB
# 3. Enable LVM on 2nd partion
# 4. Grow partition 1 (/) to 100% (0-10GB)
# 5. Re-Read partition layout
# 6. resize / partition to use the whole 10GB
# 7. Create LVM PV
# 8. Create LVM LV
#
# The final disk layout is:
# #0    0 - 10GB - XFS on /
# #1 10GB - 100% - lvm
#
# The reason for this approach is that when using CentOS on Hetzner you cannot use
# the "disk_setup" mechanism from cloud-init, it simply is ignored.

growpart:
  mode: off

runcmd:
  - printf "fix\n" | parted ---pretend-input-tty /dev/sda print        # Fix parted error, first
  - [ parted, -s, "/dev/sda", mkpart, primary, ext2, "10GB", "100%" ]  # create a new partition, starting at 10GB
  - [ parted, -s, "/dev/sda", set, "2", lvm, on ]                      # set LVM flag
  - [ growpart, "/dev/sda", "1" ]                                      # grow first partition to 10GB
  - [ partx, --update, "/dev/sda" ]                                    # reload partition table
  - [ resize2fs, /dev/sda1 ]                                           # resize first partition (/) to 10GB
  - [ pvcreate, "/dev/sda2" ]                                          # create PV on /dev/sda2 (100%-10GB)
  - [ vgcreate, vg1, "/dev/sda2" ]                                     # create VG, adding PV /dev/sda2


repo_update: true
repo_upgrade: all

packages:
  - lvm2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment