Merge ^/head/usr.bin/mkimg@265546
Eliminate mkimg_seek() by moving the lseek(2) calls into fdcopy().
While here, minimize use of the global tmpfd by passing it as ofd
to mkimg.
Abstract the use of ftruncate(2) behind mkimg_set_size().
Add image.c and image.h. These files will contain functions to implement
the management of the raw image that's being created. The most notable of
the API is that there's no file descriptor argument. This is because the
image is managed in memory.

Once we have the in-memory (raw) image, we can write it out acording to
different formats. This two-pass approach shields schemes from formats
and formats from schemes.
Switch to the image API:
1.  Replace calls to mkimg_set_size() with calls to image_set_size()
2.  Remove the mkimg_set_size() function
3.  As above but for mkimg_write() and image_write()

Note that this breaks mkimg(1). The image API has no implementation.
Hence doing it on my branch :-)
Replace 2 instances of fdcopy() with image_copyin(). With this, the
mkimg() function does not need the ofd argument and all references
to an output file descriptor in the input phase are gone.
Provide a file-based implementation for the image API. This almost
copies the logic verbatim from mkimg.c to image.c. The difference
is that in image.c the temporary file is always created, whereas
before we only created a temporary file when writing to stdout.

Cleanup mkimg.c now that the song and dance of using a temporary
file is gone.
When write sparse files, make sure to truncate(2) the file. Otherwise
any hole at the end is lost and the file is not of the right size.
Add a linker set for output formats. This is mostly copied and trimmed
from the linker set for schemes. Add stubs for raw and vmdk output
formats so as to demonstrate that the usage message is correct.
Add the -f option for specifying the output format. Update the manpage
accordingly. Print the scheme and the format when running verbose.
Define the sparse extent header and document the steps to take
to create the VMDK file. Add some notes as well.

What I needed to know (roughly) is what interface the image API
is to provide and whether there are (knowledge) gaps that prevent
me from implementing it for real.
Have image_write() call sparse_write() so that we can use SEEK_HOLE
and SEEK_DATA to "quickly" find occupied sectors. This is all short-
lived, because the image should not be kept in a file, but in memory.
To be precise: the image API should be both efficient and scalable
and using a file is not efficient -- it works, which is what I need
right now.
Add format_write() that calls the write() function of the output
format. Have the raw format use image_copyout() for now.
Add image_get_size() so formats can query about the image size.
Create the VMDK (embedded) descriptor file and round its size
to a multiple of 512.

TODO: We need the name of the output file -- it's in the extent
description. For now, just use "mkimg.vmdk".
Give output formats a chance to (re-)size the image before the
scheme adds the partitioning metadata. This is needed by VMDK
to round the image size to the grain size.
Commit rough but working code:
o   The VMDK image can be queried and comverted by qemu-img
o   VMware Fusion finds no errors when recovering using
    'vmware-vdiskmanager -R' and the disk can be added to any
    vortual machine.
Have the format resize twice. The first time is before we call
scheme_write(). The second time is immediately before we call
the format's write function. The first call is needed to have
the scheme know the right image size. The second call is needed
to compensate for the scheme adjusting the size while writing.
As it appears the image must be rounded to the nearest megabyte,
Check allocation errors and free memory we allocated.
Give sparse_write() the same prototype as write(2) as it's supposed to
be a drop-in replacement.