Skip to content

Instantly share code, notes, and snippets.

@utkonos
Last active January 10, 2024 03:21
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save utkonos/718b150de4f86054c37ac798c02b54c6 to your computer and use it in GitHub Desktop.
Save utkonos/718b150de4f86054c37ac798c02b54c6 to your computer and use it in GitHub Desktop.
Simple Python Script to Edit an Ubuntu ISO to Add Automated Server Install Capability
import io
import pathlib
import pycdlib
ubuntu = pathlib.Path('ubuntu-22.04.1-live-server-amd64.iso')
new_iso_path = pathlib.Path('ubuntu-22.04.1-live-server-amd64-auto.iso')
iso = pycdlib.PyCdlib()
iso.open(ubuntu)
extracted = io.BytesIO()
iso.get_file_from_iso_fp(extracted, iso_path='/BOOT/GRUB/GRUB.CFG;1')
extracted.seek(0)
data = extracted.read()
print(data.decode())
new = data.replace(b' ---', b'quiet autoinstall ds=nocloud\;s=/cdrom/nocloud/ ---')
print(new.decode())
iso.rm_file(iso_path='/BOOT/GRUB/GRUB.CFG;1', rr_name='grub.cfg')
iso.add_fp(io.BytesIO(new), len(new), '/BOOT/GRUB/GRUB.CFG;1', rr_name='grub.cfg')
iso.add_directory('/NOCLOUD', rr_name='nocloud')
user_data = b"""#cloud-config
autoinstall:
version: 1
identity:
hostname: ubuntu-server
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
username: ubuntu
"""
iso.add_fp(io.BytesIO(user_data), len(user_data), '/NOCLOUD/USER_DATA;1', rr_name='user-data')
iso.add_fp(io.BytesIO(b''), len(b''), '/NOCLOUD/META_DATA;1', rr_name='meta-data')
iso.write(new_iso_path)
iso.close()
import io
import pathlib
import pycdlib
ubuntu = pathlib.Path('ubuntu-22.04.1-live-server-amd64.iso')
new_iso = pathlib.Path('ubuntu-22.04.1-live-server-amd64-auto.iso')
iso = pycdlib.PyCdlib()
iso.open(ubuntu)
extracted = io.BytesIO()
iso.get_file_from_iso_fp(extracted, iso_path='/BOOT/GRUB/GRUB.CFG;1')
extracted.seek(0)
data = extracted.read()
print(data.decode())
new = data.replace(b' ---', b'quiet autoinstall ---').replace(b'timeout=30', b'timeout=1')
print(new.decode())
iso.rm_file(iso_path='/BOOT/GRUB/GRUB.CFG;1', rr_name='grub.cfg')
iso.add_fp(io.BytesIO(new), len(new), '/BOOT/GRUB/GRUB.CFG;1', rr_name='grub.cfg')
iso.write(new_iso)
iso.close()
import io
import pathlib
import pycdlib
new_iso = pathlib.Path('ubuntu-22.04-auto.iso')
iso = pycdlib.PyCdlib()
iso.new(rock_ridge='1.09', vol_ident='CIDATA')
user_data = b"""#cloud-config
autoinstall:
version: 1
identity:
hostname: ubuntu-server
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
username: ubuntu
"""
iso.add_fp(io.BytesIO(user_data), len(user_data), '/USERDATA;1', rr_name='user-data')
iso.add_fp(io.BytesIO(b''), len(b''), '/METADATA;1', rr_name='meta-data')
iso.write(new_iso)
iso.close()
@utkonos
Copy link
Author

utkonos commented Dec 27, 2022

I have updated the scripts to the most recent LTS. I have also just tested the scripts using 22.10, ubuntu-22.10-live-server-amd64.iso, and it all works fine with that as well.

@porala Sorry, I haven't had a achance to look into how to skip forward past the menu that you're referring to. It doesn't stop at that point, one just needs to wait a bit longer. I don't need to skip past this for my use case, so I just don't have time to look into how to do that.

@changchichung
Copy link

@porala you can try to edit boot/grub/grub.cfg , update the timeout parameter

@axlroden
Copy link

axlroden commented Jan 11, 2023

For changing the grub menu, just do a complete replacement of it, you can delete menu points, and edit timeout parameters if needed.
Example untested:

import io
import pathlib

import pycdlib

ubuntu = pathlib.Path('ubuntu-22.04.1-live-server-amd64.iso')
new_iso = pathlib.Path('ubuntu-22.04.1-live-server-amd64-auto.iso')

iso = pycdlib.PyCdlib()
iso.open(ubuntu)

extracted = io.BytesIO()
iso.get_file_from_iso_fp(extracted, iso_path='/BOOT/GRUB/GRUB.CFG;1')
extracted.seek(0)
data = """YOUR GRUB CFG FILE HERE
"""

iso.rm_file(iso_path='/BOOT/GRUB/GRUB.CFG;1', rr_name='grub.cfg')
iso.add_fp(io.BytesIO(data), len(data), '/BOOT/GRUB/GRUB.CFG;1', rr_name='grub.cfg')

iso.write(new_iso)
iso.close()

@2Wdavidcunliffe
Copy link

Great work, any chance you can add in the md5 process to remove the verification issue from the edit of grub and to just plain remove the enforced check?

@axlroden
Copy link

axlroden commented Jan 23, 2023

Clear out the md5sum.txt file to cancel install verification of files.

iso.rm_file(iso_path='/md5sum.txt;1', rr_name='md5sum.txt')
iso.add_fp(io.BytesIO("", len(""), '/md5sum.txt;1', rr_name='md5sum.txt')

You could edit the md5 sum of grub in that file if you felt like it for real verification..

@2Wdavidcunliffe
Copy link

Thanks, I personally don't require a verification check. I just happened to notice during the process it was failing validation from the change to the grub.cfg. Thanks for posting the additional content to disable the check and thanks for your work! Made my life easier that's for sure :)

@2Wdavidcunliffe
Copy link

Made some modifications to disable the verifications for future reference.

import io
import pathlib

import pycdlib

ubuntu = pathlib.Path('ubuntu-20.04.5-live-server-amd64.iso')
new_iso = pathlib.Path('ubuntu-20.04.5-live-server-amd64-auto.iso')

iso = pycdlib.PyCdlib()
iso.open(ubuntu)

extracted = io.BytesIO()
iso.get_file_from_iso_fp(extracted, iso_path='/BOOT/GRUB/GRUB.CFG;1')
extracted.seek(0)
data = extracted.read()
print(data.decode())

new = data.replace(b'Install Ubuntu Server', b'Autoinstall Ubuntu Server - Disabled Verfification').replace(b' ---', b'quiet autoinstall fsck.mode=skip ---').replace(b'timeout=30', b'timeout=1')
print(new.decode())

iso.rm_file(iso_path='/BOOT/GRUB/GRUB.CFG;1', rr_name='grub.cfg')
iso.add_fp(io.BytesIO(new), len(new), '/BOOT/GRUB/GRUB.CFG;1', rr_name='grub.cfg')

iso.write(new_iso)
iso.close()

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