How to Bootstrap Your Custom Linux Distro in AWS: A Complete Step‑by‑Step Guide

How to Bootstrap Your Custom Linux Distro in AWS: A Complete Step‑by‑Step Guide

Introduction

Cloud computing has made building and testing custom operating systems more straightforward. In this guide, we'll start with a known Arch Linux instance, attach a second disk, partition and format it, and deploy your custom Linux archive (custom_linux.tar.bz2) into the new partitions (e.g. mapping to /, /boot, /home as needed), install GRUB to make it bootable, create an AMI from that disk, and finally explain how cloud‑init plays its part on first boot. We'll also cover both Intel and ARM instance options so you can choose the best architecture for your needs.

Free Tier Reminder: AWS provides a free trial that currently offers up to 12 months of t2.micro/t3.micro (or t4g.micro for ARM) usage plus bandwidth. Use the free tier to experiment and build—but remember, don't abuse it unless you wish to pay for the extra usage over and above the free tier.

Step 1: Launching Your Known Instance

  1. Launch a Base Instance:
Start by launching an Arch Linux (or another lightweight distro of your choice) EC2 instance from the AWS Marketplace.
Why? You need a "known good" base system from which to work and prepare your custom environment.
  1. Select the Instance Architecture:
AWS now offers Intel‑based (x86_64) and ARM‑based (AWS Graviton, such as t4g.micro) instances. Choose the one that fits your performance and cost criteria.
Intel vs. ARM: Intel instances are widely compatible and sometimes preferred for legacy software, while ARM instances (e.g. t4g) are typically more cost‑effective and energy‑efficient.

Step 2: Attaching a Secondary Disk

Add a New EBS Volume:
In the AWS Console, navigate to your instance’s "Storage" tab and attach a new EBS volume. For example, attach a 25 GB volume that will be used to host your custom OS files.
Tip: Make sure you attach the volume in the same availability zone as your instance.
Verify the New Disk:
Once attached, log into your instance via SSH and run:
lsblk

This confirms the new disk (e.g. /dev/xvdb) is visible.

Step 3: Partitioning the New Disk

  1. Run Your Partitioning Tool:
  2. Use a tool like cfdisk or gdisk to partition the new disk. For example:
sudo cfdisk /dev/xvdb
  • Plan a Three‑Partition Layout:
    • Partition 1 (/dev/xvdb1): A small BIOS boot or EFI partition (1–100 MB) for bootloader files.
    • Partition 2 (/dev/xvdb2): The OS's root partition (e.g. 8 GB).
    • Partition 3 (/dev/xvdb3): A home/data partition using the remaining space.
    • Why? A logical separation allows you to independently manage boot, system, and user files.
  • Save and Write the Partition Table:
    After creating the partitions, write the changes to the disk.

Step 4: Formatting the Partitions

Format Each Partition with an Appropriate Filesystem:

For example:

sudo mkfs.vfat /dev/xvdb1 # For BIOS boot or EFI partition (FAT32 is common for EFI)
sudo mkfs.ext4 /dev/xvdb2 # Root partition
sudo mkfs.ext4 /dev/xvdb3 # Home partition
  • Why? Ext4 is stable and widely supported on Linux; EFI systems often require FAT32.
  • Mount the New Partitions:
  • Create mount points and mount them:
sudo mount /dev/xvdb2 /mnt
sudo mkdir /mnt/boot
sudo mount /dev/xvdb1 /mnt/boot
sudo mkdir /mnt/home
sudo mount /dev/xvdb3 /mnt/home

Step 5: Deploying Your Custom Linux Archive

  1. Prepare Your Custom Archive:
  2. Ensure your custom_linux.tar.bz2 archive is structured to match your partition layout (with directories that will extract into /, /boot, and /home).
  3. Extract the Archive:
  4. Copy the archive to your instance (e.g. via SCP) and extract it to the new disk:
sudo tar xvjf /tmp/custom_linux.tar.bz2 -C /mnt
  • Why? This step "installs" your custom OS onto the new disk.
  1. Verify File Placement:
  2. Check that files for the root, boot, and home directories are correctly placed.

Step 6: Installing GRUB Bootloader

  1. Chroot into the New System:
  2. Change root into your new environment:
sudo arch-chroot /mnt
  1. Install GRUB:
  2. Run the GRUB installer (adjust command if you're using UEFI or BIOS; here, we assume BIOS mode):
grub-install --target=i386-pc /dev/xvdb
grub-mkconfig -o /boot/grub/grub.cfg

Why? GRUB is required to boot the OS. Installing it on the new disk makes the custom OS bootable independently.

exit

Step 7: Creating a Custom AMI

aws ec2 create-snapshot --volume-id <volume-id> --description "Custom Arch Linux Snapshot"
aws ec2 register-image \
  --name "Custom-Arch-AMI" \
  --architecture x86_64 \
  --root-device-name /dev/xvda \
  --block-device-mappings DeviceName=/dev/xvda,Ebs={SnapshotId=<snapshot-id>,VolumeSize=<size>}
  1. Why? Creating a custom AMI lets you launch new instances with your custom Linux distro.

Step 8: Integrating Cloud‑init for First‑Boot Customization

  1. What Is Cloud‑init?
  2. Cloud‑init is a powerful tool that runs during an instance's first boot. It reads user data (provided as YAML in the EC2 launch configuration) to set up the hostname, install packages, configure users, and run custom commands.
  3. Supplying User‑Data:
  4. You can supply a cloud-config file when launching a new instance from your custom AMI. For example:
#cloud-config
hostname: custom-arch-instance
package_update: true
packages:
  - vim
  - git
runcmd:
  - echo "Custom Arch Linux instance booted!"

Why? Cloud‑init ensures that every new instance is properly configured, even if additional customizations are needed at launch.

Step 9: Intel vs. ARM in Bootstrapping Your OS

AWS offers both Intel (x86_64) and ARM (AWS Graviton) instances:

  • Intel Instances:
They typically have broad compatibility with existing software. The steps above will work as written on Intel instances.
  • ARM Instances:
For Graviton‑based instances (e.g. t4g.micro), ensure your custom archive is built with ARM‑compatible binaries and libraries. The partitioning, formatting, GRUB (or an ARM‑appropriate bootloader like U-Boot for some cases), and cloud‑init configurations remain similar, but package names and binaries might differ.

Why the Choice Matters:

Using ARM can be cost‑effective and energy‑efficient, but your custom Linux distro must be compiled for ARM. Intel offers more legacy support if that is a concern.

Final Thoughts

Following this guide gives you a detailed, step‑by‑step roadmap to bootstrap a custom Linux distro in AWS. Here's a quick recap:

  1. Launch a known base instance (choose Intel or ARM-based on your needs) and use AWS's free tier wisely.
  2. Attach a secondary disk and verify its availability.
  3. Partition and format the new disk into boot, root, and home partitions.
  4. Extract your custom OS archive into the correct mount points.
  5. Chroot and install GRUB to make your OS bootable.
  6. Create a snapshot and register a custom AMI to quickly deploy new instances.
  7. Leverage cloud‑init to configure first‑boot settings automatically.
  8. Choose the correct architecture (Intel vs. ARM) to balance performance, cost, and compatibility.

Remember: AWS's free trial is a fantastic way to experiment—but use it responsibly. With this approach, you can build and test your custom Linux distro and prepare a clean image for AWS Marketplace distribution.

Happy bootstrapping, and may your instances always boot up smoothly!