Preserve Existing Partition Sizes with Cloud-Init Auto-Install Ubuntu 24.04.1
Cloud-Init is a popular tool for automating the boot process of cloud instances. It provides several features like setting up users, installing packages, and configuring the system. However, by default, Cloud-Init does not preserve the existing partition sizes during the installation process. In this article, we will discuss how to preserve the existing partition sizes while using Cloud-Init for auto-installing Ubuntu 24.04.1.
Background
When you use Cloud-Init for auto-installing Ubuntu, it creates new partitions for the root filesystem and swap space by default. This behavior can be problematic if you want to preserve the existing partition sizes. For instance, if you have already set up your system with specific partition sizes, you might not want to resize them during the installation process.
Solution
To preserve the existing partition sizes during the Cloud-Init installation process, you can use the cloud-config file to define the partition sizes. Here's how to do it:
Create a Cloud-Config File
First, you need to create a cloud-config file. You can create this file on your local machine or on the cloud instance using SSH. The file should be located in the root directory of the instance, typically at /cloud-config.yaml or /cloud-config.json. In this example, we will use a YAML file.
Define Partition Sizes
Next, you need to define the partition sizes in the cloud-config file. Here's an example of how to define the root filesystem and swap partition sizes:
disk_size: 20GB
root_partition:
size: ${disk_size}
overwrite_files: [/etc/fstab, /etc/crypttab]
partitions:
- size: 10GB
name: swap
boot: true
format: swap
In this example, we define a disk size of 20GB and set the root partition to use the entire disk, except for the swap partition. We also define a swap partition of size 10GB.
Install Cloud-Init
Before you can use the cloud-config file, you need to install Cloud-Init on your Ubuntu instance. You can install it using the following command:
sudo apt-get update
Then, install Cloud-Init:
sudo apt-get install cloud-init
Configure Cloud-Init
Once you have installed Cloud-Init, you need to configure it to use the cloud-config file. You can do this by creating a file named user-data in the root directory of the instance. This file should contain the following:
#!/bin/bash set -o nounset set -o errexit set -o pipefail
if [ -f /cloud-config.yaml ]; then cloud-config --no-overwrite config=$(/path/to/your/cloud-config/file) fi
Replace /path/to/your/cloud-config/file with the path to your cloud-config file. This script will be executed during the boot process and will apply the configuration defined in the cloud-config file.
Reboot Instance
Finally, reboot the instance to apply the changes:
sudo reboot
Summary
- Cloud-Init resizes partitions by default during installation, but you can preserve existing partition sizes by defining them in a
cloud-configfile. - Create a
cloud-configfile with the desired partition sizes and install Cloud-Init on the Ubuntu instance. - Configure Cloud-Init to use the
cloud-configfile during the boot process and reboot the instance to apply the changes.