Difference between revisions of "Bering-uClibc 4.x - User Guide - Appendices - Working with Disk Image Files"

From bering-uClibc
Jump to: navigation, search
(Added navigation links to new Next appendix)
(Expanded based on use of kpartx for partition management)
Line 12: Line 12:
  
 
Under some circumstances, especially when developing and testing [[Bering-uClibc 4.x]] using a Virtual PC, it can be convenient to work with a "virtual" disk drive image rather than "physical" disk drive media. The following notes provide hints and tips on how to do this using various standard Linux tools.
 
Under some circumstances, especially when developing and testing [[Bering-uClibc 4.x]] using a Virtual PC, it can be convenient to work with a "virtual" disk drive image rather than "physical" disk drive media. The following notes provide hints and tips on how to do this using various standard Linux tools.
 +
 +
Many readers will already be familiar with mounting disk image files (in particular ISO9660 <code class="filename">.iso</code> image files) using a "loopback device", for example with commands like:
 +
mount -o loop image.iso /mnt/
 +
The main complication arises when disk ''partitions'' are involved, since it is typically necessary to work with the ''complete disk image'' at one level, in order to create and manipulate the partitions, and then to work with ''individual partition images'' at another level in order to work with files in those partitions.
 +
The problem is that the partitions start at different places within the overall disk image file and it can be difficult to work out what "offset" to use when loop-mounting individual partitions. The <tt>kpartx</tt> utility provides a neat solution to this problem.
 +
 +
The commands on this page were developed and tested on Fedora 15.
 +
Some commands must be run as user <tt>root</tt> (prefixed with '#') whereas other commands can be run as any user (prefixed with '$').
 +
 +
==Raw Hard Disk Image File==
 +
===Create the Blank Image File===
 +
There are several ways of creating an empty disk image file.
 +
One of the easiest is to use the QEMU image manipulation utility:
 +
$ qemu-img create -f raw sda.raw 1G
 +
 +
===Work with the Whole Disk Image===
 +
The whole disk image cannot be "mounted" since it doesn't directly contain a filesystem.
 +
It can however be mapped as a loopback device:
 +
# losetup /dev/loop0 sda.raw
 +
 +
At this point it is possible to manipulate the disk partition table using the usual tools: <tt>cfdisk</tt>, <tt>parted</tt> etc. For example:
 +
# parted /dev/loop0 print
 +
 +
Once the disk image contains a partition table use the <tt>kpartx</tt> tool to map the partitions:
 +
# kpartx -a /dev/loop0
 +
This creates additional device files in the <code class="filename">/dev/mapper/</code> directory corresponding to each Partition within the disk image. For example, if there are two partitions:
 +
$ ls  /dev/mapper/loop*
 +
/dev/mapper/loop0p1  /dev/mapper/loop0p2
 +
 +
===Work with a Disk Partition Image===
 +
If not already formatted each disk partition needs a filesystem. For example:
 +
# mkfs.vfat /dev/mapper/loop0p1
 +
 +
Once formatted the disk partition can be mounted:
 +
# mount /dev/mapper/loop0p1 /mnt/
 +
The files on the partition can then be manipulated using all the usual tools.
 +
 +
===Unmounting===
 +
When finished:
 +
# umount /mnt/
 +
# kpartx -d /dev/loop0
 +
# losetup -d /dev/loop0
  
  

Revision as of 11:20, 6 April 2012

Appendices - Working with Disk Image Files
Prev Bering-uClibc 4.x - User Guide Next


Introduction

Under some circumstances, especially when developing and testing Bering-uClibc 4.x using a Virtual PC, it can be convenient to work with a "virtual" disk drive image rather than "physical" disk drive media. The following notes provide hints and tips on how to do this using various standard Linux tools.

Many readers will already be familiar with mounting disk image files (in particular ISO9660 .iso image files) using a "loopback device", for example with commands like:

mount -o loop image.iso /mnt/

The main complication arises when disk partitions are involved, since it is typically necessary to work with the complete disk image at one level, in order to create and manipulate the partitions, and then to work with individual partition images at another level in order to work with files in those partitions. The problem is that the partitions start at different places within the overall disk image file and it can be difficult to work out what "offset" to use when loop-mounting individual partitions. The kpartx utility provides a neat solution to this problem.

The commands on this page were developed and tested on Fedora 15. Some commands must be run as user root (prefixed with '#') whereas other commands can be run as any user (prefixed with '$').

Raw Hard Disk Image File

Create the Blank Image File

There are several ways of creating an empty disk image file. One of the easiest is to use the QEMU image manipulation utility:

$ qemu-img create -f raw sda.raw 1G

Work with the Whole Disk Image

The whole disk image cannot be "mounted" since it doesn't directly contain a filesystem. It can however be mapped as a loopback device:

# losetup /dev/loop0 sda.raw

At this point it is possible to manipulate the disk partition table using the usual tools: cfdisk, parted etc. For example:

# parted /dev/loop0 print 

Once the disk image contains a partition table use the kpartx tool to map the partitions:

# kpartx -a /dev/loop0

This creates additional device files in the /dev/mapper/ directory corresponding to each Partition within the disk image. For example, if there are two partitions:

$ ls  /dev/mapper/loop*
/dev/mapper/loop0p1  /dev/mapper/loop0p2

Work with a Disk Partition Image

If not already formatted each disk partition needs a filesystem. For example:

# mkfs.vfat /dev/mapper/loop0p1

Once formatted the disk partition can be mounted:

# mount /dev/mapper/loop0p1 /mnt/

The files on the partition can then be manipulated using all the usual tools.

Unmounting

When finished:

# umount /mnt/
# kpartx -d /dev/loop0
# losetup -d /dev/loop0


Further information at:



Prev Up Next