🎉 Announcing new lower pricing — up to 40% lower costs for Cloud Servers and Cloud SQL! Read more →

How to Partition a Boot Disk on a Cloud Server

When you create a Brightbox Server the root partition is automatically expanded to fill the entire disk on first-boot. Normally this is exactly what you want, but sometimes you want to partition the disk and allocate some of the block storage to another use (LVM for example).

Cloud-init is a tool pre-installed in most cloud server images that can execute various actions on first-boot and can setup disks and filesystems. The disk_setup primitive is useful but won’t work with a boot disk, as it is already mounted by the time that part of cloud config is running. So how can we do it?

The trick is to run the partitioning command at boot time using the bootcmd section. Just set this as user data when creating your server:

#cloud-config
bootcmd:
  - [cloud-init-per, once, addpartition, sgdisk, /dev/vda, "-e", "-n=0:10G:0", -t, "0:8e00"]

This moves the GPT backup block to the end of the disk where it belongs and then creates a new partition starting 10GiB into the disk filling the rest of the disk, before labelling it as an LVM partition. When cloud-init expands the root partition, it will expand up to the 10GiB mark, rather than filling the entire disk as it usually does.

ubuntu@srv-5o6p2:~$ sudo sgdisk -p /dev/vda
Disk /dev/vda: 125829120 sectors, 60.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 667E99FF-8081-4EF3-B7BA-4501EDB81B4D
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 125829086
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1          227328        20971519   9.9 GiB     8300  
   2        20971520       125829086   50.0 GiB    8E00  
  14            2048           10239   4.0 MiB     EF02  
  15           10240          227327   106.0 MiB   EF00

Now you’re free to add your new partition to LVM

ubuntu@srv-5o6p2:~$ sudo partprobe
ubuntu@srv-5o6p2:~$ sudo pvcreate /dev/vda2
  Physical volume "/dev/vda2" successfully created.
ubuntu@srv-5o6p2:~$ sudo pvs
  PV         VG Fmt  Attr PSize   PFree  
  /dev/vda2     lvm2 ---  <50.00g <50.00g

Want to give this a go right now?

You can sign up for Brightbox in just a couple of minutes and get a £50 free credit

Get started with Brightbox Sign up takes just two minutes...