Skip to content

Instantly share code, notes, and snippets.

@s3rj1k

s3rj1k/HowTo

Last active Jun 21, 2021
Embed
What would you like to do?
Ubuntu 20.04.2 AutoInstall
# 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.2/ubuntu-20.04.2-live-server-amd64.iso
# Create ISO distribution dirrectory:
mkdir -p iso/nocloud/
# Extract ISO using 7z:
7z x ubuntu-20.04.2-live-server-amd64.iso -x'![BOOT]' -oiso
# Or extract ISO using xorriso and fix permissions:
xorriso -osirrox on -indev "ubuntu-20.04.2-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.2-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.2-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
@lucas-dehandschutter

This comment has been minimized.

Copy link

@lucas-dehandschutter lucas-dehandschutter commented May 13, 2020

Hello,

Is this actually working?
I tried to reproduce this on a fresh Synology VM but the installer GUI keeps displaying.

My user-data file is a copy from a cloud-config file generated by a previous 20.04 manual install inside the /var/log/installer/autoinstall-user-data folder.

best regards,

Lucas

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented May 13, 2020

Hi @lucas-dehandschutter, works as expected, been using this for setting up 4 servers already.

Are you using UEFI boot or Legacy?

I can confirm that none UEFI works, did not have a chance to test on UEFI, but it should also work. Nothing UEFI related is used.

Probably your issue is with cloud-config (user-data) itself, try using empty one just like in gist to confirm.

@lucas-dehandschutter

This comment has been minimized.

Copy link

@lucas-dehandschutter lucas-dehandschutter commented May 25, 2020

Hello @s3rj1k

Sorry for this late answer but I was on holiday for a few days.

I've followed your tutorial excepted for one point:
Your following command was not working on my ubuntu 20.04 desktop linux because he could'nt find /usr/lib/syslinux/bios/isohdpfx.bin

xorriso -as mkisofs -r \
  -V Ubuntu\ custom\ amd64 \
  -o ubuntu-20.04-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

I had to install ISOLINUX to find this file:
sudo apt-get install isolinux

Then I changed your command to

xorriso -as mkisofs -r \
 ...
  -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin  \
  ...

I created a new QEMU virtual machine and asked her to boot on the freshly builded iso but i'm still getting a prompt at ubuntu install asking to choose the langage...I can't figure out if QEMU uses UEFI boot.

An other thing also, when I execute your sed command to add the autoinstall parameter on the grub.cfg file, i'm getting the following result:


if loadfont /boot/grub/font.pf2 ; then
	set gfxmode=auto
	insmod efi_gop
	insmod efi_uga
	insmod gfxterm
	terminal_output gfxterm
fi

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

set timeout=5
menuentry "Install Ubuntu Server" {
	set gfxpayload=keep
	linux	/casper/vmlinuz   quiet  autoinstall ds=nocloud;s=/cdrom/nocloud/ ---
	initrd	/casper/initrd
}
menuentry "Install Ubuntu Server (safe graphics)" {
	set gfxpayload=keep
	linux	/casper/vmlinuz   quiet  nomodeset autoinstall ds=nocloud;s=/cdrom/nocloud/ ---
	initrd	/casper/initrd
}
grub_platform
if [ "$grub_platform" = "efi" ]; then
menuentry 'Boot from next volume' {
	exit
}
menuentry 'UEFI Firmware Settings' {
	fwsetup
}
fi

So, it is not the initrd command that was modified as suggested in your comment but the vmlinux line.
Did you get the same output?

Thx

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented May 25, 2020

@lucas-dehandschutter
Is on ArchLinux, so it is ok that on Ubuntu it is in different location. Updated gist to show hot to run this on Ubuntu. Thanks.

-isohybrid-mbr /usr/lib/syslinux/bios/isohdpfx.bin

I am also getting that language prompt despite the fact that it was chosen it preseed config.
I actually stopped trying to hide it, as my intention was to install minimal OS and not to do fully automated install.

On the grub side, I assumed that linux /casper/vmlinuz quiet nomodeset --- is kernel params, so just like for syslinux I did a sed on it.

Does this grub config work for you?

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Jun 1, 2020

@lucas-dehandschutter

grub (EFI) setup is fixed by sed -i 's|---|autoinstall ds=nocloud\;s=/cdrom/nocloud/ ---|g' iso/boot/grub/grub.cfg

note that semicolon is escaped by \

@zepx

This comment has been minimized.

Copy link

@zepx zepx commented Jun 9, 2020

I was wondering if you have tried it with a VM (like VirtualBox)?

I tried testing this method on VirtualBox, but it doesn't seem like the autoinstall works at all. I am still getting a GUI prompt for language settings, network, proxy and etc.

Is there something missing?

Edit: I tried with UEFI turned off on VirtualBox and it seems to work.

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Jun 9, 2020

@zepx works perfectly fine on bare-metal with UEFI off and with latest fix with UEFI on.

I do not see why it would fail on VM.

It never was supposed to be full autoinstall, I mentioned this earlier, this is a way to install minimal Ubuntu using late-commands.

You can view it like an example on how to use late-commands in your autoinstall config.

It does though hide some dialog by default and leaves only essential ones. (to set up networking and do partitioning)

@wpbrown

This comment has been minimized.

Copy link

@wpbrown wpbrown commented Jun 16, 2020

@s3rj1k bravo! tested with QEMU/UEFI.... works as cdrom or drive. thanks!

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Jun 16, 2020

@wpbrown Thanks for testing it out. :)

@changchichung

This comment has been minimized.

Copy link

@changchichung changchichung commented Jun 24, 2020

-isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin 

I can not find this file in my ubuntu focal laptop , should I install some packages ??

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Jun 24, 2020

@changchichung, yes, syslinux

@onedr0p

This comment has been minimized.

Copy link

@onedr0p onedr0p commented Sep 19, 2020

Fun times, your sed command for the grub.cfg should be:

sed -i 's|---|autoinstall ds=nocloud\\\;s=/cdrom/nocloud/ ---|g' iso/boot/grub/grub.cfg
@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Sep 19, 2020

@onedr0p Thanks, that grub with escapes :)

Updated gist.

@AbelTaylor

This comment has been minimized.

Copy link

@AbelTaylor AbelTaylor commented Sep 25, 2020

I have a question. I use iso to install offline,It can be installed smoothly using the method you provided, now I want to copy some deb packages to ubuntu and install them during the automatic installation process. May I ask, how should user-data be written?

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Sep 25, 2020

Hi @myzhuh, I am not sure how this is supposed to work in terms of official support.

But you can do this easily with late-commands section.
ISO is mounted during whole installation process as RO to /cdrom.
You just have to do dpkg -i /cdrom/deb/*.deb in late-commands.

Hope that helps.

@onedr0p

This comment has been minimized.

Copy link

@onedr0p onedr0p commented Sep 25, 2020

For anyone that comes across this gist, I have created a repo with this information that allows you to build the ISO image with Docker. No need to muck around with tools on your host system other than Docker :)

@AbelTaylor

This comment has been minimized.

Copy link

@AbelTaylor AbelTaylor commented Sep 30, 2020

@s3rj1k,Thank you very much, it took effect, and there is one last little problem. During the deployment process, I tried to use systemd to add a boot-up item, and moved a XXX.service file to the /etc/systemd/system directory, this XXX.service It has been verified elsewhere and is correct. After the file is moved, I need to execute systemctl daemon-reload and systemctl enable XXX. The systemctl daemon-reload command is executed successfully. However, when the systemctl enable is executed, an error as shown in the figure will appear. Have you encountered this problem before?
show

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Sep 30, 2020

@myzhuh This will not work.

  • Systemd requires DBUS to be present, as it uses it for messaging.
  • Installer is using chroot to prepare rootfs.
  • DBUS will not work as expected in chroot.
  • All DBUS signals that are send in chroot will propagate to original system (installer system).
  • Systemd actually refuses to run any service in chroot, even systemd enable will not work.

To enable unit in chrooted system you would have to manually create symlink.

@AbelTaylor

This comment has been minimized.

Copy link

@AbelTaylor AbelTaylor commented Oct 21, 2020

@s3rj1k
thank you so much, Finally, I didn’t find the problem, but I replaced the systemctl enable with another method, which is use "ln -sf /etc/systemd/system/XXX.service /target/etc/systemd/system/multi-user.target.wants/XXX.service"

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Oct 21, 2020

@myzhuh No problem :)

@wangrichard

This comment has been minimized.

Copy link

@wangrichard wangrichard commented Oct 26, 2020

Thanks mate for sharing the detailed process. I tried to modify a bit the user-config as below in order to be fully automated.
All the others keeps your configurations.

Tested under Legacy boot, it works perfectly fully unattended after boot up.
But when I tested with UEFI boot, it always stops at the language selection, and then have to manually go on step by step.
Not sure anything makes it different between UEFI and Legacy boot.

#cloud-config
autoinstall:
version: 1
early-commands:
- systemctl stop ssh # otherwise packer tries to connect and exceed max attempts
network:
network:
version: 2
ethernets:
eth0:
dhcp4: yes
dhcp-identifier: mac
apt:
preserve_sources_list: false
primary:
- arches: [amd64]
uri: "http://archive.ubuntu.com/ubuntu/"
ssh:
install-server: yes
allow-pw: no
identity:
hostname: ubuntu-00
password: "$6$FhcddHFVZ7ABA4Gi$9l4yURWASWe8xEa1jzI0bacVLvhe3Yn4/G3AnU11K3X0yu/mICVRxfo6tZTB2noKljlIRzjkVZPocdf63MtzC0" # root
username: ubuntu # root doesn't work
packages:
- open-vm-tools
user-data:
disable_root: false
late-commands:
- echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu
- sed -ie 's/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX="net.ifnames=0 ipv6.disable=1 biosdevname=0"/' /target/etc/default/grub
- curtin in-target --target /target update-grub2

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Oct 26, 2020

Hi @wangrichard
You could be experiencing this issue:
https://gist.github.com/s3rj1k/55b10cd20f31542046018fcce32f103e#gistcomment-3460045

Does minimal config (the one that is in this gist) works for you when using UEFI?

@wangrichard

This comment has been minimized.

Copy link

@wangrichard wangrichard commented Oct 26, 2020

Thanks mate for the hint. That solves with the mentioned fix below.
sed -i 's|---|autoinstall ds=nocloud\;s=/cdrom/nocloud/ ---|g' iso/boot/grub/grub.cfg

I was misled by below statement:
grub (EFI) setup is fixed by sed -i 's|---|autoinstall ds=nocloud;s=/cdrom/nocloud/ ---|g' iso/boot/grub/grub.cfg

Really appreciate your aids!

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Oct 26, 2020

@wangrichard No prob, command is already fixed in gist.

@sjerman

This comment has been minimized.

Copy link

@sjerman sjerman commented Nov 20, 2020

This fragment avoids having to disable the checksum ..

# re-generate md5sum
# 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
@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Nov 20, 2020

@sjerman Thanks, will add as an option to Gist.

@cesarjorgemartinez

This comment has been minimized.

Copy link

@cesarjorgemartinez cesarjorgemartinez commented Dec 8, 2020

Hi,

I try to automate build for 20.04.1 live server but fails.
The identity section is not processed (not create the user, not set the hostname), and packer fails to connect to ssh. The network I think that also not processed.

In month april with the 20.04 it working correctly.

This is my Ubuntu builder:
https://github.com/cesarjorgemartinez/automate-virtual-machine-linux-images/tree/master/Ubuntu20Minimal

This is my issue:
cesarjorgemartinez/automate-virtual-machine-linux-images#42

What changes between 20.04 and 20.04.1 to work with this version?

Regards,
Cesar Jorge

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Dec 9, 2020

@cesarjorgemartinez Hi, am not sure how a HashiCorp/Packer issue is related to this gist.

@lwbt

This comment has been minimized.

Copy link

@lwbt lwbt commented Dec 29, 2020

You can exclude files and directories which you don't want to have extracted. So:

7z x ubuntu-20.04.1-live-server-amd64.iso -x'![BOOT]' -oiso

saves you from having to perform:

rm -rf 'iso/[BOOT]/'
@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Dec 29, 2020

@lwbt Thanks, updated gist.

@jantytgat

This comment has been minimized.

Copy link

@jantytgat jantytgat commented Jan 7, 2021

Hi,

I try to automate build for 20.04.1 live server but fails.
The identity section is not processed (not create the user, not set the hostname), and packer fails to connect to ssh. The network I think that also not processed.

In month april with the 20.04 it working correctly.

This is my Ubuntu builder:
https://github.com/cesarjorgemartinez/automate-virtual-machine-linux-images/tree/master/Ubuntu20Minimal

This is my issue:
cesarjorgemartinez/automate-virtual-machine-linux-images#42

What changes between 20.04 and 20.04.1 to work with this version?

Regards,
Cesar Jorge

@cesarjorgemartinez I have exactly the same issue with Packer.
The problem is not in Packer though, it is subiquity which is failing (as I've found after several hours of troubleshooting).
It eventually fails in the run_unattended_upgrades step, which cannot be disabled.

Guess we'll have to wait until 20.04.2 comes out...

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Jan 7, 2021

@jantytgat Does a regular install inside a VM works with your unattended script?

@jantytgat

This comment has been minimized.

Copy link

@jantytgat jantytgat commented Jan 7, 2021

@jantytgat Does a regular install inside a VM works with your unattended script?

My unattended script was working till like a month ago.
Nothing has changed, but the installer crashed.

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Jan 7, 2021

@jantytgat I mean as of now, can you replicate the issue without Packer, using regular ISO and VM?

@jantytgat

This comment has been minimized.

Copy link

@jantytgat jantytgat commented Jan 8, 2021

@jantytgat I mean as of now, can you replicate the issue without Packer, using regular ISO and VM?

No, but guess what: it works again without changes 🤯

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Jan 9, 2021

@jantytgat I do not think that this issue was with packer or with this gist itself.

Maybe ISO changed in-between our discussion.

@jantytgat

This comment has been minimized.

Copy link

@jantytgat jantytgat commented Jan 9, 2021

@s3rjk that was what I was saying all along. 😄
But it is bizarre that it happened, as the ISO hasn't changed since August I think.

My bet is on the automated update of the installer in 20.04.1, and that it somehow crashed in the unattended upgrades on the final step.
Found some traces of Python scripts crashing everything and causing a reboot.

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Jan 9, 2021

@jantytgat That ISO installer uses snap to install an OS, when internet connection is available it fetches newer snap image :)

@mcginty

This comment has been minimized.

Copy link

@mcginty mcginty commented Feb 10, 2021

Thanks so much for this reference! This is incredibly helpful.

Two small notes:

  • you can go without the 7z dependency by using xorriso instead:

    xorriso -osirrox on -indev "ubuntu-20.04.1-live-server-amd64.iso" -extract / iso
  • And you can also use the same command for Arch/Ubuntu if you extract isohdpfx.bin from the iso with

    dd if="ubuntu-20.04.1-live-server-amd64.iso" bs=1 count=432 of="isohdpfx.bin"
@pat0s

This comment has been minimized.

Copy link

@pat0s pat0s commented Feb 21, 2021

@s3rj1k
Can someone help me? When I try to reboot, the installer crashes and shows me a window with error message.
pop_up_window

Full report.
ERROR

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Feb 21, 2021

@mcginty Thanks, updated gist with xorriso option.
dd is a nice trick but would like to avoid that :)

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Feb 21, 2021

@pat0s looks like something wrong with md5sum of kernel extra module package

@jotihojr

This comment has been minimized.

Copy link

@jotihojr jotihojr commented Mar 4, 2021

Thanks dude! Worked like a charm :-)

The only thing I changed, was beautify that find somewhat:
(cd iso; find ! -name md5sum.txt ! -path './isolinux/*' -follow -type f -exec md5sum {} \+ >../md5sum.txt)

@joern-iwersen

This comment has been minimized.

Copy link

@joern-iwersen joern-iwersen commented Mar 26, 2021

Thank you so much for this document and the continuing "living" improvement process. This is the best source of information about cloud-init auto installation i've found!

One issue i always run into: When installing the generated iso on a Virtual Box VM I get an "generating crash report" error at the end of the installation. When i restart the VM and remove the mounted image, everythink works well. But i'd like to get rid of this error because a user could think that the installation went wrong. Does anyone already have experience with this?
2021-03-11 15_52_06

@mcginty

This comment has been minimized.

Copy link

@mcginty mcginty commented Mar 29, 2021

@joern-iwersen that looks like your fstabupdate.sh didn't return 0.

I noticed this issue also when setting up my ISO: the Ubuntu installer says "Install complete!" even when your late commands don't complete successfully. It's truly annoying.

@joern-iwersen

This comment has been minimized.

Copy link

@joern-iwersen joern-iwersen commented Mar 30, 2021

Thank you very much for your quick reply. I added an “exit 0” to the fstabupdate.sh file (now it’s post-setup.sh) but the error is still the same. The installation process runs until the process is done and I can hit the “reboot now” button. In this second the crash report is triggered.
2021-03-30 16_10_41

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Mar 30, 2021

@joern-iwersen How about, for testing purposes only, replacing entire post install script with exit 0 to verify that error comes in fact from script and not from wrong installer presets?

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Mar 30, 2021

@pat0s Updated gist for Ubuntu 20.04.2

@mtaylor-a

This comment has been minimized.

Copy link

@mtaylor-a mtaylor-a commented Mar 31, 2021

Is there any way to adapt this to netboot? Would I point my TFTP downloaded grub.cfg to an HTML site instead of /cdrom?

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Mar 31, 2021

@mtaylor-a NFS should work I assume, did not test this

@joern-iwersen

This comment has been minimized.

Copy link

@joern-iwersen joern-iwersen commented Mar 31, 2021

@s3rj1k thank you for your hint. Good point, i just tested it, but the error is still the same. So i don't think that this comes from an invalid script. One thing i noticed is that the line "running curtin hook" still has its loading icon / spinner at the end of the setup. Is that normal? Or is there may be another process that is not exited correctly?

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Mar 31, 2021

@joern-iwesen

Can you please test again with minimal user-data config?
Something like this:

#cloud-config
autoinstall:
  version: 1
  interactive-sections:
    - network
    - storage
  locale: en_US.UTF-8
  keyboard:
    layout: us
  ssh:
    allow-pw: true
    install-server: false
  user-data:
    disable_root: false

I am assuming that Canonicial yet again done it's dark deed, broke late-commands support that is.

@joern-iwersen

This comment has been minimized.

Copy link

@joern-iwersen joern-iwersen commented Apr 7, 2021

Even with this minimal setup the error occurs. I worked around this issue by using this code to unmount the iso and reboot the system automatically (late-commands):

#cloud-config
autoinstall:
  version: 1
  interactive-sections:
    - locale
    - keyboard
    - identity
  user-data:
    disable_root: false
  late-commands:
    - curtin in-target --target=/target -- echo "Please wait...will reboot automatically"
    - curtin in-target --target=/target -- umount cdrom/
    - reboot

I read about that this issue can occur when the installer is not able to unmount the iso file. Now i got some special errors and warnings but the system is able to reboot.
2021-04-07 10_56_48
2021-04-07 15_44_11

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Apr 7, 2021

@joern-iwersen are you sure that this is not a network issue with ISO getting mounted with iKVM (BMC).
I assume that you use some kind of remote IPMI management and VirtualMedia?

@Drachlowski

This comment has been minimized.

Copy link

@Drachlowski Drachlowski commented Apr 16, 2021

@s3rj1k
Can you help me?

I have the following error:
image

image

My user-data looks like this:


#cloud-config
autoinstall:
  version: 1
  identity:
    hostname: ubuntu-server
    password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
    username: ubuntu
  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
@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Apr 16, 2021

@Drachlowski please provide logs from View full report

Also please note that I am no in any way related to Canonical and do this in my free time.

@Drachlowski

This comment has been minimized.

Copy link

@Drachlowski Drachlowski commented Apr 19, 2021

@s3rj1k

These are the error-messages, which i got:

image

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Apr 19, 2021

@Drachlowski

This comment has been minimized.

Copy link

@Drachlowski Drachlowski commented Apr 19, 2021

@s3rj1k

It doesn't work at all, i got the same error-messages. These are appearing after the auto security updates

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Apr 19, 2021

@Drachlowski

This comment has been minimized.

Copy link

@Drachlowski Drachlowski commented Apr 20, 2021

@s3rj1k

I found the solution, why it didn't worked. I downloaded the ubuntu-server-iso directly from the ubuntu-Website and that was the reason, why I had troubles with the autoinstall. When you are using the iso, you got with the wget command, it works perfectly

@kukat

This comment has been minimized.

Copy link

@kukat kukat commented Apr 28, 2021

not working with qemu, stop at language selector view

#create disk
qemu-img create -f qcow2 ubuntu-20.04.2-live-server-amd64.qcow2 20G

#run vm
qemu-system-x86_64 \
    -machine type=q35,accel=hvf \
    -smp 4 \
    -hda ubuntu-20.04.2-live-server-amd64.qcow2 \
    -cdrom ./ubuntu-20.04.2-live-server-amd64-autoinstall.iso \
    -m 4G \
    -vga virtio \
    -usb \
    -device usb-tablet \
    -display default,show-cursor=on
@s3rj1k

This comment has been minimized.

@andycui66

This comment has been minimized.

Copy link

@andycui66 andycui66 commented May 13, 2021

I have a Ubuntu Server 20.04.2 and installed syslinux. But I cannot find the isohdpfx.bin file.
`
vagrant@vagrant:/usr/lib/syslinux$ ls -al
total 44
drwxr-xr-x 4 root root 4096 May 13 12:27 .
drwxr-xr-x 83 root root 4096 May 13 12:27 ..
drwxr-xr-x 4 root root 4096 May 13 12:27 mbr
-rw-r--r-- 1 root root 26920 Dec 23 2019 memdisk
drwxr-xr-x 5 root root 4096 May 13 12:27 modules

vagrant@vagrant:/usr/lib/syslinux$ ls -al mbr
total 28
drwxr-xr-x 4 root root 4096 May 13 12:27 .
drwxr-xr-x 4 root root 4096 May 13 12:27 ..
-rw-r--r-- 1 root root 439 Dec 23 2019 altmbr.bin
drwxr-xr-x 4 root root 4096 May 13 12:27 diag
drwxr-xr-x 2 root root 4096 May 13 12:27 extra
-rw-r--r-- 1 root root 440 Dec 23 2019 gptmbr.bin
-rw-r--r-- 1 root root 440 Dec 23 2019 mbr.bin
`

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented May 13, 2021

@andycui66 you need isolinux package installed

@brsolomon-deloitte

This comment has been minimized.

Copy link

@brsolomon-deloitte brsolomon-deloitte commented May 28, 2021

The gist still displays the following comment:

## Should look similar to this: initrd=/casper/initrd quiet autoinstall ds=nocloud;s=/cdrom/nocloud/ ---

And yet the initial grub.cfg for a stock 20.04.2 ISO looks like this

menuentry "Install Ubuntu Server" {
	set gfxpayload=keep
	linux	/casper/vmlinuz   quiet  ---
	initrd	/casper/initrd
}

Therefore the sed will not modify the initrd line, it will modify the linux line, as pointed out in https://gist.github.com/s3rj1k/55b10cd20f31542046018fcce32f103e#gistcomment-3317342.

So which one is it? It seems like this has not been fully clarified or there is some ambiguity in the gist.

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented May 28, 2021

@brsolomon-deloitte

Should look similar to this was initially referring to isolinux config line

How do you propose changing comment to remove ambiguity?

@brsolomon-deloitte

This comment has been minimized.

Copy link

@brsolomon-deloitte brsolomon-deloitte commented May 28, 2021

Should look similar to this was initially referring to isolinux config line

Ah, I see, thank you. Though it is just a detail, perhaps the comment could say something such as

# Modifies iso/isolinux/txt.cfg to look similar to:
# append   initrd=/casper/initrd quiet  autoinstall ds=nocloud;s=/cdrom/nocloud/ ---

Which would make less ambiguous which file is referred to and what directive/key is modified there.

@brsolomon-deloitte

This comment has been minimized.

Copy link

@brsolomon-deloitte brsolomon-deloitte commented May 28, 2021

I've tested this with Ubuntu Server 20.04.2.0 and booting from the resulting ISO with VirtualBox 6.1.22. Thanks for all the time you put in here @s3rj1k. The resulting ISO supports an unattended install (no prompts).

Providing some additional details about setup for others using this method in the interest of reproducibility.

I am using as a build VM an EC2 instance using AMI ami-00399ec92321828f5 (Ubuntu 20.04.2 LTS). This VM holds the user-data file and shell script that builds a remastered ISO placed under /opt.

All commands run as root. Bare-minimum user-data - place at /root/user-data:

#cloud-config
autoinstall:
  version: 1
  identity:
    hostname: sensor
    password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
    realname: Ubuntu user
    username: ubuntu
  locale: en_US.UTF-8
  storage:
    layout:
      name: lvm
  ssh:
    install-server: false
    allow-pw: true
    authorized-keys: []
  user-data:
    disable_root: false
  late-commands:
    - curtin in-target --target=/target -- umount cdrom/
    - reboot

This hash is for the password ubuntu similar to the examples in Ubuntu docs.

Build script, placed at /root/build-iso.sh:

#!/bin/bash

if [[ $EUID -ne 0 ]]; then
    echo 'Must be root (UID 0)'
    exit 1
fi

set -ex

apt-get update -y
apt-get install -y --no-install-recommends \
    isolinux \
    syslinux \
    xorriso

pushd /opt
if [[ ! -f 'ubuntu-20.04.2-live-server-amd64.iso' ]]; then
    curl -fsSL -O http://releases.ubuntu.com/20.04/ubuntu-20.04.2-live-server-amd64.iso
fi
mkdir -p iso/nocloud/

# -osirrox on copy files from ISO image to disk filesystem
# -indev=the input drive from which to load ISO image
# -extract iso_rr_path disk_path: Copy the file objects at and
#  underneath iso_rr_path to their corresponding addresses at and underneath disk_path
/usr/bin/xorriso \
    -osirrox on \
    -indev 'ubuntu-20.04.2-live-server-amd64.iso' \
    -extract / iso

# Files are missing w bits, this will make them 644 rather than 444
chmod -R +w iso

touch iso/nocloud/meta-data
cp -a ~/user-data iso/nocloud/user-data

# Update boot flags with cloud-init autoinstall
# Modifies iso/isolinux/txt.cfg to look similar to:
# append   initrd=/casper/initrd quiet  autoinstall ds=nocloud;s=/cdrom/nocloud/ ---
/usr/bin/sed -i 's|---|autoinstall ds=nocloud\\\;s=/cdrom/nocloud/ ---|g' iso/boot/grub/grub.cfg
/usr/bin/sed -i 's|---|autoinstall ds=nocloud;s=/cdrom/nocloud/ ---|g' iso/isolinux/txt.cfg

# Disable mandatory md5 checksum on boot
/usr/bin/md5sum iso/.disk/info > iso/md5sum.txt
/usr/bin/sed -i 's|iso/|./|g' iso/md5sum.txt

# Create Install ISO from extracted dir
# https://www.ecma-international.org/wp-content/uploads/ECMA-119_4th_edition_june_2019.pdf
/usr/bin/xorriso \
    -as mkisofs \
    -r \
    -volid ubuntu-nsm-amd64 \
    -o 'unattended-ubuntu-20.04.2-live-server-amd64.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

/usr/bin/stat unattended-ubuntu-20.04.2-live-server-amd64.iso
popd

Place at /root/build-iso.sh and chmod +x.

Then execute /root/build-iso.sh. This will leave /opt/unattended-ubuntu-20.04.2-live-server-amd64.iso

SCP this from the 'build VM' to your host where you have VirtualBox installed.

Create a New stock VM in Virtualbox; in this example I used:

  • 4096 M RAM
  • 32 G disk

Settings > Storage > Storage Devices > Controller:IDE > Empty > Select the disk icon > Choose a disk file > select local path to .iso.

Select Start. Be patient as several screens may appear to hang but will then come through.

You may need to press Enter once to be taken into the login prompt for ubuntu.

Note: this script is not idempotent. Probably a good candidate for conversion to Ansible to achieve that.

@soutrikand

This comment has been minimized.

Copy link

@soutrikand soutrikand commented Jun 16, 2021

The post shared by @brsolomon-deloitte worked like a charm. Was wondering how i could copy files to the ISO and have the same stuff in the target system so that i can install few apps directly from the debfiles as part of post-install.

The copy doesnt seem to work if i put in latecommand:

  • cp -r /cdrom/copythrough /target/home/

Any idea where i might go wrong?

@brsolomon-deloitte

This comment has been minimized.

Copy link

@brsolomon-deloitte brsolomon-deloitte commented Jun 16, 2021

Was wondering how i could copy files to the ISO and have the same stuff in the target system so that i can install few apps directly from the debfiles as part of post-install.

This should be possible, just double-check your src path.

Example - I have stuff inside /opt/isocopy/preseed and then call

/usr/bin/xorriso -as mkisofs -o xxx.iso ... /opt/isocopy

Then in late-commands use

- cp -a /cdrom/preseed/script.sh /target/usr/sbin/

Where script.sh was, prior to xorriso, put at /opt/isocopy/preseed/script.sh

@rharikri

This comment has been minimized.

Copy link

@rharikri rharikri commented Jun 21, 2021

Hi. Under the late-command section. I have been trying to run the script that asks for super user privileges by default to start its execution.

  • Whenever I use: curtin in-target --target=/target bash /media/script1.sh ( the sudo password prompt appears ) ==> I get the error as non-zero exit status 1. Usually without this sudo, the script wont run properly. I assume it is asking because of the password that is required to run this script as described.

  • However, I tried inputting the password like: curtin in-target --target=/target -- echo "<the_password>\n" | sudo -S bash /media/script1.sh ( wherein the -S stands for stdin ). But I still get the same non-zero exit status 1 .

  • Does the password need to be followed by \n ?

  • Do I have to specify the password in the hash format or normal format?

Kindly help me(probably with a proper commandline), I have been banging my head on this for such a long time. Thank you for your time!

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Jun 21, 2021

@rharikri I think that you don't need sudo, installer runs as root

@rharikri

This comment has been minimized.

Copy link

@rharikri rharikri commented Jun 21, 2021

@s3rj1k Thank you for the reply. May I confirm that curtin in-target --target=/target -- bash /media/script1.sh is the right way? Because the problem happens only with script1.sh (since it is the only one that prompts sudo to execute) and all other scripts (which do not invoke sudo prompt by default)
curtin in-target --target=/target -- bash /media/script2.sh and curtin in-target --target=/target -- bash /media/script3.sh run without any issue.

@s3rj1k

This comment has been minimized.

Copy link
Owner Author

@s3rj1k s3rj1k commented Jun 21, 2021

@rharikri I don't thinks that installer has sudo installed and configured, just patch-up your script, remove all sudo calls, this should help.

Also curtin in-target executes in chroot, so your installed system should already include configured sudo.

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