Skip to content

Commit

Permalink
rebuilding site Mo 20. Nov 22:55:59 CET 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
16EAGLE committed Nov 20, 2023
1 parent e4479c5 commit 1b2c293
Show file tree
Hide file tree
Showing 50 changed files with 1,583 additions and 138 deletions.
158 changes: 121 additions & 37 deletions blog/2023-11-14_how-to-create-an-ubuntu-server-autoinstall-image.Rmd
Original file line number Diff line number Diff line change
@@ -1,83 +1,167 @@
---
title: "How to create an Ubuntu Server autoinstall image for a headless system"
title: "How to create an Ubuntu Server auto-install image for a headless, ssh-ready system"
description: ""
date: "2023-11-15T08:00:00-00:00"
draft: true
keywords: ["how-to", "autoinstall", "ubuntu", "linux", "server", "headless", "sysadmin"]
tags: ["how-to", "autoinstall", "ubuntu", "linux", "server", "headless", "sysadmin"]
slug: "how-to-create-an-ubuntu-server-autoinstall-image-for-a-headless-system"
slug: "how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system"
stylesheet: post.css
---


## Introduction

## Step 1: Extract Ubuntu Server .iso image
Installing Ubuntu onto a computer is not complicated: One just needs to boot a live image (e.g. from a USB drive or served via the network) and the user is prompted with the option to install Ubuntu onto the host's internal hard drive. After a few steps, the installation is done and the system is ready.

extract ubuntu server iso image source files
However, this process is interactive, i.e. it requires user input to complete the installation. For example, the user needs to actively select the option to install Ubuntu (otherwise, the image is just run safely as a live system without writing anything to the system's hard drives) and select a locale or define a password. This is fine as long as there is a) a user and b) input and output devices to interact with the system.

```
7z -y x ubuntu-22.04.3-live-server-amd64.iso -osource-files
```
**But: What, if neither of these exist?**

## Step 2: Re-configure GRUB
There are scenarios, where this is the case, e.g. if you want to remotely install Ubuntu onto a headless server (physical, virtual machine, cloud instance) or set-up many devices in one go that should all be pre-configured and ready to use right away.

edit
Then, it may come handy to fully automatize the installation of Ubuntu, without any user interaction, guided by pre-configured settings. The solution to this problem is called *autoinstall*. Every Ubuntu image already comes with the necessary tools to configure an auto-install.

```
nano source-files/boot/grub/grub.cfg
```
This how-to explains, how you tweak a vanilla Ubuntu image so that it will be auto-installed to the device it gets booted from using [Cubic](https://github.com/PJ-Singh-001/Cubic), a GUI wizard to create customized Ubuntu/Debian live images.

add
## Step 1: Download an Ubuntu image

```
menuentry "Autoinstall Ubuntu Server" {
set gfxpayload=keep
linux /casper/vmlinuz quiet autoinstall ds=nocloud\;s=/cdrom/server/ ---
initrd /casper/initrd
}
```
First, you need a default Ubuntu image. Download it from the Ubuntu website: https://ubuntu.com/download.

rename and move boot
For this example, we will use an *Ubuntu Server* image, as our target system is a remote server that does not need to host a GUI. You may as well choose an *Ubuntu Desktop* image, if that better suits your needs – the procedure described below remain the same.

```
mv '[BOOT]' ../BOOT
cd ..
```
## Step 2: Install Cubic

## Step 3: Create cloud-config
To take apart, edit/re-configure and later-on rebuild the Ubuntu image, we will use *Cubic*. There is a dedicated repository that you can use:

```
mkdir source-files/server
touch source-files/server/meta-data
nano source-files/server/user-data
sudo apt-add-repository universe
sudo apt-add-repository ppa:cubic-wizard/release
sudo apt update
sudo apt install --no-install-recommends cubic
```

minimal example
## Step 3: Define a project directory and extract the Ubuntu image using Cubic

Open up *Cubic*, choose a project directory (*Fig. 1*) and select *Next*.

<br>
![](/img/how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system/fig1.png)
<figcaption>*Figure 1: Select a project directory to which all files should be saved*</figcaption>

Select the downloaded Ubuntu `.iso` file. If needed, make adjustments to meta information of the custom image you want to create on the right side of the screen (*Fig. 2*). Select *Customize*.

<br>
![](/img/how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system/fig2.png)
<figcaption>*Figure 2: Image selection and custom disk settings*</figcaption>

## Step 4: Use the virtual terminal to pre-install software or add files

On the next screen, *Cubic* displays a *virtual terminal* (*Fig. 3*) which you can use to pre-install software packages, e.g. using `apt`, create directories and files and pre-configure applications. In this how-to, I am pre-installing the programming language `julia` using `apt` as an example.

If you do not want to add/change anything specific and have a clean Ubuntu autoinstall image, you can skip this step.

<br>
![](/img/how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system/fig3.png)
<figcaption>*Figure 3: Cubic's virtual terminal for making changes to the image's file system or pre-installing software*</figcaption>

## Step 5: Create a cloud-init config

On the next screen, switch to the **Preseed** tab. Here, one can add files that are saved to the `/preseed` directory. There, we will store our `cloud-init` configuration.

For this how-to, we will use a minimal `cloud-init` example configuration that you can expand/alter as you like.

Using the *Create-a-new-file*-button in the top row, create a file named `meta-data` and leave it empty. Create another file named `user-data` and add the following config to it:

```
#cloud-config
autoinstall:
version: 1
identity:
hostname: ubuntu-server
password:
password: "$6$2rnd0MFiuNd/2s5Y$GkgcxTNFnOPoS0tscsshBb.sAJWwjbuwGttc.Ppx8QMheGAMjQy9Vhiw/HXFNNlTvhWTut.tM.qING/Pl0ofY/"
username: administrator
```

create new image including changes
Here, we use `ubuntu-server` as the name of our new machine and define a `administrator` as the default username. For this how-to, I create a hash of the password `ubuntutest` which one would use to log in to a machine created using this image (*Fig. 4*).

<br>
![](/img/how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system/fig4.png)
<figcaption>*Figure 4: The Preseed tab in Cubic to add a cloud-init configuration.*</figcaption>

You may change these settings as you like. To generate a password hash of you own custom password, use `openssl passwd -6`

## Step 7: Edit the boot configuration

Next, we need to make sure that the above defined `cloud-init` config is used during boot. For this, we need to edit the bootloader configuration. The default bootloader of Ubuntu is `GRUB`.

In *Cubic*, switch to the **Boot** tab (*Fig. 5*) and navigate to the file `/boot/grub/grub.cfg`. There, add a new `menuentry` above the ones already present by copying and pasting the below snippet:

```
xorriso -as mkisofs -r -V 'Ubuntu 22.04 LTS autoinstall' -o ../ubuntu-22.04-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 .
menuentry "Autoinstall Ubuntu Server" {
set gfxpayload=keep
linux /casper/vmlinuz quiet autoinstall ds=nocloud\;s=/cdrom/preseed/ ---
initrd /casper/initrd.gz
}
```

With this, you added a new boot option to the `GRUB` bootloader menu that will be first on the list of options and thus will be selected by default. The second line of the `menuentry` instructs `GRUB` to load the Linux kernel. If you compare the `menuentry` that was at the first position before your edit with the one you just added, you see that we added a few `cloud-init` boot options:

* `quiet`: kernel paramater that disables most log messages.
* `autoinstall`: boots kernel in autoinstall mode
* `ds=nocloud\;s=/cdrom/preseed/`: sets the data source from where to get the autoinstall configuration from.

For the last data source parameter, we use the option `nocloud`. With this method, we can set a path (or network address) to the location where the `cloud-init` configuration is stored using the SMBIOS serial number option of the Linux kernel command line. In our case, the configuration is stored within the live image in the folder `/preseed`, thus the path reads `/cdrom/preseed` (instead, you could also serve a directory via the network which could look something like this instead: `http://10.42.42.42/cloud-init/configs/`). Note that we need to escape the `;` character in the `GRUB` config using `\` to pass the SMBIOS option, since in `GRUB` the semicolon character terminates a command.

Last, as an optional change, you may want to adjust the timer controlling how long `GRUB` should wait until proceeding with the default (first) entry in the first line of the same file. For this how-to, I set the timer to 5 seconds by editing the first line of `/boot/grub/grub.cfg` as follows:

```
set timeout=5
```

autoinstall image done
<br>
![](/img/how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system/fig5.png)
<figcaption>*Figure 5: Editing the GRUB configuration in grub.cfg using the Boot tab in Cubic.*</figcaption>


## Step 8: Generate customized autoinstall Ubuntu live image

On the next screen, you may select the compression of the image's file system. If speed or size does not matter, you can proceed with the default selection by selecting **Generate**.

After a few moments, your customized autoinstall Ubuntu live image has been exported the project directory as an `.iso` file (*Fig. 6*). You may now use this image to autoinstall Ubuntu onto a device of your choice.

<br>
![](/img/how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system/fig6.png)
<figcaption>*Figure 6: Final screen of the Cubic wizard with a summary on the newly created image.*</figcaption>

## Step 9: Boot customized autoinstall Ubuntu live image

On a machine of your choice, boot the image, e.g. from a USB flash drive or network storage. For this how-to, I used a virtual machine and attached the `.iso` file as boot image.

*Note that Flashing an autoinstall Ubuntu live image to a USB flash drive and then plugging it into a physical computer is dangerous, as Ubuntu will install itself without user interaction onto that computer if the USB flash drive is booted! Usually, a USB flash drive may not be the default boot option but it could be depending how the computers boot manager was set up.*

As shown in *Fig. 7*, the *GRUB* boot screen shows the autoinstall entry added above as the default option. As soon as the set timer is up, the kernel is booted with autoinstall.

<br>
![](/img/how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system/fig7.png)
<figcaption>*Figure 7: The GRUB boot screen, showing the menuentry we have added in grub.cfg*</figcaption>

You may follow the progress of the installation in case a screen is attached to your system (*Fig. 8*).

<br>
![](/img/how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system/fig8.png)
<figcaption>*Figure 8: Ubuntu installing itself without user interaction using cloud-init autoinstall*</figcaption>

## Step 4 (optional): Customize image using cubic
When the file system has been created and Ubuntu has been installed to it, the system reboots from its internal drive (*Fig. 9*)

link to another article for cubic
<br>
![](/img/how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system/fig9.png)
<figcaption>*Figure 9: Ubuntu rebooting from the internal drive after the autoinstall finished*</figcaption>

After the first boot is completed, the system is ready, prompting you a login screen (*Fig 10*).

<br>
![](/img/how-to-create-an-ubuntu-server-auto-install-image-for-a-headless-ssh-ready-system/fig10.png)
<figcaption>*Figure 10: Ubuntu login screen after a successfull autoinstall*</figcaption>

Done! You can now login to your newly created machine, either directly or via `ssh` using the credentials configured as `user-data` above. In case you need to change Ubuntu's default network configuration, you can do so via the `user-data` config file.

0 comments on commit 1b2c293

Please sign in to comment.