GuidesAndNotes/custom-ubuntu-guide.md

143 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

2025-02-21 09:41:12 +00:00
# Creating a Custom Ubuntu Server Installation Image
This guide walks through the process of creating a customized Ubuntu Server installation image with automated setup.
## Prerequisites
Install the required tools:
```bash
sudo apt install xorriso
```
## Step 1: Extract the Original ISO Contents
```bash
# Create working directory
mkdir -p ubuntu-custom/{extract,iso}
# Enter the working directory
cd ubuntu-custom
# Download the iso file if we don't have it already
test -e ubuntu-24.04.2-live-server-amd64.iso || curl -O https://releases.ubuntu.com/24.04.2/ubuntu-24.04.2-live-server-amd64.iso
# Mount the original ISO
sudo mount -o loop ubuntu-24.04.2-live-server-amd64.iso iso
# Copy all contents
rsync -av iso/ extract/
# Unmount when done
sudo umount iso/
```
## Step 2: Create Autoinstall Configuration
```bash
# Create directory for autoinstall files
chmod u+w extract/casper/
mkdir -p extract/casper/autoinstall/
# Create user-data file with your configuration
cat > extract/casper/autoinstall/user-data << 'EOF'
#cloud-config
autoinstall:
version: 1
locale: en_US
keyboard:
layout: us
storage:
layout:
name: direct
identity:
hostname: myserver
username: user
# Generate with: mkpasswd --method=SHA-512
password: $6$rounds=4096$....
ssh:
install-server: yes
authorized-keys:
- ssh-rsa AAAAB3N....
packages:
- openssh-server
- fail2ban
- ufw
user-data:
disable_root: true
late-commands:
- echo 'user ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/user
- curtin in-target -- ufw allow OpenSSH
- curtin in-target -- ufw --force enable
EOF
# Create empty meta-data file (required)
touch extract/casper/autoinstall/meta-data
```
## Step 3: Modify Boot Configuration
Edit the GRUB configuration to enable autoinstall:
```bash
# Backup original config
chmod u+w extract/boot/grub/
cp extract/boot/grub/grub.cfg extract/boot/grub/grub.cfg.backup
# Edit the grub.cfg file
nano extract/boot/grub/grub.cfg
```
Replace or modify the first menuentry to:
```
menuentry "Automated Ubuntu Server Install" {
set gfxpayload=keep
linux /casper/vmlinuz autoinstall ds=nocloud\;s=/cdrom/casper/autoinstall/ quiet ---
initrd /casper/initrd
}
```
## Step 4: Create the Custom ISO
```bash
xorriso -as mkisofs -r \
-V "Ubuntu Custom" \
-o custom-ubuntu.iso \
-J -joliet-long \
-b boot/grub/i386-pc/eltorito.img \
-no-emul-boot -boot-load-size 4 -boot-info-table \
-eltorito-alt-boot -e EFI/boot/bootx64.efi \
-no-emul-boot \
extract/
```
## Step 5: Test the ISO
Test the ISO using a virtual machine:
```bash
# With QEMU
qemu-img create -f qcow2 test-disk.qcow2 20G
qemu-system-x86_64 -cdrom custom-ubuntu.iso \
-drive file=test-disk.qcow2,format=qcow2,if=virtio \
-m 2048
```
## Additional Customization Options
You can expand the `user-data` file to include:
1. Additional packages
2. Custom scripts to run during installation
3. Network configuration
4. More complex storage layouts with custom partitioning
5. Time zone and locale settings
## Troubleshooting
If the installer doesn't automate:
- Check that the paths in the kernel parameters match where you placed the autoinstall files
- Verify the syntax of your user-data file
- Ensure both user-data and meta-data files exist