rootdev: fix -d, add -c and -r. ifdefs for so use
Multiple changes:
- Ensure the -d flag trims off all of the partition info and only if there is a partition
- Add -c, device node creation for platform-agnostic /dev/ROOT, /dev/ROOT0 and /dev/ROOT1
- Add -r, resolve the first slave if the device is a device-mapper device
- Add -s, create a symlink from the found device to /dev/ACTIVE_ROOT
- Adds short-circuiting if /dev/ACTIVE_ROOT exists to both dm resolution and
normal lookups
- Add support for building a library with a reusable header and interfaces
I'll follow this up with a change to install <rootdev/rootdev.h> and the .so in
the ebuild if the interface is at all interesting. If prefered, I can just add:
rootdev -s -r to chromeos_startup, then AU can just rely on readlink(/dev/ACTIVE_ROOT).
BUG=chromium-os:5988
TEST=built for x86-generic testing with vroot: -r, -s, -c, -d and combos
switching to non-vroot to test now
can someone test arm for me?
Change-Id: Ibab8072afb012ea77d457517f1849e0917d02892
Review URL: http://codereview.chromium.org/3141038
diff --git a/COPYING b/COPYING
deleted file mode 100644
index 623b625..0000000
--- a/COPYING
+++ /dev/null
@@ -1,340 +0,0 @@
- GNU GENERAL PUBLIC LICENSE
- Version 2, June 1991
-
- Copyright (C) 1989, 1991 Free Software Foundation, Inc.
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
-
- Preamble
-
- The licenses for most software are designed to take away your
-freedom to share and change it. By contrast, the GNU General Public
-License is intended to guarantee your freedom to share and change free
-software--to make sure the software is free for all its users. This
-General Public License applies to most of the Free Software
-Foundation's software and to any other program whose authors commit to
-using it. (Some other Free Software Foundation software is covered by
-the GNU Library General Public License instead.) You can apply it to
-your programs, too.
-
- When we speak of free software, we are referring to freedom, not
-price. Our General Public Licenses are designed to make sure that you
-have the freedom to distribute copies of free software (and charge for
-this service if you wish), that you receive source code or can get it
-if you want it, that you can change the software or use pieces of it
-in new free programs; and that you know you can do these things.
-
- To protect your rights, we need to make restrictions that forbid
-anyone to deny you these rights or to ask you to surrender the rights.
-These restrictions translate to certain responsibilities for you if you
-distribute copies of the software, or if you modify it.
-
- For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must give the recipients all the rights that
-you have. You must make sure that they, too, receive or can get the
-source code. And you must show them these terms so they know their
-rights.
-
- We protect your rights with two steps: (1) copyright the software, and
-(2) offer you this license which gives you legal permission to copy,
-distribute and/or modify the software.
-
- Also, for each author's protection and ours, we want to make certain
-that everyone understands that there is no warranty for this free
-software. If the software is modified by someone else and passed on, we
-want its recipients to know that what they have is not the original, so
-that any problems introduced by others will not reflect on the original
-authors' reputations.
-
- Finally, any free program is threatened constantly by software
-patents. We wish to avoid the danger that redistributors of a free
-program will individually obtain patent licenses, in effect making the
-program proprietary. To prevent this, we have made it clear that any
-patent must be licensed for everyone's free use or not licensed at all.
-
- The precise terms and conditions for copying, distribution and
-modification follow.
-
- GNU GENERAL PUBLIC LICENSE
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
-
- 0. This License applies to any program or other work which contains
-a notice placed by the copyright holder saying it may be distributed
-under the terms of this General Public License. The "Program", below,
-refers to any such program or work, and a "work based on the Program"
-means either the Program or any derivative work under copyright law:
-that is to say, a work containing the Program or a portion of it,
-either verbatim or with modifications and/or translated into another
-language. (Hereinafter, translation is included without limitation in
-the term "modification".) Each licensee is addressed as "you".
-
-Activities other than copying, distribution and modification are not
-covered by this License; they are outside its scope. The act of
-running the Program is not restricted, and the output from the Program
-is covered only if its contents constitute a work based on the
-Program (independent of having been made by running the Program).
-Whether that is true depends on what the Program does.
-
- 1. You may copy and distribute verbatim copies of the Program's
-source code as you receive it, in any medium, provided that you
-conspicuously and appropriately publish on each copy an appropriate
-copyright notice and disclaimer of warranty; keep intact all the
-notices that refer to this License and to the absence of any warranty;
-and give any other recipients of the Program a copy of this License
-along with the Program.
-
-You may charge a fee for the physical act of transferring a copy, and
-you may at your option offer warranty protection in exchange for a fee.
-
- 2. You may modify your copy or copies of the Program or any portion
-of it, thus forming a work based on the Program, and copy and
-distribute such modifications or work under the terms of Section 1
-above, provided that you also meet all of these conditions:
-
- a) You must cause the modified files to carry prominent notices
- stating that you changed the files and the date of any change.
-
- b) You must cause any work that you distribute or publish, that in
- whole or in part contains or is derived from the Program or any
- part thereof, to be licensed as a whole at no charge to all third
- parties under the terms of this License.
-
- c) If the modified program normally reads commands interactively
- when run, you must cause it, when started running for such
- interactive use in the most ordinary way, to print or display an
- announcement including an appropriate copyright notice and a
- notice that there is no warranty (or else, saying that you provide
- a warranty) and that users may redistribute the program under
- these conditions, and telling the user how to view a copy of this
- License. (Exception: if the Program itself is interactive but
- does not normally print such an announcement, your work based on
- the Program is not required to print an announcement.)
-
-These requirements apply to the modified work as a whole. If
-identifiable sections of that work are not derived from the Program,
-and can be reasonably considered independent and separate works in
-themselves, then this License, and its terms, do not apply to those
-sections when you distribute them as separate works. But when you
-distribute the same sections as part of a whole which is a work based
-on the Program, the distribution of the whole must be on the terms of
-this License, whose permissions for other licensees extend to the
-entire whole, and thus to each and every part regardless of who wrote it.
-
-Thus, it is not the intent of this section to claim rights or contest
-your rights to work written entirely by you; rather, the intent is to
-exercise the right to control the distribution of derivative or
-collective works based on the Program.
-
-In addition, mere aggregation of another work not based on the Program
-with the Program (or with a work based on the Program) on a volume of
-a storage or distribution medium does not bring the other work under
-the scope of this License.
-
- 3. You may copy and distribute the Program (or a work based on it,
-under Section 2) in object code or executable form under the terms of
-Sections 1 and 2 above provided that you also do one of the following:
-
- a) Accompany it with the complete corresponding machine-readable
- source code, which must be distributed under the terms of Sections
- 1 and 2 above on a medium customarily used for software interchange; or,
-
- b) Accompany it with a written offer, valid for at least three
- years, to give any third party, for a charge no more than your
- cost of physically performing source distribution, a complete
- machine-readable copy of the corresponding source code, to be
- distributed under the terms of Sections 1 and 2 above on a medium
- customarily used for software interchange; or,
-
- c) Accompany it with the information you received as to the offer
- to distribute corresponding source code. (This alternative is
- allowed only for noncommercial distribution and only if you
- received the program in object code or executable form with such
- an offer, in accord with Subsection b above.)
-
-The source code for a work means the preferred form of the work for
-making modifications to it. For an executable work, complete source
-code means all the source code for all modules it contains, plus any
-associated interface definition files, plus the scripts used to
-control compilation and installation of the executable. However, as a
-special exception, the source code distributed need not include
-anything that is normally distributed (in either source or binary
-form) with the major components (compiler, kernel, and so on) of the
-operating system on which the executable runs, unless that component
-itself accompanies the executable.
-
-If distribution of executable or object code is made by offering
-access to copy from a designated place, then offering equivalent
-access to copy the source code from the same place counts as
-distribution of the source code, even though third parties are not
-compelled to copy the source along with the object code.
-
- 4. You may not copy, modify, sublicense, or distribute the Program
-except as expressly provided under this License. Any attempt
-otherwise to copy, modify, sublicense or distribute the Program is
-void, and will automatically terminate your rights under this License.
-However, parties who have received copies, or rights, from you under
-this License will not have their licenses terminated so long as such
-parties remain in full compliance.
-
- 5. You are not required to accept this License, since you have not
-signed it. However, nothing else grants you permission to modify or
-distribute the Program or its derivative works. These actions are
-prohibited by law if you do not accept this License. Therefore, by
-modifying or distributing the Program (or any work based on the
-Program), you indicate your acceptance of this License to do so, and
-all its terms and conditions for copying, distributing or modifying
-the Program or works based on it.
-
- 6. Each time you redistribute the Program (or any work based on the
-Program), the recipient automatically receives a license from the
-original licensor to copy, distribute or modify the Program subject to
-these terms and conditions. You may not impose any further
-restrictions on the recipients' exercise of the rights granted herein.
-You are not responsible for enforcing compliance by third parties to
-this License.
-
- 7. If, as a consequence of a court judgment or allegation of patent
-infringement or for any other reason (not limited to patent issues),
-conditions are imposed on you (whether by court order, agreement or
-otherwise) that contradict the conditions of this License, they do not
-excuse you from the conditions of this License. If you cannot
-distribute so as to satisfy simultaneously your obligations under this
-License and any other pertinent obligations, then as a consequence you
-may not distribute the Program at all. For example, if a patent
-license would not permit royalty-free redistribution of the Program by
-all those who receive copies directly or indirectly through you, then
-the only way you could satisfy both it and this License would be to
-refrain entirely from distribution of the Program.
-
-If any portion of this section is held invalid or unenforceable under
-any particular circumstance, the balance of the section is intended to
-apply and the section as a whole is intended to apply in other
-circumstances.
-
-It is not the purpose of this section to induce you to infringe any
-patents or other property right claims or to contest validity of any
-such claims; this section has the sole purpose of protecting the
-integrity of the free software distribution system, which is
-implemented by public license practices. Many people have made
-generous contributions to the wide range of software distributed
-through that system in reliance on consistent application of that
-system; it is up to the author/donor to decide if he or she is willing
-to distribute software through any other system and a licensee cannot
-impose that choice.
-
-This section is intended to make thoroughly clear what is believed to
-be a consequence of the rest of this License.
-
- 8. If the distribution and/or use of the Program is restricted in
-certain countries either by patents or by copyrighted interfaces, the
-original copyright holder who places the Program under this License
-may add an explicit geographical distribution limitation excluding
-those countries, so that distribution is permitted only in or among
-countries not thus excluded. In such case, this License incorporates
-the limitation as if written in the body of this License.
-
- 9. The Free Software Foundation may publish revised and/or new versions
-of the General Public License from time to time. Such new versions will
-be similar in spirit to the present version, but may differ in detail to
-address new problems or concerns.
-
-Each version is given a distinguishing version number. If the Program
-specifies a version number of this License which applies to it and "any
-later version", you have the option of following the terms and conditions
-either of that version or of any later version published by the Free
-Software Foundation. If the Program does not specify a version number of
-this License, you may choose any version ever published by the Free Software
-Foundation.
-
- 10. If you wish to incorporate parts of the Program into other free
-programs whose distribution conditions are different, write to the author
-to ask for permission. For software which is copyrighted by the Free
-Software Foundation, write to the Free Software Foundation; we sometimes
-make exceptions for this. Our decision will be guided by the two goals
-of preserving the free status of all derivatives of our free software and
-of promoting the sharing and reuse of software generally.
-
- NO WARRANTY
-
- 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
-FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
-OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
-PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
-OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
-TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
-PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
-REPAIR OR CORRECTION.
-
- 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
-WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
-REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
-INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
-OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
-TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
-YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
-PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGES.
-
- END OF TERMS AND CONDITIONS
-
- How to Apply These Terms to Your New Programs
-
- If you develop a new program, and you want it to be of the greatest
-possible use to the public, the best way to achieve this is to make it
-free software which everyone can redistribute and change under these terms.
-
- To do so, attach the following notices to the program. It is safest
-to attach them to the start of each source file to most effectively
-convey the exclusion of warranty; and each file should have at least
-the "copyright" line and a pointer to where the full notice is found.
-
- <one line to give the program's name and a brief idea of what it does.>
- Copyright (C) <year> <name of author>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
-
-Also add information on how to contact you by electronic and paper mail.
-
-If the program is interactive, make it output a short notice like this
-when it starts in an interactive mode:
-
- Gnomovision version 69, Copyright (C) year name of author
- Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
- This is free software, and you are welcome to redistribute it
- under certain conditions; type `show c' for details.
-
-The hypothetical commands `show w' and `show c' should show the appropriate
-parts of the General Public License. Of course, the commands you use may
-be called something other than `show w' and `show c'; they could even be
-mouse-clicks or menu items--whatever suits your program.
-
-You should also get your employer (if you work as a programmer) or your
-school, if any, to sign a "copyright disclaimer" for the program, if
-necessary. Here is a sample; alter the names:
-
- Yoyodyne, Inc., hereby disclaims all copyright interest in the program
- `Gnomovision' (which makes passes at compilers) written by James Hacker.
-
- <signature of Ty Coon>, 1 April 1989
- Ty Coon, President of Vice
-
-This General Public License does not permit incorporating your program into
-proprietary programs. If your program is a subroutine library, you may
-consider it more useful to permit linking proprietary applications with the
-library. If this is what you want to do, use the GNU Library General
-Public License instead of this License.
diff --git a/LICENCE b/LICENCE
deleted file mode 120000
index d24842f..0000000
--- a/LICENCE
+++ /dev/null
@@ -1 +0,0 @@
-COPYING
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d251496
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,27 @@
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/Makefile b/Makefile
index 873a392..75d0702 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,19 @@
+# Copyright (C) 2010 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
-rootdev : rootdev.c
+all: rootdev librootdev.so.1.0
-.PHONY: clean clobber
-clean clobber:
- rm -f rootdev
+rootdev: rootdev.c main.c
+ $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ -O2 -Wall
+
+librootdev.so.1.0: rootdev.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -shared -fPIC \
+ -Wl,-soname,librootdev.so.1 $< -o $@
+ ln -s $@ librootdev.so.1
+ ln -s $@ librootdev.so
+
+clean:
+ rm -f rootdev librootdev.so*
+
+.PHONY: clean
diff --git a/README.chromium b/README.chromium
index 5ed10c8..6e4e903 100644
--- a/README.chromium
+++ b/README.chromium
@@ -1,16 +1,15 @@
-URL: git://git.debian.org/~lamont/util-linux.git
-Version: commit 717db2c8177203fe242ea35b31bc312abe9aa3c9
-License: GPL v2
-License File: files/COPYING
+Chromium OS version of rootdev
-Description: Prints the current rootfs device.
+Inspired by git://git.debian.org/~lamont/util-linux.git / 717db2c8177203fe242ea35b31bc312abe9aa3c9
-Local Modification:
+Description: Performs operations to discover and annotate the root block device
-- Removed every bit of upstream code except the part that determines the
- rootfs device.
-- Added better error messages.
+Prior to this commit, rootdev was derived from the above link. However, it has
+been rewritten in its entirety and licensed as per LICENSE.
-
-
-
+chromeos-rootdev:
+- Provides core functionality in a library: librootdev
+- Walks sysfs to discover the block devices
+- Supports resolving through to /sys/block/XXX/slaves/*/dev devices
+- Will test and, optionally, symlink to the /dev entry for standard devices.
+- Is testable.
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..61d20ca
--- /dev/null
+++ b/main.c
@@ -0,0 +1,157 @@
+/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Driver for using rootdev.c from the commandline
+ */
+#include <err.h>
+#include <errno.h>
+#include <getopt.h>
+#include <linux/limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "rootdev.h"
+
+static void print_help(const char *progname) {
+ fprintf(stderr,
+ "%s [OPTIONS] [PATH]\n"
+ "Outputs the containing device for the specified PATH.\n"
+ "With no arguments, '/' is assumed.\n"
+ "\n"
+ "Options:\n"
+ " -h\tthis message.\n"
+ "\n"
+ " -c\tcreate the /dev node if it cannot be found\n"
+ " -d\treturn the block device only if possible\n"
+ " -i\treturn path even if the node doesn't exist\n"
+ " -s\tif possible, return the first slave of the root device\n"
+ "\n"
+ " --block [path]\tset the path to block under the sys mount point\n"
+ " --dev [path]\tset the path to dev mount point\n"
+ " --major [num]\tset the major number of the rootdev\n"
+ " --minor [num]\tset the minor number of the rootdev\n",
+ progname);
+}
+
+static int flag_help = 0;
+static int flag_use_slave = 0;
+static int flag_strip_partition = 0;
+static int flag_ignore = 0;
+static int flag_create = 0;
+static int flag_major = 0;
+static int flag_minor = 0;
+static const char *flag_path = "/";
+static char *flag_block_path = "/sys/block";
+static char *flag_dev_path = "/dev";
+
+static void parse_args(int argc, char **argv) {
+ while (1) {
+ int c;
+ int option_index = 0;
+ static const struct option long_options[] = {
+ {"c", no_argument, &flag_create, 1},
+ {"d", no_argument, &flag_strip_partition, 1},
+ {"h", no_argument, &flag_help, 1},
+ {"i", no_argument, &flag_ignore, 1},
+ {"s", no_argument, &flag_use_slave, 1},
+ /* Long arguments for testing. */
+ {"block", required_argument, NULL, 'b'},
+ {"dev", required_argument, NULL, 'd'},
+ {"major", required_argument, NULL, 'M'},
+ {"minor", required_argument, NULL, 'm'},
+ {0, 0, 0, 0}
+ };
+ c = getopt_long_only(argc, argv, "", long_options, &option_index);
+
+ if (c == -1)
+ break;
+
+ if (c == '?') {
+ flag_help = 1;
+ break;
+ }
+
+ switch (c) {
+ case 'b':
+ flag_block_path = optarg;
+ break;
+ case 'd':
+ flag_dev_path = optarg;
+ break;
+ case 'M':
+ flag_major = atoi(optarg);
+ break;
+ case 'm':
+ flag_minor = atoi(optarg);
+ break;
+ }
+
+ }
+
+ if (flag_create && flag_strip_partition) {
+ flag_help = 1;
+ warnx("-c and -d are incompatible at present.");
+ return;
+ }
+
+ if (optind < argc) {
+ flag_path = argv[optind++];
+ }
+
+ if (optind < argc) {
+ fprintf(stderr, "Too many free arguments: %d\n", argc - optind);
+ flag_help = 1;
+ }
+}
+
+int main(int argc, char **argv) {
+ struct stat path_stat;
+ char path[PATH_MAX];
+ int ret;
+ dev_t root_dev;
+ parse_args(argc, argv);
+
+ if (flag_help) {
+ print_help(argv[0]);
+ return 1;
+ }
+
+ if (flag_major || flag_minor) {
+ root_dev = makedev(flag_major, flag_minor);
+ } else {
+ /* Yields the containing dev_t in st_dev. */
+ if (stat(flag_path, &path_stat) != 0)
+ err(1, "Cannot stat(%s)", flag_path);
+ root_dev = path_stat.st_dev;
+ }
+
+ ret = rootdev_wrapper(path, sizeof(path),
+ flag_use_slave,
+ flag_strip_partition,
+ &root_dev,
+ flag_block_path,
+ flag_dev_path);
+
+ if (ret == 1 && flag_create) {
+ /* TODO(wad) add flag_force to allow replacement */
+ ret = 0;
+ if (mknod(path, S_IFBLK | S_IRUSR | S_IWUSR, root_dev) && errno != EEXIST) {
+ warn("failed to create %s", path);
+ ret = 1;
+ }
+ }
+
+ if (flag_ignore && ret > 0)
+ ret = 0;
+
+ if (path[0] != '\0')
+ printf("%s\n", path);
+
+ return ret;
+}
diff --git a/rootdev.c b/rootdev.c
index c3dc175..867974f 100644
--- a/rootdev.c
+++ b/rootdev.c
@@ -1,100 +1,389 @@
-/* Taken from util-linux source. GPLv2.
- * Emits the current rootfs device.
- * Works by searching /dev recursively for a BLK device with the same device
- * number as '/'.
+/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Implements root device discovery via sysfs with optional bells and whistles.
*/
-#include <stdio.h>
-#include <err.h>
-#include <sys/types.h>
+#include "rootdev.h"
+
+#include <ctype.h>
#include <dirent.h>
-#include <sys/stat.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+static const char *kDefaultSearchPath = "/sys/block";
+static const char *kDefaultDevPath = "/dev";
-static int
-find_dev_recursive(char *dirnamebuf, int number, int deviceOnly) {
- DIR *dp;
- struct dirent *dir;
- struct stat s;
- int dirnamelen = 0;
+/* Encode the root device structuring here for Chromium OS */
+static const char kActiveRoot[] = "/dev/ACTIVE_ROOT";
+static const char kRootDev[] = "/dev/ROOT";
+static const char kRootA[] = "/dev/ROOT0";
+static const char kRootB[] = "/dev/ROOT1";
- if ((dp = opendir(dirnamebuf)) == NULL)
- err(1, "can't read directory %s", dirnamebuf);
- dirnamelen = strlen(dirnamebuf);
- while ((dir = readdir(dp)) != NULL) {
- if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
- continue;
- if (dirnamelen + 1 + strlen(dir->d_name) > PATH_MAX)
- continue;
- dirnamebuf[dirnamelen] = '/';
- strcpy(dirnamebuf+dirnamelen+1, dir->d_name);
- if (lstat(dirnamebuf, &s) < 0)
- continue;
- if ((s.st_mode & S_IFMT) == S_IFBLK && s.st_rdev == number){
- if (deviceOnly) {
- int len = strlen(dirnamebuf);
- char c = 0;
- do {
- c = dirnamebuf[len-1];
- --len;
- }while(c > 0 && c < 9 && len > 0);
- /* arm has "p" for partition */
- if (dirnamebuf[len-1] == 'p')
- --len;
- dirnamebuf[len]='\0';
- }
- return 1;
- }
- if ((s.st_mode & S_IFMT) == S_IFDIR &&
- find_dev_recursive(dirnamebuf, number, deviceOnly))
- return 1;
- }
- dirnamebuf[dirnamelen] = 0;
- closedir(dp);
- return 0;
+struct part_config {
+ const char *name;
+ int offset;
+};
+
+#define CHROMEOS_PRIMARY_PARTITION 3
+static const struct part_config kPrimaryPart[] = { { kRootA, 0 },
+ { kRootDev, -3 },
+ { kRootB, 2 } };
+#define CHROMEOS_SECONDARY_PARTITION 5
+static const struct part_config kSecondaryPart[] = { { kRootB, 0 },
+ { kRootDev, -5 },
+ { kRootA, -2 } };
+
+/* The number of entries in a part_config so we could add RootC easily. */
+static const int kPartitionEntries = 3;
+
+/* Converts a file of %u:%u -> dev_t. */
+static dev_t devt_from_file(const char *file) {
+ char candidate[10]; /* TODO(wad) system-provided constant? */
+ ssize_t bytes = 0;
+ unsigned int major = 0;
+ unsigned int minor = 0;
+ dev_t dev = 0;
+ int fd = -1;
+
+ /* Never hang. Either get the data or return 0. */
+ fd = open(file, O_NONBLOCK | O_RDONLY);
+ if (fd < 0)
+ return 0;
+ bytes = read(fd, candidate, sizeof(candidate));
+ close(fd);
+
+ /* 0:0 should be considered the minimum size. */
+ if (bytes < 3)
+ return 0;
+ candidate[bytes] = 0;
+ if (sscanf(candidate, "%u:%u", &major, &minor) == 2) {
+ /* candidate's size artificially limits the size of the converted
+ * %u to safely convert to a signed int. */
+ dev = makedev(major, minor);
+ }
+ return dev;
}
-void usage(){
- printf ("rootdev \n\t-d (for device only)\n");
+/* Walks sysfs and will recurse into any directory/link that represents
+ * a block device to find sub-devices (partitions).
+ * If dev == 0, the first device in the directory will be returned. */
+static int match_sysfs_device(char *name, size_t name_len,
+ const char *basedir, dev_t *dev) {
+ int found = -1;
+ size_t basedir_len;
+ DIR *dirp = NULL;
+ struct dirent *entry = NULL;
+ struct dirent *next = NULL;
+ char *working_path = NULL;
+ long working_path_size = 0;
+
+ if (!name || !name_len || !basedir || !dev) {
+ warnx("match_sysfs_device: invalid arguments supplied");
+ return -1;
+ }
+ basedir_len = strlen(basedir);
+ if (!basedir_len) {
+ warnx("match_sysfs_device: basedir must not be empty");
+ return -1;
+ }
+
+ errno = 0;
+ dirp = opendir(basedir);
+ if (!dirp) {
+ /* Don't complain if the directory doesn't exist. */
+ if (errno != ENOENT)
+ warn("match_sysfs_device:opendir(%s)", basedir);
+ return found;
+ }
+
+ /* Grab a platform appropriate path to work with.
+ * Ideally, this won't vary under sys/block. */
+ working_path_size = pathconf(basedir, _PC_NAME_MAX) + 1;
+ /* Fallback to PATH_MAX on any pathconf error. */
+ if (working_path_size < 0)
+ working_path_size = PATH_MAX;
+
+ working_path = malloc(working_path_size);
+ if (!working_path) {
+ warn("malloc(dirent)");
+ closedir(dirp);
+ return found;
+ }
+
+ /* Allocate a properly sized entry. */
+ entry = malloc(offsetof(struct dirent, d_name) + working_path_size);
+ if (!entry) {
+ warn("malloc(dirent)");
+ free(working_path);
+ closedir(dirp);
+ return found;
+ }
+
+ while (readdir_r(dirp, entry, &next) == 0 && next) {
+ size_t candidate_len = strlen(entry->d_name);
+ size_t path_len = 0;
+ dev_t found_devt = 0;
+ /* Ignore the usual */
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+ continue;
+ /* TODO(wad) determine how to best bubble up this case. */
+ if (candidate_len > name_len)
+ continue;
+ /* Only traverse directories or symlinks (to directories ideally) */
+ switch (entry->d_type) {
+ case DT_UNKNOWN:
+ case DT_DIR:
+ case DT_LNK:
+ break;
+ default:
+ continue;
+ }
+ /* Determine path to block device number */
+ path_len = snprintf(working_path, working_path_size, "%s/%s/dev",
+ basedir, entry->d_name);
+ /* Ignore if truncation occurs. */
+ if (path_len != candidate_len + basedir_len + 5)
+ continue;
+
+ found_devt = devt_from_file(working_path);
+ /* *dev == 0 is a wildcard. */
+ if (!*dev || found_devt == *dev) {
+ snprintf(name, name_len, "%s", entry->d_name);
+ *dev = found_devt;
+ found = 1;
+ break;
+ }
+
+ /* Recurse one level for devices that may have a matching partition. */
+ if (major(found_devt) == major(*dev) && minor(*dev) > minor(found_devt)) {
+ sprintf(working_path, "%s/%s", basedir, entry->d_name);
+ found = match_sysfs_device(name, name_len, working_path, dev);
+ if (found > 0)
+ break;
+ }
+ }
+
+ free(working_path);
+ free(entry);
+ closedir(dirp);
+ return found;
}
-int main(int argc, char *argv[]) {
- struct stat s;
- char *file = "/";
- static char name[PATH_MAX+1];
- int deviceOnly=0;
- int c;
- extern char *optarg;
- extern int optind, optopt;
- while ((c = getopt(argc, argv, "hd")) != -1) {
- switch(c) {
- case 'd':
- deviceOnly=1;
- break;
- case 'h':
- default:
- usage();
- return 1;
- }
- }
- if (argc - optind >= 1)
- file = argv[optind];
+const char *rootdev_get_partition(const char *dst, size_t len) {
+ const char *end = dst + strnlen(dst, len);
+ const char *part = end - 1;
+ if (!len)
+ return NULL;
- if (stat(file, &s) < 0)
- err(1, "unable to stat %s", file);
+ if (!isdigit(*part--))
+ return NULL;
- if (!s.st_dev)
- err(1, "unknown device number 0");
+ while (part > dst && isdigit(*part)) part--;
+ part++;
- strcpy(name, "/dev");
+ if (part >= end)
+ return NULL;
- if (!find_dev_recursive(name, s.st_dev, deviceOnly)) {
- fprintf(stderr, "unable to find match\n");
- return 1;
- }
+ return part;
+}
- printf("%s\n", name);
+void rootdev_strip_partition(char *dst, size_t len) {
+ char *part = (char *)rootdev_get_partition(dst, len);
+ if (!part)
+ return;
+ /* For devices that end with a digit, the kernel uses a 'p'
+ * as a separator. E.g., mmcblk1p2. */
+ if (*(part - 1) == 'p')
+ part--;
+ *part = '\0';
+}
- return 0;
+int rootdev_symlink_active(const char *path) {
+ int ret = 0;
+ /* Don't overwrite an existing link. */
+ errno = 0;
+ if ((symlink(path, kActiveRoot)) && errno != EEXIST) {
+ warn("failed to symlink %s -> %s", kActiveRoot, path);
+ ret = -1;
+ }
+ return ret;
+}
+
+int rootdev_get_device(char *dst, size_t size, dev_t dev,
+ const char *search) {
+ struct stat active_root_statbuf;
+
+ if (search == NULL)
+ search = kDefaultSearchPath;
+
+ /* Check if the -s symlink exists. */
+ if ((stat(kActiveRoot, &active_root_statbuf) == 0) &&
+ active_root_statbuf.st_rdev == dev) {
+ /* Note, if the link is not fully qualified, this won't be
+ * either. */
+ ssize_t len = readlink(kActiveRoot, dst, PATH_MAX);
+ if (len > 0) {
+ dst[len] = 0;
+ return 0;
+ }
+ /* If readlink fails or is empty, fall through */
+ }
+
+ snprintf(dst, size, "%s", search);
+ if (match_sysfs_device(dst, size, dst, &dev) <= 0) {
+ fprintf (stderr, "unable to find match\n");
+ return 1;
+ }
+
+ return 0;
+}
+
+int rootdev_get_device_slave(char *slave, size_t size, dev_t *dev,
+ const char *device, const char *search) {
+ char dst[PATH_MAX];
+ int len = 0;
+
+ if (search == NULL)
+ search = kDefaultSearchPath;
+
+ /* So far, I've only seen top-level block devices with slaves. */
+ len = snprintf(dst, sizeof(dst), "%s/%s/slaves", search, device);
+ if (len < 0 || len != strlen(device) + strlen(search) + 8) {
+ warnx("rootdev_get_device_slave: device name too long");
+ return -1;
+ }
+ *dev = 0;
+ if (match_sysfs_device(slave, size, dst, dev) <= 0)
+ return -1;
+
+ return 0;
+}
+
+int rootdev_create_devices(const char *name, dev_t dev, bool symlink) {
+ int ret = 0;
+ unsigned int major = major(dev);
+ unsigned int minor = minor(dev);
+ int i;
+ const struct part_config *config;
+ const char *part_s = rootdev_get_partition(name, strlen(name));
+
+ if (part_s == NULL) {
+ warnx("create_devices: unable to determine partition");
+ return -1;
+ }
+
+ switch (atoi(part_s)) {
+ case CHROMEOS_PRIMARY_PARTITION:
+ config = kPrimaryPart;
+ break;
+ case CHROMEOS_SECONDARY_PARTITION:
+ config = kSecondaryPart;
+ break;
+ default:
+ warnx("create_devices: unable to determine partition: %s",
+ part_s);
+ return -1;
+ }
+
+ for (i = 0; i < kPartitionEntries; ++i) {
+ dev = makedev(major, minor + config[i].offset);
+ errno = 0;
+ if (mknod(config[i].name,
+ S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
+ dev) && errno != EEXIST) {
+ warn("failed to create %s", config[i].name);
+ return -1;
+ }
+ }
+
+ if (symlink)
+ ret = rootdev_symlink_active(config[0].name);
+ return ret;
+}
+
+int rootdev_get_path(char *path, size_t size, const char *device,
+ dev_t dev, const char *dev_path) {
+ int path_len;
+ struct stat dev_statbuf;
+
+ if (!dev_path)
+ dev_path = kDefaultDevPath;
+
+ if (!path || !size || !device)
+ return -1;
+
+ path_len = snprintf(path, size, "%s/%s", dev_path, device);
+ if (path_len != strlen(dev_path) + 1 + strlen(device))
+ return -1;
+
+ if (stat(path, &dev_statbuf) != 0)
+ return 1;
+
+ if (dev && dev != dev_statbuf.st_rdev)
+ return 2;
+
+ return 0;
+}
+
+int rootdev_wrapper(char *path, size_t size,
+ bool full, bool strip,
+ dev_t *dev,
+ const char *search, const char *dev_path) {
+ int res = 0;
+ char devname[PATH_MAX];
+ if (!search)
+ search = kDefaultSearchPath;
+ if (!dev_path)
+ dev_path = kDefaultDevPath;
+ if (!dev)
+ return -1;
+
+ res = rootdev_get_device(devname, sizeof(devname), *dev, search);
+ if (res != 0)
+ return res;
+
+ if (full)
+ res = rootdev_get_device_slave(devname, sizeof(devname), dev, devname,
+ search);
+
+ /* TODO(wad) we should really just track the block dev, partition number, and
+ * dev path. When we rewrite this, we can track all the sysfs info
+ * in the class. */
+ if (strip) {
+ /* When we strip the partition, we don't want get_path to return non-zero
+ * because of dev mismatch. Passing in 0 tells it to not test. */
+ *dev = 0;
+ rootdev_strip_partition(devname, size);
+ }
+
+ res = rootdev_get_path(path, size, devname, *dev, dev_path);
+
+ return res;
+}
+
+int rootdev(char *path, size_t size, bool full, bool strip) {
+ struct stat root_statbuf;
+
+ /* Yields the containing dev_t in st_dev. */
+ if (stat("/", &root_statbuf) != 0)
+ return -1;
+
+ return rootdev_wrapper(path,
+ size,
+ full,
+ strip,
+ &root_statbuf.st_dev,
+ NULL, /* default /sys dir */
+ NULL); /* default /dev dir */
}
diff --git a/rootdev.h b/rootdev.h
new file mode 100644
index 0000000..03b8bd8
--- /dev/null
+++ b/rootdev.h
@@ -0,0 +1,104 @@
+/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Interface for root device discovery via sysfs with optional
+ * bells and whistles.
+ */
+#ifndef ROOTDEV_ROOTDEV_H_
+#define ROOTDEV_ROOTDEV_H_
+
+#include <stdbool.h>
+#include <sys/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * rootdev: returns the path to the root device in @path
+ * @path: pre-allocated char array the result will be written to
+ * @size: size of @path
+ * @full: whether to try to do full resolution. E.g., device-mapper
+ * @strip: whether to remove the partition # or not.
+ *
+ * Returns 0 on success, non-zero on error.
+ */
+int rootdev(char *path, size_t size, bool full, bool strip);
+
+/* All interface below this point will most definitely be C specific. If
+ * we rewrite this as a C++ class, only the above generic interface should
+ * still be provided.
+ */
+
+/**
+ * rootdev_wrapper: rootdev equivalent with paths can be substituted.
+ */
+int rootdev_wrapper(char *path, size_t size,
+ bool full, bool strip,
+ dev_t *dev,
+ const char *search, const char *dev_path);
+/**
+ * rootdev_get_device: finds the /dev path for @dev
+ * @dst: destination char array
+ * @size: size of @dst
+ * @dev: dev_t specifying the known root device
+ * @search: path to search under. NULL for default.
+ *
+ * Returns 0 on success, non-zero on error.
+ *
+ * The name of the devices is placed in @dst. It will not
+ * be qualified with /dev/ by default.
+ */
+int rootdev_get_device(char *dst, size_t size, dev_t dev,
+ const char *search);
+
+/**
+ * rootdev_get_device_slave: returns the first device under @device/slaves
+ * @slave: destination char array for storing the result
+ * @size: size of @slave
+ * @dev: pointer to a dev_t to populate
+ * @device: name of the device to probe, like "sdb"
+ * @search: path to search under. NULL for default.
+ *
+ * Returns 0 on success, non-zero on failure.
+ * It is safe for @device == @slave.
+ */
+int rootdev_get_device_slave(char *slave, size_t size, dev_t *dev,
+ const char *device, const char *search);
+
+/**
+ * rootdev_get_path: converts a device name to a path in the device tree
+ * @path: char array to store the path
+ * @size: size of @devpath
+ * @device: name of the device
+ * @dev: optional expected dev_t of the node.
+ * @dev_path: path to dev tree. NULL for default (/dev)
+ *
+ * A @dev of 0 is ignored.
+ *
+ * @path is populated for all return codes.
+ * Returns 0 on success and non-zero on error:
+ * -1 on unexpected errors (@path may be invalid)
+ * 1 on no existing @path
+ * 2 @path exists but the dev_t value is mismatched.
+ *
+ * Nb, this function does NOT search /dev for a match. It performs a normal
+ * string concatenation and probes for the existence. If udev has moved,
+ * or otherwise renamed, the device, a positive value is returned.
+ * The caller may then use the dev_t and @path to create the node with
+ * mknod(2).
+ */
+int rootdev_get_path(char *path, size_t size, const char *device, dev_t dev,
+ const char *dev_path);
+
+const char *rootdev_get_partition(const char *dst, size_t len);
+void rootdev_strip_partition(char *dst, size_t len);
+int rootdev_symlink_active(const char *path);
+int rootdev_create_devices(const char *name, dev_t dev, bool symlink);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* ROOTDEV_ROOTDEV_H_ */
diff --git a/rootdev_test.sh b/rootdev_test.sh
new file mode 100755
index 0000000..8c188c3
--- /dev/null
+++ b/rootdev_test.sh
@@ -0,0 +1,291 @@
+#!/bin/sh
+# Copyright (C) 2010 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+# Simple functional test harness for rootdev
+# TODO(wad) rootdev should be rewritten in C++ and gmocked.
+
+set -u
+
+warn () {
+ echo "WARN: $@" 1>&2
+}
+
+error () {
+ echo "ERROR: $@" 1>&2
+ exit 1
+}
+
+PASS_COUNT=0
+pass () {
+ echo "PASS:$1" 1>&2
+ PASS_COUNT=$((PASS_COUNT + 1))
+ return 0
+}
+
+FAIL_COUNT=0
+fail () {
+ echo "FAIL:$1" 1>&2
+ FAIL_COUNT=$((FAIL_COUNT + 1))
+ return 0
+}
+
+WORKDIR=
+cleanup () {
+ if [ -n "$WORKDIR" ]; then
+ rm -rf "$WORKDIR"
+ fi
+ trap - EXIT
+}
+
+setup () {
+ WORKDIR=$(mktemp -d rootdev_test.XXXXXXX)
+ if [ ! -d "$WORKDIR" ]; then
+ error "Failed to create temporary work dir"
+ fi
+ trap cleanup EXIT
+}
+
+run_test () {
+ setup
+ echo "RUN:$1" 1>&2
+ eval $1
+ ret=$?
+ cleanup
+ if [ $ret -eq 0 ]; then
+ pass $1
+ else
+ fail $1
+ fi
+}
+
+expect () {
+ cond="$1"
+ eval test $1
+ if [ $? -ne 0 ]; then
+ warn "expect: $1"
+ return 1
+ fi
+ return 0
+}
+
+if [ ! -e ./rootdev ]; then
+ error "rootdev must be in the cwd"
+fi
+
+if [ "$USER" != "root" ]; then
+ error "Must be run as root to use mknod ($USER)"
+fi
+
+t00_bad_sys_dir () {
+ out=$(./rootdev --block $WORKDIR 2>/dev/null)
+ expect "$? -ne 0" || return 1
+ expect "-z '$out'" || return 1
+}
+run_test t00_bad_sys_dir
+
+h00_setup_sda_tree() {
+ local block=$1
+ local dev=$2
+ mkdir -p $block
+ mkdir -p $dev
+ mkdir -p $block/sda/sda1
+ mkdir -p $block/sda/sda2
+ echo "10:0" > $block/sda/dev
+ echo "10:1" > $block/sda/sda1/dev
+ echo "10:2" > $block/sda/sda2/dev
+ mknod $dev/sda1 b 10 1
+ mknod $dev/sda2 b 10 2
+ mknod $dev/sda b 10 0
+}
+
+t01_sys_dev_match () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h00_setup_sda_tree $block $dev
+
+ out=$(./rootdev --dev $dev --block $block --major 10 --minor 1 2>/dev/null)
+ expect "$? -eq 0" || return 1
+ expect "'$dev/sda1' = '$out'" || return 1
+}
+run_test t01_sys_dev_match
+
+t02_sys_dev_match_block () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h00_setup_sda_tree $block $dev
+
+ out=$(./rootdev --dev $dev --block $block --major 10 --minor 0 2>/dev/null)
+ expect "$? -eq 0" || return 1
+ expect "'$dev/sda' = '$out'" || return 1
+}
+run_test t02_sys_dev_match_block
+
+t03_sys_dev_match_block_no_dev () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h00_setup_sda_tree $block $dev
+ rm $dev/sda
+
+ out=$(./rootdev --dev $dev --block $block --major 10 --minor 0 2>/dev/null)
+ expect "$? -eq 1" || return 1
+ expect "'$dev/sda' = '$out'" || return 1
+}
+run_test t03_sys_dev_match_block_no_dev
+
+t04_sys_dev_match_block_no_dev_ignore () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h00_setup_sda_tree $block $dev
+ rm $dev/sda
+
+ out=$(./rootdev -i --dev $dev --block $block --major 10 --minor 0 2>/dev/null)
+ expect "$? -eq 0" || return 1
+ expect "'$dev/sda' = '$out'" || return 1
+}
+run_test t04_sys_dev_match_block_no_dev_ignore
+
+
+h01_setup_dm_tree() {
+ local block=$1
+ local dev=$2
+ mkdir -p $block
+ mkdir -p $dev
+ mkdir -p $block/dm-0
+ mkdir -p $block/dm-0/slaves/sda1
+ echo "254:0" > $block/dm-0/dev
+ echo "10:1" > $block/dm-0/slaves/sda1/dev
+ mknod $dev/dm-0 b 254 0
+}
+
+t05_match_dm () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h00_setup_sda_tree $block $dev
+ h01_setup_dm_tree $block $dev
+
+ out=$(./rootdev --dev $dev --block $block --major 254 --minor 0 \
+ 2>/dev/null)
+ expect "$? -eq 0" || return 1
+ expect "'$dev/dm-0' = '$out'" || return 1
+}
+run_test t05_match_dm
+
+t06_match_dm_slave () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h00_setup_sda_tree $block $dev
+ h01_setup_dm_tree $block $dev
+
+ out=$(./rootdev -s --dev $dev --block $block --major 254 --minor 0 \
+ 2>/dev/null)
+ expect "$? -eq 0" || return 1
+ expect "'$dev/sda1' = '$out'" || return 1
+}
+run_test t06_match_dm_slave
+
+t07_safe_fail_on_no_slave () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h00_setup_sda_tree $block $dev
+ h01_setup_dm_tree $block $dev
+
+ out=$(./rootdev -s --dev $dev --block $block --major 10 --minor 1 \
+ 2>/dev/null)
+ expect "$? -eq 0" || return 1
+ expect "'$dev/sda1' = '$out'" || return 1
+}
+run_test t07_safe_fail_on_no_slave
+
+t08_safe_fail_on_no_slave_dev () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h00_setup_sda_tree $block $dev
+ h01_setup_dm_tree $block $dev
+ # If the matching dev node is missing, an error code will be returned
+ # but the path will still represent the slave.
+ rm $dev/sda1
+
+ out=$(./rootdev -s --dev $dev --block $block --major 254 --minor 0 \
+ 2>/dev/null)
+ expect "$? -eq 1" || return 1
+ expect "'$dev/sda1' = '$out'" || return 1
+}
+run_test t08_safe_fail_on_no_slave_dev
+
+t09_safe_fail_on_no_slave_dev_ignore () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h00_setup_sda_tree $block $dev
+ h01_setup_dm_tree $block $dev
+ # If the matching dev node is missing, an error code will be returned
+ # but the path will still represent the slave.
+ rm $dev/sda1
+
+ out=$(./rootdev -i -s --dev $dev --block $block --major 254 --minor 0 \
+ 2>/dev/null)
+ expect "$? -eq 0" || return 1
+ expect "'$dev/sda1' = '$out'" || return 1
+}
+run_test t09_safe_fail_on_no_slave_dev_ignore
+
+h02_setup_mmc_tree() {
+ local block=$1
+ local dev=$2
+ mkdir -p $block
+ mkdir -p $dev
+ mkdir -p $block/mmcblk0/mmcblk0p1
+ mkdir -p $block/mmcblk0/mmcblk0p2
+ echo "11:0" > $block/mmcblk0/dev
+ echo "11:1" > $block/mmcblk0/mmcblk0p1/dev
+ echo "11:2" > $block/mmcblk0/mmcblk0p2/dev
+ mknod $dev/mmcblk0 b 11 0
+ mknod $dev/mmcblk0p1 b 11 1
+ mknod $dev/mmcblk0p2 b 11 2
+}
+
+t10_mmcdev () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h02_setup_mmc_tree $block $dev
+ out=$(./rootdev --dev $dev --block $block --major 11 --minor 2 \
+ 2>/dev/null)
+ expect "$? -eq 0" || return 1
+ expect "'$dev/mmcblk0p2' = '$out'" || return 1
+}
+run_test t10_mmcdev
+
+t11_mmcdev_strip () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h02_setup_mmc_tree $block $dev
+ out=$(./rootdev -d --dev $dev --block $block --major 11 --minor 2 \
+ 2>/dev/null)
+ expect "$? -eq 0" || return 1
+ expect "'$dev/mmcblk0' = '$out'" || return 1
+}
+run_test t11_mmcdev_strip
+
+t12_sda_strip () {
+ local block=$WORKDIR/sys/block
+ local dev=$WORKDIR/dev
+ h00_setup_sda_tree $block $dev
+ out=$(./rootdev -d --dev $dev --block $block --major 10 --minor 2 \
+ 2>/dev/null)
+ expect "$? -eq 0" || return 1
+ expect "'$dev/sda' = '$out'" || return 1
+}
+run_test t12_sda_strip
+
+# TODO(wad) add node creation tests
+
+TEST_COUNT=$((PASS_COUNT + FAIL_COUNT))
+
+echo "----"
+echo "Test passed: $PASS_COUNT / $TEST_COUNT"
+echo "Test failed: $FAIL_COUNT / $TEST_COUNT"
+
+if [ $FAIL_COUNT -ne 0 ]; then
+ exit 1
+fi