Mount disk image linux

How to mount a multi-partition disk image in Linux?

I downloaded an raw SD card image that has two partitions. It caused some file system errors when I tried to dd it directly into an SD card. I am not sure if the card is defective or the image. Is there a way to examine this image without writing it to a physical card? Like trying to mount the partitions separately or checking the tables?

5 Answers 5

You can use kpartx or partx to create loop devices for the partitions on the image, and then mount them. So either:

$ sudo kpartx -v -a file.iso add map loop0p1 (253:17): 0 8382464 linear 7:1 2048 $ mount /dev/mapper/loop0p1 ./mnt_point . do something with the partition . $ umount ./mnt_point $ kpartx -d -v file.iso del devmap : loop0p1 loop deleted : /dev/loop0 
$ sudo partx -a -v file.iso partition: none, disk: file.iso, lower: 0, upper: 0 Trying to use '/dev/loop0' for the loop device /dev/loop0: partition table type 'dos' detected range recount: max partno=1, lower=0, upper=0 /dev/loop0: partition #1 added $ mount /dev/loop0p1 ./mnt_point . do something with the partition . $ umount /dev/loop0p1 ./mnt_point $ sudo partx -d -v /dev/loop0 partition: none, disk: /dev/loop0, lower: 0, upper: 0 /dev/loop0: partition #1 removed 

Thanks, it worked. However, it changed the modification or creation date of «file.iso» (created via «cat /dev/sda > file.iso» in my case). I could see that kpartx changed the date via «stat file.iso». Did it change any of the bytes of the ISO file? I hope not. I wish I knew that kpartx or partx might modify the disk image/file.iso file before using the kpartx software. Maybe in the future I will get hashes of «file.iso» (of some other disk or partition) before and after using kpartx with it to see if kpartx modified any file data.

You can check by setting the file to read-only, but I feel pretty confident that neither tool modifies the data.

losetup -Pf in util-linux >= 2.21 (Ubuntu 16.04)

sudo losetup -Pf disk.img sudo mkdir /mnt/loop0p1 sudo mount /dev/loop0p1 /mnt/loop0p1 

losetup -P automation

Here are functions to automate if further. 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" ) 

The answer by @Catskul and @Cristian Ciupitu is perfectly fine, but it misses the loop unmount command. So if you have to do a second image, you will end up with using loop1, loop2 etc.

Читайте также:  Reached target shutdown astra linux

you can check which loop devices are connected to which images by calling losetup:

pk:~# partx -v -a /home/pkolmann/img/Test.img partition: none, disk: /home/pkolmann/img/Test.img, lower: 0, upper: 0 Trying to use '/dev/loop1' for the loop device /dev/loop1: partition table type 'dos' detected range recount: max partno=2, lower=0, upper=0 /dev/loop1: partition #1 added /dev/loop1: partition #2 added pk:~# losetup NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC /dev/loop1 0 0 0 0 /home/pkolmann/img/Test.img 0 512 /dev/loop0 0 0 0 0 /home/pkolmann/img/Test.img 0 512 

after unmounting the partitions with

pk:~# partx -v -d /dev/loop0 partition: none, disk: /dev/loop0, lower: 0, upper: 0 /dev/loop0: partition #1 removed /dev/loop0: partition #2 removed pk:~# partx -v -d /dev/loop1 partition: none, disk: /dev/loop1, lower: 0, upper: 0 /dev/loop1: partition #1 removed /dev/loop1: partition #2 removed 

the loop devices are still used:

pk:~# losetup NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC /dev/loop1 0 0 0 0 /home/pkolmann/img/Test.img 0 512 /dev/loop0 0 0 0 0 /home/pkolmann/img/Test.img 0 512 

These need to be removed extra:

wspk:~# losetup -d /dev/loop0 wspk:~# losetup -d /dev/loop1 wspk:~# losetup 

Источник

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:

Читайте также:  H linux одним файлом

Источник

How to Mount Disk Image (*.img) File on Linux

In some cases, we need to mount disk image to modify O/S files. Let’s review the Mount Disk Image (*.img) File on Linux.

There are many cases I used and remember one of them during the ODA VM Cloning process to get zero down time I change the IP configuration to not get IP conflict.

Mount Disk Image (*.img) File on Linux

[[email protected] test]# losetup -f /dev/loop0 [[email protected] test]# losetup /dev/loop0 System.img [[email protected] test]# kpartx -a /dev/loop0 [[email protected] test]# vgscan Reading all physical volumes. This may take a while. Found volume group "vg_crpapp" using metadata type lvm2 [[email protected] test]# vgchange -ay vg_crpapp 2 logical volume(s) in volume group "vg_crpapp" now active [[email protected] test]# ls -lrt /dev/mapper/vg_crpapp* brw-rw---- 1 root disk 252, 67 Nov 5 16:32 /dev/mapper/vg_crpapp-lv_swap brw-rw---- 1 root disk 252, 66 Nov 5 16:32 /dev/mapper/vg_crpapp-lv_root [[email protected] test]# mkdir /image [[email protected] test]# mount /dev/mapper/vg_crpapp-lv_root /CRP2/ vi /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=none IPADDR=1.1.1.2 PREFIX=24 GATEWAY=1.1.1.1 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="eth0" UUID=60ba15ca-4460-40bf-9ae5-c041539f83ad ONBOOT=yes DEVICE=eth0 USERCTL=no LAST_CONNECT=1471983821

Источник

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

Источник

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