View on GitHub

pycdlib

Python library to read and write ISOs

Example: Creating a bootable ISO (El Torito)

This example will show how to create a bootable El Torito ISO. Here’s the complete code for the example:

try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

import pycdlib

iso = pycdlib.PyCdlib()

iso.new()

bootstr = b'boot\n'
iso.add_fp(BytesIO(bootstr), len(bootstr), '/BOOT.;1')

iso.add_eltorito('/BOOT.;1')

iso.write('eltorito.iso')

iso.close()

Let’s take a closer look at the code.

try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

import pycdlib

As usual, import the necessary libraries, including pycdlib.

iso = pycdlib.PyCdlib()

iso.new()

Create a new PyCdlib object, and then create a new, basic ISO.

bootstr = b'boot\n'
iso.add_fp(BytesIO(bootstr), len(bootstr), '/BOOT.;1')

Add a file called /BOOT.;1 to the ISO. This is the file that contains the data to be used to boot the ISO when placed into a computer. The name of the file can be anything (and can even be nested in directories), but the contents have to be very specific. Getting the appropriate data into the boot file is beyond the scope of this tutorial; see isolinux for one way of getting the appropriate data. Suffice it to say that the example code that we are using above will not actually boot, but is good enough to show the PyCdlib API usage.

iso.add_eltorito('/BOOT.;1')

Add El Torito to the ISO, making the boot file “/BOOT.;1”. After this call, the ISO is actually bootable. By default, the add_eltorito method will use so-called “no emulation” booting, which allows arbitrary data in the boot file. “Hard drive” and “floppy” emulation is also supported, though these options are more esoteric and need specifically configured boot data to work properly.

iso.write('eltorito.iso')

iso.close()

Write the ISO out to a file, and close out the PyCdlib object.


<-- Example: Extracting data from an existing ISO
Top
Example: Creating an ISO with Rock Ridge -->