First version of zfs bash completions

Originally written in 2010 for use with zfs-fuse.
Update bash completion

Check whether /dev/zfs is writable, and prefix zfs commands with sudo if
not. Far from bulletproof, but an incremental improvement nevertheless.
I don't want to try and make it *too* smart as the completions need to
load as nearly instantaneously as possible; this should at least cover
the most common cases.

Also, don't include snapshots in general-purpose completions (get,
inherit, list) by default. This makes working with large numbers of
snapshots considerably easier.
Improve bash completions for zfs get and list

The completion script now has some understanding of the switches to
these commands, and does a better job with comma-separated argument
lists.
Improve bash completion of user properties

These can be arbitrary values, and contain a colon, so need to be
handled specially as bash considers the colon to be a token separator.
Improve bash completions involving snapshots

This is just a first pass, allowing the possibility to complete snapshot
names without first having to build a list of every snapshot on the
system, and using the facility to flesh out the completion for zfs
clone, rollback, and send.
Allow completions to expand more snapshot names

It's now possible to get snapshot names expanded if the current argument
ends with an @, in places which previously wouldn't expand snapshots for
performance reasons.
Assorted bash completion improvements

Most notably a slightly less cumbersome way of specifying the switches
available for a given command.
bash completion: use builtins where possible

This makes the code more consistent and a little cleaner.

(Plus builtins are faster, not that that's likely to result in any
practical difference.)

There are still some uses of expr in places where it looked like the
most concise readable option.
Add completions for zfs snapshot, upgrade, destroy
Fix typo in bash completion

'zfs get -o recieved ...' should, of course, be 'received'.
Dramatically improve the speed of snapshot listing

Take advantage of the special-case optimisation of "zfs list -o name -s
name -t snapshot" when listing snapshots.

This makes completions offering snapshot names enormously faster when
there are hundreds or thousands of snapshots.
Improve partition detection on lesser used devices

The format strings in efi_get_info() are intended to extract both the
main device and partition number. However, this is only done correctly
for hd, sd and vd devices. The format strings for ram, dm-, md and loop
devices misparse the input. This causes the partition device to be
incorrectly labelled as the main device with the partition being
labelled 0.

Reported-by: ilovezfs <ilovezfs@icloud.com>
Signed-off-by: Richard Yao <ryao@gentoo.org>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #2175
Dynamically create loop devices

Several of the in-tree regression tests depend on the availability
of loop devices.  If for some reason no loop devices are available
the tests will fail.

Normally this isn't an issue because most Linux distributions create
8 loop devices by default.  This is enough for our purposes.  However,
recent Fedora releases have only been creating a single loop device
and this leads to failures.  Alternately, if something else of the
system is using the loop devices we may see failures.

The fix for this is to update the support scripts to dynamically
create loop devices as needed.  The scripts need only create a node
under /dev/ and the loop driver with create the minor.  This behavior
has been supported by the loop driver for ages.

Additionally this patch updates cleanup_loop_devices() to cleanup
loop devices which have already had their file store deleted.  This
helps prevent stale loop devices from accumulating on the system due
to test failures.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Closes #2249
Set errno for mkdirp() called with NULL path ptr

If mkdirp() is called with a NULL ptr for the path arg, it will return
-1 with errno unchanged.  This is unexpected since on error it should
return -1 and set errno to one of the error values listed for mkdir(2).

This commit sets errno = ENOENT for this NULL ptr case.  This is in
accordance with the errors specified by mkdir(2):

  ENOENT
  A component of the path prefix does not exist or is a null pathname.

Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #2248
Replace zed_file_create_dirs() with mkdirp()

When processing directory components starting from the root dir,
zed_file_create_dirs() contained a bug in checking the return value of
mkdir().  A typo was made, and the test for (mkdir_errno != EEXIST) was
erroneously written as (mkdir_errno == EEXIST).  If some of the leading
directory components already existed, this bug would cause the routine
to exit before creating the remaining directory components.

Instead of fixing the above mkdir_errno test, this commit replaces
zed_file_create_dirs() with mkdirp().  This cleanup was already
planned, and zed_file_create_dirs() only existed because I didn't
realize mkdirp() was already in tree at the time.

Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #2248
Merge branch 'upstream' into snapshot/ubuntu/trusty
Move zfs_completion.bash into the DEB overlay.

The bash completion part for ZoL now has an upstream repo:

* https://github.com/Aneurin/zfs-bash

Changes to this component are likely to be infrequent, so directly import
the project instead of using git-submodule.
Merge branch 'zfs-bash' into snapshot/ubuntu/trusty

Conflicts:
	debian/tree/zfsutils/etc/bash_completion.d/zfs
Update changelog for 0.6.2-2.1~trusty~8.gbpf34206 release