Brain Dump

A place to store my random thoughts and anything else I might find useful.

Archive for April, 2010

How-to: mount an uncompressed g4l image

Posted by mzanfardino on April 9, 2010

Occasionally it becomes necessary to mount an uncompressed g4l image (created with g4l explicitly with compression none). This is a simple process that can be accomplished with just a few commands. Firstly you must determine what the offset of the partition within the image you wish to mount. This is done using parted. Once you have the offset of the partition use mount to mount the partition, giving mount the offset discovered by parted.

This example assumes a file named: SMCCAPCON_HMBN_20100106.img

$ parted /home/ftp/img/SMCCAPCON_HMBN_20100106.img unit B print
WARNING: You are not superuser. Watch out for permissions.
Warning: Unable to open /home/ftp/img/SMCCAPCON_HMBN_20100106.img read-write
(Permission denied). /home/ftp/img/SMCCAPCON_HMBN_20100106.img has been opened
read-only.
Warning: Unable to open /home/ftp/img/SMCCAPCON_HMBN_20100106.img read-write
(Permission denied). /home/ftp/img/SMCCAPCON_HMBN_20100106.img has been opened
read-only.
Model: (file)
Disk /home/ftp/img/SMCCAPCON_HMBN_20100106.img: 4098834432B
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32256B 3857656319B 3857624064B primary ext3 boot
2 3857656320B 4096189439B 238533120B extended
5 3857688576B 4096189439B 238500864B logical linux-swap

Note that this image contains an entire disk image which has been formatted with a primary partition (#1) which contains root and an extended partition (#5) which contains the swap file.

The next step is to mount the root partition as a loop-back device to a local path. For this example create a mount point at /tmp/mnt (this mount point can of course be anywhere on your file system). Since the image contains both a root partition and the swap partition, it’s critical to use the offset of the root partition as a parameter for the mount command. The offset for root in this example is 32256B.

$ mkdir /tmp/mnt
$ sudo mount -o loop,offset=32256B /home/ftp/img/SMCCAPCON_HMBN_20100106.img /tmp/mnt
The partition is now mounted on /tmp/mnt. To unmount the partition, simply use umount as usual.
$ umount /tmp/mnt

NOTE: If it happens that the image is compressed using lzop, simply uncompress the image first, then take the same actions above. To uncompress a lzop file, use lzop.

$ lzop -x SMCCAPCON_HMBN_20100106.lzo
The resulting file for this example will be SMCCAPCON_HMBN_20100106, not SMCCAPCON_HMBN_20100106.img.
To verify the file type, use the file command.
$ file SMCCAPCON_HMBN_20100106.lzo
SMC_CAPCON_PLM_2010-01-21.lzo: lzop compressed data - version 1.020, LZO1X-1, os: Unix

$ file SMCCAPCON_HMBN_20100106
SMC_CAPCON_PLM_2010-01-21: x86 boot sector; partition 1: ID=0x83, active, starthead 1, startsector 63, 7534422 sectors; partition 2: ID=0x5, starthead 0,
startsector 7534485, 337365 sectors

NOTE: There maybe a need to resize the partitions of an image file. It is possible to resize the partitions using gparted. gparted can be called with the image file as a parameter. However, in my case gparted was unable to resize my partition (could not find the device to resize). I tried mounting the partition to /dev/loop0 using:

$ sudo losetup /dev/loo0 my.img

This succeeded in mounting the image to /dev/loop0 but again gparted could not find the individual partitions to mount (/dev/loop0p1 in my case). This left me to attempt to use ‘parted’. Another road-block, as my partition is ext4 and parted does not support ext4 (at least, the version I’m running).

parted suggested I use resize2fs but again, no individual partition to resize! This is becoming a tricky process.

So what did I need? I needed access to the individual partitions inside the image as accessible devices. Specifically, using the tool ‘kpartx’ I was able to mount each partition in the image to a device mapper such as /dev/mapper/loop0p1. Now I could use ‘resize2fs’ to resize /dev/mapper/loop0p1!

Advertisements

Posted in Uncategorized | 1 Comment »