Mounting Disk Image in Raw format
I know that using -t we can specify the file system but what is the terminology for a RAW (dd) file, which can be passed as an argument to the mount command. If my method to mount this file system is wrong please help me out in doing the same.
$ file -s nps-2010-emails.dd nps-2010-emails.dd: x86 boot sector; partition 1: starthead 254, startsector 1, 20479 sectors, extended partition table (last)\011, code offset 0x0 $ fdisk -l nps-2010-emails.dd Disk nps-2010-emails.dd: 10 MB, 10485760 bytes 255 heads, 63 sectors/track, 1 cylinders, total 20480 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Device Boot Start End Blocks Id System nps-2010-emails.dd1 1 20479 10239+ b W95 FAT32
3 Answers 3
From http://major.io/2010/12/14/mounting-a-raw-partition-file-made-with-dd-or-dd_rescue-in-linux/, there’s a suggestion to use an offset. First obtain the offset via fdisk(8) and then specify it with the offset option to mount . Use fdisk to determine the starting sector of the partition and the sector size. Then calculate offset in bytes using the starting sector number and sector size in bytes. See Mount single partition from image of entire disk (device) for an example. Finally:
mount -o offset= nps-2010-emails.dd /media/manu/
In a typical hard disk, the cells holding the data are grouped. The groupings are called sectors. The way we usually partition things, the first few sectors are kept aside for giving information about the partitions, leaving a gap. So if we have an image of an entire disk, these sectors also get included. Now, the mount command cannot directly start at the first byte, as the partition doesn’t start at the first byte. So, we will have to tell mount how many bytes to skip (so that it can avoid the extra information)and get to the actual partition. This is called the offset. Now each sector can store a certain amount of information in bytes, which is called the size of a sector. We take the total size of information that can be stored in this gap by multiplying the size of a sector, with the size of the gap in number of sectors.
From the output of fdisk there, you can see the sector size is 512 bytes and it starts at sector 1. So the offset is 1*512=512. Try the following command:
mount -t vfat -o offset=512 ps-2010-emails.dd /media/manu/
I added the filesystem type since fdisk gave it as FAT32. To mount it for writing as well, use -o offset=512,rw instead.
Mounting LVM raw disk image on Linux
The other day I needed to extract some files from a raw disk backup; problem is I never remember the commands and always have to look them up. For future reference here is how to do it.
Once you have the raw disk image create a loop device from it:
sudo losetup -f -P rawdisk.img
You should now have a loop device with all the partitions from the original raw disk:
sudo lsblk -f
...
NAME FSTYPE FSVER UUID
loop1
├─loop1p1
├─loop1p2 ext4 1.0 7f54d4b9-ad5e-4e49-8a24-27e62258e2c0
└─loop1p3 LVM2_member LVM2 001 23c515ac-e138-47ad-b4b3-cbaa119a90d3
Now let LVM know about the new LVM device:
You should now see the volume groups and logical volumes associated with the raw disk partition:
sudo vgs
...
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- 142.00g 0
sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi------- 142.00g
Activate the volume group and LVs:
Mount the LV like any other and you’re good to go:
sudo mkdir /mnt/backup-restore
sudo mount /dev/mapper/ubuntu--vg-ubuntu--lv /mnt/backup-restore
sudo umount /mnt/backup-restore
sudo lvchange -an /dev/mapper/ubuntu--vg-ubuntu--lv
sudo losetup -d /dev/loop1
sudo pvscan --cache
Mounting Raw and qcow2 Images
Mounting Raw and qcow2 images in order to inspect and use them doesn’t have to be difficult. After searching the internet, we found a couple of recommendations on how to do it. Here is what we did ourselves on an Ubuntu 16.04 Linux host.
Mounting The Raw Image
Associate the raw image with a loop device:
losetup /dev/loop0 image.raw
Map the partitions to loop devices:
You should be able to mount the partitions now:
mount /dev/mapper/loop0p1 /mnt/t01
where /mnt/t01 is a previously-existing mount point or directory.
For LVM partitions, determine the volume group name and activate it:
vgscan vgchange -ay vg_volgroupname
Mount the desired logical volume:
mount /dev/mapper/vg_volgroupname-lg_logicalgroupname /mnt/t02
where /mnt/t02 is another pre-existing mount point or directory.
Unmounting The Raw Image
Unmount the previously mounted partitions:
umount /dev/t02 umount /dev/t01
Deactivate the volume group:
vgchange -an vg_volgroupname
Undo the mapping of the partitions to the loop devices:
Mounting The qcow2 Image
Here, we shall use the QEMU Network Block Device Driver for the purposes of mounting the qcow2 image.
First, load the nbd driver.
Connect nbd to the image using qemu-nbd:
qemu-nbd -c /dev/nbd0 disk1.qcow2
Using fdisk, check the existing partitions. Mount the regular Linux partitions as is:
For LVM partitions, associate a loopback device to the LVM partition:
losetup /dev/loop0 /dev/nbd0p2
See the LVM partitions under /dev/mapper:
You should also be able to display the logical partitions using lvdisplay and the volume groups with vgdisplay. Use vgchange as above to activate the volume group.
Mount the regular LVM partitions as usual:
mount /dev/mapper/vg_volgroupname-lv_logicalgroupname /mnt/t02
Unmounting the qcow2 Image
Unmount the partitions from the qcow2 image:
umount /mnt/t02 umount /mnt/t01
Deactivate the volume group:
vgchange -an vg_volgroupname
Remove the loopback device:
Disconnect the nbd device:
Finally, remove the nbd kernel module:
We have successfully used the above procedures in mounting and unmounting raw and qcow2 images used in Linux KVM.
The procedures described above have been adapted for this article from these URLs:
Copyright © 2022. Wavespear LLC. All Rights Reserved.
Linux® is a registered trademark of Linus Torvalds in the U.S. and other countries.
Tux was created by Larry Ewing (https://isc.tamu.edu/~lewing/linux/).
Picture: Summit Supercomputer
License: https://creativecommons.org/licenses/by/2.0/
Author: Oak Ridge National Laboratory
Link: https://www.flickr.com/photos/oakridgelab/31430045337
Copyright: Oak Ridge National Laboratory
Image is based on original file from link above.
We are not associated with Oak Ridge National Laboratory
Mounting raw image files and kpartx
I’m going to demonstrate a tool for working with raw image files of hard disks. That tool is kpartx. It is used to read block devices and to create device mappings of partitions. IOW, each device file in /dev/mapper will represent a disk partition or a disk volume.
Let me show you an example and you will see what I mean.
Sometimes I use the dd command to backup my hard drives. It’s easy to do. You boot from a live CD and you use the dd command and netcat to backup your drive over your LAN to your backup server. I covered that in a previous post, so I won’t go into great detail again. I’ll skip ahead.
$ sudo dd if=/dev/sda | nc 192.168.140.11 12345
Now with my raw image file (gothbook.img) on my backup server, I want to examine that image file. I can use kpartx to list any partitions that are to be found on that drive image.
# kpartx -l gothbook.img loop1p1 : 0 512000 /dev/loop1 63 loop1p2 : 0 512000 /dev/loop1 512063 loop1p3 : 0 45056000 /dev/loop1 1024063 loop1p5 : 0 8388608 /dev/loop1 46090548 loop1p6 : 0 39070017 /dev/loop1 54492543 loop1p7 : 0 62733762 /dev/loop1 93562623
I can see from the output of kpartx that my drive image contains 6 partitions. I can see their starting offsets. The first column tells me the names of the device files that will be created if I choose to add these device partitions. Lets add them now.
# kpartx -a -v gothbook.img add map loop1p1 (253:6): 0 512000 linear /dev/loop1 63 add map loop1p2 (253:7): 0 512000 linear /dev/loop1 512063 add map loop1p3 (253:8): 0 45056000 linear /dev/loop1 1024063 add map loop1p5 (253:9): 0 8388608 linear /dev/loop1 46090548 add map loop1p6 (253:10): 0 39070017 linear /dev/loop1 54492543 add map loop1p7 (253:11): 0 62733762 linear /dev/loop1 93562623 # ls -l /dev/mapper total 0 crw-rw---- 1 root root 10, 62 2010-06-15 17:40 control brw-rw-r-- 1 neil neil 253, 6 2010-08-16 00:28 loop1p1 brw-rw-r-- 1 neil neil 253, 7 2010-08-16 00:28 loop1p2 brw-rw-r-- 1 neil neil 253, 8 2010-08-16 00:28 loop1p3 brw-rw-r-- 1 neil neil 253, 9 2010-08-16 00:28 loop1p5 brw-rw-r-- 1 neil neil 253, 10 2010-08-16 00:28 loop1p6 brw-rw-r-- 1 neil neil 253, 11 2010-08-16 00:28 loop1p7
The preceeding command added six device map files to /dev/mapper. Each of these device files corresponds to a partition from that hard drive image. We can now use these device files to mount these partitions and access any files they contain.
# mkdir /mnt/sysimage # mount /dev/mapper/loop1p6 /mnt/sysimage # ls /mnt/sysimage bin dev initrd.img lost+found opt sbin sys var boot etc initrd.img.old media proc selinux tmp vmlinuz cdrom home lib mnt root srv usr vmlinuz.old
After mounting the device file, you can access the files contained on that partition. When you are done, don’t forget to umount the partition and disconnect the device map files using kpartx.
# umount /mnt/sysimage # kpartx -d -v gothbook.img