How to mount a disk image from the command line?

  • 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.

    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?

  • 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.

  • 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
    

    This is basically `localhost` (loopback!) for disk images.

    `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)

  • 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"
    )
    
  • 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.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM