Archive for the 'losetup' Category

Mounting a Partition Within a Disk Image

Last time I walked through creating a sparse disk image using dd and cp --sparse=always. OK, we have a disk image. Now what?

Normally it would suffice to just set up a loop device and then mount, but this disk image doesn't just contain a filesystem. It has 4 partitions each with their own filesystem. This means in order to mount one of the filesystems we have to take a few extra steps.

There is an easy and a hard way to do this. I'll start with the hard way..

The Hard Way

To manually mount a particular partition on the disk we need to find a little information about where the partition is located on the disk. We need to use fdisk to find the offsets of the start and end of the partition.

dustymabe@media: mnt>sudo fdisk -l /mnt/lenovo.img Disk /mnt/lenovo.img: 500.1 GB, 500107862016 bytes 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 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: 0xf82e2761 Device Boot Start End Blocks Id System /mnt/lenovo.img1 * 2048 411647 204800 7 HPFS/NTFS/exFAT /mnt/lenovo.img2 411648 884611071 442099712 7 HPFS/NTFS/exFAT /mnt/lenovo.img3 884611072 946051071 30720000 7 HPFS/NTFS/exFAT /mnt/lenovo.img4 946051072 976361471 15155200 12 Compaq diagnostics

From the fdisk output we can see that the second partition starts at 411648 sectors times 512 bytes (210763776 bytes) into the disk image. We can also see that the partition ends at 884611071 sectors times 512 bytes (452920868352 bytes). This means that the size of the partition is 452920868352 - 210763776 = 452710104576 bytes.

We can use this information to set up a loop device using losetup like so:

dustymabe@media: >sudo losetup -v -f -o 210763776 --sizelimit 452710104576 /mnt/lenovo.img dustymabe@media: > dustymabe@media: >losetup -a /dev/loop0: []: (/mnt/lenovo.img), offset 210763776, sizelimit 452710104576 dustymabe@media: > dustymabe@media: >sudo blkid /dev/loop0 /dev/loop0: LABEL="Windows7_OS" UUID="7C64B5C764B58504" TYPE="ntfs"

Finally, the loop device can be mounted just like a normal block device:

dustymabe@media: >sudo mount -o ro /dev/loop0 ~/Desktop/mount/ dustymabe@media: >ls ~/Desktop/mount/Users All Users Default Default User desktop.ini dustymabe Public

The Easy Way

You can use partx to probe a disk image and detect all partitions. You can also have partx tell the kernel about all partitions and add devices for each partition. An example of this is shown below:

dustymabe@media: >sudo losetup -v -f /mnt/lenovo.img dustymabe@media: >sudo losetup -a /dev/loop0: [2145]:12 (/mnt/lenovo.img) dustymabe@media: > dustymabe@media: >sudo partx --show /dev/loop0 [sudo] password for dustymabe: NR START END SECTORS SIZE NAME UUID 1 2048 411647 409600 200M 2 411648 884611071 884199424 421.6G 3 884611072 946051071 61440000 29.3G 4 946051072 976361471 30310400 14.5G dustymabe@media: > dustymabe@media: >sudo partx -v --add /dev/loop0 partition: none, disk: /dev/loop0, lower: 0, upper: 0 /dev/loop0: partition table type 'dos' detected /dev/loop0: partition #1 added /dev/loop0: partition #2 added /dev/loop0: partition #3 added /dev/loop0: partition #4 added dustymabe@media: > dustymabe@media: > dustymabe@media: >sudo blkid /dev/loop0* /dev/loop0p1: LABEL="SYSTEM_DRV" UUID="0840B6DE40B6D224" TYPE="ntfs" /dev/loop0p2: LABEL="Windows7_OS" UUID="7C64B5C764B58504" TYPE="ntfs" /dev/loop0p3: LABEL="LENOVO" UUID="7652C00A52BFCCDD" TYPE="ntfs" /dev/loop0p4: LABEL="LENOVO_PART" UUID="983479C03479A244" TYPE="ntfs"

Now you can directly mount /dev/loop0p2 just as we did before.

dustymabe@media: >sudo mount -o ro /dev/loop0p2 ~/Desktop/mount/ dustymabe@media: >ls ~/Desktop/mount/Users/ All Users Default Default User desktop.ini dustymabe Public dustymabe@media: >

There ya go.. Two ways to mount partitions within a disk image. If I get a chance I'll go over a third using some free tools from the virt community.


Create A Disk Image Without Enough Free Space

Recently I purchased a new laptop. Typically my first move is to ditch Windows or Mac (yes I've done both) and install Linux. This time, just in case I ever want to completely restore the system (recovery partition and all), I decided to make a disk image of the entire hard drive.

Being that the capacity of the hard drive is close to 500 GB it actually turned out that I didn't have enough free space to store the entire image. However, since it was a brand new computer I knew there was plenty of zeroes on the disk and thus plenty of opportunity to reduce the actual size of the disk image by converting those zeroes into holes in a sparse file .

Now usually when creating a disk image I would just use dd, but dd doesn't have any options that are sparse file aware. Luckily I was able to find a page that addressed my particular predicament.

The secret sauce here is the --sparse=always option of cp. By piping the output of dd to cp, I could then copy my disk image! The next step was to fire up a live cd, connect an external hard drive and run the following command to create the disk image:
dd if=/dev/sda | cp --sparse=always /dev/stdin lenovo.img
Just to be careful I decided to md5sum both the disk image and the block device to make sure they had the same sums:
dustymabe@media: >sudo md5sum lenovo.img fbd07bc0fc46622e0bfc1ac2b44915e5 lenovo.img ubuntu@ubuntu:~$ sudo md5sum /dev/sda fbd07bc0fc46622e0bfc1ac2b44915e5 /dev/sda
Success! So how much space did I actually save by make the image a sparse image? We can see by using the du command:
dustymabe@media: mnt>du -sh lenovo.img 38G lenovo.img dustymabe@media: mnt>du -sh --apparent-size lenovo.img 466G lenovo.img
This means that the disk image only actually took 38 GB of disk space but would have taken 466 GB if I had not made it a sparse disk image... Mission accomplished!!

Until next time...