90

I know how to mount a drive that has a corresponding device file in /dev, but I don't know how to do this for a disk image that does not represent a physical device and does not have an analogue in /dev (e.g. an ISO file or a floppy image). I know I can do this in Mac OS X by double-clicking on the disk image's icon in Finder, which will mount the drive automatically, but I would like to be able to do this from the terminal. I'm not sure if there is a general Unix way of doing this, or if this is platform-specific.

1
  • 1
    Do you mean you want to do it on the command line on OS X? You mention it, but it might be better to be explicit if the question is specific to a certain os. Also, what type of a disk image do you mean? .iso?
    – ilkkachu
    Commented Oct 15, 2016 at 8:21

4 Answers 4

93

If it was a hard-drive image with a MBR partition table, I would fdisk the image to find the offset for the partition I need to mount.

fdisk -lu /path/disk.img

Then I would mount it passing the offset.

mount -o loop,offset=xxxx /path/disk.img /mnt/disk.img.partition

The offset value is in bytes, whereas fdisk shows a block count, so you should multiply the value from the "Begin" or "Start" column of the fdisk output by 512 (or whatever the block size is) to obtain the offset to mount at.

2
78

On most modern GNU system the mount command can handle that:

mount -o loop file.iso /mnt/dir

to unmount you can just use the umount command

umount /mnt/dir

If your OS doesn't have this option you can create a loop device:

losetup -f # this will print the first available loop device ex:/dev/loop0
losetup /dev/loop0 /path/file.iso #associate loop0 with the specified file
mount /dev/loop0 /mnt/dir #It may be necessary specify the type (-t iso9660)

to umount you can use -d:

umount /mnt/dir
losetup -d /dev/loop0

If the file have partitions, example a HD image, you can use the -P parameter (depending on you OS), it will map the partitions in the file content:

losetup -P /dev/loop0 /path/file.iso # will create /dev/loop0 
ls /dev/loop0p* #the partitions in the format /dev/loop0pX
4
  • This is basically localhost (loopback!) for disk images. Commented Oct 14, 2016 at 16:17
  • 3
    losetup and mount -o loop are Linux specific. It won't work on GNU distributions using a different kernel (like hurd, illumos or kFreeBSD though illumos and FreeBSD will have the equivalent with a different syntax) Commented Oct 14, 2016 at 22:07
  • Here are some functions to further automate losetup: unix.stackexchange.com/a/430415/32558 Commented Mar 15, 2018 at 15:11
  • 1
    I don't understand the meaning of a "loop device" nor why you need -o loop to mount the image file as one. Can you explain please? I see the Wikipedia link but still don't understand. I have an embedded Linux rootfs.ext2 roof filesystem image, and it doesn't seem to make any difference when I mount it with vs without -o loop to inspect the files. Commented Aug 11, 2022 at 1:29
17

losetup -P automation for multi partition images

How to mount a disk image from the command line? | Unix & Linux Stack Exchange mentioned losetup -P, and here are some handy Bash functions to further automate things. Usage:

$ los my.img
/dev/loop0
/mnt/loop0p1
/mnt/loop0p2

$ ls /mnt/loop0p1
/whatever
/files
/youhave
/there

$ sudo losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                                                                      DIO
/dev/loop1         0      0         0  0 /full/path/to/my.img

$ # Cleanup.
$ losd 0
$ ls /mnt/loop0p1
$ ls /dev | grep loop0
loop0

Source:

los() (
  img="$1"
  dev="$(sudo losetup --show -f -P "$img")"
  echo "$dev"
  for part in "$dev"?*; do
    if [ "$part" = "${dev}p*" ]; then
      part="${dev}"
    fi
    dst="/mnt/$(basename "$part")"
    echo "$dst"
    sudo mkdir -p "$dst"
    sudo mount "$part" "$dst"
  done
)
losd() (
  dev="/dev/loop$1"
  for part in "$dev"?*; do
    if [ "$part" = "${dev}p*" ]; then
      part="${dev}"
    fi
    dst="/mnt/$(basename "$part")"
    sudo umount "$dst"
  done
  sudo losetup -d "$dev"
)
6

Try:

mount -o loop /path/to/file.iso /mnt

You might add, after loop:

  • -t msdos for floppy
  • -t iso9660 for CD-ROM image

Linux usually tries to guess the file type.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .