Easy Custom Ubuntu Server Auto-Install Guide

Configure a custom Ubuntu server ISO for seamless automatic installation

Easy Custom Ubuntu Server Auto-Install Guide

Think back to a time when setting up Ubuntu servers felt like doing a puzzle on each computer, taking forever and keeping you up late. Now, picture a special file – let's call it a magic tool. This file works like a wizard's wand, making the whole setup process automatic. It's like a shortcut that saves you tons of time and energy. With this magic file (we'll call it a custom Ubuntu Server ISO), you set up all your servers quickly and smoothly, like magic! No more staying up late – just a simple and efficient way to get things done. That's the power of custom Ubuntu Server ISOs – making things easy and hassle-free.

What is Auto-installation

Auto installation is installing any system software with a non-interactive process or automating manual work during installation.

For example, If you want to install a Linux server with your preferred configuration like partitioning and installation of packages or running any other application with automation.

There is a lack of good documentation for creating custom Ubuntu ISO for auto-installation. So, we’ll create a well-documented blog for the customization of ISO.

Let’s start with the ISO image

An optical disc image, also known as an ISO image, is a complete binary replica of an optical disc, preserving its file system and data exactly as they were on the original disc. This image typically adheres to the ISO 9660 file system and mirrors the structure of the source disc.

Prerequisites

Download the Ubuntu Server ISO from here.

There is no need to have an Ubuntu desktop to create a custom Ubuntu ISO.

Install a package for unpacking ISO images like p7zip-full and We’re going to use xorriso to build ISO with configuration. (You can use other tools like mkisofs). "xorriso" is more advanced and versatile than "mkisofs”.

Use command-not-found if you don’t know package name or command to install packages.

Customizing the ISO

Step 1: Set up the build environment

Create directory for unpacking iso files

mkdir custom_ubuntu_iso
cd custom_ubuntu_iso

move the downloaded ISO to the custom_ubuntu_iso directory and create directory called source-files/ in custom_ubuntu_iso/ for user-data configuration file

mkdir source-files

Step 2: Unpack files from the Ubuntu 22.04 live server ISO

7z -y x ubuntu-22.04.3-live-server-amd64.iso -osource-files

This command extracts the contents of "ubuntu-22.04.3-live-server-amd64.iso" using 7-Zip, placing the files in the "source-files" directory.

mv '[BOOT]' ../BOOT

Following extraction, this command moves the '[BOOT]' directory to the parent directory "../BOOT," preserving the essential boot files' structure.

Step 3: Edit the ISO grub.cfg file

Edit source-files/boot/grub/grub.cfg and add the following

menuentry "Autoinstall Ubuntu Server" {
    set gfxpayload=keep
    linux   /casper/vmlinuz quiet autoinstall ds=nocloud\;s=/cdrom/server/  ---
    initrd  /casper/initrd
}

Note: Use a backslash “\” before the semicolon to escape it. This adds autoinstall kernel directive with cloud-init of type “nocloud” and specifies the directory for installer configuration in /cdrom/server/.

The menuentry in the GRUB configuration is a directive that defines a new entry in the GRUB menu. GRUB (Grand Unified Bootloader) is a bootloader used in many Linux distributions. Each menuentry specifies a configuration for booting an operating system or a kernel.

You can reduce default timeout in grub.cfg

Step 4: Add your custom autoinstall user-data files

user-data is the YAML file in source-files/ directory. user-data contains all the configurations which going to configure iso and automatically install ubuntu.

meta-data is the empty file in source-files/ . The meta-data file is like an ID card that cloud-init looks for when setting up the system. Even though it's empty for our purposes, it's still needed. If we were setting up servers in a cloud environment, this file would contain important information for the setup. For example hostname, user account details.

add the directory for the user-data and meta-data files

mkdir source-files/server

touch source-files/server/meta-data

touch source-files/server/user-data

user-data example file

#cloud-config  # This indicates that this file follows cloud-config syntax
autoinstall:    # Beginning of the autoinstall configuration
  version: 1    # Specify the version of autoinstall, in this case, version 1

  storage:      # Storage configuration for partitioning and filesystem
    layout:    # Layout configuration for storage
      name: direct    # Use direct layout, which means a simple partitioning scheme
      partitions:    # Define the partitions
        - name: primary    # Partition name
          number: 1        # Partition number
          size: 100%       # Use 100% of available space
          type: ext4       # Filesystem type is ext4
          wipe: superblock    # Wipe the superblock
          preserve_layout: false    # Do not preserve layout
          overwrite: true    # Overwrite existing data
      filesystems:    # Filesystems to be created
        - device: /dev/disk/by-id/*    # Specify the device
          format: ext4    # Format as ext4
          label: ubuntu    # Label the filesystem as ubuntu

  locale: en_US.UTF-8    # Set the locale to en_US.UTF-8

  keyboard:    # Keyboard configuration
    layout: us    # Set the keyboard layout to US

  identity:    # Identity configuration for system identification
    hostname: my-ubuntu    # Set the hostname to my-ubuntu
    password: $6$BzxtO10GpXY=$jktreLGnGEPKO1NpmYzcCLdazunGlSSvltzSyRUYh2ZJ5JKzvUOGbX7jOuj3oaeQYnQ/KkT2V9EzlpFetW9oH/    # Set the hashed password for the user

  username: myuser    # Set the username to myuser

  ssh:    # SSH configuration
    allow-pw: true    # Allow password authentication
    install-server: true    # Install the SSH server

  apt:    # APT package manager configuration
    primary:    # Primary APT repository configuration
      - arches: [default]    # Default architecture
        uri: http://us.archive.ubuntu.com/ubuntu/    # APT repository URI
    packages:    # List of packages to be installed
      - build-essential
      - network-manager
      - vim
      - docker.io
      - docker-compose
      - wget
      - sqlite
    package_update: true    # Update package list before installation
    package_upgrade: true    # Upgrade installed packages

  late-commands:    # Late commands to be executed after installation
    - find /target/etc/netplan/ -name ".yaml" -exec sh -c 'mv "$1" "$1-orig"' _ {} \;    # Find netplan YAML files and rename them
    - |    # Use a multiline script to create a netplan YAML file
      cat <<EOF | sudo tee /target/etc/netplan/01-netcfg.yaml
      network:
        version: 2
        renderer: NetworkManager
      EOF
    - curtin in-target --target /target netplan generate    # Generate netplan configuration
    - curtin in-target --target /target netplan apply    # Apply netplan configuration
    - curtin in-target --target /target systemctl enable NetworkManager.service    # Enable NetworkManager service
    - sed -ie 's/GRUB_TIMEOUT=./GRUB_TIMEOUT=1/' /target/etc/default/grub    # Set GRUB timeout to 1

user-data:    # User data section
  runcmd:    # Commands to be executed during first boot
    - usermod -aG docker myuser    # Add myuser to the docker group
    - cd /home/myuser    # Change directory to /home/myuser

Use openssl passwd to generate password.

For example:

openssl passwd -6 -salt $(openssl rand -base64 8) password

Step 5: Generate a new Ubuntu auto-install ISO

The xorriso command is a powerful tool for working with ISO images.

xorriso -indev ubuntu-22.04.3-live-server-amd64.iso -report_el_torito as_mkisofs

In this instance:

  • -indev: Specifies the input ISO image (ubuntu-22.04.3-live-server-amd64.iso).

  • -report_el_torito as_mkisofs: Instructs xorriso to inspect the El Torito boot information and report it in a format compatible with mkisofs.

The generated output can be redirected to a shell script (e.g., mkisofs_cmd.sh), which will contain a mkisofs command. This command can then be customized or executed to create a new ISO image with the El Torito boot information.

Navigate to the source files directory:

cd source-files

Use xorriso to create a custom ISO image:

xorriso -as mkisofs -r \
  -V 'Ubuntu 22.04 LTS AUTO (EFIBIOS)' \
  -o ../ubuntu-autoinstall.iso \
  --grub2-mbr ../BOOT/1-Boot-NoEmul.img \
  -partition_offset 16 \
  --mbr-force-bootable \
  -append_partition 2 28732ac11ff8d211ba4b00a0c93ec93b ../BOOT/2-Boot-NoEmul.img \
  -appended_part_as_gpt \
  -iso_mbr_part_type a2a0d0ebe5b9334487c068b6b72699c7 \
  -c '/boot.catalog' \
  -b '/boot/grub/i386-pc/eltorito.img' \
    -no-emul-boot -boot-load-size 4 -boot-info-table --grub2-boot-info \
  -eltorito-alt-boot \
  -e '--interval:appended_partition_2:::' \
  -no-emul-boot \
  .
  • -as mkisofs: Selects the ISO creation mode.

  • -r: Uses Rock Ridge naming conventions for better compatibility.

  • -V 'Ubuntu 22.04 LTS AUTO (EFIBIOS)': Specifies the volume ID of the ISO.

  • -o ../ubuntu-autoinstall.iso: Sets the output file for the new ISO.

  • --grub2-mbr ../BOOT/1-Boot-NoEmul.img: Specifies the Master Boot Record (MBR) file for GRUB.

  • -partition_offset 16: Sets the partition offset to 16 sectors.

  • --mbr-force-bootable: Forces the MBR to be bootable.

  • -append_partition 2 28732ac11ff8d211ba4b00a0c93ec93b ../BOOT/2-Boot-NoEmul.img: Appends a partition to the ISO with specific parameters.

  • -appended_part_as_gpt: Treats the appended partition as a GPT (GUID Partition Table) entry.

  • -iso_mbr_part_type a2a0d0ebe5b9334487c068b6b72699c7: Specifies the MBR partition type.

  • -c '/boot.catalog': Sets the path to the catalog file.

  • -b '/boot/grub/i386-pc/eltorito.img': Specifies the path to the boot image.

  • -no-emul-boot -boot-load-size 4 -boot-info-table --grub2-boot-info: Configures options for the boot image (no emulation, load size, info table, and GRUB2 boot info).

  • -eltorito-alt-boot: Enables an alternative boot specification.

  • -e '--interval:appended_partition_2:::': Sets the boot catalog entry for the alternative boot.

  • -no-emul-boot: Configures the alternative boot to have no emulation.

  • .: Specifies the source directory for the ISO content.

This command creates a customized ISO image with specific boot configurations for Ubuntu 22.04 LTS, including GRUB settings and appended partitions.

Test your new auto-installation ISO

Note: If your installing iso on bare metal, the installation can erase whole disk storage.

Virtualbox

Once you have created your customized auto-install ISO, it's time to test it on a virtual machine. We'll use VirtualBox for this demonstration.

  1. Open VirtualBox: Launch the VirtualBox application on your machine.

  2. Create a New Virtual Machine:

    • Click on the "New" button in the VirtualBox toolbar.

    • Enter a name for your virtual machine, select "Linux" as the type, and choose "Ubuntu" as the version. Click "Next."

  3. Allocate Memory:

    • Choose the amount of memory (RAM) for your virtual machine. Ensure it meets the system requirements for your Ubuntu version. Click "Next."
  4. Create a Virtual Hard Disk:

    • Create a new virtual hard disk. Choose the size and type. Click "Create."
  5. Configure Settings:

    • Select your newly created virtual machine in the VirtualBox Manager.

    • Click on "Settings."

    • Under the "System" tab, uncheck Floppy in the Boot Order section.

    • Go to the "Storage" tab and attach your custom autoinstall ISO to the virtual optical drive.

  6. Start the Virtual Machine:

    • Click "Start" to boot up the virtual machine with the auto-installation ISO.
  7. Observe the Installation Process:

    • The auto-installation process should begin automatically, guided by the configurations you set in your custom ISO.
  8. Review and Troubleshoot:

    • Monitor the installation process for any errors or unexpected behavior. If needed, review your auto-install configuration and make adjustments. You probably going to get error because of wrong or unknown syntax. You can refer overall format for the syntax, Also you can discuss or get solution of your problem on github discussion.
  9. Verify the Result:

    • Once the installation is complete, log in to the system and verify that your custom configurations, software packages, and settings have been applied.

Testing your auto-install ISO in a virtual environment ensures a smooth deployment process before using it on physical hardware. Installation can take up to 20 minutes.

Automation Tools

Streamlining Ubuntu Server Setup with Ansible Automation

In today's fast-paced digital landscape, automating server installations is crucial for efficiency and consistency. Ansible, a powerful open-source automation tool, can simplify the process of setting up Ubuntu servers.

Prerequisites:

  1. Ansible is installed on your local machine.

  2. Ubuntu ISO file downloaded.

By automating Ubuntu server installations with Ansible, you can ensure consistency and save time. Ansible provides a simple yet powerful approach to streamline the deployment process. Feel free to customize the playbook to fit your specific requirements and scale your automation efforts.

Additional resources

Red Hat: Preparing for a Network Installation

Discuss your problem with custom Ubuntu ISO

Did you find this article valuable?

Support Things for DevOps by becoming a sponsor. Any amount is appreciated!