CLOSE

Before crafting an bootable iso file, first let's get familiar with the ISO file system.

An ISO file system, commonly referred to as ISO 9660, is a standard file system format for optical disc media, such as CD-ROMs and DVD-ROMs. It was developed to ensure that data can be read across different operating systems and devices, making it ideal for distributing software, music, and other digital content. The term "ISO" comes from the International Organization for Standardization (ISO), which established this standard.

Key Features of ISO 9660

1 Compatibility:

  • Designed to be widely compatible across different operating systems, including Unix, Windows, and macOS.
  • Ensures that the same disc can be read on various platforms without compatibility issues.

2 Structure:

  • Organizes data into a hierarchical directory structure.
  • Uses a volume descriptor that includes metadata about the disc, such as volume identifier and volume space size.

3 File Naming:

  • Original standard (Level 1) supports filenames up to 8 characters with a 3-character extension (similar to the old DOS 8.3 format).
  • Allows only uppercase letters, digits, and underscores in filenames.
  • Extensions like Joliet (for Windows) and Rock Ridge (for Unix) provide support for longer filenames, lowercase letters, and additional file attributes.

4 Directories:

  • Directories can be nested up to 8 levels deep in the original standard.
  • Extensions like Rock Ridge remove this limitation.

5 File Size:

  • Supports files up to 4 GB in size.

ISO 9660 Extensions

1 Joliet:

  • Developed by Microsoft to provide better support for Windows.
  • Allows filenames up to 64 characters in length.
  • Supports Unicode, enabling the use of international characters.

2 Rock Ridge:

  • Developed for Unix and Unix-like systems.
  • Preserves Unix file attributes, such as file permissions and symbolic links.
  • Supports long filenames and deeper directory structures.

Creating and Using ISO Files

Creating an ISO File

To create an ISO file, various tools can be used, such as mkisofs on Unix-like system or tools like ImgBurn on Windows.

Example:

mkisofs -o mydisk.iso -R -J /path/to/directory
  • -o mydisk.iso: Specifies the output ISO file.
  • -R: Includes Rock Ridge extensions.
  • -J: Includes Joliet extensions.
  • /path/to/directory: Specifies the directory to be included in the ISO image.

Mounting an ISO File

On Unix-like systems, an ISO file can be mounted using the loopback device:

sudo mount -o loop mydisk.iso /mnt

This command mounts the ISO file to the /mnt directory, allowing you to access its contents as if it were a physical disc.


Create an Bootable ISO File (with bootloader)

Creating an ISO file with a basic "Hello, World!" bootloader involves several steps, including writing the bootloader code, assembling it, creating a bootable binary image, and then packaging it into an ISO file. Here's a step-by-step guide with detailed explanations:

Step 1: Write the Bootloader Code

The bootloader is a small piece of software that is executed when a computer boots up. For this example, we'll write a very simple bootloader that prints "Hello, World!" to the screen.

Create a file named boot.asm and write the following assembly code:

[BITS 16]
[ORG 0x7C00]

start:
    ; Set video mode
    mov ah, 0x0E   ; BIOS teletype function
    mov bx, 0x0007 ; Page number (0) and attributes (7 for white text)

    ; Print "Hello, World!"
    mov si, hello_message
print_char:
    lodsb
    cmp al, 0
    je done
    int 0x10
    jmp print_char

done:
    hlt

hello_message db 'Hello, World!', 0

times 510-($-$$) db 0   ; Pad the rest of the 512-byte boot sector with zeros
dw 0xAA55               ; Boot sector signature

Explanation:

  • [BITS 16] and [ORG 0x7C00]:
    • BITS 16 tells the assembler that this code is written for 16-bit mode.
    • ORG 0x7C00 sets the origin to 0x7C00, which is the memory address where the BIOS loads the bootloader.
  • start:
    • This is the entry point of our bootloader.
  • Video Mode Setup:
    • mov ah, 0x0E and mov bx, 0x0007: Sets up the BIOS teletype function to print characters in white.
  • Print Loop:
    • mov si, hello_message: Load the address of the message into si.
    • lodsb: Load the next byte at si into al and increment si.
    • cmp al, 0: Check if the byte is the null terminator (end of the string).
    • je done: Jump to done if the end of the string is reached.
    • int 0x10: Call BIOS interrupt to print the character in al.
    • jmp print_char: Repeat until the end of the string.
  • Padding:
    • times 510-($-$$) db 0: Pads the bootloader to 510 bytes.
    • dw 0xAA55: Boot sector signature that BIOS looks for.

Step 2: Assemble the Bootloader

Use NASM (Netwide Assembler) to assemble the bootloader:

nasm -f bin boot.asm -o boot.bin

This command creates a binary file boot.bin from the assembly source.

Step 3: Create a Bootable Image

We need to create a bootable floppy disk image to hold our bootloader. We can use dd to create a 1.44MB floppy image and copy the bootloader into it:

dd if=/dev/zero of=boot.img bs=512 count=2880
dd if=boot.bin of=boot.img conv=notrunc

Explanation of the Commands:

  • dd if=/dev/zero of=boot.img bs=512 count=2880:
    • Creates a 1.44MB empty floppy image (boot.img).
  • dd if=boot.bin of=boot.img conv=notrunc:
    • Copies boot.bin into the beginning of boot.img without truncating it.

Step 4: Create an ISO File

Finally, we use a tool like mkisofs to create an ISO file from the floppy image.

mkisofs is a command-line utility used to create an ISO 9660 filesystem, which is a standard format for CD-ROM images. It allows you to create an ISO image from a directory of files, making it useful for creating bootable CDs and DVDs.

mkisofs -o hello.iso -b boot.img .

Explanation of the Command:

  • mkisofs -o hello.iso -b boot.img .:
    • -o hello.iso: Specifies the output ISO file.
    • -b boot.img: Specifies the boot image to use.
    • .: Specifies the current directory as the source for the ISO content.

Additional mkisofs Options

You might want to use additional options for specific use cases. Here are some useful options:

  • -J: Generate Joliet directory records (useful for Windows compatibility).
  • -R: Generate Rock Ridge directory records (useful for Unix compatibility).
  • -V "Label": Specify a volume label for the disc.
  • -iso-level: Set the ISO 9660 level (1 to 4, where higher levels support longer filenames).

Example with additional options:

mkisofs -o hello.iso -b boot.img -J -R -V "My Bootable Disc" .

Step 5: Testing the ISO File

You can test the ISO file using an emulator like QEMU or VirtualBox.

Test with QEMU:

qemu-system-x86_64 -cdrom hello.iso

Test with VirtualBox:

  • Create a new virtual machine.
  • Set the boot order to boot from the CD/DVD drive first.
  • Attach the hello.iso as a virtual CD/DVD.
  • Start the virtual machine.

This command starts a virtual machine and boots from hello.iso, where you should see "Hello, World!" printed on the screen.