HOW TO USE DISK IMAGES IN LINUX
Copyright (C) 2004 Daniel Brodzik
This document is free software; you may distribute an/or modify it under the terms of the GNU Free Documentation License, either version 1.2 or, at your option, any later version, with no Front-Cover Texts, no Back-Cover Texts, and no Invariant Sections.
INTRODUCTION
Sooner or later, you may have to manipulate a disk image. A disk image is a file that contains all the data on a disk—including the boot sector and file system itself—in one file. Under Windows, the utilities to work with disk images, like WinImage, cost money and are almost never open-source. Under Linux, and most other Unices, the software to do this is included with the operating system, and the commands to manipulate disk images are the very same ones you'd use to manipulate a real disk.
This document explains how to create disk images, how to mount/unmount them, and how to write them to a real disk. Note that you have to be root to use most or all of these commands.
TO CREATE A DISK IMAGE FROM AN EXISTING DISK, type 'cp device image_name'
where:
device is the device filename of the drive you want to make an image of. Here are some common ones:
/dev/fd0 -- first floppy drive (A: under DOS and Wincrap)
/dev/fd1 -- second floppy drive (B: under DOS and Wincrap)
/dev/cdrom – your first (or only) CD-ROM drive
image_name is the filename of the disk image you want to create.
TO MOUNT A DISK IMAGE, type 'mount image_name mount_point -o loop [-t type]'
where
image_name is the name of the disk image you want to mount
mount_point is the name of the directory you wish to mount the image to, which might be in '/mnt', which is made up of mount points.
type is the kernel name of the filesystem on the disk image you want to mount. You probably only need this if you're mounting HFS disk images (HFS is the filesystem of the Macintosh). Common types are 'ext2' for the Second Extended File System (which is one of Linux's native file systems); 'ext3' for the Third Extended File System (another one of Linux's native filesystems; has journaling support, which is a revolutionary way to avoid having to do a filesystem check every time you have a bad shutdown); 'vfat' for DOS/Wincrap FAT12, FAT16, and FAT32 with long file name support; 'msdos' for FAT12 and FAT16 WITHOUT long file name support; 'hfs' for Macintosh Hierarchial File System (HFS) disks; 'hfsplus' for Macintosh HFS+ disks (from MacOS 8 and later); and 'ntfs' for Micro$oft Wincrap NT/2000/XP's New Technology File System disks. Many other types also exist.
The '-o loop' option tells Linux to mount a file instead of trying to mount a block device. For this to work, you must be using a kernel with the 'loopback' driver compiled in. Most distributions compile this in.
To unmount a disk image (or anything else, for that matter), type 'umount mount_point' OR 'umount image_file_or_device'. It took me two weeks to figure out that the command to unmount disks was 'umount' instead of 'unmount'!
TO CREATE A NEW, EMPTY DISK IMAGE, type 'dd if=/dev/zero of=filename bs=blocksize count=size' to create an empty file (all zeros/nulls, that is), where:
filename is the file name of the new blank file you want to create
blocksize is the size of each block. This is the unit size for the next parameter. To make it easy, I recommend using 1 for this parameter and specifying the units in the next parameter.
size is the size of the new file you wish to create. For example, use '3' for three bytes, '3k' for 3 binary kilobytes (3072 bytes), '3kD' for 3 decimal kilobytes (3000 bytes), '3M' for 3 binary megabytes, or '3MD' for 3 decimal megabytes. In other words, the valid suffixes after the number are 'k' for kilo, 'M' for mega, 'G' for giga, or 'T' for tera, and there can be an uppercase 'D' after the suffix to indicate that the units should be decimal and not binary.
Now, type 'mkfs.type filename', where:
type is the type of file system you wish to have on the disk image. Under my Linux kernel, this can be any of the following: ext2, ext3, minix, reiserfs, vfat, xfs. See 'TO MOUNT A DISK IMAGE', above, for brief explanations of the file system types.
filename is your disk image.
TO COMPLETELY ERASE A DISK, type 'cp /dev/zero /dev/device'. This procedure COMPLETELY erases a disk, even to the point of removing any existing file system! USE EXTREME CAUTION!