KeblaOS

Disk Image

Created: 14th April 2025

Disk.img image

Introduction

A disk.img is a disk image file β€” a complete byte-for-byte copy of a physical disk (like a hard drive, SSD, USB stick, or CD/DVD). It acts as a virtual disk, storing everything the physical disk would: partitions, boot records, filesystems, and data files.

πŸ”§ Key Properties:

πŸ›  Common Uses:

Overview

A disk image (disk.img) is a file that emulates a physical disk, containing all the data, partition tables, boot sectors, and filesystem metadata. Disk images are widely used for emulation, virtualization, OS development, backup, and software testing.

In this project, I am creating a disk.img inside the Disk directory, which will serve as a virtual disk drive for QEMU during the development and testing of the KeblaOS operating system.

Hard Drive image

The Makefile includes a command:

make build_disk

This command creates a 512 MB FAT32-formatted disk image and mounts it on the Disk/mnt directory.

This note demonstrates various use cases and steps related to working with disk.img.


πŸ“¦ 1. Creating a Blank Disk Image

We create a blank disk image of 512MB filled with zeros:

dd if=/dev/zero of=Disk/disk.img bs=1M count=512

🧱 2. Partitioning the Disk with MBR (Master Boot Record)

To add a partition table and create a FAT32 partition:

parted Disk/disk.img --script -- mklabel msdos
parted Disk/disk.img --script -- mkpart primary fat32 1MiB 100%

The 1MiB offset is to align the partition properly for performance and compatibility.


πŸ” 3. Check if disk.img Is Mounted

mount | grep disk.img

This command helps you verify if the image is already mounted.


πŸ”„ 4. Check Loop Device Associations

losetup -l

Output might look like:

NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0         0      0         0  0 /path/to/KeblaOS/Disk/disk.img

πŸ“Œ 5. Check Mounted Partition from Image

lsblk

Output (simplified):

loop0       7:0    0   512M  0 loop
️└─loop0p1 259:0    0   511M  0 part /mnt/disk

This tells us /dev/loop0p1 is the partition we want to mount.


πŸ•΅οΈ 6. Check Which Processes Are Using disk.img

sudo lsof | grep disk.img

Helpful when umount fails due to active use.


❌ 7. Unmount Disk Image (If Mounted)

sudo umount Disk/mnt || true

Use || true to avoid script failure if it’s already unmounted.


πŸ”“ 8. Detach the Loop Device

sudo losetup -d /dev/loop0 || true

This releases the loop device from the image file.


πŸ› οΈ 9. Mounting the Partition from disk.img

After partitioning, attach and mount it:

sudo losetup -fP Disk/disk.img
sudo mount /dev/loop0p1 Disk/mnt

πŸ“ 10. Browsing Contents Inside disk.img

Once mounted, you can directly browse or manipulate files inside Disk/mnt just like a normal filesystem.

    1. Check the partitions inside disk.img
      fdisk -l disk.img
      

      This will show you if disk.img contains partitions and where they start (sector offset).

  1. Mount a specific partition inside disk.img Suppose you found a partition starting at sector 2048. To mount it:
    sudo mount -o loop,offset=$((2048 * 512)) disk.img /mnt
    

    Here, 2048 is the sector number and 512 is the sector size in bytes.

  2. If it’s a raw filesystem (no partition table)
    sudo mount -o loop disk.img /mnt
    
  3. Use losetup (optional, advanced)
    sudo losetup -fP disk.img   # Associate loop devices
    lsblk                       # Check loop devices
    sudo mount /dev/loopXpY /mnt
    
  4. πŸͺŸ On Windows, or with GUI tools: Use OSFMount

OSFMount Screenshot

Free tool to mount .img files as a drive

Allows mounting individual partitions or whole image

Great for viewing FAT, NTFS, ext2/3 (with limitations)


πŸ₯ͺ 11. Using disk.img with QEMU

You can use this image as a virtual hard disk:

qemu-system-x86_64 -drive file=Disk/disk.img,format=raw

βœ… Summary

Disk images are essential for OS development. They simulate real disk environments, allowing for:

With tools like dd, parted, losetup, and mount, disk.img becomes a powerful part of your system programming toolkit.


Β© 2025 KeblaOS Project. All rights reserved.