Automate Ubuntu Installation
As a software engineer, I frequently needed to create new Linux VMs for testing and other purposes. The traditional way of installing Linux on a VM is very inefficient, which consumes a lot of my time. Automating the process is very appealing to me.
In this post, I’m going to give you step-by-step instructions of how to do that.
Build Ubuntu Auto-Install Iso Image
Prerequisites
To build an auto-install ubuntu image, we’ll need to install the following packages first
sudo apt update && sudo apt install p7zip wget xorriso whois
Set Up Build Environment
Next, let’s download the latest ubuntu image from the official website. The version we are going to be using is ubuntu 20.04.
# Go to your home directory.
cd
# Create a directory for our image building process.
mkdir ubuntu-auto-install-image-building
cd ubuntu-auto-install-image-building/
# Download the latest package.
wget https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-live-server-amd64.iso
# Create a source directory.
mkdir ubuntu-iso-sources
# Unpack the iso to the ubuntu-iso-sources dir.
7z -y x ubuntu-22.04.1-live-server-amd64.iso -oubuntu-iso-sources
# Go into the source directory.
cd ubuntu-iso-sources
# Move BOOT directory outside, since this is not needed in the final iso.
mv \[BOOT\] ../BOOT
Update Image Files
After the source files are unpacked, let’s update some files.
Create An Grub Entry
The first file to update is the grub file, i.e., ~/ubuntu-auto-install-image-building/ubuntu-iso-sources/boot/grub/grub.cfg
. We’ll need to do two things:
- Add an autoinstall entry.
- Make wait time shorter (this is optional).
The updated file looks like this. The highlighted lines are modified.
set timeout=5
loadfont unicode
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
menuentry "Autoinstall Ubuntu Server" {
set gfxpayload=keep
linux /casper/vmlinuz quiet autoinstall ds=nocloud\;s=/cdrom/server/ ---
initrd /casper/initrd
}
menuentry "Try or Install Ubuntu Server" {
set gfxpayload=keep
linux /casper/vmlinuz ---
initrd /casper/initrd
}
grub_platform
if [ "$grub_platform" = "efi" ]; then
menuentry 'Boot from next volume' {
exit 1
}
menuentry 'UEFI Firmware Settings' {
fwsetup
}
else
menuentry 'Test memory' {
linux16 /boot/memtest86+.bin
}
fi
Create File user-data
Next, we’ll need to create a user-data file, ~/ubuntu-auto-install-image-building/ubuntu-iso-sources/server/user-data
, with the following content.
#cloud-config
autoinstall:
version: 1
keyboard:
layout: us
identity:
hostname: ubuntu-server
password: "$y$j9T$qiKEYsg5oLCDV.XxExdEB/$41TEJJWV7he/aTyHQEDAWJyfKceDtCYVk.Agh9aAvk3"
username: swe
ssh:
allow-pw: true
install-server: true
This is a minimum setup, which only creates a user with a password. The password is devmemo-passwd
. The password string above is generated using the following command
mkpasswd "devmemo-passwd"
Note that the above config is really a minimum one. Check out this page if you need more configurations.
Create File meta-data
For our use case, we don’t need any data in meta-data file. So we’ll just need to create an empty file.
touch ~/ubuntu-auto-install-image-building/ubuntu-iso-sources/server/meta-data
Create An Auto-Installation Iso Image
# Create the auto-installation iso image.
xorriso -as mkisofs -r \
-V 'Ubuntu 22.04 LTS (Auto Install)' \
-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 .
# Check out the image.
cd ..
ls -alh ubuntu-22.04-autoinstall.iso
Now the ubuntu auto-install image is created. You can use it to auto install ubuntu on physical machines or VMs.
Create Your VMs
After you finish the last section, you already have an ubuntu auto-installation image. This section is totally optional.
In this section, I’ll demonstrate how to use this image on proxmox.
Upload The Image To Proxmox Host
First, let’s upload the image to the VM host. You can either use the web UI, or directly copy the iso into /var/lib/vz/template/iso
.
Create A VM
Next, let’s create a VM, put the iso into its cdrom, and make the cdrom the boot device.
qm create 1000 --name vm1000 --cores 4 --memory 8192 \
--scsi0 file=ssd:32 --net0 virtio,bridge=vmbr0 \
--cdrom local:iso/ubuntu-22.04-autoinstall.iso
For example, the above command will create a 4 core, 8GB ram, and 32GB ssd VM. Start the VM, and then all you have to do is to wait until the OS is fully installed.