Accessing a Disk Image File

A handy computer tool is the loop device. The loop device is used to mount files as file systems.

In the house network is a collection of image files from disks. All of the disk images are files. All are backups of some kind. Some of the images are for historical references. Some are partition images and some are full hard disk images. Some are floppy disk images.

The image files of full hard disks and floppies can be copied to respective physical media with the dd command. Hard disk partition images can be copied the same way but only to a disk partition of the same or larger size. Or image files can be mounted and files within those file systems can be copied.

That is fine and dandy except most of the time all that is desired is browsing or finding files. This happens a couple times a year with the historical images.

The important tool is the losetup command. This command is used to configure and control loop devices.

Accessing a floppy disk image is straightforward:


    losetup -f
    losetup /dev/loop0 floppy.img
    mount -o ro /dev/loop0 /mountpoint
    

Likewise for a hard disk partition image:


    losetup -f
    losetup /dev/loop0 partition.img
    mount -o ro /dev/loop0 /mountpoint
    

Accessing a partition on a full disk image requires a few more steps. Disks cannot be mounted. Only partitions can be mounted. To mount a partition in a full disk image file requires knowing where the partition begins inside the image file.

The first two steps remain the same:


    losetup -f
    losetup /dev/loop0 disk.img
    

At that point disk tools such as fdisk can be used to view the image file just like a physical hard disk. The fdisk -l command will reveal the needed information to mount a partition. Important is knowing the disk sector size and starting sector of the desired partition.

The fdisk -l command lists the partitions using /dev/loop0 as part of the partition device name.

Here is a snapshot of a 20 GB hard disk that was used in a 486 computer to dual boot Windows for Workgroups (WFWG) 3.11 and Slackware 11.0:


    Disk /dev/loop0: 18.65 GiB, 20020396032 bytes, 39102336 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0x0009d558

    Device        Boot    Start      End  Sectors   Size Id Type
    /dev/loop0p1             63   996029   995967 486.3M  6 FAT16
    /dev/loop0p2         996030  1044224    48195  23.5M 83 Linux
    /dev/loop0p3        1044225 39102209 38057985  18.1G  5 Extended
    /dev/loop0p5        1044288  3148739  2104452     1G 82 Linux swap
    /dev/loop0p6        3148803 11534669  8385867     4G 83 Linux
    /dev/loop0p7       11534733 13639184  2104452     1G 83 Linux
    /dev/loop0p8       13639248 15743699  2104452     1G 83 Linux
    /dev/loop0p9       15743763 17848214  2104452     1G 83 Linux
    /dev/loop0p10      17848278 19952729  2104452     1G 83 Linux
    /dev/loop0p11      19952793 32531624 12578832     6G 83 Linux
    /dev/loop0p12      32531688 34636139  2104452     1G 83 Linux
    /dev/loop0p13      34636203 39102209  4466007   2.1G 83 Linux
    

To mount any partition in this hard disk image file, multiply the sector size by the starting sector. For example, to mount the FAT16 partition — in this case the original MS-DOS and WFWG 3.11 partition, multiply 512 * 63 = 32256.

To mount that partition:

mount -o ro,loop,offset=32256 disk.img /mountpoint

A good idea is avoiding inadvertent changes because these images are archived for historical purposes. The -o ro option mounts the partition as read-only. Additional protection is copying the disk image file to a different location and working with the copy. Another option is configuring the loop device read-only.

Posted: Category: Tutorial Tagged: General

Next: Booting a 486 from a CD

Previous: Setting a System Wakeup Time