Created: 14th April 2025
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.
dd
, qemu-img
, or disk utilities.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.
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
.
We create a blank disk image of 512MB filled with zeros:
dd if=/dev/zero of=Disk/disk.img bs=1M count=512
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.
disk.img
Is Mountedmount | grep disk.img
This command helps you verify if the image is already mounted.
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
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.
disk.img
sudo lsof | grep disk.img
Helpful when umount
fails due to active use.
sudo umount Disk/mnt || true
Use || true
to avoid script failure if itβs already unmounted.
sudo losetup -d /dev/loop0 || true
This releases the loop device from the image file.
disk.img
After partitioning, attach and mount it:
sudo losetup -fP Disk/disk.img
sudo mount /dev/loop0p1 Disk/mnt
disk.img
Once mounted, you can directly browse or manipulate files inside Disk/mnt
just like a normal filesystem.
fdisk -l disk.img
This will show you if disk.img contains partitions and where they start (sector offset).
sudo mount -o loop,offset=$((2048 * 512)) disk.img /mnt
Here, 2048 is the sector number and 512 is the sector size in bytes.
sudo mount -o loop disk.img /mnt
sudo losetup -fP disk.img # Associate loop devices
lsblk # Check loop devices
sudo mount /dev/loopXpY /mnt
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)
disk.img
with QEMUYou can use this image as a virtual hard disk:
qemu-system-x86_64 -drive file=Disk/disk.img,format=raw
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.