Bering-uClibc 4.x - User Guide - Appendices - Working with Disk Image Files
Appendices - Working with Disk Image Files | ||
---|---|---|
Prev | Bering-uClibc 4.x - User Guide | Next |
Contents
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 Files
Create a 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 256M
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:
Virtual Machine Disk Image Files
Virtual Machine technologies such as VMware, VirtualBox and QEMU use their own file formats which are typically more efficient than "raw" image files. One disadvantage of these file formats is they cannot be processed directly by native Linux tools.
The QEMU qcow2
file format is used as an example.
Some utilities (such as qemu-nbd
) can help with specific virtual image formats and the guestfish utility provides a very flexible and generic solution for many disk operations.
Create a Blank Image File
$ qemu-img create -f qcow2 sda.qcow2 256M
Using qemu-nbd
A qcow2 image cannot be mounted directly since it doesn't use a file format that the standard tools recognize. The workaround is to use a QEMU tool which presents a qcow2 image as a Network Block Device. Before this can be used the kernel module must be loaded:
# modprobe nbd max_part=8
The qemu-nbd
command is a lot like the losetup
command.
It is used to associate a pseudo device with a disk image file. For example:
# qemu-nbd -c /dev/nbd0 sda.qcow2
This command stays running in the foreground so either run it in a separate shell or in the background (&).
From then on everything is the same as when using /dev/loop0
. For example:
# cfdisk /dev/nbd0 # kpartx -a /dev/nbd0 # mkfs.vfat -n LEAF_BUC_V4 /dev/mapper/nbd0p1 # syslinux /dev/mapper/nbd0p1 # kpartx -d /dev/nbd0 # qemu-nbd -d /dev/nbd0
Using guestfish
The guestfish
utility, part of libguestfs, is a tool for manipulating virtual disk image files.
See the guestfish man page for more information.
A key benefit is that it does not need "root" permissions.
Some examples:
- List the partitions in image file
sda.qcow2
:$ guestfish add sda.qcow2 : run : part-list /dev/vda
- Copy all the files from the
/tmp/mydir/
directory on the host OS to the root directory of the first partition within the disk image file, then list the files:$ guestfish add sda.qcow2 : run : mount /dev/vda1 / : copy-in /tmp/mydir/* / : ls /
When the guestfish
command is run with no arguments it enters an interactive command-line session.
Prev | Up | Next |