Python Mercenaries
in
t
g
p
b
f

How To Make an Ubuntu Server 22 Autoinstall USB Stick

written by Darren Lauser, edited by Alan Cugler on 2023-10-09

Ubuntu autoinstall is only designed and working for Ubuntu Server installs, not Ubuntu Desktop installs. However, if you want a Desktop GUI on your server, you can simply install the ubuntu-desktop package after the system is built. Keep in mind some configurations will be different since that desktop would be based off of the Ubuntu Server image. I tried installing such packages during the autoinstall, but that feature did not work very well.

Most instruction in this 'How To' are focused in the bash terminal.

Workstation prep

  1. Create a working directory in your home directory.
$ mkdir autoinstall
$ cd autoinstall
  1. Download the Ubuntu 22 Server ISO.

  2. Copy the downloaded file to your working directory, e.g.:

$ cp ~/Downloads/ubuntu-22.04.2-live-server-amd64.iso source.iso
  1. Make a configuration file.

One way to configure autoinstall is to first do an interactive install. When you do an interactive install of Ubuntu Server 22, it will create a /var/log/installer/user-data file that captures all of the choices you made during the install. That file can be used with autoinstall to automatically make those same choices for future installs.

Note

The example user-data file below has an encrypted password hash displayed. The encrypted password is ubuntu.

#cloud-config
autoinstall:
  apt:
    disable_components: []
    geoip: true
    preserve_sources_list: false
    primary:
    - arches:
      - amd64
      - i386
      uri: http://us.archive.ubuntu.com/ubuntu
    - arches:
      - default
      uri: http://ports.ubuntu.com/ubuntu-ports
  drivers:
    install: false
  identity:
    hostname: tmp-autoinstall
    password: $6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0
    realname: Robot
    username: ubuntu
  kernel:
    package: linux-generic
  keyboard:
    layout: us
    toggle: null
    variant: ''
  locale: en_US.UTF-8
  network:
    version: 2
    ethernets:
      alleths:
        match:
          name: e*
        dhcp4: true
  source:
    id: ubuntu-server
    search_drivers: false
  ssh:
    allow-pw: true
    authorized-keys: []
    install-server: true
  storage:
    layout:
      name: lvm
  updates: security
  version: 1

Of course, you can also make changes to this file to suit your needs, or you can create one from scratch guided by the docs: https://ubuntu.com/server/docs/install/autoinstall

  1. Put that user-data file here in your working directory.

  2. Install livefs-editor to create the autoinstall ISO image.

$ sudo apt -y install git xorriso squashfs-tools python3-debian gpg liblz4-tool python3-pip
$ git clone https://github.com/mwhudson/livefs-editor
$ cd livefs-editor/
$ sudo python3 -m pip install .
$ cd ..
  1. Choose the safe option, or the unsafe option.

At this stage of the process, there are two choices:

If you want to fully automate, and remove that confirmation step, there is a different option to use for the livefs-edit command: --add-autoinstall-config

If you are opting for the unsafe option below, then you can skip the grub.cfg file here because the livefs-edit command will sufficiently edit the existing grub.cfg file.

These steps (8A), with the blue border, are for the safe option. If you want the safe option, then skip the steps with the yellow border. If you want the unsafe option (8B), then skip the steps with the blue border.

8A. Safe option:

Put the grub.cfg file here in your working directory.

You can create the grub.cfg file you want using this content as a reference:

set timeout=10

loadfont unicode

set menu_color_normal=white/black
set menu_color_highlight=black/light-gray

menuentry "Autoinstall Ubuntu" {
    set gfxpayload=keep
        linux   /casper/vmlinuz auto=true priority=critical locale=en_US splash ds='nocloud;s=/cdrom/CIDATA/' fsck.mode=skip quiet ---
    initrd	/casper/initrd
}

The following command makes a "safe" USB stick because it requires you to confirm by typing "yes" before it starts writing to disk.

SAFE USB -- because the install asks for confirmation.

$ sudo livefs-edit source.iso UbuntuAutoinstall.iso --shell --cp /home/$USER/autoinstall/user-data new/iso/CIDATA/user-data --cp /home/$USER/autoinstall/grub.cfg new/iso/boot/grub/grub.cfg

If the above command fails to find the livefs-edit command, scroll up a bit to see the WARNING message telling you where it was installed.

At the prompt, run the following two commands, then hold the control key pressed and hit the "D" key.

# mkdir new/iso/CIDATA
# touch new/iso/CIDATA/meta-data

Hit [control]+[d]

8B. UNSAFE!! -- because the install does not ask for confirmation.

$ sudo livefs-edit source.iso UbuntuAutoinstall.iso --add-autoinstall-config /home/$USER/autoinstall/user-data

If the above command fails to find the livefs-edit command, scroll up a bit to see the WARNING message telling you where it was installed.

  1. Plug in the USB and find it with:
$ lsblk
  1. Ensure the USB is not mounted (change the below commands to reflect the device file name you found in step 9 above).
$ sudo umount /dev/sdg /dev/sdg1
  1. Copy data to USB.

WARNING!!

Be absolutely sure the device file, e.g., /dev/sdg, you type below is the USB stick you found in step 9 above, or you will destroy your file system.

$ sudo dd bs=4M if=UbuntuAutoinstall.iso of=/dev/sdg conv=fdatasync status=progress

From my experience, the progress indicator is inconsistent and unreliable. So, if you see nothing, just wait for the prompt to return. If you see some progress indicator, don't believe it.

Try It Out

After you get the prompt back, take this USB stick and insert it in the system you want to install and power on. If the boot configuration is set to boot from USB, then it will load the grub menu that says "Autoinstall Ubuntu" as the only menu item. You can either wait for the 10 seconds to time out, or hit enter to choose the default. It will load the installer system, then ask you to type "yes" to confirm before it starts writing to disk. If you take a lunch break and come back, it might be asking you to type "yes" again. This means it rebooted and was still set to prefer booting from USB. So, you can just power it off, unplug the USB, and power it on again to boot from your newly installed boot drive.

There are more details in the web pages below, however, I found much of the instructions did not work. So, I wrote this blog article to document at least one way that actually works.

References:

Disclaimer:

Ubuntu, and the Ubuntu logo, are registered trademarks of Canonical.


« Previous | How To Make an Ubuntu Server 22 Autoinstall USB Stick | Next »