Managing Raw Disk/File System Image Files
Xen users frequently deal with raw file system image files. While this isn't the ideal method for managing virtual machine storage it is the format of choice for redistribution. This article is a simple cheat sheet that will help you deal with sparse and regular image files.
To create a blank image file run:
dd if=/dev/zero bs=1M count=X > image.img
Where X is the size of the file in MB.
To enlarge an image file run:
dd if=/dev/zero bs=1M count=X >> image.img
Where X is the amount of space you want to add in MB.
To create a sparse file run:
dd if=/dev/zero of=image.img seek=X bs=1M count=0
Where X is the size of the image in MB.
ℹImportant! Sparse files are a nuanced topic. Please read this article as well if you intend to use them: Everything You Never Wanted to But Absolutely Have to Know About Sparse Files
To enlarge a sparse file run:
dd if=/dev/zero of=image.img seek=X bs=1M count=0
Where seek=X should be the current size of the sparse file plus the amount of space you wish to grow the image by in MB.
To enlarge an ext2 or ext3 file system to fill an expanded disk image run:
e2fsck -f image.img
e2resizefs image.img
e2fsck -f image.img
To create an ext2 file system on an image file run:
mke2fs image.img
To create an ext3 file system on an image file run:
mke2fs -j image.img
I often find that mke2fs' defaults for file systems between 2 and 6 gigs are not efficient for full linux installs, make sure you have enough inodes and to really save on space reduce the block size to 1KB. Optionally you can force the defaults for a "small" file system by using the -T flag:
mke2fs -j -T small image.img
This helps clip off a lot of the wasted space created by the (Gentoo in particular but any given flavour's) large number of small files.
To make a reiserfs file system run:
mkreiserfs image.img
Note that reiser's ability to incorporate files smaller than the block size into its B-* tree probably makes assigning a smaller (than 4096B) block size more costly than it's worth.
To expand a reiserfs file system to fill the available space in an image file run
resize_reiserfs image.img
To make an XFS file system run:
mkfs.xfs image.img
XFS caches heavily making it on one hand a decent file-based image performer at the cost of being slightly more fragile than other solutions. Another bonus is it is designed to scale up easily, however I tend to only use XFS as the "mother" FS on battery-backed hardware RAID setups and use ext3 for VM images due to its comparative resilience and the higher level of crash vulnerability virtual machines have over their host counterparts.
To expand an XFS file system to use all available space on a partition image run:
mount -o loop image.img /mnt/image
xfs_growfs /mnt/image
To mount an image file run
mount -o loop image.img /mnt/image
Remember not to perform any operations (like DD) on an image file while it is mounted.
To chroot into an image file with a working linux installation after mounting run:
mount -o bind /dev /mnt/image/dev
mount -t proc none /mnt/image/proc
chroot /mnt/image /bin/bash
Include /bin/bash where the default shell might be less permissive, i.e. standard sh or leave it off if the type of shell is unimportant.
To copy a file system's contents verbatim to another file system mount both then run:
cp -ax /mnt/image1/* /mnt/image2/
You should do this to a freshly created sparse or regular file to increase the effectiveness of compression when redistributing an image. In a regular file, the empty space that would otherwise be completely zeroed out instead contains remnants of deleted files.
Comments
There are no comments for this item.