Wednesday, December 26, 2012

How to mount partitions in an ".img" file.

Files that end with ".img" are byte-by-byte copies the information on a hard disk, or "images" of the disk. These files are a bit different from ".iso" files in that they are images of filesystems intended for fast random access, especially hard disk drives, and flash drives. They almost certainly contain a FDISK partition table. IOS files, on the other hand, are an image of a CD, DVD, or BluRay disk, and so they have a rather different internal data structure.

Images can be created with the Unix/Linux command "dd", to do a quick dirty backup of an entire system, including the partition table and boot sector. You can use these images to copy them directly to hard disks to avoid having to go through the whole process of installing an operating system for every disk.

Embedded systems like Raspberry Pi, Pandaboard, and Beagleboard often build an operating system image and distribute it as an ".img" file. Intel's now defunct Mobile Linux "Moblin" distributed their operating system in this way. At one point, so did Ubuntu, for their "Netbook Remix."

How can you look at the contents of these image files? Is there a way to "plug-in" these image files and see the files inside, as if you were plugging a USB-stick into your computer? With Linux, of course there is!

As root, using "mount"

Find where the mount point begins by looking at the partition table. You need the offset in bytes. The "parted" command "unit B" sets units to bytes when displaying partition tables.

 % printf 'unit B\nprint\nquit\n' | parted raspberry-pi.img
 WARNING: You are not superuser.  Watch out for permissions.
 GNU Parted 2.3
 Using /home/ramin/boot-disks/raspberry-pi.img
 Welcome to GNU Parted! Type 'help' to view a list of commands.
 (parted) unit B
 (parted) print
 Model:  (file)
 Disk /home/ramin/boot-disks/raspberry-pi.img: 1939865600B
 Sector size (logical/physical): 512B/512B
 Partition Table: msdos

 Number Start     End         Size        Type    File system  Flags
  1      4194304B   62914559B   58720256B primary fat16        lba
  2     62914560B 1939865599B 1876951040B primary ext4

 (parted) quit

You can mount a partition in a file using the loopback option, giving the offset of the partition.

 % mkdir ./part2
 % sudo mount -o loop,offset=4194304 -t msdos ./part2
 % sudo mount -o loop,offset=62914560 -t ext4 ./part2

The "-t ext4" parameter is optional for "ext" filesystems.

The advantage of using "mount" is that you can read and write files filesystem, you can change anything, and the changes stick. The disadvantage is you need to be the superuser, but this isn't an issue on most people's personal computer.

As a normal user, using "grub-mount"

Grub has utilities which can make use of the "fuse" library, and the loopback device. First, you need to find out how Grub refers to the partitions in the image (you must use full paths here):

 % grub-fstest "$PWD/raspberry-pi.img" ls
 (loop0) (loop0,msdos2) (loop0,msdos1) (host) 

The "grub-fstest" has a "ls" command which prints each item it finds in the device hierarchy. The "host" device is your host operating system's filesystem, ignore this. Since you specified a regular file as the device to test, the device hierarchy is "loop*", in this case loop0 because 0 is currently the first available loopback device on my system. Grub found two partitions in the file, which are now listed as "loop,msdos1" and "loop,msdos2". These will be the values used for "grub-mount".

 % mkdir part1; mkdir part2
 % grub-mount -r loop,msdos1 "$PWD/raspberry-pi.img" "$PWD/part1"
 % grub-mount -r loop,msdos2 "$PWD/raspberry-pi.img" "$PWD/part2"

Now, these paritions are mounted as read-only filesystems through the loopback device.

The disadvantage of using "grub-mount" is that you need the "fuse" library installed on your system, and that you have read-only access. You cannot modify the files or directories. The advantage is that you don't need superuser privileges to do it.

No comments: