Mount file as disk linux

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?

4 Answers 4

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.

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.

+1 This is the solution that worked for me. Thank you for including details on how to calculate the offset as well.

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

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

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)

Читайте также:  Линукс узнать размер папок

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.

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 
los() ( img="$1" dev="$(sudo losetup --show -f -P "$img")" echo "$dev" for part in "$dev"?*; do if [ "$part" = "$p*" ]; then part="$" 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" = "$p*" ]; then part="$" fi dst="/mnt/$(basename "$part")" sudo umount "$dst" done sudo losetup -d "$dev" ) 

Источник

Use A File As A Linux Block Device

Just like when creating a SWAP file, you can create a file on a disk and present it as a block device. The block device would have a maximum file size of the backing file, and (as long as it’s not in use) be moved around like a normal file. For example, I could create a 1GB file on the filesystem and make Linux treat the file as a disk mounted in /dev/. And guess what – that’s what we’re going to do.

Create a file and filesystem to use as a block device

First off, use dd to create a 1GB file on an existing disk that we’ll use for our storage device:

dd if=/dev/zero of=/root/diskimage bs=1M count=1024

Then ‘format’ the file to give it the structure of a filesystem. For this example we’re going to use ext4 but you could choose any filesystem that meets your needs.

Читайте также:  Linux kernel struct file

You’ll be promoted with Proceed anyway?. Type y and press return to proceed with the process.

mke2fs 1.42.5 (29-Jul-2012) /root/diskimage is not a block special device. Proceed anyway? (y,n) y Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done

Mounting a loop device

Before mounting the file we need to check that there is a free /dev/loopX loopback device that we can use to represent our new block device.

Run the below command, and if there is any output then check if it’s one of your loop devices, which will more than likely reference /dev/loop as the mounted device. If you do have a reference to our loop device then see the below section on Unmounting a loop device, or choose a number higher than the highest listed loop device, for example: usually there are several loop devices, starting with loop0 and going up in value to loop1, loop2, and so on.

cat /proc/mounts | grep /dev/loop

Once you have the file that you’d like to mount and a free loop device then you can go ahead and mount the file as a block device. You have two options:

  1. Mount the file as a block device only
  2. Mount the file as a block device and mount the filesystem of it on a local mount point (eg. /mnt/mymountpoint).

For option 1; to only mount the file as a device in /dev/, run the below command and change /root/diskimage to the path of the file you’d like to mount. loop0 can also be incremented as explained above.

losetup /dev/loop0 /root/diskimage

If you’d like this to be remounted after a machine reboot then add the above line to the rc.local file.

losetup /dev/loop0 /root/diskimage

For option 2; to mount the file and the filesystem on it, use the mount command. You must have already created the mount point locally before running the command, as you would when mounting a disk or NFS share.

Читайте также:  Линукс командная строка запустить файл

Then run the mount command and specify the loop device, the path of the file and the path to mount the filesystem on:

mount -o loop=/dev/loop0 /root/diskimage /mnt/mymountpoint

To check the file has been mounted you can use the df command:

df -h | grep mymountpoint /dev/loop0 976M 1.3M 924M 1% /mnt/mymountpoint

Unmounting a loop device

If you’ve mounted the filesystem on the block device using the mount command then make sure it’s unmounted before proceeding.

To then free the loop0 device (or which ever loop device you’ve used) you’ll need the losetup command with the d switch.

Источник

How can I mount a disk image?

I have a disk image myimage.disk which contains the partition table and a primary partition (i.e. a FAT32 filesystem). Think that as a USB pen image. I want to mount the primary partition to a local directory. I know how to mount a partition image using the loop utils but here I have disk image. My guess is that I have to mount the image «skipping» the partition table but how can I do that?

See also superuser.com/questions/117136/… You may want to use simply losetup —partscan —find —show disk.img followed by mount /dev/loop0p1 /mnt/disk

5 Answers 5

The kpartx tool makes this easier. It creates loop devices in /dev/mapper for each partition in your image. Then you can mount the loop device that corresponds with your desired partition without having to calculate the offset manually.

For example, to mount the first partition of the disk image:

kpartx -a -v myimage.disk mount /dev/mapper/loop0p1 /mnt/myimage 

When you’re done with the image, remove the loop devices:

umount /mnt/myimage kpartx -d -v myimage.disk 

Alternatively, if you have a recent kernel, and pass loop.max_part=63 on boot (if loop is built-in) or to modprobe (if loop is a module), then you can do it this way:

losetup /dev/loop0 myimage.disk partprobe /dev/loop0 # Re-read partition table if /dev/loop0 was used with a different image before mount /dev/loop0p1 /mnt/myimage 

When you’re done with the loop:

Источник

Оцените статью
Adblock
detector