How to mount a raw disk image
In this post, I will share how you can mount a raw disk image such as an image generated with dd. Raw disk image or RAW Image Format is a bit-for-bit copy of disk data, without any metadata information on files. In Linux, dd is a popular tool for data transfer by duplicating entire disk for instance. Let us create a disk image of a mount with an EXT3 file system –
[root@kauai src]# dd if=/dev/sdb of=disk.img 7233761+0 records in 7233760+0 records out 3703685120 bytes (3.7 GB) copied, 236.166 s, 15.7 MB/s [root@kauai src]# ls -alh disk.img -rw-r--r--. 1 root root 3.5G Jan 15 18:44 disk.img
We have copied a mount with multiple files into a single disk.img file which we can copy to another system. Now let us examine the raw disk layout, that we can use to mount as a file system –
[root@kauai src]# fdisk -lu disk.img You must set cylinders. You can do this from the extra functions menu. Disk disk.img: 0 MB, 0 bytes 124 heads, 62 sectors/track, 0 cylinders, total 0 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: 0xdebbbd93 Device Boot Start End Blocks Id System disk.img 630416 945623 157604 83 Linux
As we can see the raw disk has 512 byte size sectors and it starts at offset 630416, given this information we can use mount command to mount the disk image –
[root@kauai src]# mount -o loop,offset=$((630416*512)) disk.img /mnt/hdisk/ [root@kauai src]# ls -al /mnt/hdisk/ total 37 drwxr-xr-x. 3 root root 1024 Jan 15 18:39 . drwxr-xr-x. 4 root root 4096 Nov 17 20:04 .. -rw-r--r--. 1 root root 15 Jan 15 18:39 file21 -rw-r--r--. 1 root root 15 Jan 15 18:39 file22 -rw-r--r--. 1 root root 15 Jan 15 18:39 file23 -rw-r--r--. 1 root root 15 Jan 15 18:39 file24 -rw-r--r--. 1 root root 15 Jan 15 18:39 file25 -rw-r--r--. 1 root root 15 Jan 15 18:39 file26 -rw-r--r--. 1 root root 15 Jan 15 18:39 file27 -rw-r--r--. 1 root root 15 Jan 15 18:39 file28 -rw-r--r--. 1 root root 15 Jan 15 18:39 file29 -rw-r--r--. 1 root root 15 Jan 15 18:39 file30 drwx------. 2 root root 12288 Jan 15 18:37 lost+found [root@kauai src]# cat /mnt/hdisk/file26 File number 26
Here we were able to mount the disk image and be able to read the content of one of the text files.
Dids / raw_img_gpt_efi.md
I originally wanted to create bootable disks for UEFI (i)PXE booting, meaning I could directly boot premade disk images over the network, no matter what they may contain.
While this guide serves my purpose well, it’s also generic enough to be extended to almost any use case. For example, you might use it as temporary or even portable storage, mountable across different operating systems, or you might use it as a disk image for a virtual machine.
DISCLAIMER: Be very careful with the commands listed below, as you could potentially not only cause data loss, but even prevent your operating system from booting, no matter how unlikely either of those may be. Pay attention to the commands, comments and differences between the guide and your local environment.
Create a blank disk image
dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync
# Mount the image on the first available loopback device losetup -f image.img # List currently mounted loopback devices # (this is just to confirm it was mounted) losetup
Create the EFI partition on the image
# Get the loopback device for the mounted image LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: ''` # Partition the loopback device # (enter the commands/characters and press enter) gdisk $LOOP_DEV_PATH o y n # 0xEF00 w y # Trigger partition discovery for the newly partitioned loopback device partprobe $LOOP_DEV_PATH # List the partitions of the loopback device # (this is only for confirming that the partitions are visible) ls $LOOP_DEV_PATH*
# Get the loopback device for the mounted image LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: ''`p1 # Format the EFI partition as FAT32 mkfs.fat -F32 $LOOP_DEV_PATH
# Get the loopback device for the mounted image LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: ''`p1 # Make sure the target mounting directory exists mkdir -p /mnt/image # Mount the EFI partition to a local path mount $LOOP_DEV_PATH /mnt/image
Working with files on the EFI partition
At this point you should be able to freely create, edit and remove files mounted under the /mnt/image path. Once you’re done, simply continue to the next step, where we will safely and cleanly unmount the partition and image/loopback device.
Unmount the EFI partition
# Unmount the EFI partition umount /mnt/image # Remove the mount point # DISCLAIMER: Be careful with this, as you could potentially lose data if unmounting was unsuccessful etc. rm -rf /mnt/image
Unmount the image/loopback device
# Get the loopback device for the mounted image LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: ''` # Unmount the loopback device losetup -d $LOOP_DEV_PATH # Verify that the image is no longer mounted losetup -l
Working with the disk image
This section assumes that you no longer have the image mounted in any way. It also assumes your image is properly formatted and you’re familiar with its partition layout.
# Mount the image as a loopback device first # -f searches for the next free loop device (no need to manually select one) # -P triggers a scan for any available partitions in the image losetup -f -P image.img # Get the loopback device for the mounted image LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: ''`p1 # Mount the EFI partition to a local path (create path first if necessary) mkdir -p /mnt/image mount $LOOP_DEV_PATH /mnt/image # List the image contents to verify that it is mounted correctly ls -lah /mnt/image
Working with files on the image
At this point you should be able to freely create, edit and remove files mounted under the /mnt/image path. Once you’re done, simply continue to the next step, where we will safely and cleanly unmount the partition and image/loopback device.
# Unmount the EFI partition umount /mnt/image # Remove the mount point # DISCLAIMER: Be careful with this, as you could potentially lose data if unmounting was unsuccessful etc. rm -rf /mnt/image # Get the loopback device path LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: ''` # Remove the loopback device losetup -d $LOOP_DEV_PATH
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.
How do you create and partition a raw disk image?
I need to create a raw disk image for use in a virtual machine, but I need to create the partitions before creating the file system or installing the operating system. Can anyone describe a process to do this?
Which virtual machine technology are you using? Because they have different disk formats (.vhd(x) for Microsoft’s, .vmdk for VMware and so on)? I think you should create a VM of type you want with a Linux LiveCD .iso mounted and the virtual disk to be partitioned. There are a lot of guides on how to use a LiveCD to partition a disk (since there are so many variants of LiveCDs around you have to find an appropriate guide for a LiveCD you chose to use).
2 Answers 2
First create a blank raw disk image (25GB in this case):
# dd if=/dev/zero of=disk.img bs=1024k seek=25600 count=0 0+0 records in 0+0 records out 0 bytes (0 B) copied, 2.7301e-05 s, 0.0 kB/s # ls -lh total 2.0G -rw-r--r-- 1 root root 25G Dec 13 11:13 disk.img
Give it a partition table:
# parted disk.img mklabel msdos
Mount it as a loopback device (the simplest way to define cylinders, heads, and sectors):
# losetup -f disk.img # losetup -a /dev/loop0: [0801]:12059103 (/path/to/disk.img)
Check that it seems to be a valid block device:
# fdisk /dev/loop0 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): p Disk /dev/loop0: 26.8 GB, 26843545600 bytes 255 heads, 63 sectors/track, 3263 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000db005 Device Boot Start End Blocks Id System
Then use fdisk to partition it as you wish:
When you’re done you need to unloop the device (it will need unmapping and unmounting first):
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