How to mount sd-card image created with dd?
I have created an image of my Raspberry Pi SD-card using dd :
The SD-card includes two partitions (one vfat, one ext4) which are automatically mounted when I plug the card in.
My question: How can I mount these partitions from the .img file?
4 Answers 4
To avoid the need to create separate images for each partition or installing a utility like kpartx , you can mount each partition individually by specifying an offset in the mount command.
First examine the partitions in the image file and determine the offset by using fdisk :
Take the Start sector of the partition you want and multiply that value by the Units size. So if you want the second partition you’ll get 540672 * 512 = 276824064 .
Now create a folder and mount the partition:
Once you are done doing what you want with the partition data:
After some additonal testing I found the solution myself: kpartx
This command created /dev/mapper/loop0p1 and /dev/mapper/loop0p2 . Afterwards these partitions can be mounted straight forward:
if your goal is to explore or modify the content of a partition (file system), this command line will mount the file system of the sd card dump my_sdcard_dump.img into the directory mount_dir.
Dealing with an image of a whole disk with multiple partitions is quite tricky. Linux was not designed to read a partition table out of a regular file, even when attached to a loopback device, so you must carefully identify the offsets of the partitions and pass them in to the mount command.
The preferable way would be to create separate images of each partition:
Now you can easily treat these files as if they were individual partitions on a disk, mounting them as you normally would a real disk partition, by mapping them to a loop device. A loop device, or loopback device, is a virtual device that provides a translation layer for Linux to treat a file as a block device (like a disk or partition).
The loop devices are typically /dev/loop0 through /dev/loop8 . Identify an unused loop device with the losetup command:
This response indicates an unassigned loop device. Now we can assign the loop device to one of our image files:
Absence of output from this command indicates success. Now /dev/loop0 is for most purposes functionally equivalent to /dev/sdf1 of your SD card, and you can mount it as you normally would:
Repeat the process using another loop device to mount the other partition. When you’re done, unmount the filesystems and unassign the loop devices:
Источник
Tutorial: How to mount raw images (.img) images on Linux
If you have a few .img files coming as disk images from devices like floppies, CDs, DVDs, SD cards, etc, you will realize that you cannot mount the in Linux, because they contain a file system that has to be mounted.
In linux you would need to use the mount command as for any physical device, however you need to know the correct syntax that is based on understanding the information related to the partition(s) available in the image.
First step is to read the partition Start point using fdisk:
In the terminal type:
sudo fdisk -l imgfile.img
You will see an output similar to the one below:
Device boot Start End Blocks Id System
imgfile.img1 * 63 266544 722233 C W95 FAT32 (LBA)
imgfile.img2 25679 25367890 245667890+ 83 Linux
As you can see there are two partitions, one that is FAT32 and the other one that it’s ExtFS. This means that to mount the first partition we have to tell Linux that we need to start at the sector 63. The standard sector size is 512 bytes, however there are other possibilities like 128 or 1024. Assuming that the place from where you are downloading the image doesn’t specify any sector size, we can type in the terminal:
sudo mount -t vfat -o loop,offset=$((63 * 512)) imgfile.img /mnt/disk
To mount the second partition, as you can imagine:
mount -t ext4 -o loop,offset=$((25679 * 512)) imgfile.img /mnt/disk1
It’s important to copy the “Start” sector number correctly, otherwise you’ll get an error message like:
mount : wrong fs type, bad option, band superblock on /dev/loop,
missing codepage or helper proggram, or other error
In some cases useful info is found in syslog – try
dmesg | tail or so
One last thing, the standard sector size for CDs and DVDs is 2352 instead of 512. If you are opening such image, you’ll have to use this value instead of 512.
Источник
andremiller.net
A while ago I thought it would be a good idea to make a backup of my Linux server by just dumping the complete disk to a file. In retrospect, it would have been much easier had I just dumped the individual filesystems.
When I finally got around to using this backup, long after the 10GB disk had perished I realized that to use the loopback device to mount a filesystem it actually needs a filesystem to mount. What I had was a disk image, including partition table and individual partitions. To further complicate matters the data partition was also not the first partition inside this image.
For reference, I created this image using the Unix ‘dd’ tool:
I followed the instructions on http://www.trekweb.com/
jasonb/articles/linux_loopback.html to try and mount the partitions inside the disk image, but ran into two problems.
To mount a partition inside the disk image you need to calculate the offset of where the partition starts. You can use fdisk to show this information to you, but you need to specify the number of cylinders if you are using a disk image.
You then also need to multiply the start and end numbers with the calculated sectors to get a byte offset.
I found another tool more useful for this task, called parted. If you are using Ubuntu, you can install it with ‘apt-get install parted’
Now we have the offsets and we can use those to mount the filesystems using the loopback device:
That mounted the first partition, the ‘boot’ partition, but this didn’t have the data on it that I was looking for. Lets try to mount partition number 3.
Oops, that doesn’t look right. According the article referred to above if you are using a util-linux below v2.12b then you cannot specify an offset higher than 32bits. I’m using util-inux 2.13 which shouldn’t have that problem, and besides, my offset is well below the 32bit limit.
The article also offers an alternative loopback implementation that supports mounting partitions within an image, but that requires patching and recompiling your kernel which I would rather not do.
Instead I decided to extra ct the filesystem from the image which would then allow me to mount it without specifying an offset.
Doing this is quite straightforward with ‘dd’. You need to give ‘dd’ a skip count, or, how far into the source to start copying, and a count, how much to copy.
Here you can either use the single byte offsets retrieved with parted or divide them by 512 and let ‘dd’ use 512 byte blocks. Copying just one byte at a time takes a very long time, so I suggest using a larger block size.
Here is the command I used to extract my filesystem. Skip is 2313360 (1184440320/512) and Count is 17719695 (9072483840/4)
After extracting the filesystem I was able to mount it without any problems.
Источник
How can I mount a partition from dd-created image of a block device (e.g. HDD) under Linux?
I have an image of the entire disk created using dd. The disk structure follows:
The image was created using:
How would I, if it is possible, mount /dev/sda1 from the image so that I’m able to read the contents?
It’s not an option to clone the HDD again, I know how to do it if I had only cloned the single partition by itself. I hope it’s still possible with the current image.
7 Answers 7
Nowadays, there is a better way, no need to use offsets or kpartx anymore:
to free up loop0, use after umount:
I ran into this problem today and wanted to update the answers just as a reminder for myself. Instead of calculating the offset on your own, you can use a tool that provides you with mountable devices from a dd image: kpartx
In the given case, it would need something like
where loop1p1 stands for the first partition, loop1p2 for the second, etc.
You’ve got the first part: fdisk -l to find the start offset. Take that number, multiply by 512, and you’ll get the offset option to mount. So, for sda1 in your case, 5 * 512 = 2560. Then run the mount:
Loopmounting is only part of the answer.
Look at http://wiki.edseek.com/guide:mount_loopback#accessing_specific_partitions_in_the_image for help on specifying the partition. I think mount -o loop,offset=32256 /path/to/image750.img /mnt will work for you. but you really should read the mentioned tutorial.
I believe loopmounting is the answer —
The above should mount it under that directory.
This should unmount it:
losetup -P automation
Method mentioned by https://superuser.com/a/684707/128124 (added in util-linux v2.21, added Ubuntu 16.04) , here are functions to automate it further. Usage:
loop module max_part config
Decent method before util-linux v2.21.
loop is a kernel module, built into the kernel in Ubuntu 14.04.
If you configure it right, Linux automatically splits up the devices for you.
says how many partitions loop devices can generate.
It is 0 by default on Ubuntu 14.04 which is why no auto-splitting happens.
To change it, we can either add:
to a file in /etc/modprobe , or:
to /etc/default/grub and then sudo update-grub .
After a reboot, when you do:
it mounts the image to a /dev/loopX device, and automatically mounts the partitions to /dev/loopXpY devices.
So this is the most convenient method if you are willing to reboot.
Источник
Mounting a raw partition file made with dd or dd_rescue in Linux
This situation might not affect everyone, but it struck me today and left me scratching my head. Consider a situation where you need to clone one drive to another with dd or when a hard drive is failing badly and you use dd_rescue to salvage whatever data you can.
Let’s say you cloned data from a drive using something like this:
Once that’s finished, you should end up with your partition table as well as the grub data from the MBR in your image file. If you run file against the image file you made, you should see something like this:
What if you want to pull some files from this image without writing it out to another disk? Mounting it like a loop file isn’t going to work:
The key is to mount the file with an offset specified. In the output from file , there is a particular portion of the output that will help you:
This means that the filesystem itself starts on sector 63. You can also view this with fdisk -l :
Since we need to scoot 63 sectors ahead, and each sector is 512 bytes long, we need to use an offset of 32,256 bytes. Fire up the mount command and you’ll be on your way:
If you made this image under duress (due to a failing drive or other emergency), you might have to check and repair the filesystem first. Doing that is easy if you make a loop device:
Once that’s complete, you can save some time and mount the loop device directly:
Источник