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.
-
1Do 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– ilkkachu2016-10-15 08:21:47 +00:00Commented Oct 15, 2016 at 8:21
4 Answers
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.
-
17
losetup -Pis generally more convenient than this method: unix.stackexchange.com/a/316407/32558Ciro Santilli OurBigBook.com– Ciro Santilli OurBigBook.com2018-03-15 15:11:27 +00:00Commented Mar 15, 2018 at 15:11 -
2+1 This is the solution that worked for me. Thank you for including details on how to calculate the offset as well.ali14– ali142022-04-18 13:04:56 +00:00Commented Apr 18, 2022 at 13:04
-
I know OP didn't ask about doing this in an OS X terminal, but
fdisk -luwill not work there.fdiskwithout flags will give a different table.icedwater– icedwater2025-05-28 06:51:04 +00:00Commented May 28 at 6:51
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.Lightness Races in Orbit– Lightness Races in Orbit2016-10-14 16:17:08 +00:00Commented Oct 14, 2016 at 16:17 -
3
losetupandmount -o loopare 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)Stéphane Chazelas– Stéphane Chazelas2016-10-14 22:07:13 +00:00Commented Oct 14, 2016 at 22:07 -
Here are some functions to further automate
losetup: unix.stackexchange.com/a/430415/32558Ciro Santilli OurBigBook.com– Ciro Santilli OurBigBook.com2018-03-15 15:11:46 +00:00Commented Mar 15, 2018 at 15:11 -
1I don't understand the meaning of a "loop device" nor why you need
-o loopto mount the image file as one. Can you explain please? I see the Wikipedia link but still don't understand. I have an embedded Linuxrootfs.ext2roof filesystem image, and it doesn't seem to make any difference when I mount it with vs without-o loopto inspect the files.Gabriel Staples– Gabriel Staples2022-08-11 01:29:30 +00:00Commented Aug 11, 2022 at 1:29
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 msdosfor floppy-t iso9660for CD-ROM image
Linux usually tries to guess the file type.