Last active
May 21, 2024 13:18
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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?
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..
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 :)
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
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: