\begin{exe} {Using a File as a Disk Partition Image}

   \begin{lfbox}
      In this first exercise, we are going to create a file
      that will be used as a container for a full hard disk
      partition image, and for all intents and purposes can be
      used like a real hard partition.  In the following
      exercise, we will show how to put more than one partition
      on it and have it behave as an entire disk.
   \end{lfbox}

   \begin{enumerate}
      \item
      Create a file full of zeros 1 GB in length:
      \begin{cmd}
$ dd if=/dev/zero of=imagefile bs=1M count=1024
      \end{cmd}

      You can make a much smaller file if you like or do not
      have that much available space in the partition you
      are creating the file on.

      \item
      Put a filesystem on it:
      \begin{cmd}
$ mkfs.ext4 imagefile
      \end{cmd}
      \begin{response}
mke2fs 1.42.9 (28-Dec-2013)
imagefile is not a block special device.
Proceed anyway? (y,n) y
Discarding device blocks: done
.....
      \end{response}
      Of course you can format with a different filesystem,
      doing \textbf{mkfs.ext3}, \textbf{mkfs.vfat},
      \textbf{mkfs.xfs} etc.
      \item
      Mount it somewhere:
      \begin{cmd}
$ mkdir mntpoint
$ sudo mount -o loop imagefile mntpoint
      \end{cmd}
      You can now use this to your heart's content, putting
      files etc. on it.
      \item
      When you are done unmount it with:
      \begin{cmd}
$ sudo umount mntpoint
      \end{cmd}
   \end{enumerate}

   An alternative method to using the \verb?loop? option to
   mount would be:
   \begin{cmd}
$ sudo losetup /dev/loop2 imagefile
$ sudo mount /dev/loop2 mntpoint
 ....
$ sudo umount mntpoint
$ sudo losetup -d /dev/loop2
   \end{cmd}
   We will discuss \textbf{losetup} in a subsequent exercise,
   and you can use \filelink{/dev/loop[0-7]} but you have to be
   careful they are not already in use, as we will explain.

   You should note that using a loop device file instead of
   a real partition can be useful, but it is pretty
   worthless for doing any kind of measurements or
   benchmarking.  This is because you are placing one
   filesystem layer on top of another, which can only have
   a negative effect on performance, and mostly you just
   use the behavior of the underlying filesystem the image
   file is created on.

\end{exe}

