Skip to content

Instantly share code, notes, and snippets.

@s3rj1k
Last active March 6, 2024 12:12
Show Gist options
  • Save s3rj1k/55b10cd20f31542046018fcce32f103e to your computer and use it in GitHub Desktop.
Save s3rj1k/55b10cd20f31542046018fcce32f103e to your computer and use it in GitHub Desktop.
Ubuntu 20.04.3 AutoInstall
# For recent versions of Ubuntu:
- https://www.pugetsystems.com/labs/hpc/ubuntu-22-04-server-autoinstall-iso/
# Docs:
- https://wiki.ubuntu.com/FoundationsTeam/AutomatedServerInstalls
- https://wiki.ubuntu.com/FoundationsTeam/AutomatedServerInstalls/ConfigReference
- https://cloudinit.readthedocs.io/en/latest/topics/datasources/nocloud.html
- https://discourse.ubuntu.com/t/please-test-autoinstalls-for-20-04/15250/53
# Download ISO Installer:
wget https://ubuntu.volia.net/ubuntu-releases/20.04.3/ubuntu-20.04.3-live-server-amd64.iso
# Create ISO distribution dirrectory:
mkdir -p iso/nocloud/
# Extract ISO using 7z:
7z x ubuntu-20.04.3-live-server-amd64.iso -x'![BOOT]' -oiso
# Or extract ISO using xorriso and fix permissions:
xorriso -osirrox on -indev "ubuntu-20.04.3-live-server-amd64.iso" -extract / iso && chmod -R +w iso
# Create empty meta-data file:
touch iso/nocloud/meta-data
# Copy user-data file:
cp user-data iso/nocloud/user-data
# Update boot flags with cloud-init autoinstall:
## Should look similar to this: initrd=/casper/initrd quiet autoinstall ds=nocloud;s=/cdrom/nocloud/ ---
sed -i 's|---|autoinstall ds=nocloud\\\;s=/cdrom/nocloud/ ---|g' iso/boot/grub/grub.cfg
sed -i 's|---|autoinstall ds=nocloud;s=/cdrom/nocloud/ ---|g' iso/isolinux/txt.cfg
# Disable mandatory md5 checksum on boot:
md5sum iso/.disk/info > iso/md5sum.txt
sed -i 's|iso/|./|g' iso/md5sum.txt
# (Optionally) Regenerate md5:
# The find will warn 'File system loop detected' and return non-zero exit status on the 'ubuntu' symlink to '.'
# To avoid that, temporarily move it out of the way
mv iso/ubuntu .
(cd iso; find '!' -name "md5sum.txt" '!' -path "./isolinux/*" -follow -type f -exec "$(which md5sum)" {} \; > ../md5sum.txt)
mv md5sum.txt iso/
mv ubuntu iso
# Create Install ISO from extracted dir (ArchLinux):
xorriso -as mkisofs -r \
-V Ubuntu\ custom\ amd64 \
-o ubuntu-20.04.3-live-server-amd64-autoinstall.iso \
-J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot \
-boot-load-size 4 -boot-info-table \
-eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot \
-isohybrid-gpt-basdat -isohybrid-apm-hfsplus \
-isohybrid-mbr /usr/lib/syslinux/bios/isohdpfx.bin \
iso/boot iso
# Create Install ISO from extracted dir (Ubuntu):
xorriso -as mkisofs -r \
-V Ubuntu\ custom\ amd64 \
-o ubuntu-20.04.3-live-server-amd64-autoinstall.iso \
-J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot \
-boot-load-size 4 -boot-info-table \
-eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot \
-isohybrid-gpt-basdat -isohybrid-apm-hfsplus \
-isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin \
iso/boot iso
# After install:
- login with 'root:root' and change root user password
- set correct hostname with 'hostnamectl'
#cloud-config
autoinstall:
version: 1
interactive-sections:
- network
- storage
locale: en_US.UTF-8
keyboard:
layout: us
ssh:
allow-pw: true
install-server: false
late-commands:
- curtin in-target --target=/target -- apt-get --purge -y --quiet=2 remove apport bcache-tools btrfs-progs byobu cloud-guest-utils cloud-initramfs-copymods cloud-initramfs-dyn-netconf friendly-recovery fwupd landscape-common lxd-agent-loader ntfs-3g open-vm-tools plymouth plymouth-theme-ubuntu-text popularity-contest rsync screen snapd sosreport tmux ufw
- curtin in-target --target=/target -- apt-get --purge -y --quiet=2 autoremove
- curtin in-target --target=/target -- apt-get clean
- sed -i 's/ENABLED=1/ENABLED=0/' /target/etc/default/motd-news
- sed -i 's|# en_US.UTF-8 UTF-8|en_US.UTF-8 UTF-8|' /target/etc/locale.gen
- curtin in-target --target=/target -- locale-gen
- ln -fs /dev/null /target/etc/systemd/system/connman.service
- ln -fs /dev/null /target/etc/systemd/system/display-manager.service
- ln -fs /dev/null /target/etc/systemd/system/motd-news.service
- ln -fs /dev/null /target/etc/systemd/system/motd-news.timer
- ln -fs /dev/null /target/etc/systemd/system/plymouth-quit-wait.service
- ln -fs /dev/null /target/etc/systemd/system/plymouth-start.service
- ln -fs /dev/null /target/etc/systemd/system/systemd-resolved.service
- ln -fs /usr/share/zoneinfo/Europe/Kiev /target/etc/localtime
- rm -f /target/etc/resolv.conf
- printf 'nameserver 8.8.8.8\nnameserver 1.1.1.1\noptions timeout:1\noptions attempts:1\noptions rotate\n' > /target/etc/resolv.conf
- rm -f /target/etc/update-motd.d/10-help-text
- rm -rf /target/root/snap
- rm -rf /target/snap
- rm -rf /target/var/lib/snapd
- rm -rf /target/var/snap
- curtin in-target --target=/target -- passwd -q -u root
- curtin in-target --target=/target -- passwd -q -x -1 root
- curtin in-target --target=/target -- passwd -q -e root
- sed -i 's|^root:.:|root:$6$3b873df474b55246$GIpSsujar7ihMzG8urUKpzF9/2yZJhR.msyFRa5ouGXOKRCVszsc4aBcE2yi3IuFVxtAGwrPKin2WAzK3qOtB.:|' /target/etc/shadow
user-data:
disable_root: false
#cloud-config
autoinstall:
version: 1
interactive-sections:
- network
- storage
locale: en_US.UTF-8
keyboard:
layout: us
ssh:
allow-pw: true
install-server: false
late-commands:
- curtin in-target --target=/target -- apt-get --purge -y --quiet=2 remove apport bcache-tools btrfs-progs byobu cloud-guest-utils cloud-initramfs-copymods cloud-initramfs-dyn-netconf friendly-recovery fwupd landscape-common lxd-agent-loader ntfs-3g open-vm-tools plymouth plymouth-theme-ubuntu-text popularity-contest screen snapd sosreport tmux ufw
- curtin in-target --target=/target -- apt-get -qq update
- curtin in-target --target=/target -- apt-get -y install wget gnupg python2.7 openssh-server
- curtin in-target --target=/target -- apt-get -qq update
- curtin in-target --target=/target -- apt-get -y dist-upgrade
- curtin in-target --target=/target -- apt-get --purge -y --quiet=2 autoremove
- sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /target/etc/ssh/sshd_config
- sed -i 's/ENABLED=1/ENABLED=0/' /target/etc/default/motd-news
- sed -i 's|# en_US.UTF-8 UTF-8|en_US.UTF-8 UTF-8|' /target/etc/locale.gen
- curtin in-target --target=/target -- locale-gen
- ln -fs /dev/null /target/etc/systemd/system/connman.service
- ln -fs /dev/null /target/etc/systemd/system/display-manager.service
- ln -fs /dev/null /target/etc/systemd/system/motd-news.service
- ln -fs /dev/null /target/etc/systemd/system/motd-news.timer
- ln -fs /dev/null /target/etc/systemd/system/plymouth-quit-wait.service
- ln -fs /dev/null /target/etc/systemd/system/plymouth-start.service
- ln -fs /dev/null /target/etc/systemd/system/systemd-resolved.service
- ln -fs /usr/share/zoneinfo/Europe/Kiev /target/etc/localtime
- rm -f /target/etc/resolv.conf
- printf 'nameserver 8.8.8.8\nnameserver 1.1.1.1\noptions timeout:1\noptions attempts:1\noptions rotate\n' > /target/etc/resolv.conf
- rm -f /target/etc/update-motd.d/10-help-text
- rm -rf /target/root/snap
- rm -rf /target/snap
- rm -rf /target/var/lib/snapd
- rm -rf /target/var/snap
- curtin in-target --target=/target -- passwd -q -u root
- curtin in-target --target=/target -- passwd -q -x -1 root
- curtin in-target --target=/target -- passwd -q -e root
- sed -i 's|^root:.:|root:$6$3b873df474b55246$GIpSsujar7ihMzG8urUKpzF9/2yZJhR.msyFRa5ouGXOKRCVszsc4aBcE2yi3IuFVxtAGwrPKin2WAzK3qOtB.:|' /target/etc/shadow
- sed -i 's/ext4 defaults/ext4 prjquota,lazytime/g' /target/etc/fstab
- findmnt --real -U -n -t ext4 -o source | awk '{system("umount -l "$1)}'
- blkid -t TYPE="ext4" -o device | awk '{system("tune2fs -O project,quota -Q prjquota "$1)}'
user-data:
disable_root: false
@dbkinghorn
Copy link

Oh man, I'm so sorry! I've been getting questions in my email from more than one place on this. I've been careless about checking where I'm replying. Gist comments can get out of hand! I blew away my last comment. Wish you all the best --Don

@bashadude
Copy link

Hi All @s3rj1k @dbkinghorn @zero-pytagoras @dmuiX I am stuck in last step can you please help me to debug this
image

After running of my late commands i got this error
These are my late commands:

  • |
    set -ex
    EFI_DEV=$(awk '$2 == "/target/boot/efi" { print $1 }' /proc/mounts)
    ROOT_DEV=$(awk '$2 ~ "^/target$" { print $1 }' /proc/mounts)
    ROOT_UUID=$(blkid -o value $ROOT_DEV | head -n 1)
    awk '$2 ~ "^/target/" { print $2 }' /proc/mounts | xargs umount
    btrfs subvolume snapshot /target /target/rootfs
    mkdir /mnt/rootfs
    mount -o subvol=rootfs $ROOT_DEV /mnt/rootfs
    mount $EFI_DEV /mnt/rootfs/boot/efi
    mount -o bind /dev /mnt/rootfs/dev
    mount -o bind /sys /mnt/rootfs/sys
    mount -o bind /proc /mnt/rootfs/proc
    sed -i "/$ROOT_UUID/s/defaults/defaults,noatime,subvol=rootfs/" /mnt/rootfs/etc/fstab
    chroot /mnt/rootfs update-grub
    chroot /mnt/rootfs grub-install --efi-directory=/boot/efi
    find /target -mindepth 1 -maxdepth 1 -not -name rootfs -exec rm -rf '{}' ;
    btrfs subvolume create /target/home
    echo "/dev/disk/by-uuid/$ROOT_UUID /home btrfs defaults,noatime,subvol=home 0 0" >> /mnt/rootfs/etc/fstab

@zero-pytagoras
Copy link

TBH it is not clear what you are trying to do any more...
you do need to add /target to bunch of places if you wish to change partitions. for example:
/mnt/rootfs/etc/fstab is suppose to be /target/mnt/rootfs/etc/fstab

@surter
Copy link

surter commented May 26, 2023

The installation of ubuntu22.04 through uefi mode pxe is successful, but after restarting, it will enter the pxe network installation instead of booting from the local hard disk. The default hard disk booting is the first boot item. Is there any way to solve this problem?

@surter
Copy link

surter commented May 26, 2023

image

@surter
Copy link

surter commented May 26, 2023

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment