2

I have an ISO image that I would like to distribute. However, to make setup easier for the user, I would like add a unique .config file to each .iso file.

Is there a way to use python to modify the iso file?

Alexis
  • 23,545
  • 19
  • 104
  • 143

1 Answers1

4

There are known ways of browsing or parsing ISO files with Python libraries (see this question), but adding a file to the ISO will require filesystem modification - which is definitely far from trivial.

You could instead try to mount the ISO on your filesystem, modify it from Python, then unmount it again. A very quick example that would work under Ubuntu:

ISO_PATH = "your_iso_path_here"

# Mount the ISO in your OS
os.system("mkdir /media/tmp_iso")
os.system("mount -o rw,loop %s /media/tmp_iso" % ISO_PATH)

# Do your Pythonic manipulation here:
new_file = open("/media/tmp_iso/.config", 'w')
new_file.write(data)
new_file.close() 

# Unmount
os.system("umount /media/tmp_iso")
os.system("rmdir /media/tmp_iso")

You'll want to use subprocess instead of os.system, among other things, but this is a start.

Community
  • 1
  • 1
Peter Sobot
  • 2,476
  • 21
  • 20
  • thanks. Interesting. So if I modify the .iso file, will it change a checksum somewhere down the line? – Alexis Oct 08 '12 at 05:48
  • 1
    Not sure the extent of internal checksums in the ISO itself, but you'd end up changing its filesystem, which is a job best left to well-maintained libraries. You'll *definitely* change the checksum of the entire ISO. – Peter Sobot Oct 08 '12 at 05:51
  • is an .iso the same as a .img? – Alexis Oct 08 '12 at 07:14
  • @AlexisK - [Wikipedia](http://en.wikipedia.org/wiki/IMG_(file_format)) suggests that's true, but I've definitely found `.img` files that don't conform to ISO 9660 before. – Peter Sobot Oct 08 '12 at 14:11