How to install apt packages offline with Ubuntu 22.04 Server Autoinstall
If you are making an autoinstall ISO based off Ubuntu server, you may be wondering how you can install packages from apt in an offline way. Unfortunately, the Ubuntu documentation for autoinstall doesn’t explain how to do this. This method works for apt but does not work for snaps. Snaps are garbage anyways. Thank you to Puget Systems for their great documentation on customizing isos. This code is provided as is with no warranty, it may brick your system or may not work as intended, you have been warned.
Here’s how (note there are some files you must create first, their contents is at the end of this list of commands):
- Place your iso file in a directory, cd in terminal into that directory
mkdir isotemp
cd isotemp
mkdir source-files
7z -y x "yourisofilelocation.iso" -osource-files
# extracts the isocd source-files
mv '[BOOT]' ../BOOT
# we don’t need these in the iso as they will be regeneratedcd ..
cd ..
# get back to root dirmkdir server
touch server/meta-data
# create blank meta-data file for autoinstall it will not work if this file doesn’t existcp /path/to/your/user-data-yaml-file server/user-data
cd ..
sudo unsquashfs source-files/casper/ubuntu-server-minimal.squashfs
# unsquash filesystem used to create target installmv squashfs-root edit
sudo mount -o bind /run/ edit/run
#make chrootsudo mkdir edit/dev
sudo mount --bind /dev/ edit/dev
sudo touch edit/chroot_script.sh
sudo chmod 777 edit/chroot_script.sh
sudo mkdir -p edit/opt/yourcustomfolder
sudo cp -rl "/dir/with/files/you/want/to/copy/." edit/opt/yourcustomfolder/
# this folder should contain any other files you need to copy to the installed system, you can skip this if you have nonechmod -R +r edit/opt/yourcustomfolder
cp ../chroot_script.sh edit/chroot_script.sh
# this is the script that will be run within the chroot and download stuff via aptchmod +x edit/chroot_script.sh
sudo chroot edit /bin/bash -c "su - -c /chroot_script.sh
” # run the chroot scriptsudo umount edit/run
# close the chrootsudo umount edit/dev
sudo rm -r edit/dev
sudo chmod +w source-files/casper/ubuntu-server-minimal.manifest
sudo chroot edit dpkg-query -W --showformat='${Package} ${Version}\n' > source-files/casper/ubuntu-server-minimal.manifest
sudo rm source-files/casper/ubuntu-server-minimal.squashfs
sudo rm source-files/casper/ubuntu-server-minimal.squashfs.gpg
sudo mksquashfs edit source-files/casper/ubuntu-server-minimal.squashfs -comp xz
#re-squash filesystemsudo printf "$(sudo du -sx --block-size=1 edit | cut -f1)" > source-files/casper/ubuntu-server-minimal.size 2>/dev/null
# re-calculate squash sizesudo rm -r edit
cd source-files
- Run the command below to make the iso file (output.iso, be sure to edit to correct path). Note the period on the final line!
xorriso -as mkisofs -r -iso-level 3 \
-V 'Ubuntu 22.04 LTS AUTO (EFIBIOS)' \
-o /path/to/output.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 \
.
Contents of chroot_script.sh
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devpts none /dev/pts
apt-get -y update
apt-get -yyqq install thunderbird # you can install packages here if you want
apt-get -yyqq install --download-only package1 package2 # or simply download them for installation by autoinstall
umount /proc
umount /sys
umount /dev/pts
Contents of user-data-yaml-file:
#cloud-config
autoinstall:
version: 1
late-commands:
# copy downloaded apt files to new system
- rm -r /target/var/cache/apt
- cp -r /media/minimal/var/cache/apt /target/var/cache/
- curtin in-target --target /target -- apt-get -yy install package1 package2 package3